github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/integration-cli/docker_cli_restart_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/integration-cli/checker"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
    14  	dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
    15  	cleanedContainerID := getIDByName(c, "test")
    16  
    17  	out, _ := dockerCmd(c, "logs", cleanedContainerID)
    18  	c.Assert(out, checker.Equals, "foobar\n")
    19  
    20  	dockerCmd(c, "restart", cleanedContainerID)
    21  
    22  	// Wait until the container has stopped
    23  	err := waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
    24  	c.Assert(err, checker.IsNil)
    25  
    26  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    27  	c.Assert(out, checker.Equals, "foobar\nfoobar\n")
    28  }
    29  
    30  func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
    31  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
    32  
    33  	cleanedContainerID := strings.TrimSpace(out)
    34  
    35  	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
    36  
    37  	getLogs := func(c *check.C) (interface{}, check.CommentInterface) {
    38  		out, _ := dockerCmd(c, "logs", cleanedContainerID)
    39  		return out, nil
    40  	}
    41  
    42  	// Wait 10 seconds for the 'echo' to appear in the logs
    43  	waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\n")
    44  
    45  	dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
    46  	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
    47  
    48  	// Wait 10 seconds for first 'echo' appear (again) in the logs
    49  	waitAndAssert(c, 10*time.Second, getLogs, checker.Equals, "foobar\nfoobar\n")
    50  }
    51  
    52  // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
    53  func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
    54  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
    55  	out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
    56  
    57  	cleanedContainerID := strings.TrimSpace(out)
    58  	out, err := inspectFilter(cleanedContainerID, "len .Mounts")
    59  	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
    60  	out = strings.Trim(out, " \n\r")
    61  	c.Assert(out, checker.Equals, "1")
    62  
    63  	source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
    64  	c.Assert(err, checker.IsNil)
    65  
    66  	dockerCmd(c, "restart", cleanedContainerID)
    67  
    68  	out, err = inspectFilter(cleanedContainerID, "len .Mounts")
    69  	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
    70  	out = strings.Trim(out, " \n\r")
    71  	c.Assert(out, checker.Equals, "1")
    72  
    73  	sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
    74  	c.Assert(err, checker.IsNil)
    75  	c.Assert(source, checker.Equals, sourceAfterRestart)
    76  }
    77  
    78  func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
    79  	out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
    80  
    81  	id := strings.TrimSpace(string(out))
    82  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
    83  	c.Assert(name, checker.Equals, "no")
    84  }
    85  
    86  func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
    87  	out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
    88  
    89  	id := strings.TrimSpace(string(out))
    90  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
    91  	c.Assert(name, checker.Equals, "always")
    92  
    93  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
    94  
    95  	// MaximumRetryCount=0 if the restart policy is always
    96  	c.Assert(MaximumRetryCount, checker.Equals, "0")
    97  }
    98  
    99  func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
   100  	out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
   101  	c.Assert(err, check.NotNil, check.Commentf(out))
   102  	c.Assert(out, checker.Contains, "maximum retry count cannot be negative")
   103  
   104  	out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
   105  
   106  	id := strings.TrimSpace(string(out))
   107  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
   108  	maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   109  
   110  	c.Assert(name, checker.Equals, "on-failure")
   111  	c.Assert(maxRetry, checker.Equals, "1")
   112  
   113  	out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
   114  
   115  	id = strings.TrimSpace(string(out))
   116  	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
   117  	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   118  
   119  	c.Assert(name, checker.Equals, "on-failure")
   120  	c.Assert(maxRetry, checker.Equals, "0")
   121  
   122  	out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
   123  
   124  	id = strings.TrimSpace(string(out))
   125  	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
   126  	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   127  
   128  	c.Assert(name, checker.Equals, "on-failure")
   129  	c.Assert(maxRetry, checker.Equals, "0")
   130  }
   131  
   132  // a good container with --restart=on-failure:3
   133  // MaximumRetryCount!=0; RestartCount=0
   134  func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
   135  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
   136  
   137  	id := strings.TrimSpace(string(out))
   138  	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
   139  	c.Assert(err, checker.IsNil)
   140  
   141  	count := inspectField(c, id, "RestartCount")
   142  	c.Assert(count, checker.Equals, "0")
   143  
   144  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   145  	c.Assert(MaximumRetryCount, checker.Equals, "3")
   146  
   147  }
   148  
   149  func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
   150  	testRequires(c, SameHostDaemon)
   151  
   152  	out, _ := runSleepingContainer(c, "-d", "--restart=always")
   153  	id := strings.TrimSpace(out)
   154  	c.Assert(waitRun(id), check.IsNil)
   155  
   156  	pidStr := inspectField(c, id, "State.Pid")
   157  
   158  	pid, err := strconv.Atoi(pidStr)
   159  	c.Assert(err, check.IsNil)
   160  
   161  	p, err := os.FindProcess(pid)
   162  	c.Assert(err, check.IsNil)
   163  	c.Assert(p, check.NotNil)
   164  
   165  	err = p.Kill()
   166  	c.Assert(err, check.IsNil)
   167  
   168  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   169  	c.Assert(err, check.IsNil)
   170  
   171  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   172  	c.Assert(err, check.IsNil)
   173  }
   174  
   175  func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
   176  	// TODO Windows. This may be portable following HNS integration post TP5.
   177  	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
   178  	dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
   179  
   180  	dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
   181  	c.Assert(waitRun("first"), check.IsNil)
   182  
   183  	dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
   184  		"--link=first:foo", "busybox", "top")
   185  	c.Assert(waitRun("second"), check.IsNil)
   186  
   187  	// ping to first and its alias foo must succeed
   188  	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   189  	c.Assert(err, check.IsNil)
   190  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   191  	c.Assert(err, check.IsNil)
   192  
   193  	// Now kill the second container and let the restart policy kick in
   194  	pidStr := inspectField(c, "second", "State.Pid")
   195  
   196  	pid, err := strconv.Atoi(pidStr)
   197  	c.Assert(err, check.IsNil)
   198  
   199  	p, err := os.FindProcess(pid)
   200  	c.Assert(err, check.IsNil)
   201  	c.Assert(p, check.NotNil)
   202  
   203  	err = p.Kill()
   204  	c.Assert(err, check.IsNil)
   205  
   206  	err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
   207  	c.Assert(err, check.IsNil)
   208  
   209  	err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
   210  
   211  	// ping to first and its alias foo must still succeed
   212  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   213  	c.Assert(err, check.IsNil)
   214  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   215  	c.Assert(err, check.IsNil)
   216  }
   217  
   218  func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
   219  	testRequires(c, SameHostDaemon)
   220  
   221  	out, _ := runSleepingContainer(c, "-d", "--restart=always")
   222  	id := strings.TrimSpace(out)
   223  	c.Assert(waitRun(id), check.IsNil)
   224  
   225  	dockerCmd(c, "restart", id)
   226  
   227  	c.Assert(waitRun(id), check.IsNil)
   228  
   229  	pidStr := inspectField(c, id, "State.Pid")
   230  
   231  	pid, err := strconv.Atoi(pidStr)
   232  	c.Assert(err, check.IsNil)
   233  
   234  	p, err := os.FindProcess(pid)
   235  	c.Assert(err, check.IsNil)
   236  	c.Assert(p, check.NotNil)
   237  
   238  	err = p.Kill()
   239  	c.Assert(err, check.IsNil)
   240  
   241  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   242  	c.Assert(err, check.IsNil)
   243  
   244  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   245  	c.Assert(err, check.IsNil)
   246  }
   247  
   248  func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
   249  	out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
   250  	out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
   251  
   252  	id1 := strings.TrimSpace(string(out1))
   253  	id2 := strings.TrimSpace(string(out2))
   254  	waitTimeout := 15 * time.Second
   255  	if testEnv.DaemonPlatform() == "windows" {
   256  		waitTimeout = 150 * time.Second
   257  	}
   258  	err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   259  	c.Assert(err, checker.IsNil)
   260  
   261  	dockerCmd(c, "restart", id1)
   262  	dockerCmd(c, "restart", id2)
   263  
   264  	// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
   265  	dockerCmd(c, "stop", id1)
   266  	dockerCmd(c, "stop", id2)
   267  	dockerCmd(c, "start", id1)
   268  	dockerCmd(c, "start", id2)
   269  
   270  	// Kill the containers, making sure the are stopped at the end of the test
   271  	dockerCmd(c, "kill", id1)
   272  	dockerCmd(c, "kill", id2)
   273  	err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   274  	c.Assert(err, checker.IsNil)
   275  	err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   276  	c.Assert(err, checker.IsNil)
   277  }
   278  
   279  func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
   280  	out, _ := runSleepingContainer(c, "--rm")
   281  
   282  	id := strings.TrimSpace(string(out))
   283  	dockerCmd(c, "restart", id)
   284  	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
   285  	c.Assert(err, checker.IsNil)
   286  
   287  	out, _ = dockerCmd(c, "ps")
   288  	c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
   289  
   290  	// Kill the container to make sure it will be removed
   291  	dockerCmd(c, "kill", id)
   292  }