docker_deprecated_api_v124_test.go 8.8 KB

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