added functionality to remove containers from all groups based on the container ID provided
This commit is contained in:
@@ -136,8 +136,23 @@ func RemoveContainerGroup(ContainerID string, GroupID string) (*Group,error) {
|
||||
// RemoveContainerGroups Remove Container from groups (i.e which ever groups has information
|
||||
// about that container). This is mostly called when a container is deleted or removed from
|
||||
// the tracked container list
|
||||
func RemoveContainerGroups(ContainerID string) {
|
||||
|
||||
func RemoveContainerGroups(ContainerID string) error {
|
||||
// Get container information based on container ID provided
|
||||
containerInfo, err := GetContainerInformation(ContainerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Get Groups information from reading the grouptrackcontainer.json file
|
||||
groups, err := ReadGroup()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Removes container information from all groups it is found in
|
||||
err = groups.RemoveContainerGroups(containerInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetGroup Gets group information based on
|
||||
@@ -194,16 +209,12 @@ func ReadGroup() (*Groups,error) {
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
|
||||
// defer the closing of our jsonFile so that we can parse it later on
|
||||
defer jsonFile.Close()
|
||||
|
||||
// read our opened xmlFile as a byte array.
|
||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||
|
||||
// we initialize our Users array
|
||||
var groups Groups
|
||||
|
||||
// we unmarshal our byteArray which contains our
|
||||
// jsonFile's content into 'users' which we defined above
|
||||
json.Unmarshal(byteValue, &groups)
|
||||
@@ -217,7 +228,6 @@ func (grp *Groups)WriteGroup() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get Path from config
|
||||
config, err := config.ConfigInit()
|
||||
if err != nil {
|
||||
@@ -228,7 +238,6 @@ func (grp *Groups)WriteGroup() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -248,4 +257,20 @@ func (grp *Group)RemoveContainerGroup(Container *TrackContainer) error {
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveContainerGroups removes container found in all groups
|
||||
func (grp *Groups)RemoveContainerGroups(Container *TrackContainer) error {
|
||||
// Iterating through all groups
|
||||
for i,group := range grp.GroupList {
|
||||
// Removes the container in the following group
|
||||
// if it exists
|
||||
err := group.RemoveContainerGroup(Container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set group information to list groups
|
||||
grp.GroupList[i] = group
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -168,3 +168,100 @@ func TestGroup_RemoveContainerGroup(t *testing.T) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// Testing that container are removed from all
|
||||
// created groups
|
||||
// Scenario:
|
||||
// - Create 2 groups
|
||||
// - Add Container information to each group
|
||||
// - Remove Container information from each group
|
||||
func TestGroups_RemoveContainerGroups(t *testing.T) {
|
||||
// Creates a new group
|
||||
group, err := CreateGroup()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
// Created another group assigned to variable group 1
|
||||
group1, err := CreateGroup()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Creating and adding the container to the
|
||||
// tracked list
|
||||
container1 ,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()
|
||||
}
|
||||
|
||||
// Adds container information to the group
|
||||
Group, err := AddContainerToGroup(container1.ID,group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
fmt.Println("Container added")
|
||||
PrettyPrint(Group)
|
||||
|
||||
// Adds container information to the group
|
||||
Group1, err := AddContainerToGroup(container1.ID,group1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
fmt.Println("Container added")
|
||||
PrettyPrint(Group1)
|
||||
|
||||
// Removing docker container from the group
|
||||
err = RemoveContainerGroups(container1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Killing docker container created
|
||||
err = docker.StopAndRemoveContainer(container1.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()
|
||||
}
|
||||
|
||||
// Removes the new group
|
||||
// it created
|
||||
err = RemoveGroup(group.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
// Removes the new group
|
||||
// it created
|
||||
err = RemoveGroup(group1.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,11 @@ func RemoveContianer(IP string,ID string) error {
|
||||
fmt.Println("success")
|
||||
}
|
||||
|
||||
// Remove container from groups it exists in
|
||||
err = RemoveContainerGroups(ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Remove container created from the tracked list
|
||||
err = RemoveTrackedContainer(ID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user