github.com/raychaser/docker@v1.5.0/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 fails") 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 }