diff --git a/client/TrackContainers.go b/client/TrackContainers.go index 82cca1c..4d15cf3 100644 --- a/client/TrackContainers.go +++ b/client/TrackContainers.go @@ -78,6 +78,44 @@ func AddTrackContainer(d *docker.DockerVM,ipAddress string) error { return nil } +// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file +func RemoveTrackedContainer(id string) error { + //Get config information to derive paths for track containers json file + config,err := config.ConfigInit() + if err != nil { + return err + } + // Getting tracked container struct + trackedContianers, err := ReadTrackContainers(config.TrackContainersPath) + // Storing index of element to remove + var removeElement int + removeElement = -1 + for i := range trackedContianers.TrackcontianerList { + if trackedContianers.TrackcontianerList[i].Id == id { + removeElement = i + break + } + } + // Checks if the element to be removed has been detected + if removeElement == -1 { + return errors.New("Container ID not found in the tracked list") + } + // Remove the detected element from the struct + trackedContianers.TrackcontianerList = append(trackedContianers.TrackcontianerList[:removeElement], trackedContianers.TrackcontianerList[removeElement+1:]...) + + // write modified information to the tracked json file + data,err := json.Marshal(trackedContianers) + if err != nil { + return err + } + err = ioutil.WriteFile(config.TrackContainersPath,data,0777) + if err != nil { + return err + } + + return nil +} + // ReadTrackContainers Reads containers which are currently tracked func ReadTrackContainers(filename string) (*TrackContainers, error) { buf, err := ioutil.ReadFile(filename) diff --git a/client/TrackContianers_test.go b/client/TrackContianers_test.go index c62135a..0f1f7c4 100644 --- a/client/TrackContianers_test.go +++ b/client/TrackContianers_test.go @@ -17,7 +17,7 @@ func TestAddTrackContainer(t *testing.T) { // Testing the AddTrackContainer Function err = AddTrackContainer(container1,"0.0.0.0") if err != nil { - + // Killing docker container created err = docker.StopAndRemoveContainer(container1.ID) if err != nil { fmt.Println(err) @@ -26,10 +26,79 @@ func TestAddTrackContainer(t *testing.T) { fmt.Println(err) t.Fail() } - + // Killing docker container created err = docker.StopAndRemoveContainer(container1.ID) if err != nil { fmt.Println(err) t.Fail() } } + +// Testing the remove container function +// NOTE: This test can also be considered as a whole flow on the process of +// tracked containers +func TestRemoveTrackedContainer(t *testing.T) { + container1 ,err := docker.BuildRunContainer(0,"false","") + if err != nil { + fmt.Println(err) + t.Fail() + } + + container2 ,err := docker.BuildRunContainer(0,"false","") + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Testing the AddTrackContainer Function and adding the first container created + err = AddTrackContainer(container1,"0.0.0.0") + if err != nil { + // Killing docker container created + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println(err) + t.Fail() + } + // Killing docker container created + err = docker.StopAndRemoveContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Testing the AddTrackContainer Function and the adding the second container created + err = AddTrackContainer(container2,"0.0.0.0") + if err != nil { + // Killing docker container created + err = docker.StopAndRemoveContainer(container2.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + fmt.Println(err) + t.Fail() + } + // Killing docker container created + err = docker.StopAndRemoveContainer(container2.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removing container 1 from the tracked list + err = RemoveTrackedContainer(container1.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } + + // Removing container 2 from the tracked list + err = RemoveTrackedContainer(container2.ID) + if err != nil { + fmt.Println(err) + t.Fail() + } +} diff --git a/client/trackcontainers/trackcontainers.json b/client/trackcontainers/trackcontainers.json index aaf05d1..30d26af 100644 --- a/client/trackcontainers/trackcontainers.json +++ b/client/trackcontainers/trackcontainers.json @@ -1 +1 @@ -{"TrackContainer":[{"ID":"b8c764b1f14f15cebc0fb7739b0ab1819906c8774c9b7ecf08c5503d5722fffc","Container":{"SSHPort":41731,"SSHUsername":"master","SSHPassword":"password","VNCPort":0,"VNCPassword":"","ID":"b8c764b1f14f15cebc0fb7739b0ab1819906c8774c9b7ecf08c5503d5722fffc","TagName":"p2p-ubuntu","ImagePath":"/home/akilan/Documents/p2p-rendering-computation/server/docker/containers/docker-ubuntu-sshd/","OpenPorts":null,"GPU":"false"},"IpAddress":"0.0.0.0"}]} \ No newline at end of file +{"TrackContainer":[]} \ No newline at end of file