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