github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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/pkg/integration/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, err := getIDByName("test")
    16  	c.Assert(err, check.IsNil)
    17  
    18  	out, _ := dockerCmd(c, "logs", cleanedContainerID)
    19  	c.Assert(out, checker.Equals, "foobar\n")
    20  
    21  	dockerCmd(c, "restart", cleanedContainerID)
    22  
    23  	// Wait until the container has stopped
    24  	err = waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
    25  	c.Assert(err, checker.IsNil)
    26  
    27  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    28  	c.Assert(out, checker.Equals, "foobar\nfoobar\n")
    29  }
    30  
    31  func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
    32  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
    33  
    34  	cleanedContainerID := strings.TrimSpace(out)
    35  
    36  	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
    37  
    38  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    39  	c.Assert(out, checker.Equals, "foobar\n")
    40  
    41  	dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
    42  
    43  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    44  
    45  	c.Assert(waitRun(cleanedContainerID), checker.IsNil)
    46  
    47  	c.Assert(out, checker.Equals, "foobar\nfoobar\n")
    48  }
    49  
    50  // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
    51  func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
    52  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
    53  	out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
    54  
    55  	cleanedContainerID := strings.TrimSpace(out)
    56  	out, err := inspectFilter(cleanedContainerID, "len .Mounts")
    57  	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
    58  	out = strings.Trim(out, " \n\r")
    59  	c.Assert(out, checker.Equals, "1")
    60  
    61  	source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
    62  	c.Assert(err, checker.IsNil)
    63  
    64  	dockerCmd(c, "restart", cleanedContainerID)
    65  
    66  	out, err = inspectFilter(cleanedContainerID, "len .Mounts")
    67  	c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out))
    68  	out = strings.Trim(out, " \n\r")
    69  	c.Assert(out, checker.Equals, "1")
    70  
    71  	sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
    72  	c.Assert(err, checker.IsNil)
    73  	c.Assert(source, checker.Equals, sourceAfterRestart)
    74  }
    75  
    76  func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
    77  	out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false")
    78  
    79  	id := strings.TrimSpace(string(out))
    80  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
    81  	c.Assert(name, checker.Equals, "no")
    82  }
    83  
    84  func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
    85  	out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
    86  
    87  	id := strings.TrimSpace(string(out))
    88  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
    89  	c.Assert(name, checker.Equals, "always")
    90  
    91  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
    92  
    93  	// MaximumRetryCount=0 if the restart policy is always
    94  	c.Assert(MaximumRetryCount, checker.Equals, "0")
    95  }
    96  
    97  func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
    98  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
    99  
   100  	id := strings.TrimSpace(string(out))
   101  	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
   102  	c.Assert(name, checker.Equals, "on-failure")
   103  
   104  }
   105  
   106  // a good container with --restart=on-failure:3
   107  // MaximumRetryCount!=0; RestartCount=0
   108  func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) {
   109  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
   110  
   111  	id := strings.TrimSpace(string(out))
   112  	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
   113  	c.Assert(err, checker.IsNil)
   114  
   115  	count := inspectField(c, id, "RestartCount")
   116  	c.Assert(count, checker.Equals, "0")
   117  
   118  	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
   119  	c.Assert(MaximumRetryCount, checker.Equals, "3")
   120  
   121  }
   122  
   123  func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) {
   124  	testRequires(c, SameHostDaemon)
   125  
   126  	out, _ := runSleepingContainer(c, "-d", "--restart=always")
   127  	id := strings.TrimSpace(out)
   128  	c.Assert(waitRun(id), check.IsNil)
   129  
   130  	pidStr := inspectField(c, id, "State.Pid")
   131  
   132  	pid, err := strconv.Atoi(pidStr)
   133  	c.Assert(err, check.IsNil)
   134  
   135  	p, err := os.FindProcess(pid)
   136  	c.Assert(err, check.IsNil)
   137  	c.Assert(p, check.NotNil)
   138  
   139  	err = p.Kill()
   140  	c.Assert(err, check.IsNil)
   141  
   142  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   143  	c.Assert(err, check.IsNil)
   144  
   145  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   146  	c.Assert(err, check.IsNil)
   147  }
   148  
   149  func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) {
   150  	// TODO Windows. This may be portable following HNS integration post TP5.
   151  	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm)
   152  	dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
   153  
   154  	dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
   155  	c.Assert(waitRun("first"), check.IsNil)
   156  
   157  	dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
   158  		"--link=first:foo", "busybox", "top")
   159  	c.Assert(waitRun("second"), check.IsNil)
   160  
   161  	// ping to first and its alias foo must succeed
   162  	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   163  	c.Assert(err, check.IsNil)
   164  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   165  	c.Assert(err, check.IsNil)
   166  
   167  	// Now kill the second container and let the restart policy kick in
   168  	pidStr := inspectField(c, "second", "State.Pid")
   169  
   170  	pid, err := strconv.Atoi(pidStr)
   171  	c.Assert(err, check.IsNil)
   172  
   173  	p, err := os.FindProcess(pid)
   174  	c.Assert(err, check.IsNil)
   175  	c.Assert(p, check.NotNil)
   176  
   177  	err = p.Kill()
   178  	c.Assert(err, check.IsNil)
   179  
   180  	err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second)
   181  	c.Assert(err, check.IsNil)
   182  
   183  	err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second)
   184  
   185  	// ping to first and its alias foo must still succeed
   186  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
   187  	c.Assert(err, check.IsNil)
   188  	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
   189  	c.Assert(err, check.IsNil)
   190  }
   191  
   192  func (s *DockerSuite) TestRestartPolicyAfterRestart(c *check.C) {
   193  	testRequires(c, SameHostDaemon)
   194  
   195  	out, _ := runSleepingContainer(c, "-d", "--restart=always")
   196  	id := strings.TrimSpace(out)
   197  	c.Assert(waitRun(id), check.IsNil)
   198  
   199  	dockerCmd(c, "restart", id)
   200  
   201  	c.Assert(waitRun(id), check.IsNil)
   202  
   203  	pidStr := inspectField(c, id, "State.Pid")
   204  
   205  	pid, err := strconv.Atoi(pidStr)
   206  	c.Assert(err, check.IsNil)
   207  
   208  	p, err := os.FindProcess(pid)
   209  	c.Assert(err, check.IsNil)
   210  	c.Assert(p, check.NotNil)
   211  
   212  	err = p.Kill()
   213  	c.Assert(err, check.IsNil)
   214  
   215  	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
   216  	c.Assert(err, check.IsNil)
   217  
   218  	err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second)
   219  	c.Assert(err, check.IsNil)
   220  }
   221  
   222  func (s *DockerSuite) TestRestartContainerwithRestartPolicy(c *check.C) {
   223  	out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
   224  	out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
   225  
   226  	id1 := strings.TrimSpace(string(out1))
   227  	id2 := strings.TrimSpace(string(out2))
   228  	waitTimeout := 15 * time.Second
   229  	if daemonPlatform == "windows" {
   230  		waitTimeout = 150 * time.Second
   231  	}
   232  	err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
   233  	c.Assert(err, checker.IsNil)
   234  
   235  	dockerCmd(c, "restart", id1)
   236  	dockerCmd(c, "restart", id2)
   237  
   238  	dockerCmd(c, "stop", id1)
   239  	dockerCmd(c, "stop", id2)
   240  	dockerCmd(c, "start", id1)
   241  	dockerCmd(c, "start", id2)
   242  }