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