github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/integration-cli/docker_cli_restart_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/integration-cli/checker" 10 "github.com/go-check/check" 11 ) 12 13 func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { 14 dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar") 15 cleanedContainerID, err := getIDByName("test") 16 c.Assert(err, check.IsNil) 17 18 out, _ := dockerCmd(c, "logs", cleanedContainerID) 19 c.Assert(out, checker.Equals, "foobar\n") 20 21 dockerCmd(c, "restart", cleanedContainerID) 22 23 // Wait until the container has stopped 24 err = waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second) 25 c.Assert(err, checker.IsNil) 26 27 out, _ = dockerCmd(c, "logs", cleanedContainerID) 28 c.Assert(out, checker.Equals, "foobar\nfoobar\n") 29 } 30 31 func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { 32 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") 33 34 cleanedContainerID := strings.TrimSpace(out) 35 36 c.Assert(waitRun(cleanedContainerID), checker.IsNil) 37 38 getLogs := func(c *check.C) (interface{}, check.CommentInterface) { 39 out, _ := dockerCmd(c, "logs", cleanedContainerID) 40 return out, nil 41 } 42 43 // Wait 10 seconds for the 'echo' to appear in the logs 44 waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\n") 45 46 dockerCmd(c, "restart", "-t", "1", cleanedContainerID) 47 c.Assert(waitRun(cleanedContainerID), checker.IsNil) 48 49 // Wait 10 seconds for first 'echo' appear (again) in the logs 50 waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\nfoobar\n") 51 } 52 53 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. 54 func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { 55 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 56 out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test") 57 58 cleanedContainerID := strings.TrimSpace(out) 59 out, err := inspectFilter(cleanedContainerID, "len .Mounts") 60 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 61 out = strings.Trim(out, " \n\r") 62 c.Assert(out, checker.Equals, "1") 63 64 source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 65 c.Assert(err, checker.IsNil) 66 67 dockerCmd(c, "restart", cleanedContainerID) 68 69 out, err = inspectFilter(cleanedContainerID, "len .Mounts") 70 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 71 out = strings.Trim(out, " \n\r") 72 c.Assert(out, checker.Equals, "1") 73 74 sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 75 c.Assert(err, checker.IsNil) 76 c.Assert(source, checker.Equals, sourceAfterRestart) 77 } 78 79 func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { 80 out, _ := dockerCmd(c, "create", "--restart=no", "busybox") 81 82 id := strings.TrimSpace(string(out)) 83 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 84 c.Assert(name, checker.Equals, "no") 85 } 86 87 func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { 88 out, _ := dockerCmd(c, "create", "--restart=always", "busybox") 89 90 id := strings.TrimSpace(string(out)) 91 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 92 c.Assert(name, checker.Equals, "always") 93 94 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 95 96 // MaximumRetryCount=0 if the restart policy is always 97 c.Assert(MaximumRetryCount, checker.Equals, "0") 98 } 99 100 func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { 101 out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox") 102 c.Assert(err, check.NotNil, check.Commentf(out)) 103 c.Assert(out, checker.Contains, "maximum retry count cannot be negative") 104 105 out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox") 106 107 id := strings.TrimSpace(string(out)) 108 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 109 maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 110 111 c.Assert(name, checker.Equals, "on-failure") 112 c.Assert(maxRetry, checker.Equals, "1") 113 114 out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox") 115 116 id = strings.TrimSpace(string(out)) 117 name = inspectField(c, id, "HostConfig.RestartPolicy.Name") 118 maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 119 120 c.Assert(name, checker.Equals, "on-failure") 121 c.Assert(maxRetry, checker.Equals, "0") 122 123 out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox") 124 125 id = strings.TrimSpace(string(out)) 126 name = inspectField(c, id, "HostConfig.RestartPolicy.Name") 127 maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 128 129 c.Assert(name, checker.Equals, "on-failure") 130 c.Assert(maxRetry, checker.Equals, "0") 131 } 132 133 // a good container with --restart=on-failure:3 134 // MaximumRetryCount!=0; RestartCount=0 135 func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) { 136 out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true") 137 138 id := strings.TrimSpace(string(out)) 139 err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second) 140 c.Assert(err, checker.IsNil) 141 142 count := inspectField(c, id, "RestartCount") 143 c.Assert(count, checker.Equals, "0") 144 145 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 146 c.Assert(MaximumRetryCount, checker.Equals, "3") 147 148 } 149 150 func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) { 151 testRequires(c, SameHostDaemon) 152 153 out, _ := runSleepingContainer(c, "-d", "--restart=always") 154 id := strings.TrimSpace(out) 155 c.Assert(waitRun(id), check.IsNil) 156 157 pidStr := inspectField(c, id, "State.Pid") 158 159 pid, err := strconv.Atoi(pidStr) 160 c.Assert(err, check.IsNil) 161 162 p, err := os.FindProcess(pid) 163 c.Assert(err, check.IsNil) 164 c.Assert(p, check.NotNil) 165 166 err = p.Kill() 167 c.Assert(err, check.IsNil) 168 169 err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) 170 c.Assert(err, check.IsNil) 171 172 err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) 173 c.Assert(err, check.IsNil) 174 } 175 176 func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) { 177 // TODO Windows. This may be portable following HNS integration post TP5. 178 testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm) 179 dockerCmd(c, "network", "create", "-d", "bridge", "udNet") 180 181 dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top") 182 c.Assert(waitRun("first"), check.IsNil) 183 184 dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second", 185 "--link=first:foo", "busybox", "top") 186 c.Assert(waitRun("second"), check.IsNil) 187 188 // ping to first and its alias foo must succeed 189 _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 190 c.Assert(err, check.IsNil) 191 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 192 c.Assert(err, check.IsNil) 193 194 // Now kill the second container and let the restart policy kick in 195 pidStr := inspectField(c, "second", "State.Pid") 196 197 pid, err := strconv.Atoi(pidStr) 198 c.Assert(err, check.IsNil) 199 200 p, err := os.FindProcess(pid) 201 c.Assert(err, check.IsNil) 202 c.Assert(p, check.NotNil) 203 204 err = p.Kill() 205 c.Assert(err, check.IsNil) 206 207 err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second) 208 c.Assert(err, check.IsNil) 209 210 err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second) 211 212 // ping to first and its alias foo must still succeed 213 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 214 c.Assert(err, check.IsNil) 215 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 216 c.Assert(err, check.IsNil) 217 } 218 219 func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) { 220 testRequires(c, SameHostDaemon) 221 222 out, _ := runSleepingContainer(c, "-d", "--restart=always") 223 id := strings.TrimSpace(out) 224 c.Assert(waitRun(id), check.IsNil) 225 226 dockerCmd(c, "restart", id) 227 228 c.Assert(waitRun(id), check.IsNil) 229 230 pidStr := inspectField(c, id, "State.Pid") 231 232 pid, err := strconv.Atoi(pidStr) 233 c.Assert(err, check.IsNil) 234 235 p, err := os.FindProcess(pid) 236 c.Assert(err, check.IsNil) 237 c.Assert(p, check.NotNil) 238 239 err = p.Kill() 240 c.Assert(err, check.IsNil) 241 242 err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) 243 c.Assert(err, check.IsNil) 244 245 err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) 246 c.Assert(err, check.IsNil) 247 } 248 249 func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) { 250 out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false") 251 out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false") 252 253 id1 := strings.TrimSpace(string(out1)) 254 id2 := strings.TrimSpace(string(out2)) 255 waitTimeout := 15 * time.Second 256 if testEnv.DaemonPlatform() == "windows" { 257 waitTimeout = 150 * time.Second 258 } 259 err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) 260 c.Assert(err, checker.IsNil) 261 262 dockerCmd(c, "restart", id1) 263 dockerCmd(c, "restart", id2) 264 265 // Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa) 266 dockerCmd(c, "stop", id1) 267 dockerCmd(c, "stop", id2) 268 dockerCmd(c, "start", id1) 269 dockerCmd(c, "start", id2) 270 271 // Kill the containers, making sure the are stopped at the end of the test 272 dockerCmd(c, "kill", id1) 273 dockerCmd(c, "kill", id2) 274 err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) 275 c.Assert(err, checker.IsNil) 276 err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) 277 c.Assert(err, checker.IsNil) 278 } 279 280 func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) { 281 out, _ := runSleepingContainer(c, "--rm") 282 283 id := strings.TrimSpace(string(out)) 284 dockerCmd(c, "restart", id) 285 err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) 286 c.Assert(err, checker.IsNil) 287 288 out, _ = dockerCmd(c, "ps") 289 c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out)) 290 291 // Kill the container to make sure it will be removed 292 dockerCmd(c, "kill", id) 293 }