1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #16917 from WeiZhang555/start-inte

use of checkers on `integration-cli/docker_cli_start_test.go`
This commit is contained in:
Vincent Demeester 2015-10-20 19:51:15 +02:00
commit b7fcc71a3c

View file

@ -5,6 +5,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "github.com/go-check/check"
) )
@ -15,9 +16,9 @@ func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
dockerCmd(c, "wait", "test") dockerCmd(c, "wait", "test")
// Expect this to fail because the above container is stopped, this is what we want // Expect this to fail because the above container is stopped, this is what we want
if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil { out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox")
c.Fatal("Expected error but got none") // err shouldn't be nil because container test2 try to link to stopped container
} c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
ch := make(chan error) ch := make(chan error)
go func() { go func() {
@ -47,12 +48,10 @@ func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
dockerCmd(c, "wait", out) dockerCmd(c, "wait", out)
startOut, exitCode, err := dockerCmdWithError("start", "-a", out) startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) { // start command should fail
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut) c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
} // start -a did not respond with proper exit code
if exitCode != 1 { c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
}
} }
@ -65,9 +64,8 @@ func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
dockerCmd(c, "wait", name) dockerCmd(c, "wait", name)
startOut, _ := dockerCmd(c, "start", "-a", name) startOut, _ := dockerCmd(c, "start", "-a", name)
if expected := "test\n"; startOut != expected { // start -a produced unexpected output
c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut) c.Assert(startOut, checker.Equals, "test\n")
}
} }
func (s *DockerSuite) TestStartRecordError(c *check.C) { func (s *DockerSuite) TestStartRecordError(c *check.C) {
@ -75,32 +73,26 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
// when container runs successfully, we should not have state.Error // when container runs successfully, we should not have state.Error
dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top") dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
stateErr, err := inspectField("test", "State.Error") stateErr, err := inspectField("test", "State.Error")
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil, check.Commentf("stateErr: %s", stateErr))
if stateErr != "" { // Expected to not have state error
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) c.Assert(stateErr, checker.Equals, "")
}
// Expect this to fail and records error because of ports conflict // Expect this to fail and records error because of ports conflict
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top") out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
if err == nil { // err shouldn't be nil because docker run will fail
c.Fatalf("Expected error but got none, output %q", out) c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
}
stateErr, err = inspectField("test2", "State.Error") stateErr, err = inspectField("test2", "State.Error")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
expected := "port is already allocated" c.Assert(stateErr, checker.Contains, "port is already allocated")
if stateErr == "" || !strings.Contains(stateErr, expected) {
c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
}
// Expect the conflict to be resolved when we stop the initial container // Expect the conflict to be resolved when we stop the initial container
dockerCmd(c, "stop", "test") dockerCmd(c, "stop", "test")
dockerCmd(c, "start", "test2") dockerCmd(c, "start", "test2")
stateErr, err = inspectField("test2", "State.Error") stateErr, err = inspectField("test2", "State.Error")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
if stateErr != "" { // Expected to not have state error but got one
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) c.Assert(stateErr, checker.Equals, "")
}
} }
func (s *DockerSuite) TestStartPausedContainer(c *check.C) { func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
@ -111,9 +103,11 @@ func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
dockerCmd(c, "pause", "testing") dockerCmd(c, "pause", "testing")
if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") { out, _, err := dockerCmdWithError("start", "testing")
c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err) // an error should have been shown that you cannot start paused container
} c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
// an error should have been shown that you cannot start paused container
c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
} }
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) { func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
@ -129,25 +123,24 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
dockerCmd(c, "stop", "parent") dockerCmd(c, "stop", "parent")
out, err := inspectField("parent", "State.Running") out, err := inspectField("parent", "State.Running")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
if out != "false" { // Container should be stopped
c.Fatal("Container should be stopped") c.Assert(out, checker.Equals, "false")
}
// start all the three containers, container `child_first` start first which should be failed // start all the three containers, container `child_first` start first which should be failed
// container 'parent' start second and then start container 'child_second' // container 'parent' start second and then start container 'child_second'
out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second") out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
if !strings.Contains(out, "Cannot start container child_first") || err == nil { // err shouldn't be nil because start will fail
c.Fatal("Expected error but got none") c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
} // output does not correspond to what was expected
c.Assert(out, checker.Contains, "Cannot start container child_first")
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} { for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
out, err := inspectField(container, "State.Running") out, err := inspectField(container, "State.Running")
c.Assert(err, check.IsNil) // Container running state wrong
if out != expected { c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
c.Fatal("Container running state wrong") // Container running state wrong
} c.Assert(out, checker.Equals, expected)
} }
} }
@ -166,19 +159,18 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
// test start and attach multiple containers at once, expected error // test start and attach multiple containers at once, expected error
for _, option := range []string{"-a", "-i", "-ai"} { for _, option := range []string{"-a", "-i", "-ai"} {
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3") out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil { // err shouldn't be nil because start will fail
c.Fatal("Expected error but got none") c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
} // output does not correspond to what was expected
c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
} }
// confirm the state of all the containers be stopped // confirm the state of all the containers be stopped
for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} { for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
out, err := inspectField(container, "State.Running") out, err := inspectField(container, "State.Running")
if err != nil { // Container running state wrong
c.Fatal(out, err) c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
} // Container running state wrong
if out != expected { c.Assert(out, checker.Equals, expected)
c.Fatal("Container running state wrong")
}
} }
} }