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