2014-09-24 22:46:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-18 22:47:48 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-09-24 22:46:57 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-09-24 22:46:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for https://github.com/docker/docker/issues/7843
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox")
|
|
|
|
dockerCmd(c, "wait", "test")
|
2014-09-24 22:46:57 -04:00
|
|
|
|
|
|
|
// Expect this to fail because the above container is stopped, this is what we want
|
2015-07-27 14:13:25 -04:00
|
|
|
if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected error but got none")
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 13:29:48 -04:00
|
|
|
ch := make(chan error)
|
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
|
2015-07-27 14:13:25 -04:00
|
|
|
if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
|
2015-04-27 13:29:48 -04:00
|
|
|
ch <- fmt.Errorf("Expected error but got none")
|
2014-09-24 22:46:57 -04:00
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-04-27 13:29:48 -04:00
|
|
|
case err := <-ch:
|
|
|
|
c.Assert(err, check.IsNil)
|
2014-09-24 22:46:57 -04:00
|
|
|
case <-time.After(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
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-21 14:01:24 -04:00
|
|
|
out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
|
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"
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "wait", out)
|
2014-10-18 22:47:48 -04:00
|
|
|
|
2015-07-27 14:13:25 -04:00
|
|
|
startOut, exitCode, err := dockerCmdWithError("start", "-a", out)
|
2014-10-18 22:47:48 -04:00
|
|
|
if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
|
|
|
if exitCode != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode)
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-09-30 04:30:58 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
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-01-06 20:58:30 -05:00
|
|
|
if expected := "test\n"; startOut != expected {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
|
2015-01-06 20:58:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartRecordError(c *check.C) {
|
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")
|
2014-09-30 04:30:58 -04:00
|
|
|
stateErr, err := inspectField("test", "State.Error")
|
2015-05-17 22:06:13 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-09-30 04:30:58 -04:00
|
|
|
if stateErr != "" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected to not have state error but got state.Error(%q)", 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")
|
2014-09-30 04:30:58 -04:00
|
|
|
if err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected error but got none, output %q", out)
|
2014-09-30 04:30:58 -04:00
|
|
|
}
|
2015-07-21 14:01:24 -04:00
|
|
|
|
2014-09-30 04:30:58 -04:00
|
|
|
stateErr, err = inspectField("test2", "State.Error")
|
2015-05-17 22:06:13 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-09-30 04:30:58 -04:00
|
|
|
expected := "port is already allocated"
|
|
|
|
if stateErr == "" || !strings.Contains(stateErr, expected) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
|
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")
|
2014-09-30 04:30:58 -04:00
|
|
|
stateErr, err = inspectField("test2", "State.Error")
|
2015-05-17 22:06:13 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-09-30 04:30:58 -04:00
|
|
|
if stateErr != "" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
|
2014-09-30 04:30:58 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-23 08:50:06 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-01-14 19:44:53 -05:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
|
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-07-27 14:13:25 -04:00
|
|
|
if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-08 00:44:25 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
|
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")
|
|
|
|
|
2015-05-17 22:06:13 -04:00
|
|
|
out, err := inspectField("parent", "State.Running")
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-08 00:44:25 -05:00
|
|
|
if out != "false" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Container should be stopped")
|
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-27 14:13:25 -04:00
|
|
|
out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second")
|
2015-03-08 00:44:25 -05:00
|
|
|
if !strings.Contains(out, "Cannot start container child_first") || err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected error but got none")
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
|
2015-05-17 22:06:13 -04:00
|
|
|
out, err := inspectField(container, "State.Running")
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-03-08 00:44:25 -05:00
|
|
|
if out != expected {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Container running state wrong")
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 05:08:22 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-16 05:08:22 -04:00
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
2015-07-21 14:01:24 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", container, "busybox", "top")
|
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-03-16 05:08:22 -04:00
|
|
|
if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected error but got none")
|
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"} {
|
2015-05-17 22:06:13 -04:00
|
|
|
out, err := inspectField(container, "State.Running")
|
2015-03-16 05:08:22 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
if out != expected {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Container running state wrong")
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|