-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create container unit test && mock docker client
patch: fix docker bug cluster unit test
- Loading branch information
Showing
15 changed files
with
711 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ default.etcd/* | |
vendor/* | ||
clean.sh | ||
.vscode/* | ||
*.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package calcium | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBackup(t *testing.T) { | ||
initMockConfig() | ||
srcPath := "/tmp" | ||
_, err = mockc.Backup(mockID, srcPath) | ||
assert.Error(t, err) | ||
assert.Equal(t, err.Error(), "BackupDir not set") | ||
|
||
config.BackupDir = "/tmp" | ||
mockc, err = New(config) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
mockc.SetStore(mockStore) | ||
ch, err := mockc.Backup(mockID, srcPath) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.Equal(t, ch.Status, "ok") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package calcium | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/docker/docker/client" | ||
"github.com/stretchr/testify/assert" | ||
"gitlab.ricebook.net/platform/core/types" | ||
) | ||
|
||
func TestPullImage(t *testing.T) { | ||
initMockConfig() | ||
|
||
nodes, err := mockc.store.GetAllNodes() | ||
if err != nil || len(nodes) == 0 { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := pullImage(nodes[0], image); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCreateContainerWithMemPrior(t *testing.T) { | ||
initMockConfig() | ||
|
||
specs := types.Specs{ | ||
Appname: "root", | ||
Entrypoints: map[string]types.Entrypoint{ | ||
"test": types.Entrypoint{ | ||
Command: "sleep 9999", | ||
Ports: []types.Port{"6006/tcp"}, | ||
HealthCheckPort: 6006, | ||
HealthCheckUrl: "", | ||
HealthCheckExpectedCode: 200, | ||
}, | ||
}, | ||
Build: []string{""}, | ||
Base: image, | ||
} | ||
opts := &types.DeployOptions{ | ||
Appname: "root", | ||
Image: image, | ||
Podname: podname, | ||
Entrypoint: "test", | ||
Count: 3, | ||
Memory: 268435456, | ||
CPUQuota: 1, | ||
} | ||
|
||
// Create Container with memory prior | ||
testlogF("Create containers with memory prior") | ||
createCh, err := mockc.createContainerWithMemoryPrior(specs, opts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ids := []string{} | ||
for msg := range createCh { | ||
assert.True(t, msg.Success) | ||
ids = append(ids, msg.ContainerID) | ||
fmt.Printf("Get Container ID: %s\n", msg.ContainerID) | ||
} | ||
|
||
// get containers | ||
clnt, _ := client.NewClient("http://127.0.0.1", "v1.29", mockDockerHTTPClient(), nil) | ||
cs := []types.Container{} | ||
for _, id := range ids { | ||
c := types.Container{ | ||
ID: id, | ||
Engine: clnt, | ||
} | ||
cs = append(cs, c) | ||
mockStore.On("GetContainer", id).Return(&c, nil) | ||
} | ||
mockStore.On("GetContainers", ids).Return(&cs, nil) | ||
|
||
// Remove Container | ||
testlogF("Remove containers") | ||
removeCh, err := mockc.RemoveContainer(ids) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for msg := range removeCh { | ||
fmt.Printf("ID: %s, Message: %s\n", msg.ContainerID, msg.Message) | ||
assert.True(t, msg.Success) | ||
} | ||
} | ||
|
||
func TestClean(t *testing.T) { | ||
initMockConfig() | ||
|
||
// delete pod | ||
if err := mockc.store.DeletePod("dev_pod", true); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCreateContainerWithCPUPrior(t *testing.T) { | ||
initMockConfig() | ||
|
||
specs := types.Specs{ | ||
Appname: "root", | ||
Entrypoints: map[string]types.Entrypoint{ | ||
"test": types.Entrypoint{ | ||
Command: "sleep 9999", | ||
Ports: []types.Port{"6006/tcp"}, | ||
HealthCheckPort: 6006, | ||
HealthCheckUrl: "", | ||
HealthCheckExpectedCode: 200, | ||
}, | ||
}, | ||
Build: []string{""}, | ||
Base: image, | ||
} | ||
opts := &types.DeployOptions{ | ||
Appname: "root", | ||
Image: image, | ||
Podname: podname, | ||
Entrypoint: "test", | ||
Count: 3, | ||
Memory: 268435456, | ||
CPUQuota: 1, | ||
} | ||
|
||
// update node | ||
mockStore.On("UpdateNode", mock.MatchedBy(func(input *types.Node) bool { | ||
return true | ||
})).Return(nil) | ||
|
||
// Create Container with memory prior | ||
testlogF("Create containers with memory prior") | ||
createCh, err := mockc.createContainerWithCPUPrior(specs, opts) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ids := []string{} | ||
for msg := range createCh { | ||
assert.True(t, msg.Success) | ||
ids = append(ids, msg.ContainerID) | ||
fmt.Printf("Get Container ID: %s\n", msg.ContainerID) | ||
} | ||
} |
Oops, something went wrong.