github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_cli_start_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/docker/docker/integration-cli/checker" 9 "github.com/docker/docker/integration-cli/cli" 10 icmd "github.com/docker/docker/pkg/testutil/cmd" 11 "github.com/go-check/check" 12 ) 13 14 // Regression test for https://github.com/docker/docker/issues/7843 15 func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) { 16 // Windows does not support link 17 testRequires(c, DaemonIsLinux) 18 dockerCmd(c, "run", "--name", "test", "busybox") 19 20 // Expect this to fail because the above container is stopped, this is what we want 21 out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox") 22 // err shouldn't be nil because container test2 try to link to stopped container 23 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 24 25 ch := make(chan error) 26 go func() { 27 // Attempt to start attached to the container that won't start 28 // This should return an error immediately since the container can't be started 29 if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil { 30 ch <- fmt.Errorf("Expected error but got none:\n%s", out) 31 } 32 close(ch) 33 }() 34 35 select { 36 case err := <-ch: 37 c.Assert(err, check.IsNil) 38 case <-time.After(5 * time.Second): 39 c.Fatalf("Attach did not exit properly") 40 } 41 } 42 43 // gh#8555: Exit code should be passed through when using start -a 44 func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) { 45 testRequires(c, DaemonIsLinux) 46 out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout() 47 out = strings.TrimSpace(out) 48 49 // make sure the container has exited before trying the "start -a" 50 cli.DockerCmd(c, "wait", out) 51 52 cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{ 53 ExitCode: 1, 54 }) 55 } 56 57 func (s *DockerSuite) TestStartAttachSilent(c *check.C) { 58 name := "teststartattachcorrectexitcode" 59 dockerCmd(c, "run", "--name", name, "busybox", "echo", "test") 60 61 // make sure the container has exited before trying the "start -a" 62 dockerCmd(c, "wait", name) 63 64 startOut, _ := dockerCmd(c, "start", "-a", name) 65 // start -a produced unexpected output 66 c.Assert(startOut, checker.Equals, "test\n") 67 } 68 69 func (s *DockerSuite) TestStartRecordError(c *check.C) { 70 // TODO Windows CI: Requires further porting work. Should be possible. 71 testRequires(c, DaemonIsLinux) 72 // when container runs successfully, we should not have state.Error 73 dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top") 74 stateErr := inspectField(c, "test", "State.Error") 75 // Expected to not have state error 76 c.Assert(stateErr, checker.Equals, "") 77 78 // Expect this to fail and records error because of ports conflict 79 out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top") 80 // err shouldn't be nil because docker run will fail 81 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 82 83 stateErr = inspectField(c, "test2", "State.Error") 84 c.Assert(stateErr, checker.Contains, "port is already allocated") 85 86 // Expect the conflict to be resolved when we stop the initial container 87 dockerCmd(c, "stop", "test") 88 dockerCmd(c, "start", "test2") 89 stateErr = inspectField(c, "test2", "State.Error") 90 // Expected to not have state error but got one 91 c.Assert(stateErr, checker.Equals, "") 92 } 93 94 func (s *DockerSuite) TestStartPausedContainer(c *check.C) { 95 // Windows does not support pausing containers 96 testRequires(c, IsPausable) 97 98 runSleepingContainer(c, "-d", "--name", "testing") 99 100 dockerCmd(c, "pause", "testing") 101 102 out, _, err := dockerCmdWithError("start", "testing") 103 // an error should have been shown that you cannot start paused container 104 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 105 // an error should have been shown that you cannot start paused container 106 c.Assert(out, checker.Contains, "Cannot start a paused container, try unpause instead.") 107 } 108 109 func (s *DockerSuite) TestStartMultipleContainers(c *check.C) { 110 // Windows does not support --link 111 testRequires(c, DaemonIsLinux) 112 // run a container named 'parent' and create two container link to `parent` 113 dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top") 114 115 for _, container := range []string{"child_first", "child_second"} { 116 dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top") 117 } 118 119 // stop 'parent' container 120 dockerCmd(c, "stop", "parent") 121 122 out := inspectField(c, "parent", "State.Running") 123 // Container should be stopped 124 c.Assert(out, checker.Equals, "false") 125 126 // start all the three containers, container `child_first` start first which should be failed 127 // container 'parent' start second and then start container 'child_second' 128 expOut := "Cannot link to a non running container" 129 expErr := "failed to start containers: [child_first]" 130 out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second") 131 // err shouldn't be nil because start will fail 132 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 133 // output does not correspond to what was expected 134 if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) { 135 c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err) 136 } 137 138 for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} { 139 out := inspectField(c, container, "State.Running") 140 // Container running state wrong 141 c.Assert(out, checker.Equals, expected) 142 } 143 } 144 145 func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) { 146 // run multiple containers to test 147 for _, container := range []string{"test1", "test2", "test3"} { 148 runSleepingContainer(c, "--name", container) 149 } 150 151 // stop all the containers 152 for _, container := range []string{"test1", "test2", "test3"} { 153 dockerCmd(c, "stop", container) 154 } 155 156 // test start and attach multiple containers at once, expected error 157 for _, option := range []string{"-a", "-i", "-ai"} { 158 out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3") 159 // err shouldn't be nil because start will fail 160 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 161 // output does not correspond to what was expected 162 c.Assert(out, checker.Contains, "you cannot start and attach multiple containers at once") 163 } 164 165 // confirm the state of all the containers be stopped 166 for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} { 167 out := inspectField(c, container, "State.Running") 168 // Container running state wrong 169 c.Assert(out, checker.Equals, expected) 170 } 171 } 172 173 // Test case for #23716 174 func (s *DockerSuite) TestStartAttachWithRename(c *check.C) { 175 testRequires(c, DaemonIsLinux) 176 cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox") 177 go func() { 178 cli.WaitRun(c, "before") 179 cli.DockerCmd(c, "rename", "before", "after") 180 cli.DockerCmd(c, "stop", "--time=2", "after") 181 }() 182 // FIXME(vdemeester) the intent is not clear and potentially racey 183 result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{ 184 ExitCode: 137, 185 }) 186 c.Assert(result.Stderr(), checker.Not(checker.Contains), "No such container") 187 } 188 189 func (s *DockerSuite) TestStartReturnCorrectExitCode(c *check.C) { 190 dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11") 191 dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12") 192 193 _, exitCode, err := dockerCmdWithError("start", "-a", "withRestart") 194 c.Assert(err, checker.NotNil) 195 c.Assert(exitCode, checker.Equals, 11) 196 _, exitCode, err = dockerCmdWithError("start", "-a", "withRm") 197 c.Assert(err, checker.NotNil) 198 c.Assert(exitCode, checker.Equals, 12) 199 }