Skip to content

Commit

Permalink
create container unit test && mock docker client
Browse files Browse the repository at this point in the history
patch: fix docker bug

cluster unit test
  • Loading branch information
wrfly committed Aug 6, 2017
1 parent 6cb8039 commit 6aac61f
Show file tree
Hide file tree
Showing 15 changed files with 711 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ default.etcd/*
vendor/*
clean.sh
.vscode/*
*.test
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ build: deps
go build -ldflags "$(GO_LDFLAGS)" -a -tags netgo -installsuffix netgo -o eru-core

test: deps
sed -i "143s/\*http.Transport/http.RoundTripper/" ./vendor/github.com/docker/docker/client/client.go
go vet `go list ./... | grep -v '/vendor/'`
go test -v `glide nv`
8 changes: 7 additions & 1 deletion cluster/calcium/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ func (c *calcium) Backup(id, srcPath string) (*types.BackupMessage, error) {
}
log.Debugf("Backup %s for container %s", srcPath, id)
container, err := c.GetContainer(id)
if err != nil {
return nil, err
}
node, err := c.GetNode(container.Podname, container.Nodename)
if err != nil {
return nil, err
}
ctx := utils.ToDockerContext(node.Engine)

resp, stat, err := node.Engine.CopyFromContainer(ctx, container.ID, srcPath)
Expand All @@ -40,7 +46,7 @@ func (c *calcium) Backup(id, srcPath string) (*types.BackupMessage, error) {
}
now := time.Now().Format("2006.01.02.15.04.05")
baseDir := filepath.Join(c.config.BackupDir, appname, entrypoint)
err = os.MkdirAll(baseDir, os.FileMode(0400))
err = os.MkdirAll(baseDir, os.FileMode(0700)) // drwx------
if err != nil {
log.Errorf("Error during mkdir %s, %v", baseDir, err)
return nil, err
Expand Down
29 changes: 29 additions & 0 deletions cluster/calcium/backup_test.go
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")
}
22 changes: 22 additions & 0 deletions cluster/calcium/build_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,25 @@ func TestSingleBuildDockerFile(t *testing.T) {
f.Close()
os.Remove(f.Name())
}

func TestBuildImageError(t *testing.T) {
initMockConfig()
reponame := "git@github.com:docker/jira-test.git"
_, err := mockc.BuildImage(reponame, "x-version", "998", "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "No build pod set in config")

mockc.config.Docker.BuildPod = "dev_pod"
_, err = mockc.BuildImage(reponame, "x-version", "998", "")
assert.Contains(t, err.Error(), "Public Key not found")
}

func TestCreateImageTag(t *testing.T) {
initMockConfig()
appname := "appname"
version := "version"
imageTag := createImageTag(mockc.config, appname, version)
cfg := mockc.config.Docker
expectTag := fmt.Sprintf("%s/%s/%s:%s", cfg.Hub, cfg.HubPrefix, appname, version)
assert.Equal(t, expectTag, imageTag)
}
145 changes: 145 additions & 0 deletions cluster/calcium/create_container_test.go
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)
}
}
Loading

0 comments on commit 6aac61f

Please sign in to comment.