github.com/rseymour/docker@v1.5.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  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
    12  	out, _, err := runCommandWithOutput(runCmd)
    13  	if err != nil {
    14  		t.Fatal(out, err)
    15  	}
    16  
    17  	cleanedContainerID := stripTrailingCharacters(out)
    18  
    19  	runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
    20  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    21  		t.Fatal(out, err)
    22  	}
    23  
    24  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    25  	out, _, err = runCommandWithOutput(runCmd)
    26  	if err != nil {
    27  		t.Fatal(out, err)
    28  	}
    29  
    30  	if out != "foobar\n" {
    31  		t.Errorf("container should've printed 'foobar'")
    32  	}
    33  
    34  	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
    35  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    36  		t.Fatal(out, err)
    37  	}
    38  
    39  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    40  	out, _, err = runCommandWithOutput(runCmd)
    41  	if err != nil {
    42  		t.Fatal(out, err)
    43  	}
    44  
    45  	if out != "foobar\nfoobar\n" {
    46  		t.Errorf("container should've printed 'foobar' twice")
    47  	}
    48  
    49  	deleteAllContainers()
    50  
    51  	logDone("restart - echo foobar for stopped container")
    52  }
    53  
    54  func TestRestartRunningContainer(t *testing.T) {
    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  		t.Fatal(out, err)
    59  	}
    60  
    61  	cleanedContainerID := stripTrailingCharacters(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  		t.Fatal(out, err)
    69  	}
    70  
    71  	if out != "foobar\n" {
    72  		t.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  		t.Fatal(out, err)
    78  	}
    79  
    80  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    81  	out, _, err = runCommandWithOutput(runCmd)
    82  	if err != nil {
    83  		t.Fatal(out, err)
    84  	}
    85  
    86  	time.Sleep(1 * time.Second)
    87  
    88  	if out != "foobar\nfoobar\n" {
    89  		t.Errorf("container should've printed 'foobar' twice")
    90  	}
    91  
    92  	deleteAllContainers()
    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  	runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
   100  	out, _, err := runCommandWithOutput(runCmd)
   101  	if err != nil {
   102  		t.Fatal(out, err)
   103  	}
   104  
   105  	cleanedContainerID := stripTrailingCharacters(out)
   106  
   107  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
   108  	out, _, err = runCommandWithOutput(runCmd)
   109  	if err != nil {
   110  		t.Fatal(out, err)
   111  	}
   112  
   113  	if out = strings.Trim(out, " \n\r"); out != "1" {
   114  		t.Errorf("expect 1 volume received %s", out)
   115  	}
   116  
   117  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
   118  	volumes, _, err := runCommandWithOutput(runCmd)
   119  	if err != nil {
   120  		t.Fatal(volumes, err)
   121  	}
   122  
   123  	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
   124  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   125  		t.Fatal(out, err)
   126  	}
   127  
   128  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
   129  	out, _, err = runCommandWithOutput(runCmd)
   130  	if err != nil {
   131  		t.Fatal(out, err)
   132  	}
   133  
   134  	if out = strings.Trim(out, " \n\r"); out != "1" {
   135  		t.Errorf("expect 1 volume after restart received %s", out)
   136  	}
   137  
   138  	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
   139  	volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
   140  	if err != nil {
   141  		t.Fatal(volumesAfterRestart, err)
   142  	}
   143  
   144  	if volumes != volumesAfterRestart {
   145  		volumes = strings.Trim(volumes, " \n\r")
   146  		volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
   147  		t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
   148  	}
   149  
   150  	deleteAllContainers()
   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  }