diff --git a/client/GroupTrackContainer.go b/client/GroupTrackContainer.go index 7437464..c82ab94 100644 --- a/client/GroupTrackContainer.go +++ b/client/GroupTrackContainer.go @@ -69,6 +69,37 @@ func RemoveGroup(GroupID string) error { return nil } +// AddContainerToGroup Adds container information to the Group based on the Group ID +func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) { + // Gets container information based on container ID provided + containerInfo, err := GetContainerInformation(ContainerID) + if err != nil { + return nil, err + } + // Gets group information based on the group ID provided + group, err := GetGroup(GroupID) + if err != nil { + return nil, err + } + // Adds container information the group + group.AddContainer(containerInfo) + + // Get Groups information from reading the grouptrackcontainer.json file + groups, err := ReadGroup() + if err != nil { + return nil, err + } + // Updating specific element in the group list with the added container + groups.GroupList[group.index] = group + // Write groups information on the grouptrackcontainer.json file + err = groups.WriteGroup() + if err != nil { + return nil, err + } + + return group, nil +} + // GetGroup Gets group information based on // group id provided func GetGroup(GroupID string) (*Group,error) { @@ -158,5 +189,11 @@ func (grp *Groups)WriteGroup() error { return err } + return nil +} + +// AddContainer Adds a container to the Tracked container list of the group +func (grp *Group)AddContainer(Container *TrackContainer) error { + grp.TrackContainerList = append(grp.TrackContainerList, Container) return nil } \ No newline at end of file diff --git a/client/GroupTrackContainer_test.go b/client/GroupTrackContainer_test.go index 11f46bb..901235f 100644 --- a/client/GroupTrackContainer_test.go +++ b/client/GroupTrackContainer_test.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker" "testing" ) @@ -15,6 +16,8 @@ func TestCreateGroup(t *testing.T) { PrettyPrint(group) } +// Testing if the group gets removed when a +// group ID is provided func TestRemoveGroup(t *testing.T) { // Creates a new group group, err := CreateGroup() @@ -23,10 +26,74 @@ func TestRemoveGroup(t *testing.T) { t.Fail() } // Removes the new group - // creates + // it created err = RemoveGroup(group.ID) if err != nil { fmt.Println(err) t.Fail() } } + +// Testing if container information is added +// to the created group +func TestAddContainerToGroup(t *testing.T) { + // Creates a new group + group, 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() + } + + PrettyPrint(Group) + + // 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() + } + +} diff --git a/client/trackcontainers/trackcontainers.json b/client/trackcontainers/trackcontainers.json index e69de29..08a05b5 100644 --- a/client/trackcontainers/trackcontainers.json +++ b/client/trackcontainers/trackcontainers.json @@ -0,0 +1,3 @@ +{ + "TrackContainer": [] +} \ No newline at end of file