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