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