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