github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_start_test.go (about)

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