docker_deprecated_api_v124_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. "github.com/docker/docker/api/types/versions"
  8. "github.com/docker/docker/integration-cli/checker"
  9. "github.com/docker/docker/internal/test/request"
  10. "github.com/go-check/check"
  11. )
  12. func formatV123StartAPIURL(url string) string {
  13. return "/v1.23" + url
  14. }
  15. func (s *DockerSuite) TestDeprecatedContainerAPIStartHostConfig(c *check.C) {
  16. name := "test-deprecated-api-124"
  17. dockerCmd(c, "create", "--name", name, "busybox")
  18. config := map[string]interface{}{
  19. "Binds": []string{"/aa:/bb"},
  20. }
  21. res, body, err := request.Post("/containers/"+name+"/start", request.JSONBody(config))
  22. c.Assert(err, checker.IsNil)
  23. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  24. if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
  25. // assertions below won't work before 1.32
  26. buf, err := request.ReadBody(body)
  27. c.Assert(err, checker.IsNil)
  28. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  29. c.Assert(string(buf), checker.Contains, "was deprecated since API v1.22")
  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.OSType == "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. res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
  45. c.Assert(err, checker.IsNil)
  46. c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
  47. bindPath := RandomTmpDirPath("test", testEnv.OSType)
  48. config = map[string]interface{}{
  49. "Binds": []string{bindPath + ":" + path},
  50. }
  51. res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
  52. c.Assert(err, checker.IsNil)
  53. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  54. pth, err := inspectMountSourceField(name, path)
  55. c.Assert(err, checker.IsNil)
  56. c.Assert(pth, checker.Equals, bindPath, check.Commentf("expected volume host path to be %s, got %s", bindPath, pth))
  57. }
  58. // Test for GH#10618
  59. func (s *DockerSuite) TestDeprecatedContainerAPIStartDupVolumeBinds(c *check.C) {
  60. // TODO Windows to Windows CI - Port this
  61. testRequires(c, DaemonIsLinux)
  62. name := "testdups"
  63. config := map[string]interface{}{
  64. "Image": "busybox",
  65. "Volumes": map[string]struct{}{"/tmp": {}},
  66. }
  67. res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
  68. c.Assert(err, checker.IsNil)
  69. c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
  70. bindPath1 := RandomTmpDirPath("test1", testEnv.OSType)
  71. bindPath2 := RandomTmpDirPath("test2", testEnv.OSType)
  72. config = map[string]interface{}{
  73. "Binds": []string{bindPath1 + ":/tmp", bindPath2 + ":/tmp"},
  74. }
  75. res, body, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
  76. c.Assert(err, checker.IsNil)
  77. buf, err := request.ReadBody(body)
  78. c.Assert(err, checker.IsNil)
  79. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  80. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  81. } else {
  82. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  83. }
  84. c.Assert(string(buf), checker.Contains, "Duplicate mount point", check.Commentf("Expected failure due to duplicate bind mounts to same path, instead got: %q with error: %v", string(buf), err))
  85. }
  86. func (s *DockerSuite) TestDeprecatedContainerAPIStartVolumesFrom(c *check.C) {
  87. // TODO Windows to Windows CI - Port this
  88. testRequires(c, DaemonIsLinux)
  89. volName := "voltst"
  90. volPath := "/tmp"
  91. dockerCmd(c, "run", "--name", volName, "-v", volPath, "busybox")
  92. name := "TestContainerAPIStartVolumesFrom"
  93. config := map[string]interface{}{
  94. "Image": "busybox",
  95. "Volumes": map[string]struct{}{volPath: {}},
  96. }
  97. res, _, err := request.Post(formatV123StartAPIURL("/containers/create?name="+name), request.JSONBody(config))
  98. c.Assert(err, checker.IsNil)
  99. c.Assert(res.StatusCode, checker.Equals, http.StatusCreated)
  100. config = map[string]interface{}{
  101. "VolumesFrom": []string{volName},
  102. }
  103. res, _, err = request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.JSONBody(config))
  104. c.Assert(err, checker.IsNil)
  105. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  106. pth, err := inspectMountSourceField(name, volPath)
  107. c.Assert(err, checker.IsNil)
  108. pth2, err := inspectMountSourceField(volName, volPath)
  109. c.Assert(err, checker.IsNil)
  110. c.Assert(pth, checker.Equals, pth2, check.Commentf("expected volume host path to be %s, got %s", pth, pth2))
  111. }
  112. // #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
  113. func (s *DockerSuite) TestDeprecatedPostContainerBindNormalVolume(c *check.C) {
  114. // TODO Windows to Windows CI - Port this
  115. testRequires(c, DaemonIsLinux)
  116. dockerCmd(c, "create", "-v", "/foo", "--name=one", "busybox")
  117. fooDir, err := inspectMountSourceField("one", "/foo")
  118. c.Assert(err, checker.IsNil)
  119. dockerCmd(c, "create", "-v", "/foo", "--name=two", "busybox")
  120. bindSpec := map[string][]string{"Binds": {fooDir + ":/foo"}}
  121. res, _, err := request.Post(formatV123StartAPIURL("/containers/two/start"), request.JSONBody(bindSpec))
  122. c.Assert(err, checker.IsNil)
  123. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  124. fooDir2, err := inspectMountSourceField("two", "/foo")
  125. c.Assert(err, checker.IsNil)
  126. c.Assert(fooDir2, checker.Equals, fooDir, check.Commentf("expected volume path to be %s, got: %s", fooDir, fooDir2))
  127. }
  128. func (s *DockerSuite) TestDeprecatedStartWithTooLowMemoryLimit(c *check.C) {
  129. // TODO Windows: Port once memory is supported
  130. testRequires(c, DaemonIsLinux)
  131. out, _ := dockerCmd(c, "create", "busybox")
  132. containerID := strings.TrimSpace(out)
  133. config := `{
  134. "CpuShares": 100,
  135. "Memory": 524287
  136. }`
  137. res, body, err := request.Post(formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
  138. c.Assert(err, checker.IsNil)
  139. b, err2 := request.ReadBody(body)
  140. c.Assert(err2, checker.IsNil)
  141. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  142. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  143. } else {
  144. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  145. }
  146. c.Assert(string(b), checker.Contains, "Minimum memory limit allowed is 4MB")
  147. }
  148. // #14640
  149. func (s *DockerSuite) TestDeprecatedPostContainersStartWithoutLinksInHostConfig(c *check.C) {
  150. // TODO Windows: Windows doesn't support supplying a hostconfig on start.
  151. // An alternate test could be written to validate the negative testing aspect of this
  152. testRequires(c, DaemonIsLinux)
  153. name := "test-host-config-links"
  154. dockerCmd(c, append([]string{"create", "--name", name, "busybox"}, sleepCommandForDaemonPlatform()...)...)
  155. hc := inspectFieldJSON(c, name, "HostConfig")
  156. config := `{"HostConfig":` + hc + `}`
  157. res, b, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
  158. c.Assert(err, checker.IsNil)
  159. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  160. b.Close()
  161. }
  162. // #14640
  163. func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfig(c *check.C) {
  164. // TODO Windows: Windows doesn't support supplying a hostconfig on start.
  165. // An alternate test could be written to validate the negative testing aspect of this
  166. testRequires(c, DaemonIsLinux)
  167. name := "test-host-config-links"
  168. dockerCmd(c, "run", "--name", "foo", "-d", "busybox", "top")
  169. dockerCmd(c, "create", "--name", name, "--link", "foo:bar", "busybox", "top")
  170. hc := inspectFieldJSON(c, name, "HostConfig")
  171. config := `{"HostConfig":` + hc + `}`
  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. // #14640
  178. func (s *DockerSuite) TestDeprecatedPostContainersStartWithLinksInHostConfigIdLinked(c *check.C) {
  179. // Windows does not support links
  180. testRequires(c, DaemonIsLinux)
  181. name := "test-host-config-links"
  182. out, _ := dockerCmd(c, "run", "--name", "link0", "-d", "busybox", "top")
  183. defer dockerCmd(c, "stop", "link0")
  184. id := strings.TrimSpace(out)
  185. dockerCmd(c, "create", "--name", name, "--link", id, "busybox", "top")
  186. defer dockerCmd(c, "stop", name)
  187. hc := inspectFieldJSON(c, name, "HostConfig")
  188. config := `{"HostConfig":` + hc + `}`
  189. res, b, err := request.Post(formatV123StartAPIURL("/containers/"+name+"/start"), request.RawString(config), request.JSON)
  190. c.Assert(err, checker.IsNil)
  191. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  192. b.Close()
  193. }
  194. func (s *DockerSuite) TestDeprecatedStartWithNilDNS(c *check.C) {
  195. // TODO Windows: Add once DNS is supported
  196. testRequires(c, DaemonIsLinux)
  197. out, _ := dockerCmd(c, "create", "busybox")
  198. containerID := strings.TrimSpace(out)
  199. config := `{"HostConfig": {"Dns": null}}`
  200. res, b, err := request.Post(formatV123StartAPIURL("/containers/"+containerID+"/start"), request.RawString(config), request.JSON)
  201. c.Assert(err, checker.IsNil)
  202. c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent)
  203. b.Close()
  204. dns := inspectFieldJSON(c, containerID, "HostConfig.Dns")
  205. c.Assert(dns, checker.Equals, "[]")
  206. }