-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit test
80 lines (68 loc) · 2.28 KB
/
unit test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import (
"bytes"
"fmt"
"github.com/docker/swarm/cluster"
"github.com/samalba/dockerclient"
"github.com/samalba/dockerclient/mockclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"testing"
)
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}
var (
mockInfo = &dockerclient.Info{
ID: "id",
Name: "name",
NCPU: 10,
MemTotal: 20,
Driver: "driver-test",
ExecutionDriver: "execution-driver-test",
KernelVersion: "1.2.3",
OperatingSystem: "golang",
Labels: []string{"foo=bar"},
}
)
func TestImportImage(t *testing.T) {
// create cluster
c := &Cluster{
engines: make(map[string]*cluster.Engine),
}
// create engione
id := "test-engine"
engine := cluster.NewEngine(id, 0)
engine.Name = id
engine.ID = id
// create mock client
client := mockclient.NewMockClient()
client.On("Info").Return(mockInfo, nil)
client.On("StartMonitorEvents", mock.Anything, mock.Anything, mock.Anything).Return()
client.On("ListContainers", true, false, "").Return([]dockerclient.Container{}, nil).Once()
client.On("ListImages").Return([]*dockerclient.Image{}, nil)
// connect client
engine.ConnectWithClient(client)
// add engine to cluster
c.engines[engine.ID] = engine
// import success
readCloser := nopCloser{bytes.NewBufferString("ok")}
client.On("ImportImage", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(readCloser, nil).Once()
callback := func(what, status string) {
// import success
assert.Equal(t, status, "Import success")
}
c.Import("-", "testImageOK", "latest", bytes.NewReader([]byte("123")), callback)
// import error
readCloser = nopCloser{bytes.NewBufferString("error")}
client.On("ImportImage", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(readCloser, fmt.Errorf("Import error")).Once()
callback = func(what, status string) {
// import error
assert.Equal(t, status, "Import error")
}
c.Import("-", "testImageError", "latest", bytes.NewReader([]byte("123")), callback)
}
~