github.com/trgill/moby@v1.13.1/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/pkg/integration/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 out, _ = dockerCmd(c, "logs", cleanedContainerID) 39 c.Assert(out, checker.Equals, "foobar\n") 40 41 dockerCmd(c, "restart", "-t", "1", cleanedContainerID) 42 43 out, _ = dockerCmd(c, "logs", cleanedContainerID) 44 45 c.Assert(waitRun(cleanedContainerID), checker.IsNil) 46 47 c.Assert(out, checker.Equals, "foobar\nfoobar\n") 48 } 49 50 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. 51 func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { 52 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 53 out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test") 54 55 cleanedContainerID := strings.TrimSpace(out) 56 out, err := inspectFilter(cleanedContainerID, "len .Mounts") 57 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 58 out = strings.Trim(out, " \n\r") 59 c.Assert(out, checker.Equals, "1") 60 61 source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 62 c.Assert(err, checker.IsNil) 63 64 dockerCmd(c, "restart", cleanedContainerID) 65 66 out, err = inspectFilter(cleanedContainerID, "len .Mounts") 67 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 68 out = strings.Trim(out, " \n\r") 69 c.Assert(out, checker.Equals, "1") 70 71 sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 72 c.Assert(err, checker.IsNil) 73 c.Assert(source, checker.Equals, sourceAfterRestart) 74 } 75 76 func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { 77 out, _ := dockerCmd(c, "create", "--restart=no", "busybox") 78 79 id := strings.TrimSpace(string(out)) 80 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 81 c.Assert(name, checker.Equals, "no") 82 } 83 84 func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { 85 out, _ := dockerCmd(c, "create", "--restart=always", "busybox") 86 87 id := strings.TrimSpace(string(out)) 88 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 89 c.Assert(name, checker.Equals, "always") 90 91 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 92 93 // MaximumRetryCount=0 if the restart policy is always 94 c.Assert(MaximumRetryCount, checker.Equals, "0") 95 } 96 97 func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { 98 out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox") 99 c.Assert(err, check.NotNil, check.Commentf(out)) 100 c.Assert(out, checker.Contains, "maximum retry count cannot be negative") 101 102 out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox") 103 104 id := strings.TrimSpace(string(out)) 105 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 106 maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 107 108 c.Assert(name, checker.Equals, "on-failure") 109 c.Assert(maxRetry, checker.Equals, "1") 110 111 out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox") 112 113 id = strings.TrimSpace(string(out)) 114 name = inspectField(c, id, "HostConfig.RestartPolicy.Name") 115 maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 116 117 c.Assert(name, checker.Equals, "on-failure") 118 c.Assert(maxRetry, checker.Equals, "0") 119 120 out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox") 121 122 id = strings.TrimSpace(string(out)) 123 name = inspectField(c, id, "HostConfig.RestartPolicy.Name") 124 maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 125 126 c.Assert(name, checker.Equals, "on-failure") 127 c.Assert(maxRetry, checker.Equals, "0") 128 } 129 130 // a good container with --restart=on-failure:3 131 // MaximumRetryCount!=0; RestartCount=0 132 func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) { 133 out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true") 134 135 id := strings.TrimSpace(string(out)) 136 err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second) 137 c.Assert(err, checker.IsNil) 138 139 count := inspectField(c, id, "RestartCount") 140 c.Assert(count, checker.Equals, "0") 141 142 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 143 c.Assert(MaximumRetryCount, checker.Equals, "3") 144 145 } 146 147 func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) { 148 testRequires(c, SameHostDaemon) 149 150 out, _ := runSleepingContainer(c, "-d", "--restart=always") 151 id := strings.TrimSpace(out) 152 c.Assert(waitRun(id), check.IsNil) 153 154 pidStr := inspectField(c, id, "State.Pid") 155 156 pid, err := strconv.Atoi(pidStr) 157 c.Assert(err, check.IsNil) 158 159 p, err := os.FindProcess(pid) 160 c.Assert(err, check.IsNil) 161 c.Assert(p, check.NotNil) 162 163 err = p.Kill() 164 c.Assert(err, check.IsNil) 165 166 err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) 167 c.Assert(err, check.IsNil) 168 169 err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) 170 c.Assert(err, check.IsNil) 171 } 172 173 func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) { 174 // TODO Windows. This may be portable following HNS integration post TP5. 175 testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm) 176 dockerCmd(c, "network", "create", "-d", "bridge", "udNet") 177 178 dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top") 179 c.Assert(waitRun("first"), check.IsNil) 180 181 dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second", 182 "--link=first:foo", "busybox", "top") 183 c.Assert(waitRun("second"), check.IsNil) 184 185 // ping to first and its alias foo must succeed 186 _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 187 c.Assert(err, check.IsNil) 188 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 189 c.Assert(err, check.IsNil) 190 191 // Now kill the second container and let the restart policy kick in 192 pidStr := inspectField(c, "second", "State.Pid") 193 194 pid, err := strconv.Atoi(pidStr) 195 c.Assert(err, check.IsNil) 196 197 p, err := os.FindProcess(pid) 198 c.Assert(err, check.IsNil) 199 c.Assert(p, check.NotNil) 200 201 err = p.Kill() 202 c.Assert(err, check.IsNil) 203 204 err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second) 205 c.Assert(err, check.IsNil) 206 207 err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second) 208 209 // ping to first and its alias foo must still succeed 210 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 211 c.Assert(err, check.IsNil) 212 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 213 c.Assert(err, check.IsNil) 214 } 215 216 func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) { 217 testRequires(c, SameHostDaemon) 218 219 out, _ := runSleepingContainer(c, "-d", "--restart=always") 220 id := strings.TrimSpace(out) 221 c.Assert(waitRun(id), check.IsNil) 222 223 dockerCmd(c, "restart", id) 224 225 c.Assert(waitRun(id), check.IsNil) 226 227 pidStr := inspectField(c, id, "State.Pid") 228 229 pid, err := strconv.Atoi(pidStr) 230 c.Assert(err, check.IsNil) 231 232 p, err := os.FindProcess(pid) 233 c.Assert(err, check.IsNil) 234 c.Assert(p, check.NotNil) 235 236 err = p.Kill() 237 c.Assert(err, check.IsNil) 238 239 err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) 240 c.Assert(err, check.IsNil) 241 242 err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) 243 c.Assert(err, check.IsNil) 244 } 245 246 func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) { 247 out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false") 248 out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false") 249 250 id1 := strings.TrimSpace(string(out1)) 251 id2 := strings.TrimSpace(string(out2)) 252 waitTimeout := 15 * time.Second 253 if daemonPlatform == "windows" { 254 waitTimeout = 150 * time.Second 255 } 256 err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) 257 c.Assert(err, checker.IsNil) 258 259 dockerCmd(c, "restart", id1) 260 dockerCmd(c, "restart", id2) 261 262 dockerCmd(c, "stop", id1) 263 dockerCmd(c, "stop", id2) 264 dockerCmd(c, "start", id1) 265 dockerCmd(c, "start", id2) 266 } 267 268 func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) { 269 out, _ := runSleepingContainer(c, "--rm") 270 271 id := strings.TrimSpace(string(out)) 272 dockerCmd(c, "restart", id) 273 err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second) 274 c.Assert(err, checker.IsNil) 275 276 out, _ = dockerCmd(c, "ps") 277 c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out)) 278 }