docker_deprecated_api_v124_test.go 9.2 KB

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