docker_deprecated_api_v124_test.go 8.6 KB

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