github.com/goern/docker@v1.9.0-rc1/integration-cli/docker_cli_restart_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/go-check/check"
     8  )
     9  
    10  func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
    11  	testRequires(c, DaemonIsLinux)
    12  	out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
    13  
    14  	cleanedContainerID := strings.TrimSpace(out)
    15  	dockerCmd(c, "wait", cleanedContainerID)
    16  
    17  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    18  	if out != "foobar\n" {
    19  		c.Errorf("container should've printed 'foobar'")
    20  	}
    21  
    22  	dockerCmd(c, "restart", cleanedContainerID)
    23  
    24  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    25  	if out != "foobar\nfoobar\n" {
    26  		c.Errorf("container should've printed 'foobar' twice, got %v", out)
    27  	}
    28  }
    29  
    30  func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
    31  	testRequires(c, DaemonIsLinux)
    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), check.IsNil)
    37  
    38  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    39  	if out != "foobar\n" {
    40  		c.Errorf("container should've printed 'foobar'")
    41  	}
    42  
    43  	dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
    44  
    45  	out, _ = dockerCmd(c, "logs", cleanedContainerID)
    46  
    47  	c.Assert(waitRun(cleanedContainerID), check.IsNil)
    48  
    49  	if out != "foobar\nfoobar\n" {
    50  		c.Errorf("container should've printed 'foobar' twice")
    51  	}
    52  }
    53  
    54  // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
    55  func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
    56  	testRequires(c, DaemonIsLinux)
    57  	out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top")
    58  
    59  	cleanedContainerID := strings.TrimSpace(out)
    60  	out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
    61  
    62  	if out = strings.Trim(out, " \n\r"); out != "1" {
    63  		c.Errorf("expect 1 volume received %s", out)
    64  	}
    65  
    66  	source, err := inspectMountSourceField(cleanedContainerID, "/test")
    67  	c.Assert(err, check.IsNil)
    68  
    69  	dockerCmd(c, "restart", cleanedContainerID)
    70  
    71  	out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Mounts }}", cleanedContainerID)
    72  	if out = strings.Trim(out, " \n\r"); out != "1" {
    73  		c.Errorf("expect 1 volume after restart received %s", out)
    74  	}
    75  
    76  	sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, "/test")
    77  	c.Assert(err, check.IsNil)
    78  
    79  	if source != sourceAfterRestart {
    80  		c.Errorf("expected volume path: %s Actual path: %s", source, sourceAfterRestart)
    81  	}
    82  }
    83  
    84  func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
    85  	testRequires(c, DaemonIsLinux)
    86  	out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false")
    87  
    88  	id := strings.TrimSpace(string(out))
    89  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
    90  	c.Assert(err, check.IsNil)
    91  	if name != "no" {
    92  		c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
    93  	}
    94  }
    95  
    96  func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
    97  	testRequires(c, DaemonIsLinux)
    98  	out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
    99  
   100  	id := strings.TrimSpace(string(out))
   101  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
   102  	c.Assert(err, check.IsNil)
   103  	if name != "always" {
   104  		c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
   105  	}
   106  
   107  	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
   108  	c.Assert(err, check.IsNil)
   109  
   110  	// MaximumRetryCount=0 if the restart policy is always
   111  	if MaximumRetryCount != "0" {
   112  		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
   113  	}
   114  }
   115  
   116  func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
   117  	testRequires(c, DaemonIsLinux)
   118  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
   119  
   120  	id := strings.TrimSpace(string(out))
   121  	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
   122  	c.Assert(err, check.IsNil)
   123  	if name != "on-failure" {
   124  		c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
   125  	}
   126  
   127  }
   128  
   129  // a good container with --restart=on-failure:3
   130  // MaximumRetryCount!=0; RestartCount=0
   131  func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
   132  	testRequires(c, DaemonIsLinux)
   133  	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
   134  
   135  	id := strings.TrimSpace(string(out))
   136  	if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5*time.Second); err != nil {
   137  		c.Fatal(err)
   138  	}
   139  	count, err := inspectField(id, "RestartCount")
   140  	c.Assert(err, check.IsNil)
   141  	if count != "0" {
   142  		c.Fatalf("Container was restarted %s times, expected %d", count, 0)
   143  	}
   144  	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
   145  	c.Assert(err, check.IsNil)
   146  	if MaximumRetryCount != "3" {
   147  		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
   148  	}
   149  
   150  }