2014-09-24 22:46:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-18 22:47:48 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-09-24 22:46:57 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-03-27 11:12:48 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2017-08-23 17:01:29 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
2014-09-24 22:46:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for https://github.com/docker/docker/issues/7843
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support link
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-02-28 05:47:37 -05:00
|
|
|
dockerCmd(c, "run", "--name", "test", "busybox")
|
2014-09-24 22:46:57 -04:00
|
|
|
|
|
|
|
// Expect this to fail because the above container is stopped, this is what we want
|
2016-02-28 05:47:37 -05:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
|
2015-10-11 03:19:28 -04:00
|
|
|
// 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-24 22:46:57 -04:00
|
|
|
|
2015-04-27 13:29:48 -04:00
|
|
|
ch := make(chan error)
|
2014-09-24 22: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
|
2016-03-24 12:43:04 -04:00
|
|
|
if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
|
|
|
|
ch <- fmt.Errorf("Expected error but got none:\n%s", out)
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 13:29:48 -04:00
|
|
|
case err := <-ch:
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-11-20 17:12:12 -05:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Attach did not exit properly")
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-18 22:47:48 -04:00
|
|
|
|
|
|
|
// gh#8555: Exit code should be passed through when using start -a
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-03-27 11:12:48 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
|
2015-04-06 09:21:18 -04:00
|
|
|
out = strings.TrimSpace(out)
|
2014-10-18 22:47:48 -04:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.DockerCmd(c, "wait", out)
|
2014-10-18 22:47:48 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
2014-09-30 04:30:58 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
|
2015-01-06 20:58:30 -05:00
|
|
|
name := "teststartattachcorrectexitcode"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
|
2015-01-06 20:58:30 -05:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "wait", name)
|
2015-01-06 20:58:30 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
startOut, _ := dockerCmd(c, "start", "-a", name)
|
2015-10-11 03:19:28 -04:00
|
|
|
// start -a produced unexpected output
|
|
|
|
c.Assert(startOut, checker.Equals, "test\n")
|
2015-01-06 20:58:30 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartRecordError(c *check.C) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// TODO Windows CI: Requires further porting work. Should be possible.
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-09-30 04:30:58 -04:00
|
|
|
// when container runs successfully, we should not have state.Error
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr := inspectField(c, "test", "State.Error")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Expected to not have state error
|
|
|
|
c.Assert(stateErr, checker.Equals, "")
|
2014-09-30 04:30:58 -04:00
|
|
|
|
|
|
|
// Expect this to fail and records error because of ports conflict
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
|
2015-10-11 03:19:28 -04:00
|
|
|
// err shouldn't be nil because docker run will fail
|
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
|
2015-07-21 14:01:24 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2015-10-11 03:19:28 -04:00
|
|
|
c.Assert(stateErr, checker.Contains, "port is already allocated")
|
2014-09-30 04:30:58 -04:00
|
|
|
|
|
|
|
// Expect the conflict to be resolved when we stop the initial container
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "stop", "test")
|
|
|
|
dockerCmd(c, "start", "test2")
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Expected to not have state error but got one
|
|
|
|
c.Assert(stateErr, checker.Equals, "")
|
2014-09-30 04:30:58 -04:00
|
|
|
}
|
2014-10-23 08:50:06 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support pausing containers
|
2016-09-08 20:31:04 -04:00
|
|
|
testRequires(c, IsPausable)
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2016-09-08 20:31:04 -04:00
|
|
|
runSleepingContainer(c, "-d", "--name", "testing")
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "pause", "testing")
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2015-10-11 03:19:28 -04: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
|
2017-08-17 15:16:30 -04:00
|
|
|
c.Assert(out, checker.Contains, "cannot start a paused container, try unpause instead")
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
2015-03-08 00:44:25 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support --link
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-08 00:44:25 -05:00
|
|
|
// run a container named 'parent' and create two container link to `parent`
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
|
|
|
|
2015-03-08 00:44:25 -05:00
|
|
|
for _, container := range []string{"child_first", "child_second"} {
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop 'parent' container
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "stop", "parent")
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, "parent", "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container should be stopped
|
|
|
|
c.Assert(out, checker.Equals, "false")
|
2015-03-08 00:44:25 -05:00
|
|
|
|
2015-04-27 16:33:30 -04:00
|
|
|
// start all the three containers, container `child_first` start first which should be failed
|
2015-03-08 00:44:25 -05:00
|
|
|
// container 'parent' start second and then start container 'child_second'
|
2015-07-29 08:21:16 -04:00
|
|
|
expOut := "Cannot link to a non running container"
|
|
|
|
expErr := "failed to start containers: [child_first]"
|
2016-01-28 09:19:25 -05:00
|
|
|
out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
|
2015-10-11 03:19:28 -04: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
|
2015-07-29 08:21:16 -04:00
|
|
|
if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
|
|
|
|
c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err)
|
|
|
|
}
|
2015-03-08 00:44:25 -05:00
|
|
|
|
|
|
|
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(out, checker.Equals, expected)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 05:08:22 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
|
2015-03-16 05:08:22 -04:00
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2016-02-23 19:24:35 -05:00
|
|
|
runSleepingContainer(c, "--name", container)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop all the containers
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "stop", container)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test start and attach multiple containers at once, expected error
|
|
|
|
for _, option := range []string{"-a", "-i", "-ai"} {
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
|
2015-10-11 03:19:28 -04: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
|
2017-05-04 01:37:55 -04:00
|
|
|
c.Assert(out, checker.Contains, "you cannot start and attach multiple containers at once")
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// confirm the state of all the containers be stopped
|
|
|
|
for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container running state wrong
|
|
|
|
c.Assert(out, checker.Equals, expected)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 19:43:30 -04:00
|
|
|
|
|
|
|
// Test case for #23716
|
|
|
|
func (s *DockerSuite) TestStartAttachWithRename(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
|
2016-06-18 19:43:30 -04:00
|
|
|
go func() {
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, "before")
|
|
|
|
cli.DockerCmd(c, "rename", "before", "after")
|
|
|
|
cli.DockerCmd(c, "stop", "--time=2", "after")
|
2016-06-18 19:43:30 -04:00
|
|
|
}()
|
2016-12-13 15:21:51 -05:00
|
|
|
// FIXME(vdemeester) the intent is not clear and potentially racey
|
2017-04-11 15:18:30 -04:00
|
|
|
result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
|
2016-12-13 15:21:51 -05:00
|
|
|
ExitCode: 137,
|
|
|
|
})
|
|
|
|
c.Assert(result.Stderr(), checker.Not(checker.Contains), "No such container")
|
2016-06-18 19:43:30 -04:00
|
|
|
}
|
2016-08-19 03:14:16 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestStartReturnCorrectExitCode(c *check.C) {
|
|
|
|
dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
|
|
|
|
dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
|
|
|
|
|
|
|
|
_, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(exitCode, checker.Equals, 11)
|
|
|
|
_, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
|
|
|
|
c.Assert(err, checker.NotNil)
|
|
|
|
c.Assert(exitCode, checker.Equals, 12)
|
|
|
|
}
|