2014-06-27 11:47:32 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-10-14 00:08:47 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-06-27 11:47:32 -04:00
|
|
|
"strings"
|
2015-09-01 17:37:04 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2015-10-14 05:29:13 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-06-27 11:47:32 -04:00
|
|
|
)
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "wait", cleanedContainerID)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(out, checker.Equals, "foobar\n")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "restart", cleanedContainerID)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(out, checker.Equals, "foobar\nfoobar\n")
|
2014-06-27 11:47:32 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
|
2014-07-29 02:46:14 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(out, checker.Equals, "foobar\n")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(waitRun(cleanedContainerID), checker.IsNil)
|
2014-07-29 02:46:14 -04:00
|
|
|
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(out, checker.Equals, "foobar\nfoobar\n")
|
2014-06-27 11:47:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-06-03 15:21:38 -04:00
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
|
2015-10-14 05:29:13 -04:00
|
|
|
out = strings.Trim(out, " \n\r")
|
|
|
|
c.Assert(out, checker.Equals, "1")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-06-03 15:21:38 -04:00
|
|
|
source, err := inspectMountSourceField(cleanedContainerID, "/test")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
dockerCmd(c, "restart", cleanedContainerID)
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-06-03 15:21:38 -04:00
|
|
|
out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
|
2015-10-14 05:29:13 -04:00
|
|
|
out = strings.Trim(out, " \n\r")
|
|
|
|
c.Assert(out, checker.Equals, "1")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-06-03 15:21:38 -04:00
|
|
|
sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, "/test")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(source, checker.Equals, sourceAfterRestart)
|
2014-06-27 11:47:32 -04:00
|
|
|
}
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(name, checker.Equals, "no")
|
2015-01-16 04:58:26 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(name, checker.Equals, "always")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2015-03-29 21:07:58 -04:00
|
|
|
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-03-29 21:07:58 -04:00
|
|
|
|
|
|
|
// MaximumRetryCount=0 if the restart policy is always
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(MaximumRetryCount, checker.Equals, "0")
|
2015-01-16 04:58:26 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(name, checker.Equals, "on-failure")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
|
|
|
}
|
2015-03-19 08:19:39 -04:00
|
|
|
|
|
|
|
// a good container with --restart=on-failure:3
|
|
|
|
// MaximumRetryCount!=0; RestartCount=0
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
|
|
|
|
|
2015-03-19 08:19:39 -04:00
|
|
|
id := strings.TrimSpace(string(out))
|
2015-10-14 05:29:13 -04:00
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5*time.Second)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2015-03-19 08:19:39 -04:00
|
|
|
count, err := inspectField(id, "RestartCount")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(count, checker.Equals, "0")
|
|
|
|
|
2015-03-19 08:19:39 -04:00
|
|
|
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-10-14 05:29:13 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(MaximumRetryCount, checker.Equals, "3")
|
2015-03-19 08:19:39 -04:00
|
|
|
|
|
|
|
}
|
2015-10-14 00:08:47 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestContainerRestartSuccess(c *check.C) {
|
2015-11-25 15:58:41 -05:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
2015-10-14 00:08:47 -04:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "top")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
|
|
|
|
|
|
|
pidStr, err := inspectField(id, "State.Pid")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
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", 5*time.Second)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 5*time.Second)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestUserDefinedNetworkWithRestartPolicy(c *check.C) {
|
2015-11-25 18:38:33 -05:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
|
2016-01-15 00:22:34 -05: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
|
|
|
|
pidStr, err := inspectField("second", "State.Pid")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|