2014-06-27 19:47:32 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2014-07-29 10:46:14 +04:00
|
|
|
"time"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-06-27 19:47:32 +04:00
|
|
|
)
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2014-06-27 19:47:32 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
|
2014-10-15 01:18:55 +04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if out != "foobar\n" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("container should've printed 'foobar'")
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
|
2014-10-15 01:18:55 +04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if out != "foobar\nfoobar\n" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("container should've printed 'foobar' twice")
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2014-06-27 19:47:32 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-27 19:47:32 +04:00
|
|
|
|
2014-07-29 10:46:14 +04:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2014-06-27 19:47:32 +04:00
|
|
|
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if out != "foobar\n" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("container should've printed 'foobar'")
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
|
2014-10-15 01:18:55 +04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
2014-07-29 10:46:14 +04:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2014-06-27 19:47:32 +04:00
|
|
|
if out != "foobar\nfoobar\n" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("container should've printed 'foobar' twice")
|
2014-06-27 19: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 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
|
2015-02-20 09:56:02 +03:00
|
|
|
|
2014-06-27 19:47:32 +04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
2015-04-06 16:21:18 +03:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if out = strings.Trim(out, " \n\r"); out != "1" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("expect 1 volume received %s", out)
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:41:01 +03:00
|
|
|
volumes, err := inspectField(cleanedContainerID, "Volumes")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
|
2014-10-15 01:18:55 +04:00
|
|
|
if out, _, err = runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-15 01:18:55 +04:00
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(out, err)
|
2014-10-15 01:18:55 +04:00
|
|
|
}
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if out = strings.Trim(out, " \n\r"); out != "1" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("expect 1 volume after restart received %s", out)
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
2015-07-08 22:41:01 +03:00
|
|
|
volumesAfterRestart, err := inspectField(cleanedContainerID, "Volumes")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-06-27 19:47:32 +04:00
|
|
|
|
|
|
|
if volumes != volumesAfterRestart {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
|
2014-06-27 19:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-16 12:58:26 +03:00
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
|
2015-01-16 12:58:26 +03:00
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, out)
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-16 12:58:26 +03:00
|
|
|
if name != "no" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
|
2015-01-16 12:58:26 +03:00
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, out)
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-16 12:58:26 +03:00
|
|
|
if name != "always" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
2015-03-30 04:07:58 +03:00
|
|
|
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-30 04:07:58 +03:00
|
|
|
|
|
|
|
// MaximumRetryCount=0 if the restart policy is always
|
|
|
|
if MaximumRetryCount != "0" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
|
2015-03-30 04:07:58 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
|
2015-01-16 12:58:26 +03:00
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err, out)
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-01-16 12:58:26 +03:00
|
|
|
if name != "on-failure" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
|
2015-01-16 12:58:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-03-19 15:19:39 +03:00
|
|
|
|
|
|
|
// a good container with --restart=on-failure:3
|
|
|
|
// MaximumRetryCount!=0; RestartCount=0
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
|
2015-03-19 15:19:39 +03:00
|
|
|
out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
|
|
|
|
if err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(string(out), err)
|
2015-03-19 15:19:39 +03:00
|
|
|
}
|
|
|
|
id := strings.TrimSpace(string(out))
|
|
|
|
if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatal(err)
|
2015-03-19 15:19:39 +03:00
|
|
|
}
|
|
|
|
count, err := inspectField(id, "RestartCount")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-19 15:19:39 +03:00
|
|
|
if count != "0" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container was restarted %s times, expected %d", count, 0)
|
2015-03-19 15:19:39 +03:00
|
|
|
}
|
|
|
|
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
|
2015-05-18 05:06:13 +03:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-19 15:19:39 +03:00
|
|
|
if MaximumRetryCount != "3" {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
|
2015-03-19 15:19:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|