github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/docker_cli_create_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/docker/docker/integration-cli/checker"
    12  	"github.com/docker/docker/integration-cli/cli"
    13  	"github.com/docker/docker/integration-cli/cli/build"
    14  	"github.com/docker/docker/internal/test/fakecontext"
    15  	"github.com/docker/docker/pkg/stringid"
    16  	"github.com/docker/go-connections/nat"
    17  	"github.com/go-check/check"
    18  )
    19  
    20  // Make sure we can create a simple container with some args
    21  func (s *DockerSuite) TestCreateArgs(c *check.C) {
    22  	// Intentionally clear entrypoint, as the Windows busybox image needs an entrypoint, which breaks this test
    23  	out, _ := dockerCmd(c, "create", "--entrypoint=", "busybox", "command", "arg1", "arg2", "arg with space", "-c", "flags")
    24  
    25  	cleanedContainerID := strings.TrimSpace(out)
    26  
    27  	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
    28  
    29  	containers := []struct {
    30  		ID      string
    31  		Created time.Time
    32  		Path    string
    33  		Args    []string
    34  		Image   string
    35  	}{}
    36  
    37  	err := json.Unmarshal([]byte(out), &containers)
    38  	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
    39  	c.Assert(containers, checker.HasLen, 1)
    40  
    41  	cont := containers[0]
    42  	c.Assert(string(cont.Path), checker.Equals, "command", check.Commentf("Unexpected container path. Expected command, received: %s", cont.Path))
    43  
    44  	b := false
    45  	expected := []string{"arg1", "arg2", "arg with space", "-c", "flags"}
    46  	for i, arg := range expected {
    47  		if arg != cont.Args[i] {
    48  			b = true
    49  			break
    50  		}
    51  	}
    52  	if len(cont.Args) != len(expected) || b {
    53  		c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
    54  	}
    55  
    56  }
    57  
    58  // Make sure we can grow the container's rootfs at creation time.
    59  func (s *DockerSuite) TestCreateGrowRootfs(c *check.C) {
    60  	// Windows and Devicemapper support growing the rootfs
    61  	if testEnv.OSType != "windows" {
    62  		testRequires(c, Devicemapper)
    63  	}
    64  	out, _ := dockerCmd(c, "create", "--storage-opt", "size=120G", "busybox")
    65  
    66  	cleanedContainerID := strings.TrimSpace(out)
    67  
    68  	inspectOut := inspectField(c, cleanedContainerID, "HostConfig.StorageOpt")
    69  	c.Assert(inspectOut, checker.Equals, "map[size:120G]")
    70  }
    71  
    72  // Make sure we cannot shrink the container's rootfs at creation time.
    73  func (s *DockerSuite) TestCreateShrinkRootfs(c *check.C) {
    74  	testRequires(c, Devicemapper)
    75  
    76  	// Ensure this fails because of the defaultBaseFsSize is 10G
    77  	out, _, err := dockerCmdWithError("create", "--storage-opt", "size=5G", "busybox")
    78  	c.Assert(err, check.NotNil, check.Commentf(out))
    79  	c.Assert(out, checker.Contains, "Container size cannot be smaller than")
    80  }
    81  
    82  // Make sure we can set hostconfig options too
    83  func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
    84  	out, _ := dockerCmd(c, "create", "-P", "busybox", "echo")
    85  
    86  	cleanedContainerID := strings.TrimSpace(out)
    87  
    88  	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
    89  
    90  	containers := []struct {
    91  		HostConfig *struct {
    92  			PublishAllPorts bool
    93  		}
    94  	}{}
    95  
    96  	err := json.Unmarshal([]byte(out), &containers)
    97  	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
    98  	c.Assert(containers, checker.HasLen, 1)
    99  
   100  	cont := containers[0]
   101  	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
   102  	c.Assert(cont.HostConfig.PublishAllPorts, check.NotNil, check.Commentf("Expected PublishAllPorts, got false"))
   103  }
   104  
   105  func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
   106  	out, _ := dockerCmd(c, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
   107  
   108  	cleanedContainerID := strings.TrimSpace(out)
   109  
   110  	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
   111  
   112  	containers := []struct {
   113  		HostConfig *struct {
   114  			PortBindings map[nat.Port][]nat.PortBinding
   115  		}
   116  	}{}
   117  	err := json.Unmarshal([]byte(out), &containers)
   118  	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
   119  	c.Assert(containers, checker.HasLen, 1)
   120  
   121  	cont := containers[0]
   122  
   123  	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
   124  	c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 4, check.Commentf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings)))
   125  
   126  	for k, v := range cont.HostConfig.PortBindings {
   127  		c.Assert(v, checker.HasLen, 1, check.Commentf("Expected 1 ports binding, for the port  %s but found %s", k, v))
   128  		c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
   129  
   130  	}
   131  
   132  }
   133  
   134  func (s *DockerSuite) TestCreateWithLargePortRange(c *check.C) {
   135  	out, _ := dockerCmd(c, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
   136  
   137  	cleanedContainerID := strings.TrimSpace(out)
   138  
   139  	out, _ = dockerCmd(c, "inspect", cleanedContainerID)
   140  
   141  	containers := []struct {
   142  		HostConfig *struct {
   143  			PortBindings map[nat.Port][]nat.PortBinding
   144  		}
   145  	}{}
   146  
   147  	err := json.Unmarshal([]byte(out), &containers)
   148  	c.Assert(err, check.IsNil, check.Commentf("Error inspecting the container: %s", err))
   149  	c.Assert(containers, checker.HasLen, 1)
   150  
   151  	cont := containers[0]
   152  	c.Assert(cont.HostConfig, check.NotNil, check.Commentf("Expected HostConfig, got none"))
   153  	c.Assert(cont.HostConfig.PortBindings, checker.HasLen, 65535)
   154  
   155  	for k, v := range cont.HostConfig.PortBindings {
   156  		c.Assert(v, checker.HasLen, 1)
   157  		c.Assert(k.Port(), checker.Equals, v[0].HostPort, check.Commentf("Expected host port %s to match published port %s", k.Port(), v[0].HostPort))
   158  	}
   159  
   160  }
   161  
   162  // "test123" should be printed by docker create + start
   163  func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
   164  	out, _ := dockerCmd(c, "create", "busybox", "echo", "test123")
   165  
   166  	cleanedContainerID := strings.TrimSpace(out)
   167  
   168  	out, _ = dockerCmd(c, "start", "-ai", cleanedContainerID)
   169  	c.Assert(out, checker.Equals, "test123\n", check.Commentf("container should've printed 'test123', got %q", out))
   170  
   171  }
   172  
   173  func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
   174  	testRequires(c, SameHostDaemon)
   175  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   176  
   177  	name := "test_create_volume"
   178  	dockerCmd(c, "create", "--name", name, "-v", prefix+slash+"foo", "busybox")
   179  
   180  	dir, err := inspectMountSourceField(name, prefix+slash+"foo")
   181  	c.Assert(err, check.IsNil, check.Commentf("Error getting volume host path: %q", err))
   182  
   183  	if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
   184  		c.Fatalf("Volume was not created")
   185  	}
   186  	if err != nil {
   187  		c.Fatalf("Error statting volume host path: %q", err)
   188  	}
   189  
   190  }
   191  
   192  func (s *DockerSuite) TestCreateLabels(c *check.C) {
   193  	name := "test_create_labels"
   194  	expected := map[string]string{"k1": "v1", "k2": "v2"}
   195  	dockerCmd(c, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")
   196  
   197  	actual := make(map[string]string)
   198  	inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
   199  
   200  	if !reflect.DeepEqual(expected, actual) {
   201  		c.Fatalf("Expected %s got %s", expected, actual)
   202  	}
   203  }
   204  
   205  func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
   206  	imageName := "testcreatebuildlabel"
   207  	buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
   208  		LABEL k1=v1 k2=v2`))
   209  
   210  	name := "test_create_labels_from_image"
   211  	expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"}
   212  	dockerCmd(c, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)
   213  
   214  	actual := make(map[string]string)
   215  	inspectFieldAndUnmarshall(c, name, "Config.Labels", &actual)
   216  
   217  	if !reflect.DeepEqual(expected, actual) {
   218  		c.Fatalf("Expected %s got %s", expected, actual)
   219  	}
   220  }
   221  
   222  func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
   223  	image := "busybox"
   224  	// Busybox on Windows does not implement hostname command
   225  	if testEnv.OSType == "windows" {
   226  		image = testEnv.PlatformDefaults.BaseImage
   227  	}
   228  	out, _ := dockerCmd(c, "run", "-h", "web.0", image, "hostname")
   229  	c.Assert(strings.TrimSpace(out), checker.Equals, "web.0", check.Commentf("hostname not set, expected `web.0`, got: %s", out))
   230  
   231  }
   232  
   233  func (s *DockerSuite) TestCreateRM(c *check.C) {
   234  	// Test to make sure we can 'rm' a new container that is in
   235  	// "Created" state, and has ever been run. Test "rm -f" too.
   236  
   237  	// create a container
   238  	out, _ := dockerCmd(c, "create", "busybox")
   239  	cID := strings.TrimSpace(out)
   240  
   241  	dockerCmd(c, "rm", cID)
   242  
   243  	// Now do it again so we can "rm -f" this time
   244  	out, _ = dockerCmd(c, "create", "busybox")
   245  
   246  	cID = strings.TrimSpace(out)
   247  	dockerCmd(c, "rm", "-f", cID)
   248  }
   249  
   250  func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
   251  	// Uses Linux specific functionality (--ipc)
   252  	testRequires(c, DaemonIsLinux, SameHostDaemon)
   253  
   254  	out, _ := dockerCmd(c, "create", "busybox")
   255  	id := strings.TrimSpace(out)
   256  
   257  	dockerCmd(c, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
   258  }
   259  
   260  func (s *DockerSuite) TestCreateByImageID(c *check.C) {
   261  	imageName := "testcreatebyimageid"
   262  	buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
   263  		MAINTAINER dockerio`))
   264  	imageID := getIDByName(c, imageName)
   265  	truncatedImageID := stringid.TruncateID(imageID)
   266  
   267  	dockerCmd(c, "create", imageID)
   268  	dockerCmd(c, "create", truncatedImageID)
   269  
   270  	// Ensure this fails
   271  	out, exit, _ := dockerCmdWithError("create", fmt.Sprintf("%s:%s", imageName, imageID))
   272  	if exit == 0 {
   273  		c.Fatalf("expected non-zero exit code; received %d", exit)
   274  	}
   275  
   276  	if expected := "invalid reference format"; !strings.Contains(out, expected) {
   277  		c.Fatalf(`Expected %q in output; got: %s`, expected, out)
   278  	}
   279  
   280  	if i := strings.IndexRune(imageID, ':'); i >= 0 {
   281  		imageID = imageID[i+1:]
   282  	}
   283  	out, exit, _ = dockerCmdWithError("create", fmt.Sprintf("%s:%s", "wrongimage", imageID))
   284  	if exit == 0 {
   285  		c.Fatalf("expected non-zero exit code; received %d", exit)
   286  	}
   287  
   288  	if expected := "Unable to find image"; !strings.Contains(out, expected) {
   289  		c.Fatalf(`Expected %q in output; got: %s`, expected, out)
   290  	}
   291  }
   292  
   293  func (s *DockerSuite) TestCreateStopSignal(c *check.C) {
   294  	name := "test_create_stop_signal"
   295  	dockerCmd(c, "create", "--name", name, "--stop-signal", "9", "busybox")
   296  
   297  	res := inspectFieldJSON(c, name, "Config.StopSignal")
   298  	c.Assert(res, checker.Contains, "9")
   299  
   300  }
   301  
   302  func (s *DockerSuite) TestCreateWithWorkdir(c *check.C) {
   303  	name := "foo"
   304  
   305  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   306  	dir := prefix + slash + "home" + slash + "foo" + slash + "bar"
   307  
   308  	dockerCmd(c, "create", "--name", name, "-w", dir, "busybox")
   309  	// Windows does not create the workdir until the container is started
   310  	if testEnv.OSType == "windows" {
   311  		dockerCmd(c, "start", name)
   312  	}
   313  	dockerCmd(c, "cp", fmt.Sprintf("%s:%s", name, dir), prefix+slash+"tmp")
   314  }
   315  
   316  func (s *DockerSuite) TestCreateWithInvalidLogOpts(c *check.C) {
   317  	name := "test-invalidate-log-opts"
   318  	out, _, err := dockerCmdWithError("create", "--name", name, "--log-opt", "invalid=true", "busybox")
   319  	c.Assert(err, checker.NotNil)
   320  	c.Assert(out, checker.Contains, "unknown log opt")
   321  
   322  	out, _ = dockerCmd(c, "ps", "-a")
   323  	c.Assert(out, checker.Not(checker.Contains), name)
   324  }
   325  
   326  // #20972
   327  func (s *DockerSuite) TestCreate64ByteHexID(c *check.C) {
   328  	out := inspectField(c, "busybox", "Id")
   329  	imageID := strings.TrimPrefix(strings.TrimSpace(string(out)), "sha256:")
   330  
   331  	dockerCmd(c, "create", imageID)
   332  }
   333  
   334  // Test case for #23498
   335  func (s *DockerSuite) TestCreateUnsetEntrypoint(c *check.C) {
   336  	name := "test-entrypoint"
   337  	dockerfile := `FROM busybox
   338  ADD entrypoint.sh /entrypoint.sh
   339  RUN chmod 755 /entrypoint.sh
   340  ENTRYPOINT ["/entrypoint.sh"]
   341  CMD echo foobar`
   342  
   343  	ctx := fakecontext.New(c, "",
   344  		fakecontext.WithDockerfile(dockerfile),
   345  		fakecontext.WithFiles(map[string]string{
   346  			"entrypoint.sh": `#!/bin/sh
   347  echo "I am an entrypoint"
   348  exec "$@"`,
   349  		}))
   350  	defer ctx.Close()
   351  
   352  	cli.BuildCmd(c, name, build.WithExternalBuildContext(ctx))
   353  
   354  	out := cli.DockerCmd(c, "create", "--entrypoint=", name, "echo", "foo").Combined()
   355  	id := strings.TrimSpace(out)
   356  	c.Assert(id, check.Not(check.Equals), "")
   357  	out = cli.DockerCmd(c, "start", "-a", id).Combined()
   358  	c.Assert(strings.TrimSpace(out), check.Equals, "foo")
   359  }
   360  
   361  // #22471
   362  func (s *DockerSuite) TestCreateStopTimeout(c *check.C) {
   363  	name1 := "test_create_stop_timeout_1"
   364  	dockerCmd(c, "create", "--name", name1, "--stop-timeout", "15", "busybox")
   365  
   366  	res := inspectFieldJSON(c, name1, "Config.StopTimeout")
   367  	c.Assert(res, checker.Contains, "15")
   368  
   369  	name2 := "test_create_stop_timeout_2"
   370  	dockerCmd(c, "create", "--name", name2, "busybox")
   371  
   372  	res = inspectFieldJSON(c, name2, "Config.StopTimeout")
   373  	c.Assert(res, checker.Contains, "null")
   374  }