added test case for either executing group ID or container ID

This commit is contained in:
2021-08-23 14:15:07 +04:00
parent 4ed2e10ac3
commit ab2bcb393b
2 changed files with 144 additions and 4 deletions

View File

@@ -232,9 +232,9 @@ func ReadHost(filename string) (*Host, error) {
return c, nil return c, nil
} }
// RunPluginCli Runs ansible plugin based on plugin name and contianer name which // RunPluginContainer Runs ansible plugin based on plugin name and contianer name which
// is derived from the tracked containers file // is derived from the tracked containers file
func RunPluginCli(PluginName string, ContainerID string) error { func RunPluginContainer(PluginName string, ContainerID string) error {
// Gets container information based on container ID // Gets container information based on container ID
ContainerInformation, err := client.GetContainerInformation(ContainerID) ContainerInformation, err := client.GetContainerInformation(ContainerID)
if err != nil { if err != nil {
@@ -267,3 +267,37 @@ func RunPluginCli(PluginName string, ContainerID string) error {
return nil return nil
} }
// CheckRunPlugin Checks if the ID belongs to the group or container
// calls the plugin function the appropriate amount of times
func CheckRunPlugin(PluginName string, ID string) error {
// Check if the ID belongs to the group or container ID
id, err := client.CheckID(ID)
if err != nil {
return err
}
// When the ID belongs to a group
if id == "group" {
// gets the group information
group, err := client.GetGroup(ID)
if err != nil {
return err
}
// Iterate through each container information in the group
// and run the plugin in each of them
for _, container := range group.TrackContainerList {
// runs plugin for each container
err := RunPluginContainer(PluginName, container.Id)
if err != nil {
return err
}
}
} else { // This means the following ID is a container ID
err := RunPluginContainer(PluginName, ID)
if err != nil {
return err
}
}
return nil
}

View File

@@ -94,7 +94,7 @@ func TestExecuteIP_ModifyHost(t *testing.T) {
// Test to ensure the cli function runs as intended and executes // Test to ensure the cli function runs as intended and executes
// the test ansible script // the test ansible script
func TestRunPluginCli(t *testing.T) { func TestRunPluginContainer(t *testing.T) {
// Create docker container and get SSH port // Create docker container and get SSH port
container1 ,err := docker.BuildRunContainer(0,"false","") container1 ,err := docker.BuildRunContainer(0,"false","")
if err != nil { if err != nil {
@@ -110,7 +110,7 @@ func TestRunPluginCli(t *testing.T) {
} }
// Running test Ansible script // Running test Ansible script
err = RunPluginCli("TestAnsible", container1.ID) err = RunPluginContainer("TestAnsible", container1.ID)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
t.Fail() t.Fail()
@@ -129,4 +129,110 @@ func TestRunPluginCli(t *testing.T) {
fmt.Println(err) fmt.Println(err)
t.Fail() t.Fail()
} }
}
// Testing the function can plugin can run with
// group ID and container ID
func TestCheckRunPlugin(t *testing.T) {
// Create docker container and get SSH port
container1 ,err := docker.BuildRunContainer(0,"false","")
if err != nil {
fmt.Println(err)
t.Fail()
}
// Create docker container and get SSH port
container2 ,err := docker.BuildRunContainer(0,"false","")
if err != nil {
fmt.Println(err)
t.Fail()
}
// Ensuring created container1 is the added to the tracked list
err = client.AddTrackContainer(container1, "0.0.0.0")
if err != nil {
fmt.Println(err)
t.Fail()
}
// Ensuring created container2 is the added to the tracked list
err = client.AddTrackContainer(container2, "0.0.0.0")
if err != nil {
fmt.Println(err)
t.Fail()
}
// Create group to add created containers
group, err := client.CreateGroup()
if err != nil {
fmt.Println(err)
t.Fail()
}
// Add container 1 to the group
_, err = client.AddContainerToGroup(container1.ID, group.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Add container 2 to the group
_, err = client.AddContainerToGroup(container2.ID, group.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// -------------------------- Main test cases -------------------------------
// Checking function against container ID
err = CheckRunPlugin("TestAnsible", container1.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Checking function against group ID
err = CheckRunPlugin("TestAnsible", group.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// ----------------------------------------------------------------------------
// Remove created group
err = client.RemoveGroup(group.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Removes container1 information from the tracker IP addresses
err = client.RemoveTrackedContainer(container1.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Removing container1 after Ansible is executed
err = docker.StopAndRemoveContainer(container1.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Removes container2 information from the tracker IP addresses
err = client.RemoveTrackedContainer(container2.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
// Removing container2 after Ansible is executed
err = docker.StopAndRemoveContainer(container2.ID)
if err != nil {
fmt.Println(err)
t.Fail()
}
} }