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