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