integration-cli/docker_cli_restart_test.go
9da6c805
 package main
 
 import (
be9b7a24
 	"os"
 	"strconv"
9da6c805
 	"strings"
8a5ab83d
 	"time"
dc944ea7
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
dc944ea7
 	"github.com/go-check/check"
9da6c805
 )
 
dc944ea7
 func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
e4468913
 	dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
c10f6ef4
 	cleanedContainerID := getIDByName(c, "test")
9da6c805
 
e4468913
 	out, _ := dockerCmd(c, "logs", cleanedContainerID)
196af472
 	c.Assert(out, checker.Equals, "foobar\n")
9da6c805
 
71868228
 	dockerCmd(c, "restart", cleanedContainerID)
9da6c805
 
48ccdd46
 	// Wait until the container has stopped
c10f6ef4
 	err := waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
48ccdd46
 	c.Assert(err, checker.IsNil)
 
71868228
 	out, _ = dockerCmd(c, "logs", cleanedContainerID)
196af472
 	c.Assert(out, checker.Equals, "foobar\nfoobar\n")
9da6c805
 }
 
dc944ea7
 func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
71868228
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
9da6c805
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
9da6c805
 
196af472
 	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
59316693
 
bae22d16
 	getLogs := func(c *check.C) (interface{}, check.CommentInterface) {
 		out, _ := dockerCmd(c, "logs", cleanedContainerID)
 		return out, nil
 	}
9da6c805
 
bae22d16
 	// Wait 10 seconds for the 'echo' to appear in the logs
 	waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\n")
9da6c805
 
bae22d16
 	dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
196af472
 	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
59316693
 
bae22d16
 	// Wait 10 seconds for first 'echo' appear (again) in the logs
 	waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\nfoobar\n")
9da6c805
 }
 
 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
dc944ea7
 func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
281c1ced
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
a899aa67
 	out := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
9da6c805
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
62a856e9
 	out, err := inspectFilter(cleanedContainerID, "len .Mounts")
 	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
196af472
 	out = strings.Trim(out, " \n\r")
 	c.Assert(out, checker.Equals, "1")
9da6c805
 
281c1ced
 	source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
196af472
 	c.Assert(err, checker.IsNil)
9da6c805
 
71868228
 	dockerCmd(c, "restart", cleanedContainerID)
9da6c805
 
62a856e9
 	out, err = inspectFilter(cleanedContainerID, "len .Mounts")
 	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
196af472
 	out = strings.Trim(out, " \n\r")
 	c.Assert(out, checker.Equals, "1")
9da6c805
 
281c1ced
 	sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
196af472
 	c.Assert(err, checker.IsNil)
 	c.Assert(source, checker.Equals, sourceAfterRestart)
9da6c805
 }
c3ed49dc
 
4ca7d4f0
 func (s *DockerSuite) TestRestartDisconnectedContainer(c *check.C) {
 	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
 
 	// Run a container on the default bridge network
 	out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
 	cleanedContainerID := strings.TrimSpace(out)
 	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
 
 	// Disconnect the container from the network
 	out, err := dockerCmd(c, "network", "disconnect", "bridge", "c0")
 	c.Assert(err, check.NotNil, check.Commentf(out))
 
 	// Restart the container
 	dockerCmd(c, "restart", "c0")
 	c.Assert(err, check.NotNil, check.Commentf(out))
 }
 
dc944ea7
 func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
9db5d649
 	out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
c3ed49dc
 
 	id := strings.TrimSpace(string(out))
62a856e9
 	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
196af472
 	c.Assert(name, checker.Equals, "no")
c3ed49dc
 }
 
dc944ea7
 func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
9db5d649
 	out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
c3ed49dc
 
 	id := strings.TrimSpace(string(out))
62a856e9
 	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
196af472
 	c.Assert(name, checker.Equals, "always")
c3ed49dc
 
62a856e9
 	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
f5310f40
 
 	// MaximumRetryCount=0 if the restart policy is always
196af472
 	c.Assert(MaximumRetryCount, checker.Equals, "0")
c3ed49dc
 }
 
dc944ea7
 func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
9db5d649
 	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")
c3ed49dc
 
 	id := strings.TrimSpace(string(out))
62a856e9
 	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
9db5d649
 	maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
 
196af472
 	c.Assert(name, checker.Equals, "on-failure")
9db5d649
 	c.Assert(maxRetry, checker.Equals, "1")
 
 	out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
c3ed49dc
 
9db5d649
 	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")
c3ed49dc
 }
13398319
 
 // a good container with --restart=on-failure:3
 // MaximumRetryCount!=0; RestartCount=0
281c1ced
 func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
71868228
 	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
 
13398319
 	id := strings.TrimSpace(string(out))
281c1ced
 	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
196af472
 	c.Assert(err, checker.IsNil)
 
62a856e9
 	count := inspectField(c, id, "RestartCount")
196af472
 	c.Assert(count, checker.Equals, "0")
 
62a856e9
 	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
196af472
 	c.Assert(MaximumRetryCount, checker.Equals, "3")
13398319
 
 }
be9b7a24
 
281c1ced
 func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
 	testRequires(c, SameHostDaemon)
be9b7a24
 
a899aa67
 	out := runSleepingContainer(c, "-d", "--restart=always")
be9b7a24
 	id := strings.TrimSpace(out)
 	c.Assert(waitRun(id), check.IsNil)
 
62a856e9
 	pidStr := inspectField(c, id, "State.Pid")
be9b7a24
 
 	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)
 
281c1ced
 	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
be9b7a24
 	c.Assert(err, check.IsNil)
 
281c1ced
 	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
be9b7a24
 	c.Assert(err, check.IsNil)
 }
74819610
 
281c1ced
 func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
 	// TODO Windows. This may be portable following HNS integration post TP5.
4f339570
 	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
74819610
 	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
62a856e9
 	pidStr := inspectField(c, "second", "State.Pid")
74819610
 
 	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)
 }
20390f65
 
 func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
 	testRequires(c, SameHostDaemon)
 
a899aa67
 	out := runSleepingContainer(c, "-d", "--restart=always")
20390f65
 	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)
 }
a705e166
 
 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))
a7eecd2b
 	waitTimeout := 15 * time.Second
18a771a7
 	if testEnv.OSType == "windows" {
a7eecd2b
 		waitTimeout = 150 * time.Second
 	}
 	err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
a705e166
 	c.Assert(err, checker.IsNil)
 
a7eecd2b
 	dockerCmd(c, "restart", id1)
 	dockerCmd(c, "restart", id2)
a705e166
 
e857785d
 	// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
a705e166
 	dockerCmd(c, "stop", id1)
 	dockerCmd(c, "stop", id2)
 	dockerCmd(c, "start", id1)
 	dockerCmd(c, "start", id2)
e857785d
 
 	// 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)
a705e166
 }
3c2886d8
 
da6609cc
 func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
a899aa67
 	out := runSleepingContainer(c, "--rm")
3c2886d8
 
 	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))
58ad9177
 
 	// Kill the container to make sure it will be removed
 	dockerCmd(c, "kill", id)
3c2886d8
 }