diff --git a/Docs/Installation.md b/Docs/Installation.md index 928b04d..116ade4 100644 --- a/Docs/Installation.md +++ b/Docs/Installation.md @@ -144,7 +144,7 @@ p2prc --tc ### Running plugin ``` -p2prc --plugin --id +p2prc --plugin --id ``` ### Create group @@ -165,7 +165,7 @@ p2prc --group ``` ### Delete container from group ``` -p2prc --rmcgroup --group --id +p2prc --rmcgroup --group --id ``` ### Delete entire group ``` diff --git a/client/TrackContainers.go b/client/TrackContainers.go index 389675d..b61d7f2 100644 --- a/client/TrackContainers.go +++ b/client/TrackContainers.go @@ -162,4 +162,15 @@ func GetContainerInformation(ID string) (*TrackContainer, error) { } } return nil, errors.New("Container not found. ") +} + +// CheckID Checks if the ID belongs to a group or a single container +func CheckID(ID string) (string,error) { + // For group checks if the 1st characters is "grp" + if ID[0:3] == "grp" { + return "group", nil + } else { + return "container", nil + } + return "",nil } \ No newline at end of file diff --git a/client/TrackContianers_test.go b/client/TrackContianers_test.go index 0f1f7c4..ec8a90d 100644 --- a/client/TrackContianers_test.go +++ b/client/TrackContianers_test.go @@ -102,3 +102,19 @@ func TestRemoveTrackedContainer(t *testing.T) { t.Fail() } } + +// Test function that checks if the ID belongs to +// a group or container running +func TestCheckID(t *testing.T) { + id := "grp123" + checkID, err := CheckID(id) + if err != nil { + fmt.Println(err) + t.Fail() + } + if checkID == "group" { + fmt.Println("pass") + } else { + t.Fail() + } +} diff --git a/cmd/action.go b/cmd/action.go index 10fff98..b8807b2 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -167,9 +167,9 @@ var CliAction = func(ctx *cli.Context) error { //Executing plugin when the plugin flag is called // --plugin if ExecutePlugin != "" { - // The execute plugin requires the container ID provided when being executed + // To execute plugin requires the container ID or group ID provided when being executed if ID != "" { - err := plugin.RunPluginCli(ExecutePlugin, ID) + err := plugin.CheckRunPlugin(ExecutePlugin, ID) if err != nil { fmt.Println(err) } else { diff --git a/plugin/plugin.go b/plugin/plugin.go index 9ab9331..06145e4 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -232,9 +232,9 @@ func ReadHost(filename string) (*Host, error) { 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 -func RunPluginCli(PluginName string, ContainerID string) error { +func RunPluginContainer(PluginName string, ContainerID string) error { // Gets container information based on container ID ContainerInformation, err := client.GetContainerInformation(ContainerID) if err != nil { @@ -267,3 +267,37 @@ func RunPluginCli(PluginName string, ContainerID string) error { 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 +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 979cbe2..bf51944 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -94,7 +94,7 @@ func TestExecuteIP_ModifyHost(t *testing.T) { // Test to ensure the cli function runs as intended and executes // the test ansible script -func TestRunPluginCli(t *testing.T) { +func TestRunPluginContainer(t *testing.T) { // Create docker container and get SSH port container1 ,err := docker.BuildRunContainer(0,"false","") if err != nil { @@ -110,7 +110,7 @@ func TestRunPluginCli(t *testing.T) { } // Running test Ansible script - err = RunPluginCli("TestAnsible", container1.ID) + err = RunPluginContainer("TestAnsible", container1.ID) if err != nil { fmt.Println(err) t.Fail() @@ -129,4 +129,110 @@ func TestRunPluginCli(t *testing.T) { fmt.Println(err) 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() + } + } \ No newline at end of file