Skip to content

Commit

Permalink
Merge pull request AliyunContainerService#1341 from Letty5411/0517-as…
Browse files Browse the repository at this point in the history
…sertfix

test: enhance cli related tests
  • Loading branch information
allencloud authored May 17, 2018
2 parents 89a5ae5 + fd8e0f7 commit 3374daf
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 284 deletions.
65 changes: 41 additions & 24 deletions test/cli_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ func (suite *PouchCreateSuite) TearDownTest(c *check.C) {
func (suite *PouchCreateSuite) TestCreateName(c *check.C) {
name := "create-normal"
res := command.PouchRun("create", "--name", name, busyboxImage)

defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

defer DelContainerForceMultyTime(c, name)
}

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

res = command.PouchRun("create", "--name", name, imageID)

defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

DelContainerForceMultyTime(c, name)
}

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

res := command.PouchRun("create", "--name", name, busyboxImage)
res.Assert(c, icmd.Success)

defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

res = command.PouchRun("create", "--name", name, busyboxImage)
c.Assert(res.Error, check.NotNil)
c.Assert(res.Stderr(), check.NotNil)

if out := res.Combined(); !strings.Contains(out, "already exist") {
c.Fatalf("unexpected output %s expected already exist\n", out)
Expand All @@ -92,9 +89,9 @@ func (suite *PouchCreateSuite) TestCreateDuplicateContainerName(c *check.C) {
func (suite *PouchCreateSuite) TestCreateWithArgs(c *check.C) {
name := "TestCreateWithArgs"
res := command.PouchRun("create", "--name", name, busyboxImage, "/bin/ls")
res.Assert(c, icmd.Success)

defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)
}

// TestCreateWithTTY is to verify tty flag.
Expand All @@ -103,9 +100,9 @@ func (suite *PouchCreateSuite) TestCreateWithArgs(c *check.C) {
func (suite *PouchCreateSuite) TestCreateWithTTY(c *check.C) {
name := "TestCreateWithTTY"
res := command.PouchRun("create", "-t", "--name", name, busyboxImage)
res.Assert(c, icmd.Success)

defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)
}

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

res := command.PouchRun("create", "-v /tmp:/tmp", "--name", funcname, busyboxImage)
res.Assert(c, icmd.Success)

defer DelContainerForceMultyTime(c, funcname)

res.Assert(c, icmd.Success)
}

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

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

res := command.PouchRun("create", "--name", name, "-l", label, busyboxImage)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

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

res := command.PouchRun("create", "--name", name, "--sysctl", sysctl, busyboxImage)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

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

res := command.PouchRun("create", "--name", name, "--security-opt", appArmor, busyboxImage)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

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

res := command.PouchRun("create", "--name", name, "--security-opt", seccomp, busyboxImage)
res.Assert(c, icmd.Success)
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

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

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

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

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

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

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

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

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

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

res := command.PouchRun("create", "--name", name, "-e TEST=true", busyboxImage)
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

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

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

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

res := command.PouchRun("create", "--name", name, "--user", user, busyboxImage)
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

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

res := command.PouchRun("create", "--name", name, "--intel-rdt-l3-cbm", intelRdt, busyboxImage)
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

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

res := command.PouchRun("create", "--name", name, "--memory-wmark-ratio", memoryWmarkRatio, "--memory-extra", memoryExtra, "--memory-force-empty-ctl", "1", "--sche-lat-switch", "1", busyboxImage)
res := command.PouchRun("create", "--name", name, "--memory-wmark-ratio",
memoryWmarkRatio, "--memory-extra", memoryExtra, "--memory-force-empty-ctl", "1",
"--sche-lat-switch", "1", busyboxImage)
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

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

res := command.PouchRun("create", "--name", name, "--oom-score-adj", oomScore, "--oom-kill-disable", busyboxImage)
res := command.PouchRun("create", "--name", name, "--oom-score-adj", oomScore,
"--oom-kill-disable", busyboxImage)
defer DelContainerForceMultyTime(c, name)

res.Assert(c, icmd.Success)

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

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

output := command.PouchRun("inspect", cname).Stdout()
result := []types.ContainerJSON{}
Expand Down
16 changes: 11 additions & 5 deletions test/cli_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func (suite *PouchExecSuite) TestExecMultiCommands(c *check.C) {
// TestExecEcho tests exec prints the output.
func (suite *PouchExecSuite) TestExecEcho(c *check.C) {
name := "TestExecEcho"
command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

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

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

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

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

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

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

command.PouchRun("exec", name, "sh", "-c", "exit 101").Assert(c, icmd.Expected{ExitCode: 101})
command.PouchRun("exec", name, "sh", "-c", "exit 0").Assert(c, icmd.Success)
Expand Down
5 changes: 3 additions & 2 deletions test/cli_help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/alibaba/pouch/test/environment"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
)

// PouchHelpSuite is the test suite for help CLI.
Expand Down Expand Up @@ -33,9 +34,9 @@ func (suite *PouchHelpSuite) TestHelpWorks(c *check.C) {
for arg, ok := range args {
res := command.PouchRun(arg)
if ok {
c.Assert(res.Error, check.IsNil)
res.Assert(c, icmd.Success)
} else {
c.Assert(res.Error, check.NotNil)
c.Assert(res.Stderr(), check.NotNil)
}
}
}
21 changes: 10 additions & 11 deletions test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func (suite *PouchInspectSuite) TearDownTest(c *check.C) {
func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
name := "inspect-format-print"

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

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

DelContainerForceMultyTime(c, name)
}

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

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

res := command.PouchRun("inspect", "-f", "{{.NotExists}}", name)
c.Assert(res.Error, check.NotNil)
res = command.PouchRun("inspect", "-f", "{{.NotExists}}", name)
c.Assert(res.Stderr(), check.NotNil)

expectString := "Template parsing error"
if out := res.Combined(); !strings.Contains(out, expectString) {
c.Fatalf("unexpected output %s expected %s", out, expectString)
}

DelContainerForceMultyTime(c, name)

}

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

for _, errCase := range errorCases {
runContainers(errCase.containers)
defer delContainers(errCase.containers)
res := command.PouchRun("inspect", "-f", "{{.Name}}", errCase.args[0], errCase.args[1])
c.Assert(res.Error, check.NotNil)
c.Assert(res.Stderr(), check.NotNil)
output := res.Combined()
c.Assert(output, check.Equals, errCase.expectedOutput)
delContainers(errCase.containers)
}
}
Loading

0 comments on commit 3374daf

Please sign in to comment.