github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/integration-cli/docker_cli_restart_test.go (about) 1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 "time" 7 8 "github.com/go-check/check" 9 ) 10 11 func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { 12 13 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar") 14 out, _, err := runCommandWithOutput(runCmd) 15 if err != nil { 16 c.Fatal(out, err) 17 } 18 19 cleanedContainerID := strings.TrimSpace(out) 20 21 runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) 22 if out, _, err = runCommandWithOutput(runCmd); err != nil { 23 c.Fatal(out, err) 24 } 25 26 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 27 out, _, err = runCommandWithOutput(runCmd) 28 if err != nil { 29 c.Fatal(out, err) 30 } 31 32 if out != "foobar\n" { 33 c.Errorf("container should've printed 'foobar'") 34 } 35 36 runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) 37 if out, _, err = runCommandWithOutput(runCmd); err != nil { 38 c.Fatal(out, err) 39 } 40 41 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 42 out, _, err = runCommandWithOutput(runCmd) 43 if err != nil { 44 c.Fatal(out, err) 45 } 46 47 if out != "foobar\nfoobar\n" { 48 c.Errorf("container should've printed 'foobar' twice") 49 } 50 51 } 52 53 func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { 54 55 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") 56 out, _, err := runCommandWithOutput(runCmd) 57 if err != nil { 58 c.Fatal(out, err) 59 } 60 61 cleanedContainerID := strings.TrimSpace(out) 62 63 time.Sleep(1 * time.Second) 64 65 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 66 out, _, err = runCommandWithOutput(runCmd) 67 if err != nil { 68 c.Fatal(out, err) 69 } 70 71 if out != "foobar\n" { 72 c.Errorf("container should've printed 'foobar'") 73 } 74 75 runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID) 76 if out, _, err = runCommandWithOutput(runCmd); err != nil { 77 c.Fatal(out, err) 78 } 79 80 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 81 out, _, err = runCommandWithOutput(runCmd) 82 if err != nil { 83 c.Fatal(out, err) 84 } 85 86 time.Sleep(1 * time.Second) 87 88 if out != "foobar\nfoobar\n" { 89 c.Errorf("container should've printed 'foobar' twice") 90 } 91 92 } 93 94 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. 95 func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { 96 97 runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top") 98 out, _, err := runCommandWithOutput(runCmd) 99 if err != nil { 100 c.Fatal(out, err) 101 } 102 103 cleanedContainerID := strings.TrimSpace(out) 104 105 runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) 106 out, _, err = runCommandWithOutput(runCmd) 107 if err != nil { 108 c.Fatal(out, err) 109 } 110 111 if out = strings.Trim(out, " \n\r"); out != "1" { 112 c.Errorf("expect 1 volume received %s", out) 113 } 114 115 runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID) 116 volumes, _, err := runCommandWithOutput(runCmd) 117 if err != nil { 118 c.Fatal(volumes, err) 119 } 120 121 runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) 122 if out, _, err = runCommandWithOutput(runCmd); err != nil { 123 c.Fatal(out, err) 124 } 125 126 runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) 127 out, _, err = runCommandWithOutput(runCmd) 128 if err != nil { 129 c.Fatal(out, err) 130 } 131 132 if out = strings.Trim(out, " \n\r"); out != "1" { 133 c.Errorf("expect 1 volume after restart received %s", out) 134 } 135 136 runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID) 137 volumesAfterRestart, _, err := runCommandWithOutput(runCmd) 138 if err != nil { 139 c.Fatal(volumesAfterRestart, err) 140 } 141 142 if volumes != volumesAfterRestart { 143 volumes = strings.Trim(volumes, " \n\r") 144 volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r") 145 c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart) 146 } 147 148 } 149 150 func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { 151 152 cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false") 153 out, _, err := runCommandWithOutput(cmd) 154 if err != nil { 155 c.Fatal(err, out) 156 } 157 158 id := strings.TrimSpace(string(out)) 159 name, err := inspectField(id, "HostConfig.RestartPolicy.Name") 160 if err != nil { 161 c.Fatal(err, out) 162 } 163 if name != "no" { 164 c.Fatalf("Container restart policy name is %s, expected %s", name, "no") 165 } 166 167 } 168 169 func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { 170 171 cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false") 172 out, _, err := runCommandWithOutput(cmd) 173 if err != nil { 174 c.Fatal(err, out) 175 } 176 177 id := strings.TrimSpace(string(out)) 178 name, err := inspectField(id, "HostConfig.RestartPolicy.Name") 179 if err != nil { 180 c.Fatal(err, out) 181 } 182 if name != "always" { 183 c.Fatalf("Container restart policy name is %s, expected %s", name, "always") 184 } 185 186 MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount") 187 if err != nil { 188 c.Fatal(err) 189 } 190 191 // MaximumRetryCount=0 if the restart policy is always 192 if MaximumRetryCount != "0" { 193 c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0") 194 } 195 196 } 197 198 func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { 199 200 cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false") 201 out, _, err := runCommandWithOutput(cmd) 202 if err != nil { 203 c.Fatal(err, out) 204 } 205 206 id := strings.TrimSpace(string(out)) 207 name, err := inspectField(id, "HostConfig.RestartPolicy.Name") 208 if err != nil { 209 c.Fatal(err, out) 210 } 211 if name != "on-failure" { 212 c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure") 213 } 214 215 } 216 217 // a good container with --restart=on-failure:3 218 // MaximumRetryCount!=0; RestartCount=0 219 func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) { 220 out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput() 221 if err != nil { 222 c.Fatal(string(out), err) 223 } 224 id := strings.TrimSpace(string(out)) 225 if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil { 226 c.Fatal(err) 227 } 228 count, err := inspectField(id, "RestartCount") 229 if err != nil { 230 c.Fatal(err) 231 } 232 if count != "0" { 233 c.Fatalf("Container was restarted %s times, expected %d", count, 0) 234 } 235 MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount") 236 if err != nil { 237 c.Fatal(err) 238 } 239 if MaximumRetryCount != "3" { 240 c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3") 241 } 242 243 }