forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_run_test.go
424 lines (340 loc) · 13.2 KB
/
cli_run_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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/util"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)
// PouchRunSuite is the test suite for run CLI.
type PouchRunSuite struct{}
func init() {
check.Suite(&PouchRunSuite{})
}
// SetUpSuite does common setup in the beginning of each test suite.
func (suite *PouchRunSuite) 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 *PouchRunSuite) TearDownTest(c *check.C) {
}
// TestRun is to verify the correctness of run container with specified name.
func (suite *PouchRunSuite) TestRun(c *check.C) {
name := "test-run"
res := command.PouchRun("run", "-d", "--name", name,
busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
res = command.PouchRun("ps").Assert(c, icmd.Success)
if out := res.Combined(); !strings.Contains(out, name) {
c.Fatalf("unexpected output %s: should contains container %s\n",
out, name)
}
}
// TestRunPrintHi is to verify run container with executing a command.
func (suite *PouchRunSuite) TestRunPrintHi(c *check.C) {
name := "test-run-print-hi"
res := command.PouchRun("run", "--name", name, busyboxImage,
"echo", "hi")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
if out := res.Combined(); !strings.Contains(out, "hi") {
c.Fatalf("unexpected output %s expected hi\n", out)
}
}
// TestRunPrintHiByImageID is to verify run container
// with executing a command by image ID.
func (suite *PouchRunSuite) TestRunPrintHiByImageID(c *check.C) {
name := "test-run-print-hi-by-image-id"
res := command.PouchRun("images")
res.Assert(c, icmd.Success)
imageID := imagesListToKV(res.Combined())[busyboxImage][0]
res = command.PouchRun("run", "--name", name, imageID, "echo", "hi")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
if out := res.Combined(); !strings.Contains(out, "hi") {
c.Fatalf("unexpected output %s expected hi\n", out)
}
}
// TestRunInWrongWay tries to run create in wrong way.
func (suite *PouchRunSuite) TestRunInWrongWay(c *check.C) {
for _, tc := range []struct {
name string
args string
}{
{name: "unknown flag", args: "-a"},
// TODO: should add the following cases if ready
// {name: "missing image name", args: ""},
} {
res := command.PouchRun("run", tc.args)
c.Assert(res.Stderr(), check.NotNil, check.Commentf(tc.name))
}
}
// Comment this flaky test.
// TestRunRestartPolicyAlways is to verify restart policy always works.
//func (suite *PouchRunSuite) TestRunRestartPolicyAlways(c *check.C) {
// name := "TestRunRestartPolicyAlways"
//
// command.PouchRun("run", "--name", name, "-d", "--restart=always",
// busyboxImage, "sh", "-c", "sleep 10000").Assert(c, icmd.Success)
// command.PouchRun("stop", name).Assert(c, icmd.Success)
// time.Sleep(5000 * time.Millisecond)
//
// res := command.PouchRun("ps")
// res.Assert(c, icmd.Success)
//
// if out := res.Combined(); !strings.Contains(out, name) {
// c.Fatalf("expect container %s to be up: %s\n", name, out)
// }
// DelContainerForceMultyTime(c,name)
//}
// TestRunRestartPolicyNone is to verify restart policy none works.
func (suite *PouchRunSuite) TestRunRestartPolicyNone(c *check.C) {
name := "TestRunRestartPolicyNone"
res := command.PouchRun("run", "--name", name, "-d",
"--restart=no", busyboxImage,
"sh", "-c", "sleep 1")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
time.Sleep(2000 * time.Millisecond)
res = command.PouchRun("ps")
res.Assert(c, icmd.Success)
if out := res.Combined(); strings.Contains(out, name) {
c.Fatalf("expect container %s to be exited: %s\n", name, out)
}
}
// TestRunWithIPCMode is to verify --specific IPC mode when running a container.
// TODO: test container ipc namespace mode.
func (suite *PouchRunSuite) TestRunWithIPCMode(c *check.C) {
name := "test-run-with-ipc-mode"
res := command.PouchRun("run", "-d", "--name", name,
"--ipc", "host", busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}
// TestRunWithUTSMode is to verify --specific UTS mode when running a container.
func (suite *PouchRunSuite) TestRunWithUTSMode(c *check.C) {
name := "test-run-with-uts-mode"
res := command.PouchRun("run", "-d", "--name", name,
"--uts", "host", busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}
// TestRunWithSysctls is to verify run container with sysctls.
func (suite *PouchRunSuite) TestRunWithSysctls(c *check.C) {
sysctl := "net.ipv4.ip_forward=1"
name := "run-sysctl"
res := command.PouchRun("run", "-d", "--name", name,
"--sysctl", sysctl, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
output := command.PouchRun("exec", name,
"cat", "/proc/sys/net/ipv4/ip_forward").Stdout()
if !strings.Contains(output, "1") {
c.Fatalf("failed to run a container with sysctls: %s", output)
}
}
// TestRunWithAppArmor is to verify run container with security option AppArmor.
func (suite *PouchRunSuite) TestRunWithAppArmor(c *check.C) {
appArmor := "apparmor=unconfined"
name := "run-apparmor"
res := command.PouchRun("run", "-d", "--name", name,
"--security-opt", appArmor, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
// TODO: do the test more strictly with effective AppArmor profile.
}
// TestRunWithSeccomp is to verify run container with security option seccomp.
func (suite *PouchRunSuite) TestRunWithSeccomp(c *check.C) {
seccomp := "seccomp=unconfined"
name := "run-seccomp"
res := command.PouchRun("run", "-d", "--name", name,
"--security-opt", seccomp, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
// TODO: do the test more strictly with effective seccomp profile.
}
// TestRunWithCapability is to verify run container with capability.
func (suite *PouchRunSuite) TestRunWithCapability(c *check.C) {
capability := "NET_ADMIN"
name := "run-capability"
res := command.PouchRun("run", "--name", name, "--cap-add", capability,
busyboxImage, "brctl", "addbr", "foobar")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}
// TestRunWithoutCapability tests running container with --cap-drop
func (suite *PouchRunSuite) TestRunWithoutCapability(c *check.C) {
capability := "chown"
name := "run-capability"
expt := icmd.Expected{
Err: "Operation not permitted",
}
command.PouchRun("run", "--name", name, "--cap-drop", capability,
busyboxImage, "chown", "755", "/tmp").Compare(expt)
defer DelContainerForceMultyTime(c, name)
}
// TestRunWithPrivilege is to verify run container with privilege.
func (suite *PouchRunSuite) TestRunWithPrivilege(c *check.C) {
name := "run-privilege"
res := command.PouchRun("run", "--name", name, "--privileged",
busyboxImage, "brctl", "addbr", "foobar")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}
// checkFileContains checks the content of fname contains expt
func checkFileContains(c *check.C, fname string, expt string) {
cmdResult := icmd.RunCommand("cat", fname)
cmdResult.Assert(c, icmd.Success)
c.Assert(strings.Contains(string(cmdResult.Stdout()), expt),
check.Equals, true)
}
//
func (suite *PouchRunSuite) TestRunAlikernelScheLatSwitch(c *check.C) {
// TODO: as runc has not implemented it, add test later
SkipIfFalse(c, environment.IsAliKernel)
}
//
func (suite *PouchRunSuite) TestRunAlikernelMemoryWmarkRatio(c *check.C) {
// TODO: as runc has not implemented it, add test later
SkipIfFalse(c, environment.IsAliKernel)
}
//
func (suite *PouchRunSuite) TestRunAlikernelMemoryExtra(c *check.C) {
// TODO: as runc has not implemented it, add test later
SkipIfFalse(c, environment.IsAliKernel)
}
//
func (suite *PouchRunSuite) TestRunAlikernelMemoryForceEmptyCtl(c *check.C) {
// TODO: as runc has not implemented it, add test later
SkipIfFalse(c, environment.IsAliKernel)
}
// TestRunWithAnnotation is to verify the valid running container
// with annotation, and verify SpecAnnotation filed has been in inspect output.
func (suite *PouchRunSuite) TestRunWithAnnotation(c *check.C) {
cname := "TestRunWithAnnotation"
res := command.PouchRun("run", "-d", "--annotation", "a=b",
"--annotation", "foo=bar",
"--name", cname, busyboxImage, "top").Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)
output := command.PouchRun("inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
// kv in map not in order.
var annotationSlice []string
for k, v := range result[0].Config.SpecAnnotation {
annotationSlice = append(annotationSlice, fmt.Sprintf("%s=%s", k, v))
}
annotationStr := strings.Join(annotationSlice, " ")
c.Assert(util.PartialEqual(annotationStr, "a=b"), check.IsNil)
c.Assert(util.PartialEqual(annotationStr, "foo=bar"), check.IsNil)
}
// TestRunWithExitCode is to verify the valid running container with exit code != 0.
func (suite *PouchRunSuite) TestRunWithExitCode(c *check.C) {
cname := "TestRunWithExitCode"
ret := command.PouchRun("run", "--name", cname, busyboxImage,
"sh", "-c", "exit 101")
defer DelContainerForceMultyTime(c, cname)
// test process exit code $? == 101
ret.Assert(c, icmd.Expected{ExitCode: 101})
// test container ExitCode == 101
output := command.PouchRun("inspect", cname).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))
}
// TestRunWithRM is to verify the valid running container with rm flag
func (suite *PouchRunSuite) TestRunWithRM(c *check.C) {
cname := "TestRunWithRM"
res := command.PouchRun("run", "--rm", "--name", cname, busyboxImage,
"echo", "hello")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)
output := command.PouchRun("inspect", cname).Stderr()
c.Assert(util.PartialEqual(output, cname+": not found"), check.IsNil)
}
// TestRunWithDisableNetworkFiles is to verify running container with disable-network-files flag.
func (suite *PouchRunSuite) TestRunWithDisableNetworkFiles(c *check.C) {
// Run a container with disable-network-files flag
cname1 := "RunWithDisableNetworkFiles"
res := command.PouchRun("run", "--disable-network-files", "--name", cname1,
busyboxImage, "ls", "/etc")
defer DelContainerForceMultyTime(c, cname1)
res.Assert(c, icmd.Success)
output := res.Stdout()
if strings.Contains(output, "hostname") {
c.Fatal("expected no /etc/hostname, but the file exists")
}
if strings.Contains(output, "hosts") {
c.Fatal("expected no /etc/hosts, but the file exists")
}
if strings.Contains(output, "resolv.conf") {
// ignore checking the existence of /etc/resolv.conf, because the busybox
// contains the file.
}
// Run a container without disable-network-files flag
cname2 := "RunWithoutDisableNetworkFiles"
res = command.PouchRun("run", "--name", cname2, busyboxImage, "ls", "/etc")
defer DelContainerForceMultyTime(c, cname2)
res.Assert(c, icmd.Success)
output = res.Stdout()
if !strings.Contains(output, "hostname") {
c.Fatal("expected /etc/hostname, but the file does not exist")
}
if !strings.Contains(output, "hosts") {
c.Fatal("expected /etc/hosts, but the file does not exist")
}
if !strings.Contains(output, "resolv.conf") {
c.Fatal("expected /etc/resolv.conf, but the file does not exist")
}
}
// TestRunWithShm is to verify the valid running container
// with shm-size
func (suite *PouchRunMemorySuite) TestRunWithShm(c *check.C) {
cname := "TestRunWithShm"
res := command.PouchRun("run", "-d", "--shm-size", "1g",
"--name", cname, busyboxImage, "top")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)
// test if the value is in inspect result
res = command.PouchRun("inspect", cname)
res.Assert(c, icmd.Success)
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(res.Stdout()), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
c.Assert(int64(*result[0].HostConfig.ShmSize),
check.Equals, int64(1073741824))
containerID := result[0].ID
res = command.PouchRun("exec", containerID, "df", "-k", "/dev/shm")
res.Assert(c, icmd.Success)
util.PartialEqual(res.Stdout(), "1048576")
}
// TestRunSetRunningFlag is to verfy whether set Running Flag in ContainerState
// when started a container
func (suite *PouchRunSuite) TestRunSetRunningFlag(c *check.C) {
cname := "TestRunSetRunningFlag"
res := command.PouchRun("run", "-d", "--name", cname, busyboxImage, "top")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)
// test if the value is in inspect result
res = command.PouchRun("inspect", cname)
res.Assert(c, icmd.Success)
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(res.Stdout()), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
c.Assert(result[0].State.Running, check.Equals, true)
}