added functionality to add container to the group

This commit is contained in:
2021-08-12 09:36:06 +04:00
parent 707592195a
commit e2f8df9dcb
3 changed files with 108 additions and 1 deletions

View File

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

View File

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

View File

@@ -0,0 +1,3 @@
{
"TrackContainer": []
}