gopkg.in/docker/docker.v20@v20.10.27/integration-cli/docker_cli_start_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strconv"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/Microsoft/hcsshim/osversion"
    12  	"github.com/docker/docker/integration-cli/cli"
    13  	"github.com/docker/docker/pkg/parsers/kernel"
    14  	"gotest.tools/v3/assert"
    15  	"gotest.tools/v3/icmd"
    16  )
    17  
    18  // Regression test for https://github.com/docker/docker/issues/7843
    19  func (s *DockerSuite) TestStartAttachReturnsOnError(c *testing.T) {
    20  	// Windows does not support link
    21  	testRequires(c, DaemonIsLinux)
    22  	dockerCmd(c, "run", "--name", "test", "busybox")
    23  
    24  	// Expect this to fail because the above container is stopped, this is what we want
    25  	out, _, err := dockerCmdWithError("run", "--name", "test2", "--link", "test:test", "busybox")
    26  	// err shouldn't be nil because container test2 try to link to stopped container
    27  	assert.Assert(c, err != nil, "out: %s", out)
    28  
    29  	ch := make(chan error, 1)
    30  	go func() {
    31  		// Attempt to start attached to the container that won't start
    32  		// This should return an error immediately since the container can't be started
    33  		if out, _, err := dockerCmdWithError("start", "-a", "test2"); err == nil {
    34  			ch <- fmt.Errorf("Expected error but got none:\n%s", out)
    35  		}
    36  		close(ch)
    37  	}()
    38  
    39  	select {
    40  	case err := <-ch:
    41  		assert.NilError(c, err)
    42  	case <-time.After(5 * time.Second):
    43  		c.Fatalf("Attach did not exit properly")
    44  	}
    45  }
    46  
    47  // gh#8555: Exit code should be passed through when using start -a
    48  func (s *DockerSuite) TestStartAttachCorrectExitCode(c *testing.T) {
    49  	testRequires(c, DaemonIsLinux)
    50  	out := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1").Stdout()
    51  	out = strings.TrimSpace(out)
    52  
    53  	// make sure the container has exited before trying the "start -a"
    54  	cli.DockerCmd(c, "wait", out)
    55  
    56  	cli.Docker(cli.Args("start", "-a", out)).Assert(c, icmd.Expected{
    57  		ExitCode: 1,
    58  	})
    59  }
    60  
    61  func (s *DockerSuite) TestStartAttachSilent(c *testing.T) {
    62  	name := "teststartattachcorrectexitcode"
    63  	dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
    64  
    65  	// make sure the container has exited before trying the "start -a"
    66  	dockerCmd(c, "wait", name)
    67  
    68  	startOut, _ := dockerCmd(c, "start", "-a", name)
    69  	// start -a produced unexpected output
    70  	assert.Equal(c, startOut, "test\n")
    71  }
    72  
    73  func (s *DockerSuite) TestStartRecordError(c *testing.T) {
    74  	// TODO Windows CI: Requires further porting work. Should be possible.
    75  	testRequires(c, DaemonIsLinux)
    76  	// when container runs successfully, we should not have state.Error
    77  	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
    78  	stateErr := inspectField(c, "test", "State.Error")
    79  	// Expected to not have state error
    80  	assert.Equal(c, stateErr, "")
    81  
    82  	// Expect this to fail and records error because of ports conflict
    83  	out, _, err := dockerCmdWithError("run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
    84  	// err shouldn't be nil because docker run will fail
    85  	assert.Assert(c, err != nil, "out: %s", out)
    86  
    87  	stateErr = inspectField(c, "test2", "State.Error")
    88  	assert.Assert(c, strings.Contains(stateErr, "port is already allocated"))
    89  	// Expect the conflict to be resolved when we stop the initial container
    90  	dockerCmd(c, "stop", "test")
    91  	dockerCmd(c, "start", "test2")
    92  	stateErr = inspectField(c, "test2", "State.Error")
    93  	// Expected to not have state error but got one
    94  	assert.Equal(c, stateErr, "")
    95  }
    96  
    97  func (s *DockerSuite) TestStartPausedContainer(c *testing.T) {
    98  	// Windows does not support pausing containers
    99  	testRequires(c, IsPausable)
   100  
   101  	runSleepingContainer(c, "-d", "--name", "testing")
   102  
   103  	dockerCmd(c, "pause", "testing")
   104  
   105  	out, _, err := dockerCmdWithError("start", "testing")
   106  	// an error should have been shown that you cannot start paused container
   107  	assert.Assert(c, err != nil, "out: %s", out)
   108  	// an error should have been shown that you cannot start paused container
   109  	assert.Assert(c, strings.Contains(strings.ToLower(out), "cannot start a paused container, try unpause instead"))
   110  }
   111  
   112  func (s *DockerSuite) TestStartMultipleContainers(c *testing.T) {
   113  	// Windows does not support --link
   114  	testRequires(c, DaemonIsLinux)
   115  	// run a container named 'parent' and create two container link to `parent`
   116  	dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
   117  
   118  	for _, container := range []string{"child_first", "child_second"} {
   119  		dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
   120  	}
   121  
   122  	// stop 'parent' container
   123  	dockerCmd(c, "stop", "parent")
   124  
   125  	out := inspectField(c, "parent", "State.Running")
   126  	// Container should be stopped
   127  	assert.Equal(c, out, "false")
   128  
   129  	// start all the three containers, container `child_first` start first which should be failed
   130  	// container 'parent' start second and then start container 'child_second'
   131  	expOut := "Cannot link to a non running container"
   132  	expErr := "failed to start containers: [child_first]"
   133  	out, _, err := dockerCmdWithError("start", "child_first", "parent", "child_second")
   134  	// err shouldn't be nil because start will fail
   135  	assert.Assert(c, err != nil, "out: %s", out)
   136  	// output does not correspond to what was expected
   137  	if !(strings.Contains(out, expOut) || strings.Contains(err.Error(), expErr)) {
   138  		c.Fatalf("Expected out: %v with err: %v  but got out: %v with err: %v", expOut, expErr, out, err)
   139  	}
   140  
   141  	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
   142  		out := inspectField(c, container, "State.Running")
   143  		// Container running state wrong
   144  		assert.Equal(c, out, expected)
   145  	}
   146  }
   147  
   148  func (s *DockerSuite) TestStartAttachMultipleContainers(c *testing.T) {
   149  	// run  multiple containers to test
   150  	for _, container := range []string{"test1", "test2", "test3"} {
   151  		runSleepingContainer(c, "--name", container)
   152  	}
   153  
   154  	// stop all the containers
   155  	for _, container := range []string{"test1", "test2", "test3"} {
   156  		dockerCmd(c, "stop", container)
   157  	}
   158  
   159  	// test start and attach multiple containers at once, expected error
   160  	for _, option := range []string{"-a", "-i", "-ai"} {
   161  		out, _, err := dockerCmdWithError("start", option, "test1", "test2", "test3")
   162  		// err shouldn't be nil because start will fail
   163  		assert.Assert(c, err != nil, "out: %s", out)
   164  		// output does not correspond to what was expected
   165  		assert.Assert(c, strings.Contains(out, "you cannot start and attach multiple containers at once"))
   166  	}
   167  
   168  	// confirm the state of all the containers be stopped
   169  	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
   170  		out := inspectField(c, container, "State.Running")
   171  		// Container running state wrong
   172  		assert.Equal(c, out, expected)
   173  	}
   174  }
   175  
   176  // Test case for #23716
   177  func (s *DockerSuite) TestStartAttachWithRename(c *testing.T) {
   178  	testRequires(c, DaemonIsLinux)
   179  	cli.DockerCmd(c, "create", "-t", "--name", "before", "busybox")
   180  	go func() {
   181  		cli.WaitRun(c, "before")
   182  		cli.DockerCmd(c, "rename", "before", "after")
   183  		cli.DockerCmd(c, "stop", "--time=2", "after")
   184  	}()
   185  	// FIXME(vdemeester) the intent is not clear and potentially racey
   186  	result := cli.Docker(cli.Args("start", "-a", "before")).Assert(c, icmd.Expected{
   187  		ExitCode: 137,
   188  	})
   189  	assert.Assert(c, !strings.Contains(result.Stderr(), "No such container"))
   190  }
   191  
   192  func (s *DockerSuite) TestStartReturnCorrectExitCode(c *testing.T) {
   193  	// Note we parse kernel.GetKernelVersion rather than system.GetOSVersion
   194  	// as test binaries aren't manifested, so would otherwise report the wrong
   195  	// build number.
   196  	if runtime.GOOS == "windows" {
   197  		v, err := kernel.GetKernelVersion()
   198  		assert.NilError(c, err)
   199  		build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
   200  		if build < osversion.RS3 {
   201  			c.Skip("FLAKY on Windows RS1, see #38521")
   202  		}
   203  	}
   204  
   205  	dockerCmd(c, "create", "--restart=on-failure:2", "--name", "withRestart", "busybox", "sh", "-c", "exit 11")
   206  	dockerCmd(c, "create", "--rm", "--name", "withRm", "busybox", "sh", "-c", "exit 12")
   207  
   208  	out, exitCode, err := dockerCmdWithError("start", "-a", "withRestart")
   209  	assert.ErrorContains(c, err, "")
   210  	assert.Equal(c, exitCode, 11, fmt.Sprintf("out: %s", out))
   211  
   212  	out, exitCode, err = dockerCmdWithError("start", "-a", "withRm")
   213  	assert.ErrorContains(c, err, "")
   214  	assert.Equal(c, exitCode, 12, fmt.Sprintf("out: %s", out))
   215  }