9da6c805 |
package main
import ( |
be9b7a24 |
"os"
"strconv" |
9da6c805 |
"strings" |
e25352a4 |
"testing" |
8a5ab83d |
"time" |
dc944ea7 |
|
33968e6c |
"github.com/docker/docker/integration-cli/checker" |
6345208b |
"gotest.tools/assert"
is "gotest.tools/assert/cmp" |
9266ff78 |
"gotest.tools/poll" |
9da6c805 |
)
|
64a928a3 |
func (s *DockerSuite) TestRestartStoppedContainer(c *testing.T) { |
e4468913 |
dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar") |
c10f6ef4 |
cleanedContainerID := getIDByName(c, "test") |
9da6c805 |
|
e4468913 |
out, _ := dockerCmd(c, "logs", cleanedContainerID) |
6345208b |
assert.Equal(c, out, "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) |
6345208b |
assert.NilError(c, err) |
48ccdd46 |
|
71868228 |
out, _ = dockerCmd(c, "logs", cleanedContainerID) |
6345208b |
assert.Equal(c, out, "foobar\nfoobar\n") |
9da6c805 |
}
|
64a928a3 |
func (s *DockerSuite) TestRestartRunningContainer(c *testing.T) { |
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 |
|
6345208b |
assert.NilError(c, waitRun(cleanedContainerID)) |
59316693 |
|
1b1fe4cc |
getLogs := func(c *testing.T) (interface{}, string) { |
bae22d16 |
out, _ := dockerCmd(c, "logs", cleanedContainerID) |
be66788e |
return out, "" |
bae22d16 |
} |
9da6c805 |
|
bae22d16 |
// Wait 10 seconds for the 'echo' to appear in the logs |
9266ff78 |
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second)) |
9da6c805 |
|
bae22d16 |
dockerCmd(c, "restart", "-t", "1", cleanedContainerID) |
6345208b |
assert.NilError(c, waitRun(cleanedContainerID)) |
59316693 |
|
bae22d16 |
// Wait 10 seconds for first 'echo' appear (again) in the logs |
9266ff78 |
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second)) |
9da6c805 |
}
// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. |
64a928a3 |
func (s *DockerSuite) TestRestartWithVolumes(c *testing.T) { |
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") |
6345208b |
assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out) |
196af472 |
out = strings.Trim(out, " \n\r") |
6345208b |
assert.Equal(c, out, "1") |
9da6c805 |
|
281c1ced |
source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") |
6345208b |
assert.NilError(c, err) |
9da6c805 |
|
71868228 |
dockerCmd(c, "restart", cleanedContainerID) |
9da6c805 |
|
62a856e9 |
out, err = inspectFilter(cleanedContainerID, "len .Mounts") |
6345208b |
assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out) |
196af472 |
out = strings.Trim(out, " \n\r") |
6345208b |
assert.Equal(c, out, "1") |
9da6c805 |
|
281c1ced |
sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") |
6345208b |
assert.NilError(c, err)
assert.Equal(c, source, sourceAfterRestart) |
9da6c805 |
} |
c3ed49dc |
|
64a928a3 |
func (s *DockerSuite) TestRestartDisconnectedContainer(c *testing.T) { |
43b15e92 |
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace, NotArm) |
4ca7d4f0 |
// Run a container on the default bridge network
out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
cleanedContainerID := strings.TrimSpace(out) |
6345208b |
assert.NilError(c, waitRun(cleanedContainerID)) |
4ca7d4f0 |
// Disconnect the container from the network |
6345208b |
out, exitCode := dockerCmd(c, "network", "disconnect", "bridge", "c0")
assert.Assert(c, exitCode == 0, out) |
4ca7d4f0 |
// Restart the container |
6345208b |
out, exitCode = dockerCmd(c, "restart", "c0")
assert.Assert(c, exitCode == 0, out) |
4ca7d4f0 |
}
|
64a928a3 |
func (s *DockerSuite) TestRestartPolicyNO(c *testing.T) { |
9db5d649 |
out, _ := dockerCmd(c, "create", "--restart=no", "busybox") |
c3ed49dc |
id := strings.TrimSpace(string(out)) |
62a856e9 |
name := inspectField(c, id, "HostConfig.RestartPolicy.Name") |
6345208b |
assert.Equal(c, name, "no") |
c3ed49dc |
}
|
64a928a3 |
func (s *DockerSuite) TestRestartPolicyAlways(c *testing.T) { |
9db5d649 |
out, _ := dockerCmd(c, "create", "--restart=always", "busybox") |
c3ed49dc |
id := strings.TrimSpace(string(out)) |
62a856e9 |
name := inspectField(c, id, "HostConfig.RestartPolicy.Name") |
6345208b |
assert.Equal(c, name, "always") |
c3ed49dc |
|
62a856e9 |
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") |
f5310f40 |
// MaximumRetryCount=0 if the restart policy is always |
6345208b |
assert.Equal(c, MaximumRetryCount, "0") |
c3ed49dc |
}
|
64a928a3 |
func (s *DockerSuite) TestRestartPolicyOnFailure(c *testing.T) { |
9db5d649 |
out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox") |
6345208b |
assert.ErrorContains(c, err, "", out)
assert.Assert(c, strings.Contains(out, "maximum retry count cannot be negative")) |
9db5d649 |
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")
|
6345208b |
assert.Equal(c, name, "on-failure")
assert.Equal(c, maxRetry, "1") |
9db5d649 |
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")
|
6345208b |
assert.Equal(c, name, "on-failure")
assert.Equal(c, maxRetry, "0") |
9db5d649 |
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")
|
6345208b |
assert.Equal(c, name, "on-failure")
assert.Equal(c, maxRetry, "0") |
c3ed49dc |
} |
13398319 |
// a good container with --restart=on-failure:3
// MaximumRetryCount!=0; RestartCount=0 |
64a928a3 |
func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *testing.T) { |
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) |
6345208b |
assert.NilError(c, err) |
196af472 |
|
62a856e9 |
count := inspectField(c, id, "RestartCount") |
6345208b |
assert.Equal(c, count, "0") |
196af472 |
|
62a856e9 |
MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") |
6345208b |
assert.Equal(c, MaximumRetryCount, "3") |
13398319 |
} |
be9b7a24 |
|
64a928a3 |
func (s *DockerSuite) TestRestartContainerSuccess(c *testing.T) { |
41999568 |
testRequires(c, testEnv.IsLocalDaemon) |
faaffd5d |
// 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. |
41999568 |
if DaemonIsWindows() {
testRequires(c, IsolationIsProcess)
} |
be9b7a24 |
|
a899aa67 |
out := runSleepingContainer(c, "-d", "--restart=always") |
be9b7a24 |
id := strings.TrimSpace(out) |
6345208b |
assert.NilError(c, waitRun(id)) |
be9b7a24 |
|
62a856e9 |
pidStr := inspectField(c, id, "State.Pid") |
be9b7a24 |
pid, err := strconv.Atoi(pidStr) |
6345208b |
assert.NilError(c, err) |
be9b7a24 |
p, err := os.FindProcess(pid) |
6345208b |
assert.NilError(c, err)
assert.Assert(c, p != nil) |
be9b7a24 |
err = p.Kill() |
6345208b |
assert.NilError(c, err) |
be9b7a24 |
|
281c1ced |
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) |
6345208b |
assert.NilError(c, err) |
be9b7a24 |
|
281c1ced |
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) |
6345208b |
assert.NilError(c, err) |
be9b7a24 |
} |
74819610 |
|
64a928a3 |
func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *testing.T) { |
281c1ced |
// TODO Windows. This may be portable following HNS integration post TP5. |
43b15e92 |
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace, NotArm) |
74819610 |
dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top") |
6345208b |
assert.NilError(c, waitRun("first")) |
74819610 |
dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
"--link=first:foo", "busybox", "top") |
6345208b |
assert.NilError(c, waitRun("second")) |
74819610 |
// ping to first and its alias foo must succeed
_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
6345208b |
assert.NilError(c, err) |
74819610 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") |
6345208b |
assert.NilError(c, err) |
74819610 |
// 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) |
6345208b |
assert.NilError(c, err) |
74819610 |
p, err := os.FindProcess(pid) |
6345208b |
assert.NilError(c, err)
assert.Assert(c, p != nil) |
74819610 |
err = p.Kill() |
6345208b |
assert.NilError(c, err) |
74819610 |
err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second) |
6345208b |
assert.NilError(c, err) |
74819610 |
err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second) |
6345208b |
assert.NilError(c, err) |
74819610 |
// ping to first and its alias foo must still succeed
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") |
6345208b |
assert.NilError(c, err) |
74819610 |
_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") |
6345208b |
assert.NilError(c, err) |
74819610 |
} |
20390f65 |
|
64a928a3 |
func (s *DockerSuite) TestRestartPolicyAfterRestart(c *testing.T) { |
41999568 |
testRequires(c, testEnv.IsLocalDaemon) |
faaffd5d |
// 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. |
41999568 |
if DaemonIsWindows() {
testRequires(c, IsolationIsProcess)
} |
20390f65 |
|
a899aa67 |
out := runSleepingContainer(c, "-d", "--restart=always") |
20390f65 |
id := strings.TrimSpace(out) |
6345208b |
assert.NilError(c, waitRun(id)) |
20390f65 |
dockerCmd(c, "restart", id)
|
6345208b |
assert.NilError(c, waitRun(id)) |
20390f65 |
pidStr := inspectField(c, id, "State.Pid")
pid, err := strconv.Atoi(pidStr) |
6345208b |
assert.NilError(c, err) |
20390f65 |
p, err := os.FindProcess(pid) |
6345208b |
assert.NilError(c, err)
assert.Assert(c, p != nil) |
20390f65 |
err = p.Kill() |
6345208b |
assert.NilError(c, err) |
20390f65 |
err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) |
6345208b |
assert.NilError(c, err) |
20390f65 |
err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) |
6345208b |
assert.NilError(c, err) |
20390f65 |
} |
a705e166 |
|
64a928a3 |
func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *testing.T) { |
a705e166 |
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) |
6345208b |
assert.NilError(c, err) |
a705e166 |
|
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 |
|
1da2e90b |
// Kill the containers, making sure they are stopped at the end of the test |
e857785d |
dockerCmd(c, "kill", id1)
dockerCmd(c, "kill", id2)
err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) |
6345208b |
assert.NilError(c, err) |
e857785d |
err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout) |
6345208b |
assert.NilError(c, err) |
a705e166 |
} |
3c2886d8 |
|
64a928a3 |
func (s *DockerSuite) TestRestartAutoRemoveContainer(c *testing.T) { |
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) |
6345208b |
assert.NilError(c, err) |
3c2886d8 |
out, _ = dockerCmd(c, "ps") |
6345208b |
assert.Assert(c, is.Contains(out, id[:12]), "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 |
} |