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