github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/integration-cli/docker_cli_restart_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "strconv" 6 "strings" 7 "time" 8 9 "github.com/docker/docker/pkg/integration/checker" 10 "github.com/go-check/check" 11 ) 12 13 func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { 14 dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar") 15 cleanedContainerID, err := getIDByName("test") 16 c.Assert(err, check.IsNil) 17 18 out, _ := dockerCmd(c, "logs", cleanedContainerID) 19 c.Assert(out, checker.Equals, "foobar\n") 20 21 dockerCmd(c, "restart", cleanedContainerID) 22 23 out, _ = dockerCmd(c, "logs", cleanedContainerID) 24 c.Assert(out, checker.Equals, "foobar\nfoobar\n") 25 } 26 27 func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { 28 out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") 29 30 cleanedContainerID := strings.TrimSpace(out) 31 32 c.Assert(waitRun(cleanedContainerID), checker.IsNil) 33 34 out, _ = dockerCmd(c, "logs", cleanedContainerID) 35 c.Assert(out, checker.Equals, "foobar\n") 36 37 dockerCmd(c, "restart", "-t", "1", cleanedContainerID) 38 39 out, _ = dockerCmd(c, "logs", cleanedContainerID) 40 41 c.Assert(waitRun(cleanedContainerID), checker.IsNil) 42 43 c.Assert(out, checker.Equals, "foobar\nfoobar\n") 44 } 45 46 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. 47 func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { 48 prefix, slash := getPrefixAndSlashFromDaemonPlatform() 49 out, _ := runSleepingContainer(c, "-d", "-v", prefix+slash+"test") 50 51 cleanedContainerID := strings.TrimSpace(out) 52 out, err := inspectFilter(cleanedContainerID, "len .Mounts") 53 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 54 out = strings.Trim(out, " \n\r") 55 c.Assert(out, checker.Equals, "1") 56 57 source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 58 c.Assert(err, checker.IsNil) 59 60 dockerCmd(c, "restart", cleanedContainerID) 61 62 out, err = inspectFilter(cleanedContainerID, "len .Mounts") 63 c.Assert(err, check.IsNil, check.Commentf("failed to inspect %s: %s", cleanedContainerID, out)) 64 out = strings.Trim(out, " \n\r") 65 c.Assert(out, checker.Equals, "1") 66 67 sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test") 68 c.Assert(err, checker.IsNil) 69 c.Assert(source, checker.Equals, sourceAfterRestart) 70 } 71 72 func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { 73 out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false") 74 75 id := strings.TrimSpace(string(out)) 76 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 77 c.Assert(name, checker.Equals, "no") 78 } 79 80 func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { 81 out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false") 82 83 id := strings.TrimSpace(string(out)) 84 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 85 c.Assert(name, checker.Equals, "always") 86 87 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 88 89 // MaximumRetryCount=0 if the restart policy is always 90 c.Assert(MaximumRetryCount, checker.Equals, "0") 91 } 92 93 func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { 94 out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false") 95 96 id := strings.TrimSpace(string(out)) 97 name := inspectField(c, id, "HostConfig.RestartPolicy.Name") 98 c.Assert(name, checker.Equals, "on-failure") 99 100 } 101 102 // a good container with --restart=on-failure:3 103 // MaximumRetryCount!=0; RestartCount=0 104 func (s *DockerSuite) TestRestartContainerwithGoodContainer(c *check.C) { 105 out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true") 106 107 id := strings.TrimSpace(string(out)) 108 err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second) 109 c.Assert(err, checker.IsNil) 110 111 count := inspectField(c, id, "RestartCount") 112 c.Assert(count, checker.Equals, "0") 113 114 MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount") 115 c.Assert(MaximumRetryCount, checker.Equals, "3") 116 117 } 118 119 func (s *DockerSuite) TestRestartContainerSuccess(c *check.C) { 120 testRequires(c, SameHostDaemon) 121 122 out, _ := runSleepingContainer(c, "-d", "--restart=always") 123 id := strings.TrimSpace(out) 124 c.Assert(waitRun(id), check.IsNil) 125 126 pidStr := inspectField(c, id, "State.Pid") 127 128 pid, err := strconv.Atoi(pidStr) 129 c.Assert(err, check.IsNil) 130 131 p, err := os.FindProcess(pid) 132 c.Assert(err, check.IsNil) 133 c.Assert(p, check.NotNil) 134 135 err = p.Kill() 136 c.Assert(err, check.IsNil) 137 138 err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second) 139 c.Assert(err, check.IsNil) 140 141 err = waitInspect(id, "{{.State.Status}}", "running", 30*time.Second) 142 c.Assert(err, check.IsNil) 143 } 144 145 func (s *DockerSuite) TestRestartWithPolicyUserDefinedNetwork(c *check.C) { 146 // TODO Windows. This may be portable following HNS integration post TP5. 147 testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace, NotArm) 148 dockerCmd(c, "network", "create", "-d", "bridge", "udNet") 149 150 dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top") 151 c.Assert(waitRun("first"), check.IsNil) 152 153 dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second", 154 "--link=first:foo", "busybox", "top") 155 c.Assert(waitRun("second"), check.IsNil) 156 157 // ping to first and its alias foo must succeed 158 _, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 159 c.Assert(err, check.IsNil) 160 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 161 c.Assert(err, check.IsNil) 162 163 // Now kill the second container and let the restart policy kick in 164 pidStr := inspectField(c, "second", "State.Pid") 165 166 pid, err := strconv.Atoi(pidStr) 167 c.Assert(err, check.IsNil) 168 169 p, err := os.FindProcess(pid) 170 c.Assert(err, check.IsNil) 171 c.Assert(p, check.NotNil) 172 173 err = p.Kill() 174 c.Assert(err, check.IsNil) 175 176 err = waitInspect("second", "{{.RestartCount}}", "1", 5*time.Second) 177 c.Assert(err, check.IsNil) 178 179 err = waitInspect("second", "{{.State.Status}}", "running", 5*time.Second) 180 181 // ping to first and its alias foo must still succeed 182 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first") 183 c.Assert(err, check.IsNil) 184 _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo") 185 c.Assert(err, check.IsNil) 186 }