2014-09-25 02:46:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2014-10-19 02:47:48 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2014-09-25 02:46:57 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-09-25 02:46:57 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIStartSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIStartSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIStartSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:46:57 +00:00
|
|
|
// Regression test for https://github.com/docker/docker/issues/7843
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartAttachReturnsOnError(c *testing.T) {
|
2016-02-03 02:23:29 +00:00
|
|
|
// Windows does not support link
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", "test", "busybox")
|
2014-09-25 02:46:57 +00:00
|
|
|
|
|
|
|
// Expect this to fail because the above container is stopped, this is what we want
|
2016-02-28 10:47:37 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
|
2015-10-11 07:19:28 +00:00
|
|
|
// err shouldn't be nil because container test2 try to link to stopped container
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2014-09-25 02:46:57 +00:00
|
|
|
|
2020-02-25 22:13:25 +00:00
|
|
|
ch := make(chan error, 1)
|
2014-09-25 02:46:57 +00:00
|
|
|
go func() {
|
|
|
|
// Attempt to start attached to the container that won't start
|
|
|
|
// This should return an error immediately since the container can't be started
|
2016-03-24 16:43:04 +00:00
|
|
|
if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
|
|
|
|
ch <- fmt.Errorf("Expected error but got none:\n%s", out)
|
2014-09-25 02:46:57 +00:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-ch:
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-11-20 22:12:12 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Attach did not exit properly")
|
2014-09-25 02:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-19 02:47:48 +00:00
|
|
|
|
|
|
|
// gh#8555: Exit code should be passed through when using start -a
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartAttachCorrectExitCode(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-03-27 15:12:48 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
|
2015-04-06 13:21:18 +00:00
|
|
|
out = strings.TrimSpace(out)
|
2014-10-19 02:47:48 +00:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.DockerCmd(c, "wait", out)
|
2014-10-19 02:47:48 +00:00
|
|
|
|
2017-03-27 15:12:48 +00:00
|
|
|
cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-10-19 02:47:48 +00:00
|
|
|
}
|
2014-09-30 08:30:58 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartAttachSilent(c *testing.T) {
|
2015-01-07 01:58:30 +00:00
|
|
|
name := "teststartattachcorrectexitcode"
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
|
2015-01-07 01:58:30 +00:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "wait", name)
|
2015-01-07 01:58:30 +00:00
|
|
|
|
2023-07-27 21:56:24 +00:00
|
|
|
startOut := cli.DockerCmd(c, "start", "-a", name).Combined()
|
2015-10-11 07:19:28 +00:00
|
|
|
// start -a produced unexpected output
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, startOut, "test\n")
|
2015-01-07 01:58:30 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartRecordError(c *testing.T) {
|
2016-02-03 02:23:29 +00:00
|
|
|
// TODO Windows CI: Requires further porting work. Should be possible.
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-09-30 08:30:58 +00:00
|
|
|
// when container runs successfully, we should not have state.Error
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
|
2016-01-28 14:19:25 +00:00
|
|
|
stateErr := inspectField(c, "test", "State.Error")
|
2015-10-11 07:19:28 +00:00
|
|
|
// Expected to not have state error
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, stateErr, "")
|
2014-09-30 08:30:58 +00:00
|
|
|
|
|
|
|
// Expect this to fail and records error because of ports conflict
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
|
2015-10-11 07:19:28 +00:00
|
|
|
// err shouldn't be nil because docker run will fail
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-07-21 18:01:24 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(stateErr, "port is already allocated"))
|
2014-09-30 08:30:58 +00:00
|
|
|
// Expect the conflict to be resolved when we stop the initial container
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "stop", "test")
|
|
|
|
cli.DockerCmd(c, "start", "test2")
|
2016-01-28 14:19:25 +00:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2015-10-11 07:19:28 +00:00
|
|
|
// Expected to not have state error but got one
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, stateErr, "")
|
2014-09-30 08:30:58 +00:00
|
|
|
}
|
2014-10-23 12:50:06 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartPausedContainer(c *testing.T) {
|
2016-02-03 02:23:29 +00:00
|
|
|
// Windows does not support pausing containers
|
2016-09-09 00:31:04 +00:00
|
|
|
testRequires(c, IsPausable)
|
2015-01-15 00:44:53 +00:00
|
|
|
|
2016-09-09 00:31:04 +00:00
|
|
|
runSleepingContainer(c, "-d", "--name", "testing")
|
2015-01-15 00:44:53 +00:00
|
|
|
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "pause", "testing")
|
2015-01-15 00:44:53 +00:00
|
|
|
|
2015-10-11 07:19:28 +00:00
|
|
|
out, _, err := dockerCmdWithError("start", "testing")
|
|
|
|
// an error should have been shown that you cannot start paused container
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 07:19:28 +00:00
|
|
|
// an error should have been shown that you cannot start paused container
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(strings.ToLower(out), "cannot start a paused container, try unpause instead"))
|
2015-01-15 00:44:53 +00:00
|
|
|
}
|
2015-03-08 05:44:25 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartMultipleContainers(c *testing.T) {
|
2016-02-03 02:23:29 +00:00
|
|
|
// Windows does not support --link
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-08 05:44:25 +00:00
|
|
|
// run a container named 'parent' and create two container link to `parent`
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
2015-07-21 18:01:24 +00:00
|
|
|
|
2015-03-08 05:44:25 +00:00
|
|
|
for _, container := range []string{"child_first", "child_second"} {
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
|
2015-03-08 05:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop 'parent' container
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "stop", "parent")
|
2015-07-21 18:01:24 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
out := inspectField(c, "parent", "State.Running")
|
2015-10-11 07:19:28 +00:00
|
|
|
// Container should be stopped
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, "false")
|
2015-03-08 05:44:25 +00:00
|
|
|
|
2015-04-27 20:33:30 +00:00
|
|
|
// start all the three containers, container `child_first` start first which should be failed
|
2015-03-08 05:44:25 +00:00
|
|
|
// container 'parent' start second and then start container 'child_second'
|
2023-07-27 21:56:24 +00:00
|
|
|
const expOut = "Cannot link to a non running container"
|
|
|
|
const expErr = "failed to start containers: [child_first]"
|
2016-01-28 14:19:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
|
2015-10-11 07:19:28 +00:00
|
|
|
// err shouldn't be nil because start will fail
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 07:19:28 +00:00
|
|
|
// output does not correspond to what was expected
|
2015-07-29 12:21:16 +00:00
|
|
|
if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
|
|
|
|
c.Fatalf("Expected out: %v with err: %v but got out: %v with err: %v", expOut, expErr, out, err)
|
|
|
|
}
|
2015-03-08 05:44:25 +00:00
|
|
|
|
|
|
|
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
|
2016-01-28 14:19:25 +00:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 07:19:28 +00:00
|
|
|
// Container running state wrong
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, expected)
|
2015-03-08 05:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 09:08:22 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartAttachMultipleContainers(c *testing.T) {
|
2015-03-16 09:08:22 +00:00
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2016-02-24 00:24:35 +00:00
|
|
|
runSleepingContainer(c, "--name", container)
|
2015-03-16 09:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop all the containers
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2023-07-27 21:56:24 +00:00
|
|
|
cli.DockerCmd(c, "stop", container)
|
2015-03-16 09:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test start and attach multiple containers at once, expected error
|
|
|
|
for _, option := range []string{"-a", "-i", "-ai"} {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
|
2015-10-11 07:19:28 +00:00
|
|
|
// err shouldn't be nil because start will fail
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 07:19:28 +00:00
|
|
|
// output does not correspond to what was expected
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, "you cannot start and attach multiple containers at once"))
|
2015-03-16 09:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// confirm the state of all the containers be stopped
|
|
|
|
for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
|
2016-01-28 14:19:25 +00:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 07:19:28 +00:00
|
|
|
// Container running state wrong
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, expected)
|
2015-03-16 09:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 23:43:30 +00:00
|
|
|
|
|
|
|
// Test case for #23716
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartAttachWithRename(c *testing.T) {
|
2016-06-18 23:43:30 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
|
2016-06-18 23:43:30 +00:00
|
|
|
go func() {
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.WaitRun(c, "before")
|
|
|
|
cli.DockerCmd(c, "rename", "before", "after")
|
|
|
|
cli.DockerCmd(c, "stop", "--time=2", "after")
|
2016-06-18 23:43:30 +00:00
|
|
|
}()
|
2016-12-13 20:21:51 +00:00
|
|
|
// FIXME(vdemeester) the intent is not clear and potentially racey
|
2017-04-11 19:18:30 +00:00
|
|
|
result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
|
2016-12-13 20:21:51 +00:00
|
|
|
ExitCode: 137,
|
|
|
|
})
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(result.Stderr(), "No such container"))
|
2016-06-18 23:43:30 +00:00
|
|
|
}
|
2016-08-19 07:14:16 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIStartSuite) TestStartReturnCorrectExitCode(c *testing.T) {
|
2022-05-07 20:29:31 +00:00
|
|
|
cli.DockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
|
|
|
|
cli.DockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
|
|
|
|
cli.Docker(cli.Args("start", "-a", "withRestart")).Assert(c, icmd.Expected{ExitCode: 11})
|
|
|
|
cli.Docker(cli.Args("start", "-a", "withRm")).Assert(c, icmd.Expected{ExitCode: 12})
|
2016-08-19 07:14:16 +00:00
|
|
|
}
|