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