docker_deprecated_api_v124_test.go 9.5 KB

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