From 9e99a9e458a728c87e9c8d9aee341ace84f96ac0 Mon Sep 17 00:00:00 2001 From: JiaminZhu Date: Tue, 13 Feb 2024 12:26:27 -0800 Subject: [PATCH] keep monitor list elements unique (#150) * keep monitor list elements unique * add unit test * remove monitor list initialization * copy list instead add --------- Co-authored-by: jiamzhu --- utils/pod/pod.go | 5 ++- utils/pod/util.go | 8 +++++ utils/pod/util_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/utils/pod/pod.go b/utils/pod/pod.go index bb17fd9..5308be6 100644 --- a/utils/pod/pod.go +++ b/utils/pod/pod.go @@ -1189,8 +1189,7 @@ func HealthCheck(files []string, podServices map[string]bool, out chan<- string) logger.Debugf("list of containers are launched : %v", containers) time.Sleep(interval) } - MonitorContainerList = make([]types.SvcContainer, len(containers)) - copy(MonitorContainerList, containers) + MonitorContainerList = CopySvcContainers(MonitorContainerList, containers) for _, c := range MonitorContainerList { logger.Infof("service : %s, containerid: %s, pid: %s", c.ServiceName, c.ContainerId, c.Pid) } @@ -1294,7 +1293,7 @@ healthCheck: } UpdateHealthCheckStatus(StepMetrics) - copy(MonitorContainerList, containers) + MonitorContainerList = CopySvcContainers(MonitorContainerList, containers) logger.Printf("Health Check List: %v", HealthCheckListId) logger.Printf("Pod Monitor List: %v", MonitorContainerList) diff --git a/utils/pod/util.go b/utils/pod/util.go index 0ae2953..c273998 100644 --- a/utils/pod/util.go +++ b/utils/pod/util.go @@ -129,3 +129,11 @@ func UpdateHealthCheckStatus(stepData map[string][]*types.StepData) { } } } + +func CopySvcContainers(to []types.SvcContainer, from []types.SvcContainer) []types.SvcContainer { + to = make([]types.SvcContainer, len(from)) + for i, c := range from { + to[i] = c + } + return to +} diff --git a/utils/pod/util_test.go b/utils/pod/util_test.go index cfc5fdc..29a97d1 100644 --- a/utils/pod/util_test.go +++ b/utils/pod/util_test.go @@ -2,6 +2,7 @@ package pod import ( "errors" + "fmt" "testing" "github.com/paypal/dce-go/types" @@ -65,3 +66,74 @@ func TestSetStepData(t *testing.T) { } } } + +func TestAddSvcContainers(t *testing.T) { + t.Run("base container list is empty", func(t *testing.T) { + var to []types.SvcContainer + to = []types.SvcContainer{ + { + ServiceName: "test1", + }, + { + ServiceName: "test2", + }, + } + res := CopySvcContainers(to, []types.SvcContainer{}) + assert.Equal(t, 0, len(res)) + fmt.Println(res) + }) + t.Run("from container list is empty", func(t *testing.T) { + var to []types.SvcContainer + to = make([]types.SvcContainer, 3) + res := CopySvcContainers(to, []types.SvcContainer{{ServiceName: "test1"}}) + assert.Equal(t, 1, len(res)) + fmt.Println(res) + }) + t.Run("copy duplicates to base container list", func(t *testing.T) { + var to []types.SvcContainer + to = []types.SvcContainer{ + { + ServiceName: "test1", + }, + { + ServiceName: "test2", + }, + } + res := CopySvcContainers(to, []types.SvcContainer{{ServiceName: "test2"}}) + assert.Equal(t, 1, len(res)) + assert.Equal(t, "test2", res[0].ServiceName) + fmt.Println(res) + }) + t.Run("copy non-duplicates to base container list", func(t *testing.T) { + var to []types.SvcContainer + to = []types.SvcContainer{ + { + ServiceName: "test1", + }, + { + ServiceName: "test2", + }, + } + res := CopySvcContainers(to, []types.SvcContainer{{ServiceName: "test3"}}) + assert.Equal(t, 1, len(res)) + assert.Equal(t, "test3", res[0].ServiceName) + fmt.Println(res) + }) + t.Run("copy duplicates & non-duplicates to base container list", func(t *testing.T) { + var to []types.SvcContainer + to = []types.SvcContainer{ + { + ServiceName: "test1", + }, + { + ServiceName: "test2", + }, + } + res := CopySvcContainers(to, []types.SvcContainer{{ServiceName: "test2"}, {ServiceName: "test3"}, {ServiceName: "test4"}}) + assert.Equal(t, 3, len(res)) + assert.Equal(t, "test2", res[0].ServiceName) + assert.Equal(t, "test3", res[1].ServiceName) + assert.Equal(t, "test4", res[2].ServiceName) + fmt.Println(res) + }) +}