github.com/eatbyte/docker@v1.6.0/integration-cli/docker_cli_restart_test.go (about)

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