github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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 struct{})
    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  			c.Fatal("Expected error but got none")
    29  		}
    30  		close(ch)
    31  	}()
    32  
    33  	select {
    34  	case <-ch:
    35  	case <-time.After(time.Second):
    36  		c.Fatalf("Attach did not exit properly")
    37  	}
    38  
    39  }
    40  
    41  // gh#8555: Exit code should be passed through when using start -a
    42  func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
    43  
    44  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
    45  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    46  	if err != nil {
    47  		c.Fatalf("failed to run container: %v, output: %q", err, out)
    48  	}
    49  
    50  	out = strings.TrimSpace(out)
    51  
    52  	// make sure the container has exited before trying the "start -a"
    53  	waitCmd := exec.Command(dockerBinary, "wait", out)
    54  	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
    55  		c.Fatalf("Failed to wait on container: %v", err)
    56  	}
    57  
    58  	startCmd := exec.Command(dockerBinary, "start", "-a", out)
    59  	startOut, exitCode, err := runCommandWithOutput(startCmd)
    60  	if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
    61  		c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
    62  	}
    63  	if exitCode != 1 {
    64  		c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
    65  	}
    66  
    67  }
    68  
    69  func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
    70  
    71  	name := "teststartattachcorrectexitcode"
    72  	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "echo", "test")
    73  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    74  	if err != nil {
    75  		c.Fatalf("failed to run container: %v, output: %q", err, out)
    76  	}
    77  
    78  	// make sure the container has exited before trying the "start -a"
    79  	waitCmd := exec.Command(dockerBinary, "wait", name)
    80  	if _, _, err = runCommandWithOutput(waitCmd); err != nil {
    81  		c.Fatalf("wait command failed with error: %v", err)
    82  	}
    83  
    84  	startCmd := exec.Command(dockerBinary, "start", "-a", name)
    85  	startOut, _, err := runCommandWithOutput(startCmd)
    86  	if err != nil {
    87  		c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
    88  	}
    89  	if expected := "test\n"; startOut != expected {
    90  		c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
    91  	}
    92  
    93  }
    94  
    95  func (s *DockerSuite) TestStartRecordError(c *check.C) {
    96  
    97  	// when container runs successfully, we should not have state.Error
    98  	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
    99  	stateErr, err := inspectField("test", "State.Error")
   100  	if err != nil {
   101  		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
   102  	}
   103  	if stateErr != "" {
   104  		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
   105  	}
   106  
   107  	// Expect this to fail and records error because of ports conflict
   108  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
   109  	if err == nil {
   110  		c.Fatalf("Expected error but got none, output %q", out)
   111  	}
   112  	stateErr, err = inspectField("test2", "State.Error")
   113  	if err != nil {
   114  		c.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
   115  	}
   116  	expected := "port is already allocated"
   117  	if stateErr == "" || !strings.Contains(stateErr, expected) {
   118  		c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
   119  	}
   120  
   121  	// Expect the conflict to be resolved when we stop the initial container
   122  	dockerCmd(c, "stop", "test")
   123  	dockerCmd(c, "start", "test2")
   124  	stateErr, err = inspectField("test2", "State.Error")
   125  	if err != nil {
   126  		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
   127  	}
   128  	if stateErr != "" {
   129  		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
   130  	}
   131  
   132  }
   133  
   134  // gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s
   135  func (s *DockerSuite) TestStartVolumesFromFailsCleanly(c *check.C) {
   136  
   137  	// Create the first data volume
   138  	dockerCmd(c, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox")
   139  
   140  	// Expect this to fail because the data test after contaienr doesn't exist yet
   141  	if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil {
   142  		c.Fatal("Expected error but got none")
   143  	}
   144  
   145  	// Create the second data volume
   146  	dockerCmd(c, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox")
   147  
   148  	// Now, all the volumes should be there
   149  	dockerCmd(c, "start", "consumer")
   150  
   151  	// Check that we have the volumes we want
   152  	out, _ := dockerCmd(c, "inspect", "--format='{{ len .Volumes }}'", "consumer")
   153  	nVolumes := strings.Trim(out, " \r\n'")
   154  	if nVolumes != "2" {
   155  		c.Fatalf("Missing volumes: expected 2, got %s", nVolumes)
   156  	}
   157  
   158  }
   159  
   160  func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
   161  	defer unpauseAllContainers()
   162  
   163  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
   164  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   165  		c.Fatal(out, err)
   166  	}
   167  
   168  	runCmd = exec.Command(dockerBinary, "pause", "testing")
   169  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   170  		c.Fatal(out, err)
   171  	}
   172  
   173  	runCmd = exec.Command(dockerBinary, "start", "testing")
   174  	if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
   175  		c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
   176  	}
   177  
   178  }
   179  
   180  func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
   181  	// run a container named 'parent' and create two container link to `parent`
   182  	cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
   183  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   184  		c.Fatal(out, err)
   185  	}
   186  	for _, container := range []string{"child_first", "child_second"} {
   187  		cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
   188  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   189  			c.Fatal(out, err)
   190  		}
   191  	}
   192  
   193  	// stop 'parent' container
   194  	cmd = exec.Command(dockerBinary, "stop", "parent")
   195  	if out, _, err := runCommandWithOutput(cmd); err != nil {
   196  		c.Fatal(out, err)
   197  	}
   198  	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
   199  	out, _, err := runCommandWithOutput(cmd)
   200  	if err != nil {
   201  		c.Fatal(out, err)
   202  	}
   203  	out = strings.Trim(out, "\r\n")
   204  	if out != "false" {
   205  		c.Fatal("Container should be stopped")
   206  	}
   207  
   208  	// start all the three containers, container `child_first` start first which should be failed
   209  	// container 'parent' start second and then start container 'child_second'
   210  	cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
   211  	out, _, err = runCommandWithOutput(cmd)
   212  	if !strings.Contains(out, "Cannot start container child_first") || err == nil {
   213  		c.Fatal("Expected error but got none")
   214  	}
   215  
   216  	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
   217  		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
   218  		out, _, err = runCommandWithOutput(cmd)
   219  		if err != nil {
   220  			c.Fatal(out, err)
   221  		}
   222  		out = strings.Trim(out, "\r\n")
   223  		if out != expected {
   224  			c.Fatal("Container running state wrong")
   225  		}
   226  
   227  	}
   228  
   229  }
   230  
   231  func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
   232  
   233  	var cmd *exec.Cmd
   234  
   235  	// run  multiple containers to test
   236  	for _, container := range []string{"test1", "test2", "test3"} {
   237  		cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
   238  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   239  			c.Fatal(out, err)
   240  		}
   241  	}
   242  
   243  	// stop all the containers
   244  	for _, container := range []string{"test1", "test2", "test3"} {
   245  		cmd = exec.Command(dockerBinary, "stop", container)
   246  		if out, _, err := runCommandWithOutput(cmd); err != nil {
   247  			c.Fatal(out, err)
   248  		}
   249  	}
   250  
   251  	// test start and attach multiple containers at once, expected error
   252  	for _, option := range []string{"-a", "-i", "-ai"} {
   253  		cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
   254  		out, _, err := runCommandWithOutput(cmd)
   255  		if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
   256  			c.Fatal("Expected error but got none")
   257  		}
   258  	}
   259  
   260  	// confirm the state of all the containers be stopped
   261  	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
   262  		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
   263  		out, _, err := runCommandWithOutput(cmd)
   264  		if err != nil {
   265  			c.Fatal(out, err)
   266  		}
   267  		out = strings.Trim(out, "\r\n")
   268  		if out != expected {
   269  			c.Fatal("Container running state wrong")
   270  		}
   271  	}
   272  
   273  }