2014-09-24 22:46:57 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-18 22:47:48 -04:00
|
|
|
"fmt"
|
2014-09-24 22:46:57 -04:00
|
|
|
"os/exec"
|
2014-10-18 22:47:48 -04:00
|
|
|
"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) {
|
2014-09-24 22:46:57 -04:00
|
|
|
|
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
|
|
|
|
if _, err := runCommand(exec.Command(dockerBinary, "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
|
|
|
|
if _, err := runCommand(exec.Command(dockerBinary, "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) {
|
2014-10-18 22:47:48 -04:00
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", out)
|
2015-01-06 20:58:30 -05:00
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to wait on container: %v", err)
|
2014-10-18 22:47:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
startCmd := exec.Command(dockerBinary, "start", "-a", out)
|
|
|
|
startOut, exitCode, err := runCommandWithOutput(startCmd)
|
|
|
|
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-01-06 20:58:30 -05:00
|
|
|
|
|
|
|
name := "teststartattachcorrectexitcode"
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "echo", "test")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
2015-01-06 20:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the container has exited before trying the "start -a"
|
|
|
|
waitCmd := exec.Command(dockerBinary, "wait", name)
|
|
|
|
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("wait command failed with error: %v", err)
|
2015-01-06 20:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
startCmd := exec.Command(dockerBinary, "start", "-a", name)
|
|
|
|
startOut, _, err := runCommandWithOutput(startCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
|
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) {
|
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")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
|
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
|
|
|
|
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
|
|
|
|
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
|
|
|
}
|
|
|
|
stateErr, err = inspectField("test2", "State.Error")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
|
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")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
|
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
|
|
|
|
|
|
|
// gh#8726: a failed Start() breaks --volumes-from on subsequent Start()'s
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartVolumesFromFailsCleanly(c *check.C) {
|
2014-10-23 08:50:06 -04:00
|
|
|
|
|
|
|
// Create the first data volume
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "data_before", "-v", "/foo", "busybox")
|
2014-10-23 08:50:06 -04:00
|
|
|
|
|
|
|
// Expect this to fail because the data test after contaienr doesn't exist yet
|
|
|
|
if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "consumer", "--volumes-from", "data_before", "--volumes-from", "data_after", "busybox")); err == nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal("Expected error but got none")
|
2014-10-23 08:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the second data volume
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "data_after", "-v", "/bar", "busybox")
|
2014-10-23 08:50:06 -04:00
|
|
|
|
|
|
|
// Now, all the volumes should be there
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "start", "consumer")
|
2014-10-23 08:50:06 -04:00
|
|
|
|
|
|
|
// Check that we have the volumes we want
|
2015-04-18 12:46:47 -04:00
|
|
|
out, _ := dockerCmd(c, "inspect", "--format='{{ len .Volumes }}'", "consumer")
|
2015-03-25 21:40:23 -04:00
|
|
|
nVolumes := strings.Trim(out, " \r\n'")
|
|
|
|
if nVolumes != "2" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Missing volumes: expected 2, got %s", nVolumes)
|
2014-10-23 08:50:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-14 19:44:53 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
|
2015-01-14 19:44:53 -05:00
|
|
|
defer unpauseAllContainers()
|
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "pause", "testing")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-01-14 19:44:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "start", "testing")
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); 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-03-08 00:44:25 -05:00
|
|
|
// run a container named 'parent' and create two container link to `parent`
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
for _, container := range []string{"child_first", "child_second"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop 'parent' container
|
|
|
|
cmd = exec.Command(dockerBinary, "stop", "parent")
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
out = strings.Trim(out, "\r\n")
|
|
|
|
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'
|
|
|
|
cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
|
|
|
|
out, _, err = runCommandWithOutput(cmd)
|
|
|
|
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"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
|
|
|
|
out, _, err = runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-08 00:44:25 -05:00
|
|
|
}
|
|
|
|
out = strings.Trim(out, "\r\n")
|
|
|
|
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-03-16 05:08:22 -04:00
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
// run multiple containers to test
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop all the containers
|
|
|
|
for _, container := range []string{"test1", "test2", "test3"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "stop", container)
|
|
|
|
if out, _, err := runCommandWithOutput(cmd); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
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"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
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"} {
|
|
|
|
cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-03-16 05:08:22 -04:00
|
|
|
}
|
|
|
|
out = strings.Trim(out, "\r\n")
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|