docker_deprecated_api_v124_test.go 8.7 KB

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