github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_start_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/pkg/integration/checker"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  // Regression test for https://github.com/docker/docker/issues/7843
    14  func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
    15  	// Windows does not support link
    16  	testRequires(c, DaemonIsLinux)
    17  	dockerCmd(c, "run", "--name", "test", "busybox")
    18  
    19  	// Expect this to fail because the above container is stopped, this is what we want
    20  	out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
    21  	// err shouldn't be nil because container test2 try to link to stopped container
    22  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
    23  
    24  	ch := make(chan error)
    25  	go func() {
    26  		// Attempt to start attached to the container that won't start
    27  		// This should return an error immediately since the container can't be started
    28  		if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
    29  			ch <- fmt.Errorf("Expected error but got none:\n%s", out)
    30  		}
    31  		close(ch)
    32  	}()
    33  
    34  	select {
    35  	case err := <-ch:
    36  		c.Assert(err, check.IsNil)
    37  	case <-time.After(5 * time.Second):
    38  		c.Fatalf("Attach did not exit properly")
    39  	}
    40  }
    41  
    42  // gh#8555: Exit code should be passed through when using start -a
    43  func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
    44  	testRequires(c, DaemonIsLinux)
    45  	out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
    46  	out = strings.TrimSpace(out)
    47  
    48  	// make sure the container has exited before trying the "start -a"
    49  	dockerCmd(c, "wait", out)
    50  
    51  	startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
    52  	// start command should fail
    53  	c.Assert(err, checker.NotNil, check.Commentf("startOut: %s", startOut))
    54  	// start -a did not respond with proper exit code
    55  	c.Assert(exitCode, checker.Equals, 1, check.Commentf("startOut: %s", startOut))
    56  
    57  }
    58  
    59  func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
    60  	name := "teststartattachcorrectexitcode"
    61  	dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
    62  
    63  	// make sure the container has exited before trying the "start -a"
    64  	dockerCmd(c, "wait", name)
    65  
    66  	startOut, _ := dockerCmd(c, "start", "-a", name)
    67  	// start -a produced unexpected output
    68  	c.Assert(startOut, checker.Equals, "test\n")
    69  }
    70  
    71  func (s *DockerSuite) TestStartRecordError(c *check.C) {
    72  	// TODO Windows CI: Requires further porting work. Should be possible.
    73  	testRequires(c, DaemonIsLinux)
    74  	// when container runs successfully, we should not have state.Error
    75  	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
    76  	stateErr := inspectField(c, "test", "State.Error")
    77  	// Expected to not have state error
    78  	c.Assert(stateErr, checker.Equals, "")
    79  
    80  	// Expect this to fail and records error because of ports conflict
    81  	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
    82  	// err shouldn't be nil because docker run will fail
    83  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
    84  
    85  	stateErr = inspectField(c, "test2", "State.Error")
    86  	c.Assert(stateErr, checker.Contains, "port is already allocated")
    87  
    88  	// Expect the conflict to be resolved when we stop the initial container
    89  	dockerCmd(c, "stop", "test")
    90  	dockerCmd(c, "start", "test2")
    91  	stateErr = inspectField(c, "test2", "State.Error")
    92  	// Expected to not have state error but got one
    93  	c.Assert(stateErr, checker.Equals, "")
    94  }
    95  
    96  func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
    97  	// Windows does not support pausing containers
    98  	testRequires(c, DaemonIsLinux)
    99  	defer unpauseAllContainers()
   100  
   101  	dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
   102  
   103  	dockerCmd(c, "pause", "testing")
   104  
   105  	out, _, err := dockerCmdWithError("start", "testing")
   106  	// an error should have been shown that you cannot start paused container
   107  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
   108  	// an error should have been shown that you cannot start paused container
   109  	c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.")
   110  }
   111  
   112  func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
   113  	// Windows does not support --link
   114  	testRequires(c, DaemonIsLinux)
   115  	// run a container named 'parent' and create two container link to `parent`
   116  	dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
   117  
   118  	for _, container := range []string{"child_first", "child_second"} {
   119  		dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
   120  	}
   121  
   122  	// stop 'parent' container
   123  	dockerCmd(c, "stop", "parent")
   124  
   125  	out := inspectField(c, "parent", "State.Running")
   126  	// Container should be stopped
   127  	c.Assert(out, checker.Equals, "false")
   128  
   129  	// start all the three containers, container `child_first` start first which should be failed
   130  	// container 'parent' start second and then start container 'child_second'
   131  	expOut := "Cannot link to a non running container"
   132  	expErr := "failed to start containers: [child_first]"
   133  	out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
   134  	// err shouldn't be nil because start will fail
   135  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
   136  	// output does not correspond to what was expected
   137  	if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
   138  		c.Fatalf("Expected out: %v with err: %v  but got out: %v with err: %v", expOut, expErr, out, err)
   139  	}
   140  
   141  	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
   142  		out := inspectField(c, container, "State.Running")
   143  		// Container running state wrong
   144  		c.Assert(out, checker.Equals, expected)
   145  	}
   146  }
   147  
   148  func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
   149  	// run  multiple containers to test
   150  	for _, container := range []string{"test1", "test2", "test3"} {
   151  		runSleepingContainer(c, "--name", container)
   152  	}
   153  
   154  	// stop all the containers
   155  	for _, container := range []string{"test1", "test2", "test3"} {
   156  		dockerCmd(c, "stop", container)
   157  	}
   158  
   159  	// test start and attach multiple containers at once, expected error
   160  	for _, option := range []string{"-a", "-i", "-ai"} {
   161  		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
   162  		// err shouldn't be nil because start will fail
   163  		c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
   164  		// output does not correspond to what was expected
   165  		c.Assert(out, checker.Contains, "You cannot start and attach multiple containers at once.")
   166  	}
   167  
   168  	// confirm the state of all the containers be stopped
   169  	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
   170  		out := inspectField(c, container, "State.Running")
   171  		// Container running state wrong
   172  		c.Assert(out, checker.Equals, expected)
   173  	}
   174  }
   175  
   176  // Test case for #23716
   177  func (s *DockerSuite) TestStartAttachWithRename(c *check.C) {
   178  	testRequires(c, DaemonIsLinux)
   179  	dockerCmd(c, "create", "-t", "--name", "before", "busybox")
   180  	go func() {
   181  		c.Assert(waitRun("before"), checker.IsNil)
   182  		dockerCmd(c, "rename", "before", "after")
   183  		dockerCmd(c, "stop", "--time=2", "after")
   184  	}()
   185  	_, stderr, _, _ := runCommandWithStdoutStderr(exec.Command(dockerBinary, "start", "-a", "before"))
   186  	c.Assert(stderr, checker.Not(checker.Contains), "No such container")
   187  }