2014-09-24 22:46:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-18 22:47:48 -04:00
|
|
|
"fmt"
|
2019-10-21 12:09:49 -04:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2014-10-18 22:47:48 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2014-09-24 22:46:57 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2019-04-17 06:39:07 -04:00
|
|
|
"github.com/Microsoft/hcsshim/osversion"
|
2017-03-27 11:12:48 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2019-10-21 12:09:49 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/kernel"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-09-24 22:46:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for https://github.com/docker/docker/issues/7843
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachReturnsOnError(c *testing.T) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support link
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-02-28 05:47:37 -05:00
|
|
|
dockerCmd(c, "run", "--name", "test", "busybox")
|
2014-09-24 22:46:57 -04:00
|
|
|
|
|
|
|
// Expect this to fail because the above container is stopped, this is what we want
|
2016-02-28 05:47:37 -05:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
|
2015-10-11 03:19:28 -04:00
|
|
|
// err shouldn't be nil because container test2 try to link to stopped container
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2014-09-24 22:46:57 -04:00
|
|
|
|
2020-02-25 17:13:25 -05:00
|
|
|
ch := make(chan error, 1)
|
2014-09-24 22:46:57 -04: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 12:43:04 -04:00
|
|
|
if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
|
|
|
|
ch <- fmt.Errorf("Expected error but got none:\n%s", out)
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 13:29:48 -04:00
|
|
|
case err := <-ch:
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-11-20 17:12:12 -05:00
|
|
|
case <-time.After(5 * time.Second):
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Attach did not exit properly")
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-18 22:47:48 -04:00
|
|
|
|
|
|
|
// gh#8555: Exit code should be passed through when using start -a
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *testing.T) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-03-27 11:12:48 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
|
2015-04-06 09:21:18 -04:00
|
|
|
out = strings.TrimSpace(out)
|
2014-10-18 22:47:48 -04:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.DockerCmd(c, "wait", out)
|
2014-10-18 22:47:48 -04:00
|
|
|
|
2017-03-27 11:12:48 -04:00
|
|
|
cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
2014-09-30 04:30:58 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachSilent(c *testing.T) {
|
2015-01-06 20:58:30 -05:00
|
|
|
name := "teststartattachcorrectexitcode"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
|
2015-01-06 20:58:30 -05:00
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "wait", name)
|
2015-01-06 20:58:30 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
startOut, _ := dockerCmd(c, "start", "-a", name)
|
2015-10-11 03:19:28 -04:00
|
|
|
// start -a produced unexpected output
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, startOut, "test\n")
|
2015-01-06 20:58:30 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartRecordError(c *testing.T) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// TODO Windows CI: Requires further porting work. Should be possible.
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-09-30 04:30:58 -04:00
|
|
|
// when container runs successfully, we should not have state.Error
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr := inspectField(c, "test", "State.Error")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Expected to not have state error
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, stateErr, "")
|
2014-09-30 04:30:58 -04:00
|
|
|
|
|
|
|
// Expect this to fail and records error because of ports conflict
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
|
2015-10-11 03:19:28 -04:00
|
|
|
// err shouldn't be nil because docker run will fail
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-07-21 14:01:24 -04:00
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(stateErr, "port is already allocated"))
|
2014-09-30 04:30:58 -04:00
|
|
|
// Expect the conflict to be resolved when we stop the initial container
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "stop", "test")
|
|
|
|
dockerCmd(c, "start", "test2")
|
2016-01-28 09:19:25 -05:00
|
|
|
stateErr = inspectField(c, "test2", "State.Error")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Expected to not have state error but got one
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, stateErr, "")
|
2014-09-30 04:30:58 -04:00
|
|
|
}
|
2014-10-23 08:50:06 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartPausedContainer(c *testing.T) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support pausing containers
|
2016-09-08 20:31:04 -04:00
|
|
|
testRequires(c, IsPausable)
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2016-09-08 20:31:04 -04:00
|
|
|
runSleepingContainer(c, "-d", "--name", "testing")
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "pause", "testing")
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2015-10-11 03:19:28 -04:00
|
|
|
out, _, err := dockerCmdWithError("start", "testing")
|
|
|
|
// an error should have been shown that you cannot start paused container
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 03:19:28 -04:00
|
|
|
// an error should have been shown that you cannot start paused container
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(strings.ToLower(out), "cannot start a paused container, try unpause instead"))
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
2015-03-08 00:44:25 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartMultipleContainers(c *testing.T) {
|
2016-02-02 21:23:29 -05:00
|
|
|
// Windows does not support --link
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-08 00:44:25 -05:00
|
|
|
// run a container named 'parent' and create two container link to `parent`
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
|
|
|
|
2015-03-08 00:44:25 -05:00
|
|
|
for _, container := range []string{"child_first", "child_second"} {
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop 'parent' container
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "stop", "parent")
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, "parent", "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container should be stopped
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, "false")
|
2015-03-08 00:44:25 -05:00
|
|
|
|
2015-04-27 16:33:30 -04:00
|
|
|
// start all the three containers, container `child_first` start first which should be failed
|
2015-03-08 00:44:25 -05:00
|
|
|
// container 'parent' start second and then start container 'child_second'
|
2015-07-29 08:21:16 -04:00
|
|
|
expOut := "Cannot link to a non running container"
|
|
|
|
expErr := "failed to start containers: [child_first]"
|
2016-01-28 09:19:25 -05:00
|
|
|
out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
|
2015-10-11 03:19:28 -04:00
|
|
|
// err shouldn't be nil because start will fail
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 03:19:28 -04:00
|
|
|
// output does not correspond to what was expected
|
2015-07-29 08:21:16 -04: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 00:44:25 -05:00
|
|
|
|
|
|
|
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container running state wrong
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, expected)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 05:08:22 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachMultipleContainers(c *testing.T) {
|
2015-03-16 05:08:22 -04:00
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2016-02-23 19:24:35 -05:00
|
|
|
runSleepingContainer(c, "--name", container)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop all the containers
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "stop", container)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// test start and attach multiple containers at once, expected error
|
|
|
|
for _, option := range []string{"-a", "-i", "-ai"} {
|
2015-07-27 14:13:25 -04:00
|
|
|
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
|
2015-10-11 03:19:28 -04:00
|
|
|
// err shouldn't be nil because start will fail
|
2019-09-11 06:57:29 -04:00
|
|
|
assert.Assert(c, err != nil, "out: %s", out)
|
2015-10-11 03:19:28 -04:00
|
|
|
// output does not correspond to what was expected
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "you cannot start and attach multiple containers at once"))
|
2015-03-16 05:08:22 -04: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 09:19:25 -05:00
|
|
|
out := inspectField(c, container, "State.Running")
|
2015-10-11 03:19:28 -04:00
|
|
|
// Container running state wrong
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, out, expected)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-18 19:43:30 -04:00
|
|
|
|
|
|
|
// Test case for #23716
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachWithRename(c *testing.T) {
|
2016-06-18 19:43:30 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
|
2016-06-18 19:43:30 -04:00
|
|
|
go func() {
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, "before")
|
|
|
|
cli.DockerCmd(c, "rename", "before", "after")
|
|
|
|
cli.DockerCmd(c, "stop", "--time=2", "after")
|
2016-06-18 19:43:30 -04:00
|
|
|
}()
|
2016-12-13 15:21:51 -05:00
|
|
|
// FIXME(vdemeester) the intent is not clear and potentially racey
|
2017-04-11 15:18:30 -04:00
|
|
|
result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
|
2016-12-13 15:21:51 -05:00
|
|
|
ExitCode: 137,
|
|
|
|
})
|
2019-09-09 17:07:46 -04:00
|
|
|
assert.Assert(c, !strings.Contains(result.Stderr(), "No such container"))
|
2016-06-18 19:43:30 -04:00
|
|
|
}
|
2016-08-19 03:14:16 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestStartReturnCorrectExitCode(c *testing.T) {
|
2019-10-21 12:09:49 -04:00
|
|
|
// Note we parse kernel.GetKernelVersion rather than system.GetOSVersion
|
|
|
|
// as test binaries aren't manifested, so would otherwise report the wrong
|
|
|
|
// build number.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
v, err := kernel.GetKernelVersion()
|
|
|
|
assert.NilError(c, err)
|
|
|
|
build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
|
2019-04-17 06:39:07 -04:00
|
|
|
if build < osversion.RS3 {
|
2019-10-21 12:09:49 -04:00
|
|
|
c.Skip("FLAKY on Windows RS1, see #38521")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 03:14:16 -04:00
|
|
|
dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
|
|
|
|
dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
|
|
|
|
|
2018-09-10 19:26:40 -04:00
|
|
|
out, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, exitCode, 11, fmt.Sprintf("out: %s", out))
|
2018-09-10 19:26:40 -04:00
|
|
|
|
|
|
|
out, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
2019-09-09 17:08:22 -04:00
|
|
|
assert.Equal(c, exitCode, 12, fmt.Sprintf("out: %s", out))
|
2016-08-19 03:14:16 -04:00
|
|
|
}
|