2014-06-27 15:47:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-14 04:08:47 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-06-27 15:47:32 +00:00
|
|
|
"strings"
|
2015-09-01 21:37:04 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-06-27 15:47:32 +00:00
|
|
|
)
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
|
2016-02-28 10:47:37 +00:00
|
|
|
dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
|
|
|
|
cleanedContainerID, err := getIDByName("test")
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-02-28 10:47:37 +00:00
|
|
|
out, _ := dockerCmd(c, "logs", cleanedContainerID)
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(out, checker.Equals, "foobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
dockerCmd(c, "restart", cleanedContainerID)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-03-16 03:10:49 +00:00
|
|
|
// Wait until the container has stopped
|
|
|
|
err = waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(out, checker.Equals, "foobar\nfoobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
|
2014-07-29 06:46:14 +00:00
|
|
|
|
2016-12-29 01:54:47 +00:00
|
|
|
getLogs := func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, _ := dockerCmd(c, "logs", cleanedContainerID)
|
|
|
|
return out, nil
|
|
|
|
}
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-12-29 01:54:47 +00:00
|
|
|
// Wait 10 seconds for the 'echo' to appear in the logs
|
|
|
|
waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-12-29 01:54:47 +00:00
|
|
|
dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
|
2014-07-29 06:46:14 +00:00
|
|
|
|
2016-12-29 01:54:47 +00:00
|
|
|
// Wait 10 seconds for first 'echo' appear (again) in the logs
|
|
|
|
waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\nfoobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
|
2016-02-24 23:21:56 +00:00
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
|
|
|
out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2015-04-06 13:21:18 +00:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2016-01-28 14:19:25 +00:00
|
|
|
out, err := inspectFilter(cleanedContainerID, "len .Mounts")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
|
2015-10-14 09:29:13 +00:00
|
|
|
out = strings.Trim(out, " \n\r")
|
|
|
|
c.Assert(out, checker.Equals, "1")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
dockerCmd(c, "restart", cleanedContainerID)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
out, err = inspectFilter(cleanedContainerID, "len .Mounts")
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
|
2015-10-14 09:29:13 +00:00
|
|
|
out = strings.Trim(out, " \n\r")
|
|
|
|
c.Assert(out, checker.Equals, "1")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(source, checker.Equals, sourceAfterRestart)
|
2014-06-27 15:47:32 +00:00
|
|
|
}
|
2015-01-16 09:58:26 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
|
2016-12-01 21:24:30 +00:00
|
|
|
out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
|
2015-01-16 09:58:26 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
2016-01-28 14:19:25 +00:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(name, checker.Equals, "no")
|
2015-01-16 09:58:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
|
2016-12-01 21:24:30 +00:00
|
|
|
out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
|
2015-01-16 09:58:26 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
2016-01-28 14:19:25 +00:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(name, checker.Equals, "always")
|
2015-01-16 09:58:26 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-03-30 01:07:58 +00:00
|
|
|
|
|
|
|
// MaximumRetryCount=0 if the restart policy is always
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(MaximumRetryCount, checker.Equals, "0")
|
2015-01-16 09:58:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
|
2016-12-01 21:24:30 +00:00
|
|
|
out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
|
|
|
|
c.Assert(err, check.NotNil, check.Commentf(out))
|
|
|
|
c.Assert(out, checker.Contains, "maximum retry count cannot be negative")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
|
2015-01-16 09:58:26 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
2016-01-28 14:19:25 +00:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2016-12-01 21:24:30 +00:00
|
|
|
maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(name, checker.Equals, "on-failure")
|
2016-12-01 21:24:30 +00:00
|
|
|
c.Assert(maxRetry, checker.Equals, "1")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
|
2015-01-16 09:58:26 +00:00
|
|
|
|
2016-12-01 21:24:30 +00:00
|
|
|
id = strings.TrimSpace(string(out))
|
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
|
|
|
c.Assert(name, checker.Equals, "on-failure")
|
|
|
|
c.Assert(maxRetry, checker.Equals, "0")
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
|
|
|
|
|
|
|
|
id = strings.TrimSpace(string(out))
|
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
|
|
|
c.Assert(name, checker.Equals, "on-failure")
|
|
|
|
c.Assert(maxRetry, checker.Equals, "0")
|
2015-01-16 09:58:26 +00:00
|
|
|
}
|
2015-03-19 12:19:39 +00:00
|
|
|
|
|
|
|
// a good container with --restart=on-failure:3
|
|
|
|
// MaximumRetryCount!=0; RestartCount=0
|
2016-02-24 23:21:56 +00:00
|
|
|
func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
|
|
|
|
|
2015-03-19 12:19:39 +00:00
|
|
|
id := strings.TrimSpace(string(out))
|
2016-02-24 23:21:56 +00:00
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
count := inspectField(c, id, "RestartCount")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(count, checker.Equals, "0")
|
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-10-14 09:29:13 +00:00
|
|
|
c.Assert(MaximumRetryCount, checker.Equals, "3")
|
2015-03-19 12:19:39 +00:00
|
|
|
|
|
|
|
}
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
out, _ := runSleepingContainer(c, "-d", "--restart=always")
|
2015-10-14 04:08:47 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
pidStr := inspectField(c, id, "State.Pid")
|
2015-10-14 04:08:47 +00:00
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(p, check.NotNil)
|
|
|
|
|
|
|
|
err = p.Kill()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
2015-10-14 04:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
2015-10-14 04:08:47 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-01-15 05:22:34 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
|
|
|
|
// TODO Windows. This may be portable following HNS integration post TP5.
|
2015-11-25 23:38:33 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
|
2016-01-15 05:22:34 +00:00
|
|
|
dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
|
|
|
|
c.Assert(waitRun("first"), check.IsNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
|
|
|
|
"--link=first:foo", "busybox", "top")
|
|
|
|
c.Assert(waitRun("second"), check.IsNil)
|
|
|
|
|
|
|
|
// ping to first and its alias foo must succeed
|
|
|
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
// Now kill the second container and let the restart policy kick in
|
2016-01-28 14:19:25 +00:00
|
|
|
pidStr := inspectField(c, "second", "State.Pid")
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(p, check.NotNil)
|
|
|
|
|
|
|
|
err = p.Kill()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
|
|
|
|
|
|
|
|
// ping to first and its alias foo must still succeed
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
|
|
|
|
|
|
|
out, _ := runSleepingContainer(c, "-d", "--restart=always")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
|
|
|
|
|
|
|
dockerCmd(c, "restart", id)
|
|
|
|
|
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
|
|
|
|
|
|
|
pidStr := inspectField(c, id, "State.Pid")
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(p, check.NotNil)
|
|
|
|
|
|
|
|
err = p.Kill()
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-04-07 14:05:29 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
|
|
|
|
out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
|
|
|
|
out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
|
|
|
|
|
|
|
|
id1 := strings.TrimSpace(string(out1))
|
|
|
|
id2 := strings.TrimSpace(string(out2))
|
2016-04-26 03:05:41 +00:00
|
|
|
waitTimeout := 15 * time.Second
|
2017-01-13 16:23:28 +00:00
|
|
|
if testEnv.DaemonPlatform() == "windows" {
|
2016-04-26 03:05:41 +00:00
|
|
|
waitTimeout = 150 * time.Second
|
|
|
|
}
|
|
|
|
err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2016-04-07 14:05:29 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2016-04-26 03:05:41 +00:00
|
|
|
dockerCmd(c, "restart", id1)
|
|
|
|
dockerCmd(c, "restart", id2)
|
2016-04-07 14:05:29 +00:00
|
|
|
|
2016-12-13 11:04:53 +00:00
|
|
|
// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
|
2016-04-07 14:05:29 +00:00
|
|
|
dockerCmd(c, "stop", id1)
|
|
|
|
dockerCmd(c, "stop", id2)
|
|
|
|
dockerCmd(c, "start", id1)
|
|
|
|
dockerCmd(c, "start", id2)
|
2016-12-13 11:04:53 +00:00
|
|
|
|
|
|
|
// Kill the containers, making sure the are stopped at the end of the test
|
|
|
|
dockerCmd(c, "kill", id1)
|
|
|
|
dockerCmd(c, "kill", id2)
|
|
|
|
err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-04-07 14:05:29 +00:00
|
|
|
}
|
2016-03-01 16:30:27 +00:00
|
|
|
|
2016-08-09 01:42:23 +00:00
|
|
|
func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
|
2016-03-01 16:30:27 +00:00
|
|
|
out, _ := runSleepingContainer(c, "--rm")
|
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
dockerCmd(c, "restart", id)
|
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "ps")
|
|
|
|
c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
|
2016-12-08 15:28:06 +00:00
|
|
|
|
|
|
|
// Kill the container to make sure it will be removed
|
|
|
|
dockerCmd(c, "kill", id)
|
2016-03-01 16:30:27 +00:00
|
|
|
}
|