2014-06-27 15:47:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2015-10-14 04:08:47 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-06-27 15:47:32 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
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"
|
2023-07-28 07:43:38 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
2022-05-07 20:28:39 +00:00
|
|
|
"gotest.tools/v3/skip"
|
2014-06-27 15:47:32 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIRestartSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIRestartSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIRestartSuite) TestRestartStoppedContainer(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
|
|
|
|
cID := getIDByName(c, "test")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
out := cli.DockerCmd(c, "logs", cID).Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "foobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "restart", cID)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2016-03-16 03:10:49 +00:00
|
|
|
// Wait until the container has stopped
|
2023-07-28 07:43:38 +00:00
|
|
|
err := waitInspect(cID, "{{.State.Running}}", "false", 20*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-03-16 03:10:49 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
out = cli.DockerCmd(c, "logs", cID).Combined()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "foobar\nfoobar\n")
|
2014-06-27 15:47:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartRunningContainer(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
cID := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'").Stdout()
|
|
|
|
cID = strings.TrimSpace(cID)
|
|
|
|
cli.WaitRun(c, cID)
|
2014-07-29 06:46:14 +00:00
|
|
|
|
2019-09-09 21:08:22 +00:00
|
|
|
getLogs := func(c *testing.T) (interface{}, string) {
|
2023-07-28 07:43:38 +00:00
|
|
|
out := cli.DockerCmd(c, "logs", cID).Combined()
|
2019-09-09 21:09:27 +00:00
|
|
|
return out, ""
|
2016-12-29 01:54:47 +00:00
|
|
|
}
|
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
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second))
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "restart", "-t", "1", cID)
|
|
|
|
cli.WaitRun(c, cID)
|
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
|
2019-08-26 15:51:40 +00:00
|
|
|
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second))
|
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.
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartWithVolumes(c *testing.T) {
|
2016-02-24 23:21:56 +00:00
|
|
|
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
|
2023-07-28 07:43:38 +00:00
|
|
|
cID := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
|
|
|
|
out, err := inspectFilter(cID, "len .Mounts")
|
|
|
|
assert.NilError(c, err, "failed to inspect %s: %s", cID, out)
|
2015-10-14 09:29:13 +00:00
|
|
|
out = strings.Trim(out, " \n\r")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "1")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
source, err := inspectMountSourceField(cID, prefix+slash+"test")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "restart", cID)
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
out, err = inspectFilter(cID, "len .Mounts")
|
|
|
|
assert.NilError(c, err, "failed to inspect %s: %s", cID, out)
|
2015-10-14 09:29:13 +00:00
|
|
|
out = strings.Trim(out, " \n\r")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "1")
|
2014-06-27 15:47:32 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
sourceAfterRestart, err := inspectMountSourceField(cID, prefix+slash+"test")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, source, sourceAfterRestart)
|
2014-06-27 15:47:32 +00:00
|
|
|
}
|
2015-01-16 09:58:26 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartDisconnectedContainer(c *testing.T) {
|
2023-08-30 15:57:11 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace)
|
2017-03-22 09:38:26 +00:00
|
|
|
|
|
|
|
// Run a container on the default bridge network
|
2023-07-28 07:43:38 +00:00
|
|
|
cID := cli.DockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top").Stdout()
|
|
|
|
cID = strings.TrimSpace(cID)
|
|
|
|
cli.WaitRun(c, cID)
|
2017-03-22 09:38:26 +00:00
|
|
|
|
|
|
|
// Disconnect the container from the network
|
2023-07-28 07:43:38 +00:00
|
|
|
result := cli.DockerCmd(c, "network", "disconnect", "bridge", "c0")
|
|
|
|
assert.Assert(c, result.ExitCode == 0, result.Combined())
|
2017-03-22 09:38:26 +00:00
|
|
|
|
|
|
|
// Restart the container
|
2023-07-28 07:43:38 +00:00
|
|
|
result = cli.DockerCmd(c, "restart", "c0")
|
|
|
|
assert.Assert(c, result.ExitCode == 0, result.Combined())
|
2017-03-22 09:38:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartPolicyNO(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
cID := cli.DockerCmd(c, "create", "--restart=no", "busybox").Stdout()
|
|
|
|
cID = strings.TrimSpace(cID)
|
|
|
|
name := inspectField(c, cID, "HostConfig.RestartPolicy.Name")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, name, "no")
|
2015-01-16 09:58:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartPolicyAlways(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
id := cli.DockerCmd(c, "create", "--restart=always", "busybox").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2016-01-28 14:19:25 +00:00
|
|
|
name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, name, "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
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, MaximumRetryCount, "0")
|
2015-01-16 09:58:26 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartPolicyOnFailure(c *testing.T) {
|
2016-12-01 21:24:30 +00:00
|
|
|
out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "", out)
|
|
|
|
assert.Assert(c, strings.Contains(out, "maximum retry count cannot be negative"))
|
2016-12-01 21:24:30 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
id := cli.DockerCmd(c, "create", "--restart=on-failure:1", "busybox").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
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")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "1")
|
2016-12-01 21:24:30 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
id = cli.DockerCmd(c, "create", "--restart=on-failure:0", "busybox").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2016-12-01 21:24:30 +00:00
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "0")
|
2016-12-01 21:24:30 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
id = cli.DockerCmd(c, "create", "--restart=on-failure", "busybox").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2016-12-01 21:24:30 +00:00
|
|
|
name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
|
|
|
|
maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, name, "on-failure")
|
|
|
|
assert.Equal(c, maxRetry, "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
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartContainerwithGoodContainer(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
id := cli.DockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2016-02-24 23:21:56 +00:00
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 09:29:13 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
count := inspectField(c, id, "RestartCount")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, count, "0")
|
2015-10-14 09:29:13 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, MaximumRetryCount, "3")
|
2015-03-19 12:19:39 +00:00
|
|
|
}
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartContainerSuccess(c *testing.T) {
|
2019-08-07 08:29:39 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2019-02-22 19:15:27 +00: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 08:29:39 +00:00
|
|
|
if DaemonIsWindows() {
|
2022-05-07 20:28:39 +00:00
|
|
|
skip.If(c, testEnv.GitHubActions())
|
2022-02-17 17:25:38 +00:00
|
|
|
testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
|
2019-08-07 08:29:39 +00:00
|
|
|
}
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
id := runSleepingContainer(c, "-d", "--restart=always")
|
|
|
|
cli.WaitRun(c, id)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 04:08:47 +00:00
|
|
|
|
2016-02-24 23:21:56 +00:00
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-10-14 04:08:47 +00:00
|
|
|
}
|
2016-01-15 05:22:34 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartWithPolicyUserDefinedNetwork(c *testing.T) {
|
2016-02-24 23:21:56 +00:00
|
|
|
// TODO Windows. This may be portable following HNS integration post TP5.
|
2023-08-30 15:57:11 +00:00
|
|
|
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace)
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "network", "create", "-d", "bridge", "udNet")
|
2016-01-15 05:22:34 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
|
|
|
|
cli.WaitRun(c, "first")
|
2016-01-15 05:22:34 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second", "--link=first:foo", "busybox", "top")
|
|
|
|
cli.WaitRun(c, "second")
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
// ping to first and its alias foo must succeed
|
|
|
|
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
// 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)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
|
|
|
|
// ping to first and its alias foo must still succeed
|
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-01-15 05:22:34 +00:00
|
|
|
}
|
2016-04-07 23:12:05 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartPolicyAfterRestart(c *testing.T) {
|
2019-08-07 08:29:39 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2019-02-22 19:15:27 +00: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 08:29:39 +00:00
|
|
|
if DaemonIsWindows() {
|
2022-05-07 20:29:31 +00:00
|
|
|
skip.If(c, testEnv.GitHubActions())
|
|
|
|
testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
|
2019-08-07 08:29:39 +00:00
|
|
|
}
|
2016-04-07 23:12:05 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
id := runSleepingContainer(c, "-d", "--restart=always")
|
|
|
|
cli.WaitRun(c, id)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "restart", id)
|
|
|
|
cli.WaitRun(c, id)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
pidStr := inspectField(c, id, "State.Pid")
|
|
|
|
|
|
|
|
pid, err := strconv.Atoi(pidStr)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
p, err := os.FindProcess(pid)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, p != nil)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
err = p.Kill()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 23:12:05 +00:00
|
|
|
|
|
|
|
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 23:12:05 +00:00
|
|
|
}
|
2016-04-07 14:05:29 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartContainerwithRestartPolicy(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
id1 := cli.DockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false").Stdout()
|
|
|
|
id1 = strings.TrimSpace(id1)
|
|
|
|
id2 := cli.DockerCmd(c, "run", "-d", "--restart=always", "busybox", "false").Stdout()
|
|
|
|
id2 = strings.TrimSpace(id2)
|
2016-04-07 14:05:29 +00:00
|
|
|
|
2016-04-26 03:05:41 +00:00
|
|
|
waitTimeout := 15 * time.Second
|
2023-06-14 09:46:00 +00:00
|
|
|
if testEnv.DaemonInfo.OSType == "windows" {
|
2016-04-26 03:05:41 +00:00
|
|
|
waitTimeout = 150 * time.Second
|
|
|
|
}
|
|
|
|
err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 14:05:29 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "restart", id1)
|
|
|
|
cli.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)
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "stop", id1)
|
|
|
|
cli.DockerCmd(c, "stop", id2)
|
|
|
|
cli.DockerCmd(c, "start", id1)
|
|
|
|
cli.DockerCmd(c, "start", id2)
|
2016-12-13 11:04:53 +00:00
|
|
|
|
2019-08-13 14:46:32 +00:00
|
|
|
// Kill the containers, making sure they are stopped at the end of the test
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "kill", id1)
|
|
|
|
cli.DockerCmd(c, "kill", id2)
|
2016-12-13 11:04:53 +00:00
|
|
|
err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-12-13 11:04:53 +00:00
|
|
|
err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-04-07 14:05:29 +00:00
|
|
|
}
|
2016-03-01 16:30:27 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIRestartSuite) TestRestartAutoRemoveContainer(c *testing.T) {
|
2023-07-28 07:43:38 +00:00
|
|
|
id := runSleepingContainer(c, "--rm")
|
|
|
|
cli.DockerCmd(c, "restart", id)
|
2016-03-01 16:30:27 +00:00
|
|
|
err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2016-03-01 16:30:27 +00:00
|
|
|
|
2023-07-28 07:43:38 +00:00
|
|
|
out := cli.DockerCmd(c, "ps").Stdout()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.Contains(out, id[:12]), "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
|
2023-07-28 07:43:38 +00:00
|
|
|
cli.DockerCmd(c, "kill", id)
|
2016-03-01 16:30:27 +00:00
|
|
|
}
|