Skip to content

Commit 3374daf

Browse files
authored
Merge pull request AliyunContainerService#1341 from Letty5411/0517-assertfix
test: enhance cli related tests
2 parents 89a5ae5 + fd8e0f7 commit 3374daf

19 files changed

+335
-284
lines changed

test/cli_create_test.go

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ func (suite *PouchCreateSuite) TearDownTest(c *check.C) {
4040
func (suite *PouchCreateSuite) TestCreateName(c *check.C) {
4141
name := "create-normal"
4242
res := command.PouchRun("create", "--name", name, busyboxImage)
43-
43+
defer DelContainerForceMultyTime(c, name)
4444
res.Assert(c, icmd.Success)
4545

4646
// create command should add newline at the end of result
4747
digStr := strings.TrimSpace(res.Combined())
4848
c.Assert(res.Combined(), check.Equals, fmt.Sprintf("%s\n", digStr))
4949

50-
defer DelContainerForceMultyTime(c, name)
5150
}
5251

5352
// TestCreateNameByImageID is to verify the correctness of creating contaier with specified name by image id.
@@ -59,27 +58,25 @@ func (suite *PouchCreateSuite) TestCreateNameByImageID(c *check.C) {
5958
imageID := imagesListToKV(res.Combined())[busyboxImage][0]
6059

6160
res = command.PouchRun("create", "--name", name, imageID)
62-
61+
defer DelContainerForceMultyTime(c, name)
6362
res.Assert(c, icmd.Success)
6463

6564
digHexStr := strings.TrimSpace(res.Combined())
6665
_, err := digest.Parse(fmt.Sprintf("%s:%s", digest.SHA256, digHexStr))
6766
c.Assert(err, check.IsNil)
6867

69-
DelContainerForceMultyTime(c, name)
7068
}
7169

7270
// TestCreateDuplicateContainerName is to verify duplicate container names.
7371
func (suite *PouchCreateSuite) TestCreateDuplicateContainerName(c *check.C) {
7472
name := "duplicate"
7573

7674
res := command.PouchRun("create", "--name", name, busyboxImage)
77-
res.Assert(c, icmd.Success)
78-
7975
defer DelContainerForceMultyTime(c, name)
76+
res.Assert(c, icmd.Success)
8077

8178
res = command.PouchRun("create", "--name", name, busyboxImage)
82-
c.Assert(res.Error, check.NotNil)
79+
c.Assert(res.Stderr(), check.NotNil)
8380

8481
if out := res.Combined(); !strings.Contains(out, "already exist") {
8582
c.Fatalf("unexpected output %s expected already exist\n", out)
@@ -92,9 +89,9 @@ func (suite *PouchCreateSuite) TestCreateDuplicateContainerName(c *check.C) {
9289
func (suite *PouchCreateSuite) TestCreateWithArgs(c *check.C) {
9390
name := "TestCreateWithArgs"
9491
res := command.PouchRun("create", "--name", name, busyboxImage, "/bin/ls")
95-
res.Assert(c, icmd.Success)
96-
9792
defer DelContainerForceMultyTime(c, name)
93+
94+
res.Assert(c, icmd.Success)
9895
}
9996

10097
// TestCreateWithTTY is to verify tty flag.
@@ -103,9 +100,9 @@ func (suite *PouchCreateSuite) TestCreateWithArgs(c *check.C) {
103100
func (suite *PouchCreateSuite) TestCreateWithTTY(c *check.C) {
104101
name := "TestCreateWithTTY"
105102
res := command.PouchRun("create", "-t", "--name", name, busyboxImage)
106-
res.Assert(c, icmd.Success)
107-
108103
defer DelContainerForceMultyTime(c, name)
104+
105+
res.Assert(c, icmd.Success)
109106
}
110107

111108
// TestPouchCreateVolume is to verify volume flag.
@@ -120,9 +117,9 @@ func (suite *PouchCreateSuite) TestPouchCreateVolume(c *check.C) {
120117
}
121118

122119
res := command.PouchRun("create", "-v /tmp:/tmp", "--name", funcname, busyboxImage)
123-
res.Assert(c, icmd.Success)
124-
125120
defer DelContainerForceMultyTime(c, funcname)
121+
122+
res.Assert(c, icmd.Success)
126123
}
127124

128125
// TestCreateInWrongWay tries to run create in wrong way.
@@ -137,7 +134,7 @@ func (suite *PouchCreateSuite) TestCreateInWrongWay(c *check.C) {
137134
// {name: "missing image name", args: ""},
138135
} {
139136
res := command.PouchRun("create", tc.args)
140-
c.Assert(res.Error, check.NotNil, check.Commentf(tc.name))
137+
c.Assert(res.Stderr(), check.NotNil, check.Commentf(tc.name))
141138
}
142139
}
143140

@@ -147,8 +144,8 @@ func (suite *PouchCreateSuite) TestCreateWithLabels(c *check.C) {
147144
name := "create-label"
148145

149146
res := command.PouchRun("create", "--name", name, "-l", label, busyboxImage)
150-
res.Assert(c, icmd.Success)
151147
defer DelContainerForceMultyTime(c, name)
148+
res.Assert(c, icmd.Success)
152149

153150
output := command.PouchRun("inspect", name).Stdout()
154151

@@ -169,8 +166,8 @@ func (suite *PouchCreateSuite) TestCreateWithSysctls(c *check.C) {
169166
name := "create-sysctl"
170167

171168
res := command.PouchRun("create", "--name", name, "--sysctl", sysctl, busyboxImage)
172-
res.Assert(c, icmd.Success)
173169
defer DelContainerForceMultyTime(c, name)
170+
res.Assert(c, icmd.Success)
174171

175172
output := command.PouchRun("inspect", name).Stdout()
176173

@@ -191,8 +188,8 @@ func (suite *PouchCreateSuite) TestCreateWithAppArmor(c *check.C) {
191188
name := "create-apparmor"
192189

193190
res := command.PouchRun("create", "--name", name, "--security-opt", appArmor, busyboxImage)
194-
res.Assert(c, icmd.Success)
195191
defer DelContainerForceMultyTime(c, name)
192+
res.Assert(c, icmd.Success)
196193

197194
output := command.PouchRun("inspect", name).Stdout()
198195

@@ -219,8 +216,8 @@ func (suite *PouchCreateSuite) TestCreateWithSeccomp(c *check.C) {
219216
name := "create-seccomp"
220217

221218
res := command.PouchRun("create", "--name", name, "--security-opt", seccomp, busyboxImage)
222-
res.Assert(c, icmd.Success)
223219
defer DelContainerForceMultyTime(c, name)
220+
res.Assert(c, icmd.Success)
224221

225222
output := command.PouchRun("inspect", name).Stdout()
226223

@@ -247,8 +244,8 @@ func (suite *PouchCreateSuite) TestCreateWithCapability(c *check.C) {
247244
name := "create-capability"
248245

249246
res := command.PouchRun("create", "--name", name, "--cap-add", capability, busyboxImage, "brctl", "addbr", "foobar")
250-
res.Assert(c, icmd.Success)
251247
defer DelContainerForceMultyTime(c, name)
248+
res.Assert(c, icmd.Success)
252249

253250
output := command.PouchRun("inspect", name).Stdout()
254251

@@ -274,8 +271,8 @@ func (suite *PouchCreateSuite) TestCreateWithPrivilege(c *check.C) {
274271
name := "create-privilege"
275272

276273
res := command.PouchRun("create", "--name", name, "--privileged", busyboxImage, "brctl", "addbr", "foobar")
277-
res.Assert(c, icmd.Success)
278274
defer DelContainerForceMultyTime(c, name)
275+
res.Assert(c, icmd.Success)
279276

280277
output := command.PouchRun("inspect", name).Stdout()
281278

@@ -291,6 +288,7 @@ func (suite *PouchCreateSuite) TestCreateEnableLxcfs(c *check.C) {
291288
name := "create-lxcfs"
292289

293290
res := command.PouchRun("create", "--name", name, "--enableLxcfs=true", busyboxImage)
291+
defer DelContainerForceMultyTime(c, name)
294292
res.Assert(c, icmd.Success)
295293

296294
output := command.PouchRun("inspect", name).Stdout()
@@ -311,6 +309,8 @@ func (suite *PouchCreateSuite) TestCreateWithEnv(c *check.C) {
311309
name := "TestCreateWithEnv"
312310

313311
res := command.PouchRun("create", "--name", name, "-e TEST=true", busyboxImage)
312+
defer DelContainerForceMultyTime(c, name)
313+
314314
res.Assert(c, icmd.Success)
315315

316316
output := command.PouchRun("inspect", name).Stdout()
@@ -334,6 +334,7 @@ func (suite *PouchCreateSuite) TestCreateWithWorkDir(c *check.C) {
334334
name := "TestCreateWithWorkDir"
335335

336336
res := command.PouchRun("create", "--name", name, "-w /tmp/test", busyboxImage)
337+
defer DelContainerForceMultyTime(c, name)
337338
res.Assert(c, icmd.Success)
338339

339340
output := command.PouchRun("inspect", name).Stdout()
@@ -353,6 +354,8 @@ func (suite *PouchCreateSuite) TestCreateWithUser(c *check.C) {
353354
user := "1001"
354355

355356
res := command.PouchRun("create", "--name", name, "--user", user, busyboxImage)
357+
defer DelContainerForceMultyTime(c, name)
358+
356359
res.Assert(c, icmd.Success)
357360

358361
output := command.PouchRun("inspect", name).Stdout()
@@ -370,6 +373,8 @@ func (suite *PouchCreateSuite) TestCreateWithIntelRdt(c *check.C) {
370373
intelRdt := "L3:<cache_id0>=<cbm0>"
371374

372375
res := command.PouchRun("create", "--name", name, "--intel-rdt-l3-cbm", intelRdt, busyboxImage)
376+
defer DelContainerForceMultyTime(c, name)
377+
373378
res.Assert(c, icmd.Success)
374379

375380
output := command.PouchRun("inspect", name).Stdout()
@@ -387,7 +392,11 @@ func (suite *PouchCreateSuite) TestCreateWithAliOSMemoryOptions(c *check.C) {
387392
memoryWmarkRatio := "30"
388393
memoryExtra := "50"
389394

390-
res := command.PouchRun("create", "--name", name, "--memory-wmark-ratio", memoryWmarkRatio, "--memory-extra", memoryExtra, "--memory-force-empty-ctl", "1", "--sche-lat-switch", "1", busyboxImage)
395+
res := command.PouchRun("create", "--name", name, "--memory-wmark-ratio",
396+
memoryWmarkRatio, "--memory-extra", memoryExtra, "--memory-force-empty-ctl", "1",
397+
"--sche-lat-switch", "1", busyboxImage)
398+
defer DelContainerForceMultyTime(c, name)
399+
391400
res.Assert(c, icmd.Success)
392401

393402
output := command.PouchRun("inspect", name).Stdout()
@@ -407,7 +416,10 @@ func (suite *PouchCreateSuite) TestCreateWithOOMOption(c *check.C) {
407416
name := "TestCreateWithOOMOption"
408417
oomScore := "100"
409418

410-
res := command.PouchRun("create", "--name", name, "--oom-score-adj", oomScore, "--oom-kill-disable", busyboxImage)
419+
res := command.PouchRun("create", "--name", name, "--oom-score-adj", oomScore,
420+
"--oom-kill-disable", busyboxImage)
421+
defer DelContainerForceMultyTime(c, name)
422+
411423
res.Assert(c, icmd.Success)
412424

413425
output := command.PouchRun("inspect", name).Stdout()
@@ -423,7 +435,10 @@ func (suite *PouchCreateSuite) TestCreateWithOOMOption(c *check.C) {
423435
// TestCreateWithAnnotation tests creating container with annotation.
424436
func (suite *PouchCreateSuite) TestCreateWithAnnotation(c *check.C) {
425437
cname := "TestCreateWithAnnotation"
426-
command.PouchRun("create", "--annotation", "a=b", "--annotation", "foo=bar", "--name", cname, busyboxImage).Stdout()
438+
res := command.PouchRun("create", "--annotation", "a=b", "--annotation", "foo=bar",
439+
"--name", cname, busyboxImage)
440+
defer DelContainerForceMultyTime(c, cname)
441+
res.Assert(c, icmd.Success)
427442

428443
output := command.PouchRun("inspect", cname).Stdout()
429444
result := []types.ContainerJSON{}
@@ -445,7 +460,9 @@ func (suite *PouchCreateSuite) TestCreateWithAnnotation(c *check.C) {
445460
// TestCreateWithUlimit tests creating container with annotation.
446461
func (suite *PouchCreateSuite) TestCreateWithUlimit(c *check.C) {
447462
cname := "TestCreateWithUlimit"
448-
command.PouchRun("create", "--ulimit", "nproc=21", "--name", cname, busyboxImage).Assert(c, icmd.Success)
463+
res := command.PouchRun("create", "--ulimit", "nproc=21", "--name", cname, busyboxImage)
464+
defer DelContainerForceMultyTime(c, cname)
465+
res.Assert(c, icmd.Success)
449466

450467
output := command.PouchRun("inspect", cname).Stdout()
451468
result := []types.ContainerJSON{}

test/cli_exec_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ func (suite *PouchExecSuite) TestExecMultiCommands(c *check.C) {
7474
// TestExecEcho tests exec prints the output.
7575
func (suite *PouchExecSuite) TestExecEcho(c *check.C) {
7676
name := "TestExecEcho"
77-
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
77+
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
7878
defer DelContainerForceMultyTime(c, name)
79+
res.Assert(c, icmd.Success)
7980

8081
out := command.PouchRun("exec", name, "echo", "test").Stdout()
8182
if !strings.Contains(out, "test") {
@@ -86,8 +87,9 @@ func (suite *PouchExecSuite) TestExecEcho(c *check.C) {
8687
// TestExecStoppedContainer test exec in a stopped container fail.
8788
func (suite *PouchExecSuite) TestExecStoppedContainer(c *check.C) {
8889
name := "TestExecStoppedContainer"
89-
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
90+
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
9091
defer DelContainerForceMultyTime(c, name)
92+
res.Assert(c, icmd.Success)
9193

9294
command.PouchRun("stop", name).Assert(c, icmd.Success)
9395

@@ -105,8 +107,9 @@ func (suite *PouchExecSuite) TestExecInteractive(c *check.C) {
105107
// TestExecAfterContainerRestart test exec in a restart container should work.
106108
func (suite *PouchExecSuite) TestExecAfterContainerRestart(c *check.C) {
107109
name := "TestExecAfterContainerRestart"
108-
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
110+
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
109111
defer DelContainerForceMultyTime(c, name)
112+
res.Assert(c, icmd.Success)
110113

111114
command.PouchRun("stop", name).Assert(c, icmd.Success)
112115

@@ -121,8 +124,10 @@ func (suite *PouchExecSuite) TestExecAfterContainerRestart(c *check.C) {
121124
// TestExecUlimit test ulimit set container.
122125
func (suite *PouchExecSuite) TestExecUlimit(c *check.C) {
123126
name := "TestExecUlimit"
124-
command.PouchRun("run", "-d", "--name", name, "--ulimit", "nproc=256", busyboxImage, "top").Assert(c, icmd.Success)
127+
res := command.PouchRun("run", "-d", "--name", name, "--ulimit", "nproc=256",
128+
busyboxImage, "top")
125129
defer DelContainerForceMultyTime(c, name)
130+
res.Assert(c, icmd.Success)
126131

127132
out := command.PouchRun("exec", name, "sh", "-c", "ulimit -p").Stdout()
128133
c.Assert(out, check.Equals, "256\n")
@@ -131,8 +136,9 @@ func (suite *PouchExecSuite) TestExecUlimit(c *check.C) {
131136
// TestExecExitCode test exit code after exec process exit.
132137
func (suite *PouchExecSuite) TestExecExitCode(c *check.C) {
133138
name := "TestExecExitCode"
134-
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
139+
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
135140
defer DelContainerForceMultyTime(c, name)
141+
res.Assert(c, icmd.Success)
136142

137143
command.PouchRun("exec", name, "sh", "-c", "exit 101").Assert(c, icmd.Expected{ExitCode: 101})
138144
command.PouchRun("exec", name, "sh", "-c", "exit 0").Assert(c, icmd.Success)

test/cli_help_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/alibaba/pouch/test/environment"
66

77
"github.com/go-check/check"
8+
"github.com/gotestyourself/gotestyourself/icmd"
89
)
910

1011
// PouchHelpSuite is the test suite for help CLI.
@@ -33,9 +34,9 @@ func (suite *PouchHelpSuite) TestHelpWorks(c *check.C) {
3334
for arg, ok := range args {
3435
res := command.PouchRun(arg)
3536
if ok {
36-
c.Assert(res.Error, check.IsNil)
37+
res.Assert(c, icmd.Success)
3738
} else {
38-
c.Assert(res.Error, check.NotNil)
39+
c.Assert(res.Stderr(), check.NotNil)
3940
}
4041
}
4142
}

test/cli_inspect_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func (suite *PouchInspectSuite) TearDownTest(c *check.C) {
3737
func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
3838
name := "inspect-format-print"
3939

40-
command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage).Assert(c, icmd.Success)
40+
res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage)
41+
defer DelContainerForceMultyTime(c, name)
42+
res.Assert(c, icmd.Success)
4143

4244
output := command.PouchRun("inspect", name).Stdout()
4345
result := []types.ContainerJSON{}
@@ -53,26 +55,23 @@ func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
5355
// inspect Memory
5456
output = command.PouchRun("inspect", "-f", "{{.HostConfig.Memory}}", name).Stdout()
5557
c.Assert(output, check.Equals, fmt.Sprintf("%d\n", result[0].HostConfig.Memory))
56-
57-
DelContainerForceMultyTime(c, name)
5858
}
5959

6060
// TestInspectWrongFormat is to verify using wrong format flag of inspect command.
6161
func (suite *PouchInspectSuite) TestInspectWrongFormat(c *check.C) {
6262
name := "inspect-wrong-format-print"
6363

64-
command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage).Assert(c, icmd.Success)
64+
res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage)
65+
defer DelContainerForceMultyTime(c, name)
66+
res.Assert(c, icmd.Success)
6567

66-
res := command.PouchRun("inspect", "-f", "{{.NotExists}}", name)
67-
c.Assert(res.Error, check.NotNil)
68+
res = command.PouchRun("inspect", "-f", "{{.NotExists}}", name)
69+
c.Assert(res.Stderr(), check.NotNil)
6870

6971
expectString := "Template parsing error"
7072
if out := res.Combined(); !strings.Contains(out, expectString) {
7173
c.Fatalf("unexpected output %s expected %s", out, expectString)
7274
}
73-
74-
DelContainerForceMultyTime(c, name)
75-
7675
}
7776

7877
// TestMultiInspect is to verify inspect command with multiple args.
@@ -134,10 +133,10 @@ func (suite *PouchInspectSuite) TestMultiInspectErrors(c *check.C) {
134133

135134
for _, errCase := range errorCases {
136135
runContainers(errCase.containers)
136+
defer delContainers(errCase.containers)
137137
res := command.PouchRun("inspect", "-f", "{{.Name}}", errCase.args[0], errCase.args[1])
138-
c.Assert(res.Error, check.NotNil)
138+
c.Assert(res.Stderr(), check.NotNil)
139139
output := res.Combined()
140140
c.Assert(output, check.Equals, errCase.expectedOutput)
141-
delContainers(errCase.containers)
142141
}
143142
}

0 commit comments

Comments
 (0)