Skip to content

Commit

Permalink
keep monitor list elements unique (#150)
Browse files Browse the repository at this point in the history
* keep monitor list elements unique

* add unit test

* remove monitor list initialization

* copy list instead add

---------

Co-authored-by: jiamzhu <jiamzhu@paypal.com>
  • Loading branch information
JiaminZhu and jiamzhu authored Feb 13, 2024
1 parent f9a46c9 commit 9e99a9e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
5 changes: 2 additions & 3 deletions utils/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions utils/pod/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
72 changes: 72 additions & 0 deletions utils/pod/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pod

import (
"errors"
"fmt"
"testing"

"github.com/paypal/dce-go/types"
Expand Down Expand Up @@ -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)
})
}

0 comments on commit 9e99a9e

Please sign in to comment.