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"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-09-01 17:37:04 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
2014-06-27 11:47:32 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartStoppedContainer(c *testing.T) {
|
2016-02-28 05:47:37 -05:00
|
|
|
dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
|
2017-01-16 05:30:14 -05:00
|
|
|
cleanedContainerID := getIDByName(c, "test")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2016-02-28 05:47:37 -05:00
|
|
|
out, _ := dockerCmd(c, "logs", cleanedContainerID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, "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
|
|
|
|
2016-03-15 23:10:49 -04:00
|
|
|
// Wait until the container has stopped
|
2017-01-16 05:30:14 -05:00
|
|
|
err := waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-15 23:10:49 -04:00
|
|
|
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ = dockerCmd(c, "logs", cleanedContainerID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, "foobar\nfoobar\n")
|
2014-06-27 11:47:32 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartRunningContainer(c *testing.T) {
|
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
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cleanedContainerID))
|
2014-07-29 02:46:14 -04:00
|
|
|
|
2019-09-09 17:08:22 -04:00
|
|
|
getLogs := func(c *testing.T) (interface{}, string) {
|
2016-12-28 20:54:47 -05:00
|
|
|
out, _ := dockerCmd(c, "logs", cleanedContainerID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return out, ""
|
2016-12-28 20:54:47 -05:00
|
|
|
}
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2016-12-28 20:54:47 -05:00
|
|
|
// Wait 10 seconds for the 'echo' to appear in the logs
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second))
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2016-12-28 20:54:47 -05:00
|
|
|
dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cleanedContainerID))
|
2014-07-29 02:46:14 -04:00
|
|
|
|
2016-12-28 20:54:47 -05:00
|
|
|
// Wait 10 seconds for first 'echo' appear (again) in the logs
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second))
|
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.
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartWithVolumes(c *testing.T) {
|
2016-02-24 18:21:56 -05:00
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2016-01-28 09:19:25 -05:00
|
|
|
out, err := inspectFilter(cleanedContainerID, "len .Mounts")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out)
|
2015-10-14 05:29:13 -04:00
|
|
|
out = strings.Trim(out, " \n\r")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, "1")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2016-02-24 18:21:56 -05:00
|
|
|
source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
out, err = inspectFilter(cleanedContainerID, "len .Mounts")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out)
|
2015-10-14 05:29:13 -04:00
|
|
|
out = strings.Trim(out, " \n\r")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, "1")
|
2014-06-27 11:47:32 -04:00
|
|
|
|
2016-02-24 18:21:56 -05:00
|
|
|
sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, source, sourceAfterRestart)
|
2014-06-27 11:47:32 -04:00
|
|
|
}
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartDisconnectedContainer(c *testing.T) {
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace, NotArm)
|
2017-03-22 05:38:26 -04:00
|
|
|
|
|
|
|
// Run a container on the default bridge network
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
|
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cleanedContainerID))
|
2017-03-22 05:38:26 -04:00
|
|
|
|
|
|
|
// Disconnect the container from the network
|
2019-04-04 09:23:19 -04:00
|
|
|
out, exitCode := dockerCmd(c, "network", "disconnect", "bridge", "c0")
|
|
|
|
assert.Assert(c, exitCode == 0, out)
|
2017-03-22 05:38:26 -04:00
|
|
|
|
|
|
|
// Restart the container
|
2019-04-04 09:23:19 -04:00
|
|
|
out, exitCode = dockerCmd(c, "restart", "c0")
|
|
|
|
assert.Assert(c, exitCode == 0, out)
|
2017-03-22 05:38:26 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyNO(c *testing.T) {
|
2016-12-01 16:24:30 -05:00
|
|
|
out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, name, "no")
|
2015-01-16 04:58:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyAlways(c *testing.T) {
|
2016-12-01 16:24:30 -05:00
|
|
|
out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, name, "always")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-03-29 21:07:58 -04:00
|
|
|
|
|
|
|
// MaximumRetryCount=0 if the restart policy is always
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, MaximumRetryCount, "0")
|
2015-01-16 04:58:26 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyOnFailure(c *testing.T) {
|
2016-12-01 16:24:30 -05:00
|
|
|
out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "", out)
|
|
|
|
assert.Assert(c, strings.Contains(out, "maximum retry count cannot be negative"))
|
2016-12-01 16:24:30 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2016-01-28 09:19:25 -05:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2016-12-01 16:24:30 -05:00
|
|
|
maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "1")
|
2016-12-01 16:24:30 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
|
2015-01-16 04:58:26 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id = strings.TrimSpace(out)
|
2016-12-01 16:24:30 -05:00
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "0")
|
2016-12-01 16:24:30 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
|
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id = strings.TrimSpace(out)
|
2016-12-01 16:24:30 -05:00
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "0")
|
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
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *testing.T) {
|
2015-07-14 16:15:00 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
|
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2016-02-24 18:21:56 -05:00
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 05:29:13 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
count := inspectField(c, id, "RestartCount")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, count, "0")
|
2015-10-14 05:29:13 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, MaximumRetryCount, "3")
|
2015-03-19 08:19:39 -04:00
|
|
|
}
|
2015-10-14 00:08:47 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartContainerSuccess(c *testing.T) {
|
2019-08-07 04:29:39 -04:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2019-02-22 14:15:27 -05:00
|
|
|
// Skipped for Hyper-V isolated containers. Test is currently written
|
|
|
|
// such that it assumes there is a host process to kill. In Hyper-V
|
|
|
|
// containers, the process is inside the utility VM, not on the host.
|
2019-08-07 04:29:39 -04:00
|
|
|
if DaemonIsWindows() {
|
2022-02-17 12:25:38 -05:00
|
|
|
testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
|
2019-08-07 04:29:39 -04:00
|
|
|
}
|
2015-10-14 00:08:47 -04:00
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d", "--restart=always")
|
2015-10-14 00:08:47 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(id))
|
2015-10-14 00:08:47 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
pidStr := inspectField(c, id, "State.Pid")
|
2015-10-14 00:08:47 -04:00
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 00:08:47 -04:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2015-10-14 00:08:47 -04:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 00:08:47 -04:00
|
|
|
|
2016-02-24 18:21:56 -05:00
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 00:08:47 -04:00
|
|
|
|
2016-02-24 18:21:56 -05:00
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 00:08:47 -04:00
|
|
|
}
|
2016-01-15 00:22:34 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *testing.T) {
|
2016-02-24 18:21:56 -05:00
|
|
|
// TODO Windows. This may be portable following HNS integration post TP5.
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, 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")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun("first"))
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
|
|
|
|
"--link=first:foo", "busybox", "top")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun("second"))
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
// ping to first and its alias foo must succeed
|
|
|
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
// Now kill the second container and let the restart policy kick in
|
2016-01-28 09:19:25 -05:00
|
|
|
pidStr := inspectField(c, "second", "State.Pid")
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
|
|
|
|
// ping to first and its alias foo must still succeed
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 00:22:34 -05:00
|
|
|
}
|
2016-04-07 19:12:05 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartPolicyAfterRestart(c *testing.T) {
|
2019-08-07 04:29:39 -04:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2019-02-22 14:15:27 -05:00
|
|
|
// Skipped for Hyper-V isolated containers. Test is currently written
|
|
|
|
// such that it assumes there is a host process to kill. In Hyper-V
|
|
|
|
// containers, the process is inside the utility VM, not on the host.
|
2019-08-07 04:29:39 -04:00
|
|
|
if DaemonIsWindows() {
|
2022-02-17 12:25:38 -05:00
|
|
|
testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
|
2019-08-07 04:29:39 -04:00
|
|
|
}
|
2016-04-07 19:12:05 -04:00
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d", "--restart=always")
|
2016-04-07 19:12:05 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(id))
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "restart", id)
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(id))
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
pidStr := inspectField(c, id, "State.Pid")
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 19:12:05 -04:00
|
|
|
|
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 19:12:05 -04:00
|
|
|
}
|
2016-04-07 10:05:29 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *testing.T) {
|
2016-04-07 10:05:29 -04:00
|
|
|
out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
|
|
|
|
out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
|
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id1 := strings.TrimSpace(out1)
|
|
|
|
id2 := strings.TrimSpace(out2)
|
2016-04-25 23:05:41 -04:00
|
|
|
waitTimeout := 15 * time.Second
|
2018-01-15 09:32:06 -05:00
|
|
|
if testEnv.OSType == "windows" {
|
2016-04-25 23:05:41 -04:00
|
|
|
waitTimeout = 150 * time.Second
|
|
|
|
}
|
|
|
|
err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 10:05:29 -04:00
|
|
|
|
2016-04-25 23:05:41 -04:00
|
|
|
dockerCmd(c, "restart", id1)
|
|
|
|
dockerCmd(c, "restart", id2)
|
2016-04-07 10:05:29 -04:00
|
|
|
|
2016-12-13 06:04:53 -05:00
|
|
|
// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
|
2016-04-07 10:05:29 -04:00
|
|
|
dockerCmd(c, "stop", id1)
|
|
|
|
dockerCmd(c, "stop", id2)
|
|
|
|
dockerCmd(c, "start", id1)
|
|
|
|
dockerCmd(c, "start", id2)
|
2016-12-13 06:04:53 -05:00
|
|
|
|
2019-08-13 10:46:32 -04:00
|
|
|
// Kill the containers, making sure they are stopped at the end of the test
|
2016-12-13 06:04:53 -05:00
|
|
|
dockerCmd(c, "kill", id1)
|
|
|
|
dockerCmd(c, "kill", id2)
|
|
|
|
err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-12-13 06:04:53 -05:00
|
|
|
err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 10:05:29 -04:00
|
|
|
}
|
2016-03-01 11:30:27 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestRestartAutoRemoveContainer(c *testing.T) {
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "--rm")
|
2016-03-01 11:30:27 -05:00
|
|
|
|
2019-08-05 11:54:15 -04:00
|
|
|
id := strings.TrimSpace(out)
|
2016-03-01 11:30:27 -05:00
|
|
|
dockerCmd(c, "restart", id)
|
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-03-01 11:30:27 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "ps")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.Contains(out, id[:12]), "container should be restarted instead of removed: %v", out)
|
2016-12-08 10:28:06 -05:00
|
|
|
|
|
|
|
// Kill the container to make sure it will be removed
|
|
|
|
dockerCmd(c, "kill", id)
|
2016-03-01 11:30:27 -05:00
|
|
|
}
|