added function to remove container

This commit is contained in:
2021-07-18 11:27:26 +04:00
parent 726292a77d
commit 51110c235b
3 changed files with 110 additions and 3 deletions

View File

@@ -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)

View File

@@ -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()
}
}

View File

@@ -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"}]}
{"TrackContainer":[]}