github.com/titanous/docker@v1.4.1/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 out, _, err = runCommandWithOutput(waitCmd); err != nil { 57 t.Fatal(out, 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 TestStartRecordError(t *testing.T) { 73 defer deleteAllContainers() 74 75 // when container runs successfully, we should not have state.Error 76 dockerCmd(t, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top") 77 stateErr, err := inspectField("test", "State.Error") 78 if err != nil { 79 t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err) 80 } 81 if stateErr != "" { 82 t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) 83 } 84 85 // Expect this to fail and records error because of ports conflict 86 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")) 87 if err == nil { 88 t.Fatalf("Expected error but got none, output %q", out) 89 } 90 stateErr, err = inspectField("test2", "State.Error") 91 if err != nil { 92 t.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err) 93 } 94 expected := "port is already allocated" 95 if stateErr == "" || !strings.Contains(stateErr, expected) { 96 t.Fatalf("State.Error(%q) does not include %q", stateErr, expected) 97 } 98 99 // Expect the conflict to be resolved when we stop the initial container 100 dockerCmd(t, "stop", "test") 101 dockerCmd(t, "start", "test2") 102 stateErr, err = inspectField("test2", "State.Error") 103 if err != nil { 104 t.Fatalf("Failed to inspect %q state's error, got error %q", "test", err) 105 } 106 if stateErr != "" { 107 t.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) 108 } 109 110 logDone("start - set state error when start fails") 111 } 112 113 // gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s 114 func TestStartVolumesFromFailsCleanly(t *testing.T) { 115 defer deleteAllContainers() 116 117 // Create the first data volume 118 dockerCmd(t, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox") 119 120 // Expect this to fail because the data test after contaienr doesn't exist yet 121 if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil { 122 t.Fatal("Expected error but got none") 123 } 124 125 // Create the second data volume 126 dockerCmd(t, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox") 127 128 // Now, all the volumes should be there 129 dockerCmd(t, "start", "consumer") 130 131 // Check that we have the volumes we want 132 out, _, _ := dockerCmd(t, "inspect", "--format='{{ len .Volumes }}'", "consumer") 133 n_volumes := strings.Trim(out, " \r\n'") 134 if n_volumes != "2" { 135 t.Fatalf("Missing volumes: expected 2, got %s", n_volumes) 136 } 137 138 logDone("start - missing containers in --volumes-from did not affect subsequent runs") 139 }