github.com/codemac/docker@v1.2.1-0.20150518222241-6a18412d5b9c/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  // gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s
   130  func (s *DockerSuite) TestStartVolumesFromFailsCleanly(c *check.C) {
   131  
   132  	// Create the first data volume
   133  	dockerCmd(c, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox")
   134  
   135  	// Expect this to fail because the data test after contaienr doesn't exist yet
   136  	if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil {
   137  		c.Fatal("Expected error but got none")
   138  	}
   139  
   140  	// Create the second data volume
   141  	dockerCmd(c, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox")
   142  
   143  	// Now, all the volumes should be there
   144  	dockerCmd(c, "start", "consumer")
   145  
   146  	// Check that we have the volumes we want
   147  	out, _ := dockerCmd(c, "inspect", "--format='{{ len .Volumes }}'", "consumer")
   148  	nVolumes := strings.Trim(out, " \r\n'")
   149  	if nVolumes != "2" {
   150  		c.Fatalf("Missing volumes: expected 2, got %s", nVolumes)
   151  	}
   152  
   153  }
   154  
   155  func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
   156  	defer unpauseAllContainers()
   157  
   158  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
   159  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   160  		c.Fatal(out, err)
   161  	}
   162  
   163  	runCmd = exec.Command(dockerBinary, "pause", "testing")
   164  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   165  		c.Fatal(out, err)
   166  	}
   167  
   168  	runCmd = exec.Command(dockerBinary, "start", "testing")
   169  	if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
   170  		c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
   171  	}
   172  
   173  }
   174  
   175  func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
   176  	// run a container named 'parent' and create two container link to `parent`
   177  	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
   178  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   179  		c.Fatal(out, err)
   180  	}
   181  	for _, container := range []string{"child_first", "child_second"} {
   182  		cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
   183  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   184  			c.Fatal(out, err)
   185  		}
   186  	}
   187  
   188  	// stop 'parent' container
   189  	cmd = exec.Command(dockerBinary, "stop", "parent")
   190  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   191  		c.Fatal(out, err)
   192  	}
   193  	out, err := inspectField("parent", "State.Running")
   194  	c.Assert(err, check.IsNil)
   195  	if out != "false" {
   196  		c.Fatal("Container should be stopped")
   197  	}
   198  
   199  	// start all the three containers, container `child_first` start first which should be failed
   200  	// container 'parent' start second and then start container 'child_second'
   201  	cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
   202  	out, _, err = runCommandWithOutput(cmd)
   203  	if !strings.Contains(out, "Cannot start container child_first") || err == nil {
   204  		c.Fatal("Expected error but got none")
   205  	}
   206  
   207  	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
   208  		out, err := inspectField(container, "State.Running")
   209  		c.Assert(err, check.IsNil)
   210  		if out != expected {
   211  			c.Fatal("Container running state wrong")
   212  		}
   213  
   214  	}
   215  
   216  }
   217  
   218  func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
   219  
   220  	var cmd *exec.Cmd
   221  
   222  	// run  multiple containers to test
   223  	for _, container := range []string{"test1", "test2", "test3"} {
   224  		cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
   225  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   226  			c.Fatal(out, err)
   227  		}
   228  	}
   229  
   230  	// stop all the containers
   231  	for _, container := range []string{"test1", "test2", "test3"} {
   232  		cmd = exec.Command(dockerBinary, "stop", container)
   233  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   234  			c.Fatal(out, err)
   235  		}
   236  	}
   237  
   238  	// test start and attach multiple containers at once, expected error
   239  	for _, option := range []string{"-a", "-i", "-ai"} {
   240  		cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
   241  		out, _, err := runCommandWithOutput(cmd)
   242  		if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
   243  			c.Fatal("Expected error but got none")
   244  		}
   245  	}
   246  
   247  	// confirm the state of all the containers be stopped
   248  	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
   249  		out, err := inspectField(container, "State.Running")
   250  		if err != nil {
   251  			c.Fatal(out, err)
   252  		}
   253  		if out != expected {
   254  			c.Fatal("Container running state wrong")
   255  		}
   256  	}
   257  
   258  }