1
0
mirror of https://github.com/moby/moby.git synced 2025-08-08 13:22:22 +03:00

Replace some checkers and assertions with gotest.tools

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2019-04-04 15:23:19 +02:00
parent 86f2ac4a6b
commit 6345208b9b
82 changed files with 2931 additions and 3030 deletions

View File

@@ -17,23 +17,22 @@ import (
"time"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/go-check/check"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
"gotest.tools/icmd"
)
func (s *DockerSuite) TestExec(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
assert.NilError(c, waitRun(strings.TrimSpace(out)))
out, _ = dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
out = strings.Trim(out, "\r\n")
c.Assert(out, checker.Equals, "test")
assert.Equal(c, strings.Trim(out, "\r\n"), "test")
}
func (s *DockerSuite) TestExecInteractive(c *check.C) {
@@ -42,22 +41,22 @@ func (s *DockerSuite) TestExecInteractive(c *check.C) {
execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
stdin, err := execCmd.StdinPipe()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
stdout, err := execCmd.StdoutPipe()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
err = execCmd.Start()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
_, err = stdin.Write([]byte("cat /tmp/file\n"))
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
r := bufio.NewReader(stdout)
line, err := r.ReadString('\n')
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
line = strings.TrimSpace(line)
c.Assert(line, checker.Equals, "test")
assert.Equal(c, line, "test")
err = stdin.Close()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
errChan := make(chan error)
go func() {
errChan <- execCmd.Wait()
@@ -65,7 +64,7 @@ func (s *DockerSuite) TestExecInteractive(c *check.C) {
}()
select {
case err := <-errChan:
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
case <-time.After(1 * time.Second):
c.Fatal("docker exec failed to exit on stdin close")
}
@@ -75,13 +74,12 @@ func (s *DockerSuite) TestExecInteractive(c *check.C) {
func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
out := runSleepingContainer(c)
cleanedContainerID := strings.TrimSpace(out)
c.Assert(waitRun(cleanedContainerID), check.IsNil)
assert.NilError(c, waitRun(cleanedContainerID))
dockerCmd(c, "restart", cleanedContainerID)
c.Assert(waitRun(cleanedContainerID), check.IsNil)
assert.NilError(c, waitRun(cleanedContainerID))
out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
outStr := strings.TrimSpace(out)
c.Assert(outStr, checker.Equals, "hello")
assert.Equal(c, strings.TrimSpace(out), "hello")
}
func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
@@ -90,18 +88,16 @@ func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
s.d.StartWithBusybox(c)
out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top")
c.Assert(err, checker.IsNil, check.Commentf("Could not run top: %s", out))
assert.NilError(c, err, "Could not run top: %s", out)
s.d.Restart(c)
out, err = s.d.Cmd("start", "top")
c.Assert(err, checker.IsNil, check.Commentf("Could not start top after daemon restart: %s", out))
assert.NilError(c, err, "Could not start top after daemon restart: %s", out)
out, err = s.d.Cmd("exec", "top", "echo", "hello")
c.Assert(err, checker.IsNil, check.Commentf("Could not exec on container top: %s", out))
outStr := strings.TrimSpace(string(out))
c.Assert(outStr, checker.Equals, "hello")
assert.NilError(c, err, "Could not exec on container top: %s", out)
assert.Equal(c, strings.TrimSpace(out), "hello")
}
// Regression test for #9155, #9044
@@ -112,23 +108,23 @@ func (s *DockerSuite) TestExecEnv(c *check.C) {
// a subsequent exec will not have LALA set/
testRequires(c, DaemonIsLinux)
runSleepingContainer(c, "-e", "LALA=value1", "-e", "LALA=value2", "-d", "--name", "testing")
c.Assert(waitRun("testing"), check.IsNil)
assert.NilError(c, waitRun("testing"))
out, _ := dockerCmd(c, "exec", "testing", "env")
c.Assert(out, checker.Not(checker.Contains), "LALA=value1")
c.Assert(out, checker.Contains, "LALA=value2")
c.Assert(out, checker.Contains, "HOME=/root")
assert.Check(c, !strings.Contains(out, "LALA=value1"))
assert.Check(c, strings.Contains(out, "LALA=value2"))
assert.Check(c, strings.Contains(out, "HOME=/root"))
}
func (s *DockerSuite) TestExecSetEnv(c *check.C) {
testRequires(c, DaemonIsLinux)
runSleepingContainer(c, "-e", "HOME=/root", "-d", "--name", "testing")
c.Assert(waitRun("testing"), check.IsNil)
assert.NilError(c, waitRun("testing"))
out, _ := dockerCmd(c, "exec", "-e", "HOME=/another", "-e", "ABC=xyz", "testing", "env")
c.Assert(out, checker.Not(checker.Contains), "HOME=/root")
c.Assert(out, checker.Contains, "HOME=/another")
c.Assert(out, checker.Contains, "ABC=xyz")
assert.Check(c, !strings.Contains(out, "HOME=/root"))
assert.Check(c, strings.Contains(out, "HOME=/another"))
assert.Check(c, strings.Contains(out, "ABC=xyz"))
}
func (s *DockerSuite) TestExecExitStatus(c *check.C) {
@@ -146,10 +142,10 @@ func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
dockerCmd(c, "pause", "testing")
out, _, err := dockerCmdWithError("exec", ContainerID, "echo", "hello")
c.Assert(err, checker.NotNil, check.Commentf("container should fail to exec new command if it is paused"))
assert.ErrorContains(c, err, "", "container should fail to exec new command if it is paused")
expected := ContainerID + " is paused, unpause the container before exec"
c.Assert(out, checker.Contains, expected, check.Commentf("container should not exec new command if it is paused"))
assert.Assert(c, is.Contains(out, expected), "container should not exec new command if it is paused")
}
// regression test for #9476
@@ -160,24 +156,24 @@ func (s *DockerSuite) TestExecTTYCloseStdin(c *check.C) {
cmd := exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
stdinRw, err := cmd.StdinPipe()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
stdinRw.Write([]byte("test"))
stdinRw.Close()
out, _, err := runCommandWithOutput(cmd)
c.Assert(err, checker.IsNil, check.Commentf("%s", out))
assert.NilError(c, err, out)
out, _ = dockerCmd(c, "top", "exec_tty_stdin")
outArr := strings.Split(out, "\n")
c.Assert(len(outArr), checker.LessOrEqualThan, 3, check.Commentf("exec process left running"))
c.Assert(out, checker.Not(checker.Contains), "nsenter-exec")
assert.Assert(c, len(outArr) <= 3, "exec process left running")
assert.Assert(c, !strings.Contains(out, "nsenter-exec"))
}
func (s *DockerSuite) TestExecTTYWithoutStdin(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
id := strings.TrimSpace(out)
c.Assert(waitRun(id), checker.IsNil)
assert.NilError(c, waitRun(id))
errChan := make(chan error)
go func() {
@@ -204,7 +200,7 @@ func (s *DockerSuite) TestExecTTYWithoutStdin(c *check.C) {
select {
case err := <-errChan:
c.Assert(err, check.IsNil)
assert.NilError(c, err)
case <-time.After(3 * time.Second):
c.Fatal("exec is running but should have failed")
}
@@ -249,7 +245,7 @@ func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
case <-time.After(3 * time.Second):
c.Fatal("Container stop timed out")
case s := <-ch:
c.Assert(s.err, check.IsNil)
assert.NilError(c, s.err)
}
}
@@ -287,7 +283,7 @@ func (s *DockerSuite) TestExecCgroup(c *check.C) {
close(errChan)
for err := range errChan {
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
}
for _, cg := range execCgroups {
@@ -311,14 +307,14 @@ func (s *DockerSuite) TestExecInspectID(c *check.C) {
id := strings.TrimSuffix(out, "\n")
out = inspectField(c, id, "ExecIDs")
c.Assert(out, checker.Equals, "[]", check.Commentf("ExecIDs should be empty, got: %s", out))
assert.Equal(c, out, "[]", "ExecIDs should be empty, got: %s", out)
// Start an exec, have it block waiting so we can do some checking
cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
"while ! test -e /execid1; do sleep 1; done")
err := cmd.Start()
c.Assert(err, checker.IsNil, check.Commentf("failed to start the exec cmd"))
assert.NilError(c, err, "failed to start the exec cmd")
// Give the exec 10 chances/seconds to start then give up and stop the test
tries := 10
@@ -329,19 +325,17 @@ func (s *DockerSuite) TestExecInspectID(c *check.C) {
if out != "[]" && out != "<no value>" {
break
}
c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still empty after 10 second"))
assert.Check(c, i+1 != tries, "ExecIDs still empty after 10 second")
time.Sleep(1 * time.Second)
}
// Save execID for later
execID, err := inspectFilter(id, "index .ExecIDs 0")
c.Assert(err, checker.IsNil, check.Commentf("failed to get the exec id"))
assert.NilError(c, err, "failed to get the exec id")
// End the exec by creating the missing file
err = exec.Command(dockerBinary, "exec", id,
"sh", "-c", "touch /execid1").Run()
c.Assert(err, checker.IsNil, check.Commentf("failed to run the 2nd exec cmd"))
err = exec.Command(dockerBinary, "exec", id, "sh", "-c", "touch /execid1").Run()
assert.NilError(c, err, "failed to run the 2nd exec cmd")
// Wait for 1st exec to complete
cmd.Wait()
@@ -354,26 +348,25 @@ func (s *DockerSuite) TestExecInspectID(c *check.C) {
if out == "[]" {
break
}
c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still not empty after 10 second"))
assert.Check(c, i+1 != tries, "ExecIDs still empty after 10 second")
time.Sleep(1 * time.Second)
}
// But we should still be able to query the execID
cli, err := client.NewClientWithOpts(client.FromEnv)
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
defer cli.Close()
_, err = cli.ContainerExecInspect(context.Background(), execID)
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
// Now delete the container and then an 'inspect' on the exec should
// result in a 404 (not 'container not running')
out, ec := dockerCmd(c, "rm", "-f", id)
c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out))
assert.Equal(c, ec, 0, "error removing container: %s", out)
_, err = cli.ContainerExecInspect(context.Background(), execID)
expected := "No such exec instance"
c.Assert(err.Error(), checker.Contains, expected)
assert.ErrorContains(c, err, "No such exec instance")
}
func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
@@ -382,10 +375,10 @@ func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
var out string
out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
idA := strings.TrimSpace(out)
c.Assert(idA, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
assert.Assert(c, idA != "", "%s, id should not be nil", out)
out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
idB := strings.TrimSpace(out)
c.Assert(idB, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
assert.Assert(c, idB != "", "%s, id should not be nil", out)
dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
dockerCmd(c, "rename", "container1", "container_new")
@@ -403,14 +396,14 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
content := runCommandAndReadContainerFile(c, fn, dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn))
c.Assert(strings.TrimSpace(string(content)), checker.Equals, "success", check.Commentf("Content was not what was modified in the container", string(content)))
assert.Equal(c, strings.TrimSpace(string(content)), "success", "Content was not what was modified in the container", string(content))
out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
contID := strings.TrimSpace(out)
netFilePath := containerStorageFile(contID, fn)
f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
if _, err := f.Seek(0, 0); err != nil {
f.Close()
@@ -429,7 +422,7 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
f.Close()
res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
c.Assert(res, checker.Equals, "success2\n")
assert.Equal(c, res, "success2\n")
}
}
@@ -440,10 +433,10 @@ func (s *DockerSuite) TestExecWithUser(c *check.C) {
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
c.Assert(out, checker.Contains, "uid=1(daemon) gid=1(daemon)")
assert.Assert(c, strings.Contains(out, "uid=1(daemon) gid=1(daemon)"))
out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
c.Assert(out, checker.Contains, "uid=0(root) gid=0(root)", check.Commentf("exec with user by id expected daemon user got %s", out))
assert.Assert(c, strings.Contains(out, "uid=0(root) gid=0(root)"), "exec with user by id expected daemon user got %s", out)
}
func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
@@ -463,7 +456,7 @@ func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
result.Assert(c, icmd.Success)
actual := strings.TrimSpace(result.Combined())
c.Assert(actual, checker.Equals, "ok", check.Commentf("exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", result.Combined()))
assert.Equal(c, actual, "ok", "exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", result.Combined())
// Check subsequent unprivileged exec cannot mknod
icmd.RunCommand(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32").Assert(c, icmd.Expected{
@@ -473,8 +466,7 @@ func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
// Confirm at no point was mknod allowed
result = icmd.RunCommand(dockerBinary, "logs", "parent")
result.Assert(c, icmd.Success)
c.Assert(result.Combined(), checker.Not(checker.Contains), "Success")
assert.Assert(c, !strings.Contains(result.Combined(), "Success"))
}
func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
@@ -487,7 +479,7 @@ func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
c.Assert(out, checker.Contains, "dockerio", check.Commentf("exec with user by id expected dockerio user got %s", out))
assert.Assert(c, strings.Contains(out, "dockerio"), "exec with user by id expected dockerio user got %s", out)
}
func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
@@ -502,11 +494,11 @@ func (s *DockerSuite) TestExecUlimits(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "testexeculimits"
runSleepingContainer(c, "-d", "--ulimit", "nofile=511:511", "--name", name)
c.Assert(waitRun(name), checker.IsNil)
assert.NilError(c, waitRun(name))
out, _, err := dockerCmdWithError("exec", name, "sh", "-c", "ulimit -n")
c.Assert(err, checker.IsNil)
c.Assert(strings.TrimSpace(out), checker.Equals, "511")
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(out), "511")
}
// #15750
@@ -516,22 +508,22 @@ func (s *DockerSuite) TestExecStartFails(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "exec-15750"
runSleepingContainer(c, "-d", "--name", name)
c.Assert(waitRun(name), checker.IsNil)
assert.NilError(c, waitRun(name))
out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
c.Assert(err, checker.NotNil, check.Commentf("%s", out))
c.Assert(out, checker.Contains, "executable file not found")
assert.ErrorContains(c, err, "", out)
assert.Assert(c, strings.Contains(out, "executable file not found"))
}
// Fix regression in https://github.com/docker/docker/pull/26461#issuecomment-250287297
func (s *DockerSuite) TestExecWindowsPathNotWiped(c *check.C) {
testRequires(c, DaemonIsWindows)
out, _ := dockerCmd(c, "run", "-d", "--name", "testing", minimalBaseImage(), "powershell", "start-sleep", "60")
c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
assert.NilError(c, waitRun(strings.TrimSpace(out)))
out, _ = dockerCmd(c, "exec", "testing", "powershell", "write-host", "$env:PATH")
out = strings.ToLower(strings.Trim(out, "\r\n"))
c.Assert(out, checker.Contains, `windowspowershell\v1.0`)
assert.Assert(c, strings.Contains(out, `windowspowershell\v1.0`))
}
func (s *DockerSuite) TestExecEnvLinksHost(c *check.C) {
@@ -539,8 +531,8 @@ func (s *DockerSuite) TestExecEnvLinksHost(c *check.C) {
runSleepingContainer(c, "-d", "--name", "foo")
runSleepingContainer(c, "-d", "--link", "foo:db", "--hostname", "myhost", "--name", "bar")
out, _ := dockerCmd(c, "exec", "bar", "env")
c.Assert(out, checker.Contains, "HOSTNAME=myhost")
c.Assert(out, checker.Contains, "DB_NAME=/bar/db")
assert.Check(c, is.Contains(out, "HOSTNAME=myhost"))
assert.Check(c, is.Contains(out, "DB_NAME=/bar/db"))
}
func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
@@ -548,7 +540,7 @@ func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
if runtime.GOOS == "windows" {
v, err := kernel.GetKernelVersion()
c.Assert(err, checker.IsNil)
assert.NilError(c, err)
build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
if build >= 17743 {
c.Skip("Temporarily disabled on RS5 17743+ builds due to platform bug")
@@ -626,7 +618,7 @@ func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
// Ensure the background sleep is still running
out, _ := dockerCmd(c, "top", "test")
c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 2)
assert.Equal(c, strings.Count(out, "busybox.exe"), 2)
// The exec should exit when the background sleep exits
select {
@@ -635,7 +627,7 @@ func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
case <-exec:
// Ensure the background sleep has actually exited
out, _ := dockerCmd(c, "top", "test")
c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 1)
assert.Equal(c, strings.Count(out, "busybox.exe"), 1)
break
}
}