volumes_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "fmt"
  16. "net/http"
  17. "path/filepath"
  18. "runtime"
  19. "strings"
  20. "testing"
  21. "time"
  22. "gotest.tools/v3/assert"
  23. "gotest.tools/v3/icmd"
  24. )
  25. func TestLocalComposeVolume(t *testing.T) {
  26. c := NewParallelCLI(t)
  27. const projectName = "compose-e2e-volume"
  28. t.Run("up with build and no image name, volume", func(t *testing.T) {
  29. // ensure local test run does not reuse previously build image
  30. c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume-nginx")
  31. c.RunDockerOrExitError(t, "volume", "rm", projectName+"-staticVol")
  32. c.RunDockerOrExitError(t, "volume", "rm", "myvolume")
  33. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up",
  34. "-d")
  35. })
  36. t.Run("access bind mount data", func(t *testing.T) {
  37. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  38. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  39. })
  40. t.Run("check container volume specs", func(t *testing.T) {
  41. res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx2-1", "--format", "{{ json .Mounts }}")
  42. output := res.Stdout()
  43. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"z","RW":true,"Propagation":""`), output)
  44. assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
  45. })
  46. t.Run("check config content", func(t *testing.T) {
  47. output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/myconfig").Stdout()
  48. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  49. })
  50. t.Run("check secrets content", func(t *testing.T) {
  51. output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/run/secrets/mysecret").Stdout()
  52. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  53. })
  54. t.Run("check container bind-mounts specs", func(t *testing.T) {
  55. res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}")
  56. output := res.Stdout()
  57. assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
  58. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
  59. })
  60. t.Run("should inherit anonymous volumes", func(t *testing.T) {
  61. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  62. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
  63. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  64. })
  65. t.Run("should renew anonymous volumes", func(t *testing.T) {
  66. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  67. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
  68. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  69. })
  70. t.Run("cleanup volume project", func(t *testing.T) {
  71. c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--volumes")
  72. ls := c.RunDockerCmd(t, "volume", "ls").Stdout()
  73. assert.Assert(t, !strings.Contains(ls, projectName+"-staticVol"))
  74. assert.Assert(t, !strings.Contains(ls, "myvolume"))
  75. })
  76. }
  77. func TestProjectVolumeBind(t *testing.T) {
  78. if composeStandaloneMode {
  79. t.Skip()
  80. }
  81. c := NewParallelCLI(t)
  82. const projectName = "compose-e2e-project-volume-bind"
  83. t.Run("up on project volume with bind specification", func(t *testing.T) {
  84. if runtime.GOOS == "windows" {
  85. t.Skip("Running on Windows. Skipping...")
  86. }
  87. tmpDir := t.TempDir()
  88. c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  89. c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"_project-data").Assert(t, icmd.Success)
  90. cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
  91. "docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
  92. icmd.RunCmd(cmd).Assert(t, icmd.Success)
  93. defer c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  94. c.RunCmd(t, "sh", "-c", "echo SUCCESS > "+filepath.Join(tmpDir, "resultfile")).Assert(t, icmd.Success)
  95. ret := c.RunDockerOrExitError(t, "exec", "frontend", "bash", "-c", "cat /data/resultfile").Assert(t, icmd.Success)
  96. assert.Assert(t, strings.Contains(ret.Stdout(), "SUCCESS"))
  97. })
  98. }
  99. func TestUpSwitchVolumes(t *testing.T) {
  100. c := NewCLI(t)
  101. const projectName = "compose-e2e-switch-volumes"
  102. t.Cleanup(func() {
  103. c.cleanupWithDown(t, projectName)
  104. c.RunDockerCmd(t, "volume", "rm", "-f", "test_external_volume")
  105. c.RunDockerCmd(t, "volume", "rm", "-f", "test_external_volume_2")
  106. })
  107. c.RunDockerCmd(t, "volume", "create", "test_external_volume")
  108. c.RunDockerCmd(t, "volume", "create", "test_external_volume_2")
  109. c.RunDockerComposeCmd(t, "-f", "./fixtures/switch-volumes/compose.yaml", "--project-name", projectName, "up", "-d")
  110. res := c.RunDockerCmd(t, "inspect", fmt.Sprintf("%s-app-1", projectName), "-f", "{{ (index .Mounts 0).Name }}")
  111. res.Assert(t, icmd.Expected{Out: "test_external_volume"})
  112. c.RunDockerComposeCmd(t, "-f", "./fixtures/switch-volumes/compose2.yaml", "--project-name", projectName, "up", "-d")
  113. res = c.RunDockerCmd(t, "inspect", fmt.Sprintf("%s-app-1", projectName), "-f", "{{ (index .Mounts 0).Name }}")
  114. res.Assert(t, icmd.Expected{Out: "test_external_volume_2"})
  115. }
  116. func TestUpRecreateVolumes(t *testing.T) {
  117. c := NewCLI(t)
  118. const projectName = "compose-e2e-recreate-volumes"
  119. t.Cleanup(func() {
  120. c.cleanupWithDown(t, projectName)
  121. })
  122. c.RunDockerComposeCmd(t, "-f", "./fixtures/recreate-volumes/compose.yaml", "--project-name", projectName, "up", "-d")
  123. res := c.RunDockerCmd(t, "volume", "inspect", fmt.Sprintf("%s_my_vol", projectName), "-f", "{{ index .Labels \"foo\" }}")
  124. res.Assert(t, icmd.Expected{Out: "bar"})
  125. c.RunDockerComposeCmd(t, "-f", "./fixtures/recreate-volumes/compose2.yaml", "--project-name", projectName, "up", "-d", "-y")
  126. res = c.RunDockerCmd(t, "volume", "inspect", fmt.Sprintf("%s_my_vol", projectName), "-f", "{{ index .Labels \"foo\" }}")
  127. res.Assert(t, icmd.Expected{Out: "zot"})
  128. }
  129. func TestUpRecreateVolumes_IgnoreBinds(t *testing.T) {
  130. c := NewCLI(t)
  131. const projectName = "compose-e2e-recreate-volumes"
  132. t.Cleanup(func() {
  133. c.cleanupWithDown(t, projectName)
  134. })
  135. c.RunDockerComposeCmd(t, "-f", "./fixtures/recreate-volumes/bind.yaml", "--project-name", projectName, "up", "-d")
  136. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/recreate-volumes/bind.yaml", "--project-name", projectName, "up", "-d")
  137. assert.Check(t, !strings.Contains(res.Combined(), "Recreated"))
  138. }
  139. func TestImageVolume(t *testing.T) {
  140. c := NewCLI(t)
  141. const projectName = "compose-e2e-image-volume"
  142. t.Cleanup(func() {
  143. c.cleanupWithDown(t, projectName)
  144. })
  145. version := c.RunDockerCmd(t, "version", "-f", "{{.Server.Version}}")
  146. major, _, found := strings.Cut(version.Combined(), ".")
  147. assert.Assert(t, found)
  148. if major == "26" || major == "27" {
  149. t.Skip("Skipping test due to docker version < 28")
  150. }
  151. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/volumes/compose.yaml", "--project-name", projectName, "up", "with_image")
  152. out := res.Combined()
  153. assert.Check(t, strings.Contains(out, "index.html"))
  154. }
  155. func TestImageVolumeRecreateOnRebuild(t *testing.T) {
  156. c := NewCLI(t)
  157. const projectName = "compose-e2e-image-volume-recreate"
  158. t.Cleanup(func() {
  159. c.cleanupWithDown(t, projectName)
  160. c.RunDockerOrExitError(t, "rmi", "-f", "image-volume-source")
  161. })
  162. version := c.RunDockerCmd(t, "version", "-f", "{{.Server.Version}}")
  163. major, _, found := strings.Cut(version.Combined(), ".")
  164. assert.Assert(t, found)
  165. if major == "26" || major == "27" {
  166. t.Skip("Skipping test due to docker version < 28")
  167. }
  168. // First build and run with initial content
  169. c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  170. "--project-name", projectName, "build", "--build-arg", "CONTENT=foo")
  171. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  172. "--project-name", projectName, "up", "-d")
  173. assert.Check(t, !strings.Contains(res.Combined(), "error"))
  174. // Check initial content
  175. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  176. "--project-name", projectName, "logs", "consumer")
  177. assert.Check(t, strings.Contains(res.Combined(), "foo"), "Expected 'foo' in output, got: %s", res.Combined())
  178. // Rebuild source image with different content
  179. c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  180. "--project-name", projectName, "build", "--build-arg", "CONTENT=bar")
  181. // Run up again - consumer should be recreated because source image changed
  182. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  183. "--project-name", projectName, "up", "-d")
  184. // The consumer container should be recreated
  185. assert.Check(t, strings.Contains(res.Combined(), "Recreate") || strings.Contains(res.Combined(), "Created"),
  186. "Expected container to be recreated, got: %s", res.Combined())
  187. // Check updated content
  188. res = c.RunDockerComposeCmd(t, "-f", "./fixtures/image-volume-recreate/compose.yaml",
  189. "--project-name", projectName, "logs", "consumer")
  190. assert.Check(t, strings.Contains(res.Combined(), "bar"), "Expected 'bar' in output after rebuild, got: %s", res.Combined())
  191. }