forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_start_test.go
341 lines (264 loc) · 11.2 KB
/
cli_start_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
"github.com/kr/pty"
)
// PouchStartSuite is the test suite for start CLI.
type PouchStartSuite struct{}
func init() {
check.Suite(&PouchStartSuite{})
}
// SetUpSuite does common setup in the beginning of each test suite.
func (suite *PouchStartSuite) SetUpSuite(c *check.C) {
SkipIfFalse(c, environment.IsLinux)
environment.PruneAllContainers(apiClient)
PullImage(c, busyboxImage)
}
// TearDownTest does cleanup work in the end of each test.
func (suite *PouchStartSuite) TearDownTest(c *check.C) {
}
// TestStartCommand tests "pouch start" work.
func (suite *PouchStartSuite) TestStartCommand(c *check.C) {
name := "start-normal"
res := command.PouchRun("create", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
command.PouchRun("start", name).Assert(c, icmd.Success)
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartInTTY tests "pouch start -i" work.
func (suite *PouchStartSuite) TestStartInTTY(c *check.C) {
// make echo server
name := "start-tty"
res := command.PouchRun("create", "--name", name, busyboxImage, "cat")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
// start tty and redirect
cmd := exec.Command(environment.PouchBinary, "start", "-a", "-i", name)
fd, err := pty.Start(cmd)
c.Assert(err, check.IsNil)
defer fd.Close()
msg := "Hello, Pouch"
// hey
_, err = fd.Write([]byte(msg + "\n"))
c.Assert(err, check.IsNil)
// what?
echo, err := bufio.NewReader(fd).ReadString('\n')
c.Assert(err, check.IsNil)
c.Assert(strings.TrimSpace(echo), check.Equals, msg)
command.PouchRun("stop", name)
}
// TestStartInWrongWay runs start command in wrong way.
func (suite *PouchStartSuite) TestStartInWrongWay(c *check.C) {
for _, tc := range []struct {
name string
args string
}{
{name: "missing container name", args: ""},
{name: "unknown flag", args: "-k"},
} {
res := command.PouchRun("start", tc.args)
c.Assert(res.Stderr(), check.NotNil, check.Commentf(tc.name))
}
}
// TestStartWithEnv starts a container with env.
func (suite *PouchStartSuite) TestStartWithEnv(c *check.C) {
name := "start-env"
env := "abc=123"
res := command.PouchRun("create", "--name", name, "-e", env, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
command.PouchRun("start", name).Assert(c, icmd.Success)
output := command.PouchRun("exec", name, "/bin/env").Stdout()
if !strings.Contains(output, env) {
c.Errorf("failed to set env: %s, %s", env, output)
}
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartWithEntrypoint starts a container with entrypoint.
func (suite *PouchStartSuite) TestStartWithEntrypoint(c *check.C) {
name := "start-entrypoint"
command.PouchRun("create", "--name", name, "--entrypoint", "sh", busyboxImage).Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
//TODO: check entrypoint really works
}
// TestStartWithWorkDir starts a container with work dir.
func (suite *PouchStartSuite) TestStartWithWorkDir(c *check.C) {
name := "start-workdir"
command.PouchRun("create", "--name", name, "--entrypoint", "pwd",
"-w", "/tmp", busyboxImage).Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
output := command.PouchRun("start", "-a", name).Stdout()
if !strings.Contains(output, "/tmp") {
c.Errorf("failed to start a container with workdir: %s", output)
}
}
// TestStartWithUser starts a container with user.
func (suite *PouchStartSuite) TestStartWithUser(c *check.C) {
name := "start-user"
user := "1001"
group := "1001"
command.PouchRun("create", "--name", name, "--user", user, busyboxImage, "id", "-u")
defer DelContainerForceMultyTime(c, name)
output := command.PouchRun("start", "-a", name).Stdout()
if !strings.Contains(output, user) {
c.Errorf("failed to start a container with user: %s", output)
}
name = "start-group"
command.PouchRun("create", "--name", name, "--user", user+":"+group, busyboxImage, "id", "-g")
defer DelContainerForceMultyTime(c, name)
output = command.PouchRun("start", "-a", name).Stdout()
if !strings.Contains(output, group) {
c.Errorf("failed to start a container with user:group : %s", output)
}
}
// TestStartWithHostname starts a container with hostname.
func (suite *PouchStartSuite) TestStartWithHostname(c *check.C) {
name := "start-hostname"
hostname := "pouch"
command.PouchRun("create", "--name", name, "--hostname", hostname, busyboxImage, "top").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
output := command.PouchRun("exec", name, "hostname").Stdout()
if !strings.Contains(output, hostname) {
c.Errorf("failed to set hostname: %s, %s", hostname, output)
}
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartWithSysctls starts a container with sysctls.
func (suite *PouchStartSuite) TestStartWithSysctls(c *check.C) {
sysctl := "net.ipv4.ip_forward=1"
name := "start-sysctl"
command.PouchRun("create", "--name", name, "--sysctl", sysctl, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
output := command.PouchRun("exec", name, "cat", "/proc/sys/net/ipv4/ip_forward").Stdout()
if !strings.Contains(output, "1") {
c.Errorf("failed to start a container with sysctls: %s", output)
}
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartWithAppArmor starts a container with security option AppArmor.
func (suite *PouchStartSuite) TestStartWithAppArmor(c *check.C) {
appArmor := "apparmor=unconfined"
name := "start-apparmor"
command.PouchRun("create", "--name", name, "--security-opt", appArmor, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
// TODO: do the test more strictly with effective AppArmor profile.
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartWithSeccomp starts a container with security option seccomp.
func (suite *PouchStartSuite) TestStartWithSeccomp(c *check.C) {
seccomp := "seccomp=unconfined"
name := "start-seccomp"
command.PouchRun("create", "--name", name, "--security-opt", seccomp, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
// TODO: do the test more strictly with effective seccomp profile.
command.PouchRun("stop", name).Assert(c, icmd.Success)
}
// TestStartWithCapability starts a container with capability.
func (suite *PouchStartSuite) TestStartWithCapability(c *check.C) {
capability := "NET_ADMIN"
name := "start-capability"
res := command.PouchRun("create", "--name", name, "--cap-add", capability, busyboxImage, "brctl", "addbr", "foobar")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
}
// TestStartWithPrivilege starts a container with privilege.
func (suite *PouchStartSuite) TestStartWithPrivilege(c *check.C) {
name := "start-privilege"
res := command.PouchRun("create", "--name", name, "--privileged", busyboxImage, "brctl", "addbr", "foobar")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
}
// TestStartWithAnnotation starts a container with annotation.
func (suite *PouchStartSuite) TestStartWithAnnotation(c *check.C) {
name := "start-annotation"
res := command.PouchRun("create", "--name", name, "--annotation", "a=b", busyboxImage, "top")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
}
// TestStartWithExitCode starts a container with annotation.
func (suite *PouchStartSuite) TestStartWithExitCode(c *check.C) {
name := "start-exitcode"
res := command.PouchRun("create", "--name", name, busyboxImage, "sh", "-c", "exit 101")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
// test process exit code $? == 101
ret := command.PouchRun("start", "-a", name)
ret.Assert(c, icmd.Expected{ExitCode: 101})
// test container ExitCode == 101
output := command.PouchRun("inspect", name).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
c.Assert(result[0].State.ExitCode, check.Equals, int64(101))
}
// TestStartWithUlimit starts a container with --ulimit.
func (suite *PouchStartSuite) TestStartWithUlimit(c *check.C) {
name := "start-ulimit"
res := command.PouchRun("create", "--name", name,
"--ulimit", "nproc=256", busyboxImage, "top")
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
}
// TestStartWithPidsLimit tests running container with --pids-limit flag.
func (suite *PouchStartSuite) TestStartWithPidsLimit(c *check.C) {
name := "TestStartWithPidsLimit"
pidfile := "/sys/fs/cgroup/pids/pids.max"
res := command.PouchRun("create", "--pids-limit", "10",
"--name", name, busyboxImage, "cat", pidfile)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
command.PouchRun("start", name).Assert(c, icmd.Success)
}
// TestStartFromCheckpoint tests start a container from a checkpoint
func (suite *PouchStartSuite) TestStartFromCheckpoint(c *check.C) {
SkipIfFalse(c, environment.IsCRIUExist)
name := "TestStartFromCheckpoint"
defer DelContainerForceMultyTime(c, name)
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
tmpDir, err := ioutil.TempDir("", "checkpoint")
c.Assert(err, check.IsNil)
defer os.RemoveAll(tmpDir)
checkpoint := "cp0"
command.PouchRun("checkpoint", "create", "--checkpoint-dir", tmpDir, name, checkpoint).Assert(c, icmd.Success)
restoredContainer := "restoredContainer"
defer DelContainerForceMultyTime(c, restoredContainer)
// image busybox not have /proc directory, we need to start busybox image and stop it
// make /proc exist, then we can restore successful
command.PouchRun("run", "-d", "--name", restoredContainer, busyboxImage).Assert(c, icmd.Success)
command.PouchRun("stop", restoredContainer).Assert(c, icmd.Success)
command.PouchRun("start", "--checkpoint-dir", tmpDir, "--checkpoint", checkpoint, restoredContainer).Assert(c, icmd.Success)
}
// TestStartMultiContainers tries to start more than one container.
func (suite *PouchStartSuite) TestStartMultiContainers(c *check.C) {
containernames := []string{"TestStartMultiContainer-1", "TestStartMultiContainer-2"}
for _, name := range containernames {
res := command.PouchRun("create", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}
res := command.PouchRun("start", containernames[0], containernames[1])
res.Assert(c, icmd.Success)
res = command.PouchRun("stop", containernames[0], containernames[1])
res.Assert(c, icmd.Success)
}