docker_deprecated_api_v124_test.go 9.1 KB

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