github.com/sevki/docker@v1.7.1/integration-cli/docker_cli_restart_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/go-check/check"
     9  )
    10  
    11  func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
    12  
    13  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
    14  	out, _, err := runCommandWithOutput(runCmd)
    15  	if err != nil {
    16  		c.Fatal(out, err)
    17  	}
    18  
    19  	cleanedContainerID := strings.TrimSpace(out)
    20  
    21  	runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
    22  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    23  		c.Fatal(out, err)
    24  	}
    25  
    26  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    27  	out, _, err = runCommandWithOutput(runCmd)
    28  	if err != nil {
    29  		c.Fatal(out, err)
    30  	}
    31  
    32  	if out != "foobar\n" {
    33  		c.Errorf("container should've printed 'foobar'")
    34  	}
    35  
    36  	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
    37  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    38  		c.Fatal(out, err)
    39  	}
    40  
    41  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    42  	out, _, err = runCommandWithOutput(runCmd)
    43  	if err != nil {
    44  		c.Fatal(out, err)
    45  	}
    46  
    47  	if out != "foobar\nfoobar\n" {
    48  		c.Errorf("container should've printed 'foobar' twice")
    49  	}
    50  
    51  }
    52  
    53  func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
    54  
    55  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
    56  	out, _, err := runCommandWithOutput(runCmd)
    57  	if err != nil {
    58  		c.Fatal(out, err)
    59  	}
    60  
    61  	cleanedContainerID := strings.TrimSpace(out)
    62  
    63  	time.Sleep(1 * time.Second)
    64  
    65  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    66  	out, _, err = runCommandWithOutput(runCmd)
    67  	if err != nil {
    68  		c.Fatal(out, err)
    69  	}
    70  
    71  	if out != "foobar\n" {
    72  		c.Errorf("container should've printed 'foobar'")
    73  	}
    74  
    75  	runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID)
    76  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    77  		c.Fatal(out, err)
    78  	}
    79  
    80  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    81  	out, _, err = runCommandWithOutput(runCmd)
    82  	if err != nil {
    83  		c.Fatal(out, err)
    84  	}
    85  
    86  	time.Sleep(1 * time.Second)
    87  
    88  	if out != "foobar\nfoobar\n" {
    89  		c.Errorf("container should've printed 'foobar' twice")
    90  	}
    91  
    92  }
    93  
    94  // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
    95  func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
    96  
    97  	runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
    98  	out, _, err := runCommandWithOutput(runCmd)
    99  	if err != nil {
   100  		c.Fatal(out, err)
   101  	}
   102  
   103  	cleanedContainerID := strings.TrimSpace(out)
   104  
   105  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
   106  	out, _, err = runCommandWithOutput(runCmd)
   107  	if err != nil {
   108  		c.Fatal(out, err)
   109  	}
   110  
   111  	if out = strings.Trim(out, " \n\r"); out != "1" {
   112  		c.Errorf("expect 1 volume received %s", out)
   113  	}
   114  
   115  	volumes, err := inspectField(cleanedContainerID, ".Volumes")
   116  	c.Assert(err, check.IsNil)
   117  
   118  	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
   119  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   120  		c.Fatal(out, err)
   121  	}
   122  
   123  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
   124  	out, _, err = runCommandWithOutput(runCmd)
   125  	if err != nil {
   126  		c.Fatal(out, err)
   127  	}
   128  
   129  	if out = strings.Trim(out, " \n\r"); out != "1" {
   130  		c.Errorf("expect 1 volume after restart received %s", out)
   131  	}
   132  
   133  	volumesAfterRestart, err := inspectField(cleanedContainerID, ".Volumes")
   134  	c.Assert(err, check.IsNil)
   135  
   136  	if volumes != volumesAfterRestart {
   137  		c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
   138  	}
   139  
   140  }
   141  
   142  func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
   143  
   144  	cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
   145  	out, _, err := runCommandWithOutput(cmd)
   146  	if err != nil {
   147  		c.Fatal(err, out)
   148  	}
   149  
   150  	id := strings.TrimSpace(string(out))
   151  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
   152  	c.Assert(err, check.IsNil)
   153  	if name != "no" {
   154  		c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
   155  	}
   156  
   157  }
   158  
   159  func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
   160  
   161  	cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
   162  	out, _, err := runCommandWithOutput(cmd)
   163  	if err != nil {
   164  		c.Fatal(err, out)
   165  	}
   166  
   167  	id := strings.TrimSpace(string(out))
   168  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
   169  	c.Assert(err, check.IsNil)
   170  	if name != "always" {
   171  		c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
   172  	}
   173  
   174  	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
   175  	c.Assert(err, check.IsNil)
   176  
   177  	// MaximumRetryCount=0 if the restart policy is always
   178  	if MaximumRetryCount != "0" {
   179  		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
   180  	}
   181  
   182  }
   183  
   184  func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
   185  
   186  	cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
   187  	out, _, err := runCommandWithOutput(cmd)
   188  	if err != nil {
   189  		c.Fatal(err, out)
   190  	}
   191  
   192  	id := strings.TrimSpace(string(out))
   193  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
   194  	c.Assert(err, check.IsNil)
   195  	if name != "on-failure" {
   196  		c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
   197  	}
   198  
   199  }
   200  
   201  // a good container with --restart=on-failure:3
   202  // MaximumRetryCount!=0; RestartCount=0
   203  func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
   204  	out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput()
   205  	if err != nil {
   206  		c.Fatal(string(out), err)
   207  	}
   208  	id := strings.TrimSpace(string(out))
   209  	if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
   210  		c.Fatal(err)
   211  	}
   212  	count, err := inspectField(id, "RestartCount")
   213  	c.Assert(err, check.IsNil)
   214  	if count != "0" {
   215  		c.Fatalf("Container was restarted %s times, expected %d", count, 0)
   216  	}
   217  	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
   218  	c.Assert(err, check.IsNil)
   219  	if MaximumRetryCount != "3" {
   220  		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
   221  	}
   222  
   223  }