github.com/OpenFlowLabs/moby@v17.12.1-ce-rc2+incompatible/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) TestRestartDisconnectedContainer(c *check.C) {
    79  	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
    80  
    81  	// Run a container on the default bridge network
    82  	out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
    83  	cleanedContainerID := strings.TrimSpace(out)
    84  	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
    85  
    86  	// Disconnect the container from the network
    87  	out, err := dockerCmd(c, "network", "disconnect", "bridge", "c0")
    88  	c.Assert(err, check.NotNil, check.Commentf(out))
    89  
    90  	// Restart the container
    91  	dockerCmd(c, "restart", "c0")
    92  	c.Assert(err, check.NotNil, check.Commentf(out))
    93  }
    94  
    95  func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
    96  	out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
    97  
    98  	id := strings.TrimSpace(string(out))
    99  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
   100  	c.Assert(name, checker.Equals, "no")
   101  }
   102  
   103  func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
   104  	out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
   105  
   106  	id := strings.TrimSpace(string(out))
   107  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
   108  	c.Assert(name, checker.Equals, "always")
   109  
   110  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   111  
   112  	// MaximumRetryCount=0 if the restart policy is always
   113  	c.Assert(MaximumRetryCount, checker.Equals, "0")
   114  }
   115  
   116  func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
   117  	out, _, err := dockerCmdWithError("create", "--restart=on-failure:-1", "busybox")
   118  	c.Assert(err, check.NotNil, check.Commentf(out))
   119  	c.Assert(out, checker.Contains, "maximum retry count cannot be negative")
   120  
   121  	out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
   122  
   123  	id := strings.TrimSpace(string(out))
   124  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
   125  	maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   126  
   127  	c.Assert(name, checker.Equals, "on-failure")
   128  	c.Assert(maxRetry, checker.Equals, "1")
   129  
   130  	out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
   131  
   132  	id = strings.TrimSpace(string(out))
   133  	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
   134  	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   135  
   136  	c.Assert(name, checker.Equals, "on-failure")
   137  	c.Assert(maxRetry, checker.Equals, "0")
   138  
   139  	out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
   140  
   141  	id = strings.TrimSpace(string(out))
   142  	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
   143  	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   144  
   145  	c.Assert(name, checker.Equals, "on-failure")
   146  	c.Assert(maxRetry, checker.Equals, "0")
   147  }
   148  
   149  // a good container with --restart=on-failure:3
   150  // MaximumRetryCount!=0; RestartCount=0
   151  func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
   152  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
   153  
   154  	id := strings.TrimSpace(string(out))
   155  	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
   156  	c.Assert(err, checker.IsNil)
   157  
   158  	count := inspectField(c, id, "RestartCount")
   159  	c.Assert(count, checker.Equals, "0")
   160  
   161  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   162  	c.Assert(MaximumRetryCount, checker.Equals, "3")
   163  
   164  }
   165  
   166  func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
   167  	testRequires(c, SameHostDaemon)
   168  
   169  	out := runSleepingContainer(c, "-d", "--restart=always")
   170  	id := strings.TrimSpace(out)
   171  	c.Assert(waitRun(id), check.IsNil)
   172  
   173  	pidStr := inspectField(c, id, "State.Pid")
   174  
   175  	pid, err := strconv.Atoi(pidStr)
   176  	c.Assert(err, check.IsNil)
   177  
   178  	p, err := os.FindProcess(pid)
   179  	c.Assert(err, check.IsNil)
   180  	c.Assert(p, check.NotNil)
   181  
   182  	err = p.Kill()
   183  	c.Assert(err, check.IsNil)
   184  
   185  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   186  	c.Assert(err, check.IsNil)
   187  
   188  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   189  	c.Assert(err, check.IsNil)
   190  }
   191  
   192  func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
   193  	// TODO Windows. This may be portable following HNS integration post TP5.
   194  	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
   195  	dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
   196  
   197  	dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
   198  	c.Assert(waitRun("first"), check.IsNil)
   199  
   200  	dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
   201  		"--link=first:foo", "busybox", "top")
   202  	c.Assert(waitRun("second"), check.IsNil)
   203  
   204  	// ping to first and its alias foo must succeed
   205  	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   206  	c.Assert(err, check.IsNil)
   207  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   208  	c.Assert(err, check.IsNil)
   209  
   210  	// Now kill the second container and let the restart policy kick in
   211  	pidStr := inspectField(c, "second", "State.Pid")
   212  
   213  	pid, err := strconv.Atoi(pidStr)
   214  	c.Assert(err, check.IsNil)
   215  
   216  	p, err := os.FindProcess(pid)
   217  	c.Assert(err, check.IsNil)
   218  	c.Assert(p, check.NotNil)
   219  
   220  	err = p.Kill()
   221  	c.Assert(err, check.IsNil)
   222  
   223  	err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
   224  	c.Assert(err, check.IsNil)
   225  
   226  	err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
   227  
   228  	// ping to first and its alias foo must still succeed
   229  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   230  	c.Assert(err, check.IsNil)
   231  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   232  	c.Assert(err, check.IsNil)
   233  }
   234  
   235  func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
   236  	testRequires(c, SameHostDaemon)
   237  
   238  	out := runSleepingContainer(c, "-d", "--restart=always")
   239  	id := strings.TrimSpace(out)
   240  	c.Assert(waitRun(id), check.IsNil)
   241  
   242  	dockerCmd(c, "restart", id)
   243  
   244  	c.Assert(waitRun(id), check.IsNil)
   245  
   246  	pidStr := inspectField(c, id, "State.Pid")
   247  
   248  	pid, err := strconv.Atoi(pidStr)
   249  	c.Assert(err, check.IsNil)
   250  
   251  	p, err := os.FindProcess(pid)
   252  	c.Assert(err, check.IsNil)
   253  	c.Assert(p, check.NotNil)
   254  
   255  	err = p.Kill()
   256  	c.Assert(err, check.IsNil)
   257  
   258  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   259  	c.Assert(err, check.IsNil)
   260  
   261  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   262  	c.Assert(err, check.IsNil)
   263  }
   264  
   265  func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
   266  	out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
   267  	out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
   268  
   269  	id1 := strings.TrimSpace(string(out1))
   270  	id2 := strings.TrimSpace(string(out2))
   271  	waitTimeout := 15 * time.Second
   272  	if testEnv.DaemonPlatform() == "windows" {
   273  		waitTimeout = 150 * time.Second
   274  	}
   275  	err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   276  	c.Assert(err, checker.IsNil)
   277  
   278  	dockerCmd(c, "restart", id1)
   279  	dockerCmd(c, "restart", id2)
   280  
   281  	// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
   282  	dockerCmd(c, "stop", id1)
   283  	dockerCmd(c, "stop", id2)
   284  	dockerCmd(c, "start", id1)
   285  	dockerCmd(c, "start", id2)
   286  
   287  	// Kill the containers, making sure the are stopped at the end of the test
   288  	dockerCmd(c, "kill", id1)
   289  	dockerCmd(c, "kill", id2)
   290  	err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   291  	c.Assert(err, checker.IsNil)
   292  	err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   293  	c.Assert(err, checker.IsNil)
   294  }
   295  
   296  func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
   297  	out := runSleepingContainer(c, "--rm")
   298  
   299  	id := strings.TrimSpace(string(out))
   300  	dockerCmd(c, "restart", id)
   301  	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
   302  	c.Assert(err, checker.IsNil)
   303  
   304  	out, _ = dockerCmd(c, "ps")
   305  	c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
   306  
   307  	// Kill the container to make sure it will be removed
   308  	dockerCmd(c, "kill", id)
   309  }