-
Notifications
You must be signed in to change notification settings - Fork 13
/
container_test.go
120 lines (113 loc) · 3.3 KB
/
container_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package meli
import (
"context"
"io/ioutil"
"testing"
)
func TestCreateContainer(t *testing.T) {
tt := []struct {
dc *DockerContainer
expected string
containerAlreadycreated bool
expectedErr error
}{
{
&DockerContainer{
ComposeService: ComposeService{Image: "busybox", Restart: "unless-stopped"},
ServiceName: "myservice",
NetworkName: "myNetworkName",
DockerComposeFile: "DockerFile",
ContainerID: "myExistingContainerId00912",
},
"myExistingContainerId00912",
true,
nil},
{
&DockerContainer{
ComposeService: ComposeService{Image: "busybox", Restart: "unless-stopped"},
ServiceName: "myservice",
NetworkName: "myNetworkName",
DockerComposeFile: "DockerFile",
ContainerID: "myContainerId001",
Rebuild: true,
},
"myContainerId001",
false,
nil},
}
var ctx = context.Background()
cli := &mockDockerClient{}
for _, v := range tt {
alreadyCreated, actual, err := CreateContainer(ctx, cli, v.dc)
if err != nil {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %s \nwanted %#+v", v.dc, err, v.expectedErr)
}
if actual != v.expected {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %s \nwanted %#+v", v.dc, actual, v.expected)
}
if alreadyCreated != v.containerAlreadycreated {
t.Errorf("\nCalled CreateContainer(%#+v) \ngot %#+v \nwanted %#+v", v.dc, alreadyCreated, true)
}
}
}
func TestContainerStart(t *testing.T) {
tt := []struct {
dc *DockerContainer
expectedErr error
}{
{&DockerContainer{ContainerID: "myContainerId"}, nil},
}
var ctx = context.Background()
cli := &mockDockerClient{}
for _, v := range tt {
err := ContainerStart(ctx, cli, v.dc)
if err != nil {
t.Errorf("\nCalled ContainerStart(%#+v) \ngot %s \nwanted %#+v", v.dc, err, v.expectedErr)
}
}
}
func TestContainerLogs(t *testing.T) {
tt := []struct {
dc *DockerContainer
expectedErr error
}{
{&DockerContainer{ContainerID: "myContainerId", FollowLogs: true, LogMedium: ioutil.Discard}, nil},
{&DockerContainer{ContainerID: "myContainerId", FollowLogs: false, LogMedium: ioutil.Discard}, nil},
}
var ctx = context.Background()
cli := &mockDockerClient{}
for _, v := range tt {
err := ContainerLogs(ctx, cli, v.dc)
if err != nil {
t.Errorf("\nCalled ContainerLogs(%#+v) \ngot %s \nwanted %#+v", v.dc, err, v.expectedErr)
}
}
}
func BenchmarkCreateContainer(b *testing.B) {
var ctx = context.Background()
cli := &mockDockerClient{}
dc := &DockerContainer{
ComposeService: ComposeService{Image: "busybox", Restart: "unless-stopped"},
ServiceName: "myservice",
NetworkName: "myNetworkName",
DockerComposeFile: "DockerFile",
ContainerID: "myExistingContainerId00912",
}
for n := 0; n < b.N; n++ {
_, _, _ = CreateContainer(ctx, cli, dc)
}
}
func BenchmarkContainerStart(b *testing.B) {
var ctx = context.Background()
cli := &mockDockerClient{}
for n := 0; n < b.N; n++ {
_ = ContainerStart(ctx, cli, &DockerContainer{ContainerID: "containerId"})
}
}
func BenchmarkContainerLogs(b *testing.B) {
var ctx = context.Background()
cli := &mockDockerClient{}
for n := 0; n < b.N; n++ {
_ = ContainerLogs(ctx, cli, &DockerContainer{ContainerID: "containerId", LogMedium: ioutil.Discard})
}
}