9ae9d7db |
package main
import ( |
65edb070 |
"fmt"
"strings" |
9ae9d7db |
"time" |
dc944ea7 |
"github.com/go-check/check" |
9ae9d7db |
)
// Regression test for https://github.com/docker/docker/issues/7843 |
dc944ea7 |
func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
dc944ea7 |
dockerCmd(c, "run", "-d", "--name", "test", "busybox")
dockerCmd(c, "wait", "test") |
9ae9d7db |
// Expect this to fail because the above container is stopped, this is what we want |
693ba98c |
if _, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil { |
dc944ea7 |
c.Fatal("Expected error but got none") |
9ae9d7db |
}
|
4203230c |
ch := make(chan error) |
9ae9d7db |
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 |
693ba98c |
if _, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil { |
4203230c |
ch <- fmt.Errorf("Expected error but got none") |
9ae9d7db |
}
close(ch)
}()
select { |
4203230c |
case err := <-ch:
c.Assert(err, check.IsNil) |
9ae9d7db |
case <-time.After(time.Second): |
dc944ea7 |
c.Fatalf("Attach did not exit properly") |
9ae9d7db |
}
} |
65edb070 |
// gh#8555: Exit code should be passed through when using start -a |
dc944ea7 |
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
012b67c3 |
out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1") |
475c6531 |
out = strings.TrimSpace(out) |
65edb070 |
// make sure the container has exited before trying the "start -a" |
012b67c3 |
dockerCmd(c, "wait", out) |
65edb070 |
|
693ba98c |
startOut, exitCode, err := dockerCmdWithError("start", "-a", out) |
65edb070 |
if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) { |
dc944ea7 |
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut) |
65edb070 |
}
if exitCode != 1 { |
dc944ea7 |
c.Fatalf("start -a did not respond with proper exit code: expected 1, got %d", exitCode) |
65edb070 |
}
} |
fb6ee865 |
|
dc944ea7 |
func (s *DockerSuite) TestStartAttachSilent(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
eeefa2dc |
name := "teststartattachcorrectexitcode" |
012b67c3 |
dockerCmd(c, "run", "--name", name, "busybox", "echo", "test") |
eeefa2dc |
// make sure the container has exited before trying the "start -a" |
012b67c3 |
dockerCmd(c, "wait", name) |
eeefa2dc |
|
012b67c3 |
startOut, _ := dockerCmd(c, "start", "-a", name) |
eeefa2dc |
if expected := "test\n"; startOut != expected { |
dc944ea7 |
c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut) |
eeefa2dc |
}
}
|
dc944ea7 |
func (s *DockerSuite) TestStartRecordError(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
fb6ee865 |
// when container runs successfully, we should not have state.Error |
dc944ea7 |
dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top") |
fb6ee865 |
stateErr, err := inspectField("test", "State.Error") |
74f8a4ec |
c.Assert(err, check.IsNil) |
fb6ee865 |
if stateErr != "" { |
dc944ea7 |
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) |
fb6ee865 |
}
// Expect this to fail and records error because of ports conflict |
693ba98c |
out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top") |
fb6ee865 |
if err == nil { |
dc944ea7 |
c.Fatalf("Expected error but got none, output %q", out) |
fb6ee865 |
} |
012b67c3 |
|
fb6ee865 |
stateErr, err = inspectField("test2", "State.Error") |
74f8a4ec |
c.Assert(err, check.IsNil) |
fb6ee865 |
expected := "port is already allocated"
if stateErr == "" || !strings.Contains(stateErr, expected) { |
dc944ea7 |
c.Fatalf("State.Error(%q) does not include %q", stateErr, expected) |
fb6ee865 |
}
// Expect the conflict to be resolved when we stop the initial container |
dc944ea7 |
dockerCmd(c, "stop", "test")
dockerCmd(c, "start", "test2") |
fb6ee865 |
stateErr, err = inspectField("test2", "State.Error") |
74f8a4ec |
c.Assert(err, check.IsNil) |
fb6ee865 |
if stateErr != "" { |
dc944ea7 |
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr) |
fb6ee865 |
}
} |
fb62e184 |
|
dc944ea7 |
func (s *DockerSuite) TestStartPausedContainer(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
02246d2d |
defer unpauseAllContainers()
|
012b67c3 |
dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top") |
02246d2d |
|
012b67c3 |
dockerCmd(c, "pause", "testing") |
02246d2d |
|
693ba98c |
if out, _, err := dockerCmdWithError("start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") { |
dc944ea7 |
c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err) |
02246d2d |
}
} |
2a5a50dc |
|
dc944ea7 |
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2a5a50dc |
// run a container named 'parent' and create two container link to `parent` |
012b67c3 |
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
2a5a50dc |
for _, container := range []string{"child_first", "child_second"} { |
012b67c3 |
dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top") |
2a5a50dc |
}
// stop 'parent' container |
012b67c3 |
dockerCmd(c, "stop", "parent")
|
74f8a4ec |
out, err := inspectField("parent", "State.Running")
c.Assert(err, check.IsNil) |
2a5a50dc |
if out != "false" { |
dc944ea7 |
c.Fatal("Container should be stopped") |
2a5a50dc |
}
|
3941623f |
// start all the three containers, container `child_first` start first which should be failed |
2a5a50dc |
// container 'parent' start second and then start container 'child_second' |
693ba98c |
out, _, err = dockerCmdWithError("start", "child_first", "parent", "child_second") |
2a5a50dc |
if !strings.Contains(out, "Cannot start container child_first") || err == nil { |
dc944ea7 |
c.Fatal("Expected error but got none") |
2a5a50dc |
}
for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} { |
74f8a4ec |
out, err := inspectField(container, "State.Running")
c.Assert(err, check.IsNil) |
2a5a50dc |
if out != expected { |
dc944ea7 |
c.Fatal("Container running state wrong") |
2a5a50dc |
}
}
} |
7dc1af14 |
|
dc944ea7 |
func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) { |
f9a3558a |
testRequires(c, DaemonIsLinux) |
7dc1af14 |
// run multiple containers to test
for _, container := range []string{"test1", "test2", "test3"} { |
012b67c3 |
dockerCmd(c, "run", "-d", "--name", container, "busybox", "top") |
7dc1af14 |
}
// stop all the containers
for _, container := range []string{"test1", "test2", "test3"} { |
012b67c3 |
dockerCmd(c, "stop", container) |
7dc1af14 |
}
// test start and attach multiple containers at once, expected error
for _, option := range []string{"-a", "-i", "-ai"} { |
693ba98c |
out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3") |
7dc1af14 |
if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil { |
dc944ea7 |
c.Fatal("Expected error but got none") |
7dc1af14 |
}
}
// confirm the state of all the containers be stopped
for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} { |
74f8a4ec |
out, err := inspectField(container, "State.Running") |
7dc1af14 |
if err != nil { |
dc944ea7 |
c.Fatal(out, err) |
7dc1af14 |
}
if out != expected { |
dc944ea7 |
c.Fatal("Container running state wrong") |
7dc1af14 |
}
}
} |