|
| 1 | +package calcium |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/mock" |
| 8 | + |
| 9 | + "github.com/docker/docker/client" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "gitlab.ricebook.net/platform/core/types" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPullImage(t *testing.T) { |
| 15 | + initMockConfig() |
| 16 | + |
| 17 | + nodes, err := mockc.store.GetAllNodes() |
| 18 | + if err != nil || len(nodes) == 0 { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + |
| 22 | + if err := pullImage(nodes[0], image); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestCreateContainerWithMemPrior(t *testing.T) { |
| 28 | + initMockConfig() |
| 29 | + |
| 30 | + specs := types.Specs{ |
| 31 | + Appname: "root", |
| 32 | + Entrypoints: map[string]types.Entrypoint{ |
| 33 | + "test": types.Entrypoint{ |
| 34 | + Command: "sleep 9999", |
| 35 | + Ports: []types.Port{"6006/tcp"}, |
| 36 | + HealthCheckPort: 6006, |
| 37 | + HealthCheckUrl: "", |
| 38 | + HealthCheckExpectedCode: 200, |
| 39 | + }, |
| 40 | + }, |
| 41 | + Build: []string{""}, |
| 42 | + Base: image, |
| 43 | + } |
| 44 | + opts := &types.DeployOptions{ |
| 45 | + Appname: "root", |
| 46 | + Image: image, |
| 47 | + Podname: podname, |
| 48 | + Entrypoint: "test", |
| 49 | + Count: 3, |
| 50 | + Memory: 268435456, |
| 51 | + CPUQuota: 1, |
| 52 | + } |
| 53 | + |
| 54 | + // Create Container with memory prior |
| 55 | + t.Log("Create containers with memory prior") |
| 56 | + createCh, err := mockc.createContainerWithMemoryPrior(specs, opts) |
| 57 | + assert.NoError(t, err) |
| 58 | + ids := []string{} |
| 59 | + for msg := range createCh { |
| 60 | + assert.True(t, msg.Success) |
| 61 | + ids = append(ids, msg.ContainerID) |
| 62 | + fmt.Printf("Get Container ID: %s\n", msg.ContainerID) |
| 63 | + } |
| 64 | + |
| 65 | + // get containers |
| 66 | + clnt, _ := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil) |
| 67 | + cs := []types.Container{} |
| 68 | + for _, id := range ids { |
| 69 | + c := types.Container{ |
| 70 | + ID: id, |
| 71 | + Engine: clnt, |
| 72 | + } |
| 73 | + cs = append(cs, c) |
| 74 | + mockStore.On("GetContainer", id).Return(&c, nil) |
| 75 | + } |
| 76 | + mockStore.On("GetContainers", ids).Return(&cs, nil) |
| 77 | + |
| 78 | + // Remove Container |
| 79 | + t.Log("Remove containers") |
| 80 | + removeCh, err := mockc.RemoveContainer(ids) |
| 81 | + assert.NoError(t, err) |
| 82 | + for msg := range removeCh { |
| 83 | + fmt.Printf("ID: %s, Message: %s\n", msg.ContainerID, msg.Message) |
| 84 | + assert.True(t, msg.Success) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestClean(t *testing.T) { |
| 89 | + initMockConfig() |
| 90 | + |
| 91 | + // delete pod |
| 92 | + err := mockc.store.DeletePod(podname, false) |
| 93 | + assert.Error(t, err) |
| 94 | + assert.Contains(t, err.Error(), "still has nodes, delete the nodes first") |
| 95 | + |
| 96 | + // force delete |
| 97 | + err = mockc.store.DeletePod(podname, true) |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +func TestCreateContainerWithCPUPrior(t *testing.T) { |
| 103 | + initMockConfig() |
| 104 | + |
| 105 | + specs := types.Specs{ |
| 106 | + Appname: "root", |
| 107 | + Entrypoints: map[string]types.Entrypoint{ |
| 108 | + "test": types.Entrypoint{ |
| 109 | + Command: "sleep 9999", |
| 110 | + Ports: []types.Port{"6006/tcp"}, |
| 111 | + HealthCheckPort: 6006, |
| 112 | + HealthCheckUrl: "", |
| 113 | + HealthCheckExpectedCode: 200, |
| 114 | + }, |
| 115 | + }, |
| 116 | + Build: []string{""}, |
| 117 | + Base: image, |
| 118 | + } |
| 119 | + opts := &types.DeployOptions{ |
| 120 | + Appname: "root", |
| 121 | + Image: image, |
| 122 | + Podname: podname, |
| 123 | + Entrypoint: "test", |
| 124 | + Count: 3, |
| 125 | + Memory: 268435456, |
| 126 | + CPUQuota: 1, |
| 127 | + } |
| 128 | + |
| 129 | + // update node |
| 130 | + mockStore.On("UpdateNode", mock.MatchedBy(func(input *types.Node) bool { |
| 131 | + return true |
| 132 | + })).Return(nil) |
| 133 | + |
| 134 | + // Create Container with memory prior |
| 135 | + t.Log("Create containers with memory prior") |
| 136 | + createCh, err := mockc.createContainerWithCPUPrior(specs, opts) |
| 137 | + assert.NoError(t, err) |
| 138 | + ids := []string{} |
| 139 | + for msg := range createCh { |
| 140 | + assert.True(t, msg.Success) |
| 141 | + ids = append(ids, msg.ContainerID) |
| 142 | + fmt.Printf("Get Container ID: %s\n", msg.ContainerID) |
| 143 | + } |
| 144 | +} |
0 commit comments