github.com/clcy1243/docker@v1.6.0-rc3/integration-cli/docker_cli_start_test.go (about)

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