9da6c805 |
package main
import (
"os/exec"
"strings" |
59316693 |
"time" |
dc944ea7 |
"github.com/go-check/check" |
9da6c805 |
)
|
dc944ea7 |
func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { |
70407ce4 |
|
9da6c805 |
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
out, _, err := runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
|
475c6531 |
cleanedContainerID := strings.TrimSpace(out) |
9da6c805 |
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) |
9f52d8e6 |
if out, _, err = runCommandWithOutput(runCmd); err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
if out != "foobar\n" { |
dc944ea7 |
c.Errorf("container should've printed 'foobar'") |
9da6c805 |
}
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) |
9f52d8e6 |
if out, _, err = runCommandWithOutput(runCmd); err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
if out != "foobar\nfoobar\n" { |
dc944ea7 |
c.Errorf("container should've printed 'foobar' twice") |
9da6c805 |
}
}
|
dc944ea7 |
func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { |
70407ce4 |
|
9da6c805 |
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
out, _, err := runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
|
475c6531 |
cleanedContainerID := strings.TrimSpace(out) |
9da6c805 |
|
59316693 |
time.Sleep(1 * time.Second)
|
9da6c805 |
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
if out != "foobar\n" { |
dc944ea7 |
c.Errorf("container should've printed 'foobar'") |
9da6c805 |
}
runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID) |
9f52d8e6 |
if out, _, err = runCommandWithOutput(runCmd); err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
|
59316693 |
time.Sleep(1 * time.Second)
|
9da6c805 |
if out != "foobar\nfoobar\n" { |
dc944ea7 |
c.Errorf("container should've printed 'foobar' twice") |
9da6c805 |
}
}
// Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. |
dc944ea7 |
func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { |
70407ce4 |
|
9da6c805 |
runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
|
475c6531 |
cleanedContainerID := strings.TrimSpace(out) |
9da6c805 |
runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
if out = strings.Trim(out, " \n\r"); out != "1" { |
dc944ea7 |
c.Errorf("expect 1 volume received %s", out) |
9da6c805 |
}
|
74f8a4ec |
volumes, err := inspectField(cleanedContainerID, ".Volumes")
c.Assert(err, check.IsNil) |
9da6c805 |
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) |
9f52d8e6 |
if out, _, err = runCommandWithOutput(runCmd); err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd) |
9f52d8e6 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
9f52d8e6 |
} |
9da6c805 |
if out = strings.Trim(out, " \n\r"); out != "1" { |
dc944ea7 |
c.Errorf("expect 1 volume after restart received %s", out) |
9da6c805 |
}
|
74f8a4ec |
volumesAfterRestart, err := inspectField(cleanedContainerID, ".Volumes")
c.Assert(err, check.IsNil) |
9da6c805 |
if volumes != volumesAfterRestart { |
dc944ea7 |
c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart) |
9da6c805 |
}
} |
c3ed49dc |
|
dc944ea7 |
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { |
c3ed49dc |
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil { |
dc944ea7 |
c.Fatal(err, out) |
c3ed49dc |
}
id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") |
74f8a4ec |
c.Assert(err, check.IsNil) |
c3ed49dc |
if name != "no" { |
dc944ea7 |
c.Fatalf("Container restart policy name is %s, expected %s", name, "no") |
c3ed49dc |
}
}
|
dc944ea7 |
func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { |
c3ed49dc |
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil { |
dc944ea7 |
c.Fatal(err, out) |
c3ed49dc |
}
id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") |
74f8a4ec |
c.Assert(err, check.IsNil) |
c3ed49dc |
if name != "always" { |
dc944ea7 |
c.Fatalf("Container restart policy name is %s, expected %s", name, "always") |
c3ed49dc |
}
|
f5310f40 |
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount") |
74f8a4ec |
c.Assert(err, check.IsNil) |
f5310f40 |
// MaximumRetryCount=0 if the restart policy is always
if MaximumRetryCount != "0" { |
dc944ea7 |
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0") |
f5310f40 |
}
|
c3ed49dc |
}
|
dc944ea7 |
func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { |
c3ed49dc |
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil { |
dc944ea7 |
c.Fatal(err, out) |
c3ed49dc |
}
id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") |
74f8a4ec |
c.Assert(err, check.IsNil) |
c3ed49dc |
if name != "on-failure" { |
dc944ea7 |
c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure") |
c3ed49dc |
}
} |
13398319 |
// a good container with --restart=on-failure:3
// MaximumRetryCount!=0; RestartCount=0 |
dc944ea7 |
func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) { |
13398319 |
out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
if err != nil { |
dc944ea7 |
c.Fatal(string(out), err) |
13398319 |
}
id := strings.TrimSpace(string(out))
if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil { |
dc944ea7 |
c.Fatal(err) |
13398319 |
}
count, err := inspectField(id, "RestartCount") |
74f8a4ec |
c.Assert(err, check.IsNil) |
13398319 |
if count != "0" { |
dc944ea7 |
c.Fatalf("Container was restarted %s times, expected %d", count, 0) |
13398319 |
}
MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount") |
74f8a4ec |
c.Assert(err, check.IsNil) |
13398319 |
if MaximumRetryCount != "3" { |
dc944ea7 |
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3") |
13398319 |
}
} |