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

     1  // This file will be removed when we completely drop support for
     2  // passing HostConfig to container start API.
     3  
     4  package main
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/integration-cli/checker"
    11  	"github.com/docker/docker/integration-cli/request"
    12  	"github.com/go-check/check"
    13  )
    14  
    15  func formatV123StartAPIURL(url string) string {
    16  	return "/v1.23" + url
    17  }
    18  
    19  func (s *DockerSuite) TestDeprecatedContainerAPIStartHostConfig(c *check.C) {
    20  	name := "test-deprecated-api-124"
    21  	dockerCmd(c, "create", "--name", name, "busybox")
    22  	config := map[string]interface{}{
    23  		"Binds": []string{"/aa:/bb"},
    24  	}
    25  	res, body, err := request.Post("/containers/"+name+"/start", request.JSONBody(config))
    26  	c.Assert(err, checker.IsNil)
    27  
    28  	buf, err := request.ReadBody(body)
    29  	c.Assert(err, checker.IsNil)
    30  
    31  	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
    32  	c.Assert(string(buf), checker.Contains, "was deprecated since API v1.22")
    33  }
    34  
    35  func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumeBinds(c *check.C) {
    36  	// TODO Windows CI: Investigate further why this fails on Windows to Windows CI.
    37  	testRequires(c, DaemonIsLinux)
    38  	path := "/foo"
    39  	if testEnv.OSType == "windows" {
    40  		path = `c:\foo`
    41  	}
    42  	name := "testing"
    43  	config := map[string]interface{}{
    44  		"Image":   "busybox",
    45  		"Volumes": map[string]struct{}{path: {}},
    46  	}
    47  
    48  	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
    49  	c.Assert(err, checker.IsNil)
    50  	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
    51  
    52  	bindPath := RandomTmpDirPath("test", testEnv.OSType)
    53  	config = map[string]interface{}{
    54  		"Binds": []string{bindPath + ":" + path},
    55  	}
    56  	res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
    57  	c.Assert(err, checker.IsNil)
    58  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
    59  
    60  	pth, err := inspectMountSourceField(name, path)
    61  	c.Assert(err, checker.IsNil)
    62  	c.Assert(pth, checker.Equals, bindPath, check.Commentf("expected volume host path to be %s, got %s", bindPath, pth))
    63  }
    64  
    65  // Test for GH#10618
    66  func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C) {
    67  	// TODO Windows to Windows CI - Port this
    68  	testRequires(c, DaemonIsLinux)
    69  	name := "testdups"
    70  	config := map[string]interface{}{
    71  		"Image":   "busybox",
    72  		"Volumes": map[string]struct{}{"/tmp": {}},
    73  	}
    74  
    75  	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
    76  	c.Assert(err, checker.IsNil)
    77  	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
    78  
    79  	bindPath1 := RandomTmpDirPath("test1", testEnv.OSType)
    80  	bindPath2 := RandomTmpDirPath("test2", testEnv.OSType)
    81  
    82  	config = map[string]interface{}{
    83  		"Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},
    84  	}
    85  	res, body, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
    86  	c.Assert(err, checker.IsNil)
    87  
    88  	buf, err := request.ReadBody(body)
    89  	c.Assert(err, checker.IsNil)
    90  
    91  	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
    92  	c.Assert(string(buf), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(buf), err))
    93  }
    94  
    95  func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumesFrom(c *check.C) {
    96  	// TODO Windows to Windows CI - Port this
    97  	testRequires(c, DaemonIsLinux)
    98  	volName := "voltst"
    99  	volPath := "/tmp"
   100  
   101  	dockerCmd(c, "run", "--name", volName, "-v", volPath, "busybox")
   102  
   103  	name := "TestContainerAPIStartVolumesFrom"
   104  	config := map[string]interface{}{
   105  		"Image":   "busybox",
   106  		"Volumes": map[string]struct{}{volPath: {}},
   107  	}
   108  
   109  	res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
   110  	c.Assert(err, checker.IsNil)
   111  	c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
   112  
   113  	config = map[string]interface{}{
   114  		"VolumesFrom": []string{volName},
   115  	}
   116  	res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
   117  	c.Assert(err, checker.IsNil)
   118  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   119  
   120  	pth, err := inspectMountSourceField(name, volPath)
   121  	c.Assert(err, checker.IsNil)
   122  	pth2, err := inspectMountSourceField(volName, volPath)
   123  	c.Assert(err, checker.IsNil)
   124  	c.Assert(pth, checker.Equals, pth2, check.Commentf("expected volume host path to be %s, got %s", pth, pth2))
   125  }
   126  
   127  // #9981 - Allow a docker created volume (ie, one in /var/lib/docker/volumes) to be used to overwrite (via passing in Binds on api start) an existing volume
   128  func (s *DockerSuite) TestDeprecatedPostContainerBindNormalVolume(c *check.C) {
   129  	// TODO Windows to Windows CI - Port this
   130  	testRequires(c, DaemonIsLinux)
   131  	dockerCmd(c, "create", "-v", "/foo", "--name=one", "busybox")
   132  
   133  	fooDir, err := inspectMountSourceField("one", "/foo")
   134  	c.Assert(err, checker.IsNil)
   135  
   136  	dockerCmd(c, "create", "-v", "/foo", "--name=two", "busybox")
   137  
   138  	bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
   139  	res, _, err := request.Post(formatV123StartAPIURL("/containers/two/start"), request.JSONBody(bindSpec))
   140  	c.Assert(err, checker.IsNil)
   141  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   142  
   143  	fooDir2, err := inspectMountSourceField("two", "/foo")
   144  	c.Assert(err, checker.IsNil)
   145  	c.Assert(fooDir2, checker.Equals, fooDir, check.Commentf("expected volume path to be %s, got: %s", fooDir, fooDir2))
   146  }
   147  
   148  func (s *DockerSuite) TestDeprecatedStartWithTooLowMemoryLimit(c *check.C) {
   149  	// TODO Windows: Port once memory is supported
   150  	testRequires(c, DaemonIsLinux)
   151  	out, _ := dockerCmd(c, "create", "busybox")
   152  
   153  	containerID := strings.TrimSpace(out)
   154  
   155  	config := `{
   156                  "CpuShares": 100,
   157                  "Memory":    524287
   158          }`
   159  
   160  	res, body, err := request.Post(formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
   161  	c.Assert(err, checker.IsNil)
   162  	b, err2 := request.ReadBody(body)
   163  	c.Assert(err2, checker.IsNil)
   164  	c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
   165  	c.Assert(string(b), checker.Contains, "Minimum memory limit allowed is 4MB")
   166  }
   167  
   168  // #14640
   169  func (s *DockerSuite) TestDeprecatedPostContainersStartWithoutLinksInHostConfig(c *check.C) {
   170  	// TODO Windows: Windows doesn't support supplying a hostconfig on start.
   171  	// An alternate test could be written to validate the negative testing aspect of this
   172  	testRequires(c, DaemonIsLinux)
   173  	name := "test-host-config-links"
   174  	dockerCmd(c, append([]string{"create", "--name", name, "busybox"}, sleepCommandForDaemonPlatform()...)...)
   175  
   176  	hc := inspectFieldJSON(c, name, "HostConfig")
   177  	config := `{"HostConfig":` + hc + `}`
   178  
   179  	res, b, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
   180  	c.Assert(err, checker.IsNil)
   181  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   182  	b.Close()
   183  }
   184  
   185  // #14640
   186  func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfig(c *check.C) {
   187  	// TODO Windows: Windows doesn't support supplying a hostconfig on start.
   188  	// An alternate test could be written to validate the negative testing aspect of this
   189  	testRequires(c, DaemonIsLinux)
   190  	name := "test-host-config-links"
   191  	dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top")
   192  	dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top")
   193  
   194  	hc := inspectFieldJSON(c, name, "HostConfig")
   195  	config := `{"HostConfig":` + hc + `}`
   196  
   197  	res, b, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
   198  	c.Assert(err, checker.IsNil)
   199  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   200  	b.Close()
   201  }
   202  
   203  // #14640
   204  func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) {
   205  	// Windows does not support links
   206  	testRequires(c, DaemonIsLinux)
   207  	name := "test-host-config-links"
   208  	out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top")
   209  	defer dockerCmd(c, "stop", "link0")
   210  	id := strings.TrimSpace(out)
   211  	dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top")
   212  	defer dockerCmd(c, "stop", name)
   213  
   214  	hc := inspectFieldJSON(c, name, "HostConfig")
   215  	config := `{"HostConfig":` + hc + `}`
   216  
   217  	res, b, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
   218  	c.Assert(err, checker.IsNil)
   219  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   220  	b.Close()
   221  }
   222  
   223  func (s *DockerSuite) TestDeprecatedStartWithNilDNS(c *check.C) {
   224  	// TODO Windows: Add once DNS is supported
   225  	testRequires(c, DaemonIsLinux)
   226  	out, _ := dockerCmd(c, "create", "busybox")
   227  	containerID := strings.TrimSpace(out)
   228  
   229  	config := `{"HostConfig": {"Dns": null}}`
   230  
   231  	res, b, err := request.Post(formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
   232  	c.Assert(err, checker.IsNil)
   233  	c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
   234  	b.Close()
   235  
   236  	dns := inspectFieldJSON(c, containerID, "HostConfig.Dns")
   237  	c.Assert(dns, checker.Equals, "[]")
   238  }