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