diff --git a/cluster/calcium/create_container_test.go b/cluster/calcium/create_container_test.go index aed66ba0c..ad133fcc0 100644 --- a/cluster/calcium/create_container_test.go +++ b/cluster/calcium/create_container_test.go @@ -4,7 +4,6 @@ import ( "fmt" "testing" - "github.com/docker/docker/client" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "gitlab.ricebook.net/platform/core/types" @@ -39,7 +38,7 @@ func TestCreateContainerWithMemPrior(t *testing.T) { assert.Equal(t, opts.Count, len(ids)) // get containers - clnt, _ := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil) + clnt := mockDockerClient() cs := []types.Container{} for _, id := range ids { c := types.Container{ diff --git a/cluster/calcium/mock_test.go b/cluster/calcium/mock_test.go index e9293fe0e..47ac5b44f 100644 --- a/cluster/calcium/mock_test.go +++ b/cluster/calcium/mock_test.go @@ -29,7 +29,7 @@ const ( APIVersion = "v1.29" mockMemory = int64(8589934592) // 8G mockID = "f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22de" - appmemory = 268435456 // 0.25 G + appmemory = int64(268435456) // 0.25 G ) var ( @@ -77,6 +77,7 @@ var ( "f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22df", "f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22dg", } + mockInspectedContainerResources = map[string]container.Resources{} ) func testlogF(format interface{}, a ...interface{}) { @@ -174,6 +175,11 @@ func mockDockerDoer(r *http.Request) (*http.Response, error) { }) case fmt.Sprintf("/containers/%s/update", containerID): testlogF("update container %s", containerID) + // update container's resources + updatedConfig := container.UpdateConfig{} + data, _ := ioutil.ReadAll(r.Body) + json.Unmarshal(data, &updatedConfig) + mockInspectedContainerResources[containerID] = updatedConfig.Resources b, err = json.Marshal(container.ContainerUpdateOKBody{}) case fmt.Sprintf("/containers/%s", containerID): testlogF("remove container %s", containerID) @@ -205,23 +211,30 @@ func mockDockerDoer(r *http.Request) (*http.Response, error) { }, nil case fmt.Sprintf("/containers/%s/json", containerID): testlogF("inspect container %s", containerID) - b, _ = json.Marshal(types.ContainerJSON{ + rscs := container.Resources{ + CPUQuota: utils.CpuPeriodBase, + Memory: appmemory, + } + nRscs := container.Resources{} + if mockInspectedContainerResources[containerID].Memory != nRscs.Memory { + rscs = mockInspectedContainerResources[containerID] + } + containerJSON := types.ContainerJSON{ ContainerJSONBase: &types.ContainerJSONBase{ ID: containerID, Image: "image:latest", Name: "name", HostConfig: &container.HostConfig{ - Resources: container.Resources{ - CPUQuota: utils.CpuPeriodBase, - Memory: appmemory, - }, + Resources: rscs, }, }, Config: &container.Config{ Labels: nil, Image: "image:latest", }, - }) + } + mockInspectedContainerResources[containerID] = containerJSON.HostConfig.Resources + b, _ = json.Marshal(containerJSON) case "/networks/bridge/disconnect": var disconnect types.NetworkDisconnect if err := json.NewDecoder(r.Body).Decode(&disconnect); err != nil { @@ -256,8 +269,12 @@ func newMockClient(doer func(*http.Request) (*http.Response, error)) *http.Clien return r } -func mockDockerHTTPClient() *http.Client { - return newMockClient(mockDockerDoer) +func mockDockerClient() *client.Client { + clnt, err := client.NewClient("http://127.0.0.1", "v1.29", newMockClient(mockDockerDoer), nil) + if err != nil { + panic(err) + } + return clnt } func errorMock(statusCode int, message string) (*http.Response, error) { @@ -295,10 +312,7 @@ func initMockConfig() { mockStore = &mockstore.MockStore{} mockc.SetStore(mockStore) - clnt, err := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil) - if err != nil { - panic(err) - } + clnt := mockDockerClient() n1 := &coretypes.Node{ Name: nodename, diff --git a/cluster/calcium/realloc.go b/cluster/calcium/realloc.go index eb4b1762c..1d33a1cbc 100644 --- a/cluster/calcium/realloc.go +++ b/cluster/calcium/realloc.go @@ -62,7 +62,7 @@ func (c *calcium) ReallocResource(ids []string, cpu float64, mem int64) (chan *t return nil, fmt.Errorf("[realloc] cpu can not below zero") } if _, ok := podCPUContainersInfo[newCPURequire]; !ok { - podCPUContainersInfo[newCPURequire] = NodeContainers{} // go map 的傻逼语法, 不这样写会有 assignment to entry in nil map 错误 + podCPUContainersInfo[newCPURequire] = NodeContainers{} podCPUContainersInfo[newCPURequire][node] = []*types.Container{} } podCPUContainersInfo[newCPURequire][node] = append(podCPUContainersInfo[newCPURequire][node], container) diff --git a/cluster/calcium/realloc_test.go b/cluster/calcium/realloc_test.go index 7f1fb85de..1c42ab0c0 100644 --- a/cluster/calcium/realloc_test.go +++ b/cluster/calcium/realloc_test.go @@ -1,11 +1,13 @@ package calcium import ( + "context" "sync" "testing" "github.com/stretchr/testify/assert" "gitlab.ricebook.net/platform/core/types" + "gitlab.ricebook.net/platform/core/utils" ) func TestReallocWithCPUPrior(t *testing.T) { @@ -157,3 +159,32 @@ func TestReallocWithMemoryPrior(t *testing.T) { } } + +func TestReallocResource(t *testing.T) { + initMockConfig() + + ids := ToUpdateContainerIDs + cpuadd := float64(0.1) + memadd := int64(1) + + ch, err := mockc.ReallocResource(ids, cpuadd, memadd) + assert.Nil(t, err) + + for msg := range ch { + assert.True(t, msg.Success) + } + + clnt := mockDockerClient() + for _, id := range ids { + CJ, _ := clnt.ContainerInspect(context.Background(), id) + + // diff memory + newMem := CJ.HostConfig.Resources.Memory + assert.Equal(t, newMem-memadd, appmemory) + + // diff CPU + newCPU := CJ.HostConfig.Resources.CPUQuota + diff := float64(newCPU) - cpuadd*utils.CpuPeriodBase + assert.Equal(t, int(diff), utils.CpuPeriodBase) + } +} diff --git a/cluster/calcium/remove_container_test.go b/cluster/calcium/remove_container_test.go index 39e7eeb3f..07ddf457a 100644 --- a/cluster/calcium/remove_container_test.go +++ b/cluster/calcium/remove_container_test.go @@ -5,7 +5,6 @@ import ( "gitlab.ricebook.net/platform/core/types" - "github.com/docker/docker/client" "github.com/stretchr/testify/assert" ) @@ -13,7 +12,7 @@ func TestRemoveContainer(t *testing.T) { initMockConfig() ids := []string{} - clnt, _ := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil) + clnt := mockDockerClient() for i := 0; i < 5; i++ { ids = append(ids, mockContainerID()) }