Skip to content

Commit

Permalink
realloc functional test
Browse files Browse the repository at this point in the history
  • Loading branch information
wrfly authored and CMGS committed Sep 3, 2017
1 parent 4d605c7 commit c6984ce
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cluster/calcium/create_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
40 changes: 27 additions & 13 deletions cluster/calcium/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -77,6 +77,7 @@ var (
"f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22df",
"f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22dg",
}
mockInspectedContainerResources = map[string]container.Resources{}
)

func testlogF(format interface{}, a ...interface{}) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/realloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions cluster/calcium/realloc_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
}
}
3 changes: 1 addition & 2 deletions cluster/calcium/remove_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (

"gitlab.ricebook.net/platform/core/types"

"github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
)

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())
}
Expand Down

0 comments on commit c6984ce

Please sign in to comment.