2014-09-25 06:46:57 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-19 06:47:48 +04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-09-25 06:46:57 +04:00
|
|
|
"time"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
2015-10-11 10:19:28 +03:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 19:46:47 +03:00
|
|
|
"github.com/go-check/check"
|
2014-09-25 06:46:57 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for https://github.com/docker/docker/issues/7843
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox")
|
|
|
|
dockerCmd(c, "wait", "test")
|
2014-09-25 06:46:57 +04:00
|
|
|
|
|
|
|
// Expect this to fail because the above container is stopped, this is what we want
|
2015-10-11 10:19:28 +03:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox")
|
|
|
|
// err shouldn't be nil because container test2 try to link to stopped container
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2014-09-25 06:46:57 +04:00
|
|
|
|
2015-04-27 20:29:48 +03:00
|
|
|
ch := make(chan error)
|
2014-09-25 06:46:57 +04:00
|
|
|
go func() {
|
|
|
|
// Attempt to start attached to the container that won't start
|
|
|
|
// This should return an error immediately since the container can't be started
|
2015-07-27 21:13:25 +03:00
|
|
|
if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
|
2015-04-27 20:29:48 +03:00
|
|
|
ch <- fmt.Errorf("Expected error but got none")
|
2014-09-25 06:46:57 +04:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 20:29:48 +03:00
|
|
|
case err := <-ch:
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-09-25 06:46:57 +04:00
|
|
|
case <-time.After(time.Second):
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Attach did not exit properly")
|
2014-09-25 06:46:57 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-19 06:47:48 +04:00
|
|
|
|
|
|
|
// gh#8555: Exit code should be passed through when using start -a
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-21 21:01:24 +03:00
|
|
|
out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
|
2015-04-06 16:21:18 +03:00
|
|
|
out = strings.TrimSpace(out)
|
2014-10-19 06:47:48 +04:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "wait", out)
|
2014-10-19 06:47:48 +04:00
|
|
|
|
2015-07-27 21:13:25 +03:00
|
|
|
startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
|
2015-10-11 10:19:28 +03:00
|
|
|
// start command should fail
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
|
|
|
|
// start -a did not respond with proper exit code
|
|
|
|
c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
|
2014-10-19 06:47:48 +04:00
|
|
|
|
|
|
|
}
|
2014-09-30 12:30:58 +04:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-01-07 04:58:30 +03:00
|
|
|
name := "teststartattachcorrectexitcode"
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
|
2015-01-07 04:58:30 +03:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "wait", name)
|
2015-01-07 04:58:30 +03:00
|
|
|
|
2015-07-21 21:01:24 +03:00
|
|
|
startOut, _ := dockerCmd(c, "start", "-a", name)
|
2015-10-11 10:19:28 +03:00
|
|
|
// start -a produced unexpected output
|
|
|
|
c.Assert(startOut, checker.Equals, "test\n")
|
2015-01-07 04:58:30 +03:00
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartRecordError(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-09-30 12:30:58 +04:00
|
|
|
// when container runs successfully, we should not have state.Error
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
|
2014-09-30 12:30:58 +04:00
|
|
|
stateErr, err := inspectField("test", "State.Error")
|
2015-10-11 10:19:28 +03:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("stateErr: %s", stateErr))
|
|
|
|
// Expected to not have state error
|
|
|
|
c.Assert(stateErr, checker.Equals, "")
|
2014-09-30 12:30:58 +04:00
|
|
|
|
|
|
|
// Expect this to fail and records error because of ports conflict
|
2015-07-27 21:13:25 +03:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
|
2015-10-11 10:19:28 +03:00
|
|
|
// err shouldn't be nil because docker run will fail
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-07-21 21:01:24 +03:00
|
|
|
|
2014-09-30 12:30:58 +04:00
|
|
|
stateErr, err = inspectField("test2", "State.Error")
|
2015-10-11 10:19:28 +03:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
|
|
|
|
c.Assert(stateErr, checker.Contains, "port is already allocated")
|
2014-09-30 12:30:58 +04:00
|
|
|
|
|
|
|
// Expect the conflict to be resolved when we stop the initial container
|
2015-04-18 19:46:47 +03:00
|
|
|
dockerCmd(c, "stop", "test")
|
|
|
|
dockerCmd(c, "start", "test2")
|
2014-09-30 12:30:58 +04:00
|
|
|
stateErr, err = inspectField("test2", "State.Error")
|
2015-10-11 10:19:28 +03:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("stateErr: %s", stateErr))
|
|
|
|
// Expected to not have state error but got one
|
|
|
|
c.Assert(stateErr, checker.Equals, "")
|
2014-09-30 12:30:58 +04:00
|
|
|
}
|
2014-10-23 16:50:06 +04:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-01-15 03:44:53 +03:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
|
2015-01-15 03:44:53 +03:00
|
|
|
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "pause", "testing")
|
2015-01-15 03:44:53 +03:00
|
|
|
|
2015-10-11 10:19:28 +03:00
|
|
|
out, _, err := dockerCmdWithError("start", "testing")
|
|
|
|
// 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.")
|
2015-01-15 03:44:53 +03:00
|
|
|
}
|
2015-03-08 08:44:25 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-08 08:44:25 +03:00
|
|
|
// run a container named 'parent' and create two container link to `parent`
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
|
|
|
|
2015-03-08 08:44:25 +03:00
|
|
|
for _, container := range []string{"child_first", "child_second"} {
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
|
2015-03-08 08:44:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop 'parent' container
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "stop", "parent")
|
|
|
|
|
2015-05-18 05:06:13 +03:00
|
|
|
out, err := inspectField("parent", "State.Running")
|
2015-10-11 10:19:28 +03:00
|
|
|
c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
|
|
|
|
// Container should be stopped
|
|
|
|
c.Assert(out, checker.Equals, "false")
|
2015-03-08 08:44:25 +03:00
|
|
|
|
2015-04-27 23:33:30 +03:00
|
|
|
// start all the three containers, container `child_first` start first which should be failed
|
2015-03-08 08:44:25 +03:00
|
|
|
// container 'parent' start second and then start container 'child_second'
|
2015-07-27 21:13:25 +03:00
|
|
|
out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
|
2015-10-11 10:19:28 +03:00
|
|
|
// err shouldn't be nil because start will fail
|
|
|
|
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")
|
2015-03-08 08:44:25 +03:00
|
|
|
|
|
|
|
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
|
2015-05-18 05:06:13 +03:00
|
|
|
out, err := inspectField(container, "State.Running")
|
2015-10-11 10:19:28 +03:00
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
|
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(out, checker.Equals, expected)
|
2015-03-08 08:44:25 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 12:08:22 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-16 12:08:22 +03:00
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "run", "-d", "--name", container, "busybox", "top")
|
2015-03-16 12:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop all the containers
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2015-07-21 21:01:24 +03:00
|
|
|
dockerCmd(c, "stop", container)
|
2015-03-16 12:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// test start and attach multiple containers at once, expected error
|
|
|
|
for _, option := range []string{"-a", "-i", "-ai"} {
|
2015-07-27 21:13:25 +03:00
|
|
|
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
|
2015-10-11 10:19:28 +03:00
|
|
|
// err shouldn't be nil because start will fail
|
|
|
|
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.")
|
2015-03-16 12:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// confirm the state of all the containers be stopped
|
|
|
|
for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
|
2015-05-18 05:06:13 +03:00
|
|
|
out, err := inspectField(container, "State.Running")
|
2015-10-11 10:19:28 +03:00
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("out: %s", out))
|
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(out, checker.Equals, expected)
|
2015-03-16 12:08:22 +03:00
|
|
|
}
|
|
|
|
}
|