github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration-cli/docker_cli_start_test.go (about)

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