docker_deprecated_api_v124_test.go 8.8 KB

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