integration-cli/docker_cli_start_test.go
9ae9d7db
 package main
 
 import (
65edb070
 	"fmt"
 	"strings"
9ae9d7db
 	"time"
dc944ea7
 
08944cde
 	"github.com/docker/docker/pkg/integration/checker"
dc944ea7
 	"github.com/go-check/check"
9ae9d7db
 )
 
 // Regression test for https://github.com/docker/docker/issues/7843
dc944ea7
 func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
b4774910
 	// Windows does not support link
f9a3558a
 	testRequires(c, DaemonIsLinux)
e4468913
 	dockerCmd(c, "run", "--name", "test", "busybox")
9ae9d7db
 
 	// Expect this to fail because the above container is stopped, this is what we want
e4468913
 	out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
08944cde
 	// err shouldn't be nil because container test2 try to link to stopped container
 	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
9ae9d7db
 
4203230c
 	ch := make(chan error)
9ae9d7db
 	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
693ba98c
 		if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
4203230c
 			ch <- fmt.Errorf("Expected error but got none")
9ae9d7db
 		}
 		close(ch)
 	}()
 
 	select {
4203230c
 	case err := <-ch:
 		c.Assert(err, check.IsNil)
88c1bc10
 	case <-time.After(5 * time.Second):
dc944ea7
 		c.Fatalf("Attach did not exit properly")
9ae9d7db
 	}
 }
65edb070
 
 // gh#8555: Exit code should be passed through when using start -a
dc944ea7
 func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
012b67c3
 	out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
475c6531
 	out = strings.TrimSpace(out)
65edb070
 
 	// make sure the container has exited before trying the "start -a"
012b67c3
 	dockerCmd(c, "wait", out)
65edb070
 
693ba98c
 	startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
08944cde
 	// 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))
65edb070
 
 }
fb6ee865
 
dc944ea7
 func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
eeefa2dc
 	name := "teststartattachcorrectexitcode"
012b67c3
 	dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
eeefa2dc
 
 	// make sure the container has exited before trying the "start -a"
012b67c3
 	dockerCmd(c, "wait", name)
eeefa2dc
 
012b67c3
 	startOut, _ := dockerCmd(c, "start", "-a", name)
08944cde
 	// start -a produced unexpected output
 	c.Assert(startOut, checker.Equals, "test\n")
eeefa2dc
 }
 
dc944ea7
 func (s *DockerSuite) TestStartRecordError(c *check.C) {
b4774910
 	// TODO Windows CI: Requires further porting work. Should be possible.
f9a3558a
 	testRequires(c, DaemonIsLinux)
fb6ee865
 	// when container runs successfully, we should not have state.Error
dc944ea7
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
62a856e9
 	stateErr := inspectField(c, "test", "State.Error")
08944cde
 	// Expected to not have state error
 	c.Assert(stateErr, checker.Equals, "")
fb6ee865
 
 	// Expect this to fail and records error because of ports conflict
693ba98c
 	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
08944cde
 	// err shouldn't be nil because docker run will fail
 	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
012b67c3
 
62a856e9
 	stateErr = inspectField(c, "test2", "State.Error")
08944cde
 	c.Assert(stateErr, checker.Contains, "port is already allocated")
fb6ee865
 
 	// Expect the conflict to be resolved when we stop the initial container
dc944ea7
 	dockerCmd(c, "stop", "test")
 	dockerCmd(c, "start", "test2")
62a856e9
 	stateErr = inspectField(c, "test2", "State.Error")
08944cde
 	// Expected to not have state error but got one
 	c.Assert(stateErr, checker.Equals, "")
fb6ee865
 }
fb62e184
 
dc944ea7
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
b4774910
 	// Windows does not support pausing containers
f9a3558a
 	testRequires(c, DaemonIsLinux)
02246d2d
 	defer unpauseAllContainers()
 
012b67c3
 	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
02246d2d
 
012b67c3
 	dockerCmd(c, "pause", "testing")
02246d2d
 
08944cde
 	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.")
02246d2d
 }
2a5a50dc
 
dc944ea7
 func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
b4774910
 	// Windows does not support --link
f9a3558a
 	testRequires(c, DaemonIsLinux)
2a5a50dc
 	// run a container named 'parent' and create two container link to `parent`
012b67c3
 	dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
 
2a5a50dc
 	for _, container := range []string{"child_first", "child_second"} {
012b67c3
 		dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
2a5a50dc
 	}
 
 	// stop 'parent' container
012b67c3
 	dockerCmd(c, "stop", "parent")
 
62a856e9
 	out := inspectField(c, "parent", "State.Running")
08944cde
 	// Container should be stopped
 	c.Assert(out, checker.Equals, "false")
2a5a50dc
 
3941623f
 	// start all the three containers, container `child_first` start first which should be failed
2a5a50dc
 	// container 'parent' start second and then start container 'child_second'
41de7a18
 	expOut := "Cannot link to a non running container"
 	expErr := "failed to start containers: [child_first]"
62a856e9
 	out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
08944cde
 	// 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
41de7a18
 	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)
 	}
2a5a50dc
 
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
62a856e9
 		out := inspectField(c, container, "State.Running")
08944cde
 		// Container running state wrong
 		c.Assert(out, checker.Equals, expected)
2a5a50dc
 	}
 }
7dc1af14
 
dc944ea7
 func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
7dc1af14
 	// run  multiple containers to test
 	for _, container := range []string{"test1", "test2", "test3"} {
ef9f13af
 		runSleepingContainer(c, "--name", container)
7dc1af14
 	}
 
 	// stop all the containers
 	for _, container := range []string{"test1", "test2", "test3"} {
012b67c3
 		dockerCmd(c, "stop", container)
7dc1af14
 	}
 
 	// test start and attach multiple containers at once, expected error
 	for _, option := range []string{"-a", "-i", "-ai"} {
693ba98c
 		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
08944cde
 		// 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.")
7dc1af14
 	}
 
 	// confirm the state of all the containers be stopped
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
62a856e9
 		out := inspectField(c, container, "State.Running")
08944cde
 		// Container running state wrong
 		c.Assert(out, checker.Equals, expected)
7dc1af14
 	}
 }