volumes_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "net/http"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. "time"
  21. "gotest.tools/v3/assert"
  22. "gotest.tools/v3/icmd"
  23. )
  24. func TestLocalComposeVolume(t *testing.T) {
  25. c := NewParallelCLI(t)
  26. const projectName = "compose-e2e-volume"
  27. t.Run("up with build and no image name, volume", func(t *testing.T) {
  28. // ensure local test run does not reuse previously build image
  29. c.RunDockerOrExitError(t, "rmi", "compose-e2e-volume-nginx")
  30. c.RunDockerOrExitError(t, "volume", "rm", projectName+"-staticVol")
  31. c.RunDockerOrExitError(t, "volume", "rm", "myvolume")
  32. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up",
  33. "-d")
  34. })
  35. t.Run("access bind mount data", func(t *testing.T) {
  36. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  37. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  38. })
  39. t.Run("check container volume specs", func(t *testing.T) {
  40. res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx2-1", "--format", "{{ json .Mounts }}")
  41. output := res.Stdout()
  42. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"z","RW":true,"Propagation":""`), output)
  43. assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
  44. })
  45. t.Run("check config content", func(t *testing.T) {
  46. output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/myconfig").Stdout()
  47. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  48. })
  49. t.Run("check secrets content", func(t *testing.T) {
  50. output := c.RunDockerCmd(t, "exec", "compose-e2e-volume-nginx2-1", "cat", "/run/secrets/mysecret").Stdout()
  51. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  52. })
  53. t.Run("check container bind-mounts specs", func(t *testing.T) {
  54. res := c.RunDockerCmd(t, "inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}")
  55. output := res.Stdout()
  56. assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
  57. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
  58. })
  59. t.Run("should inherit anonymous volumes", func(t *testing.T) {
  60. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  61. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
  62. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  63. })
  64. t.Run("should renew anonymous volumes", func(t *testing.T) {
  65. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  66. c.RunDockerComposeCmd(t, "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
  67. c.RunDockerOrExitError(t, "exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  68. })
  69. t.Run("cleanup volume project", func(t *testing.T) {
  70. c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "--volumes")
  71. ls := c.RunDockerCmd(t, "volume", "ls").Stdout()
  72. assert.Assert(t, !strings.Contains(ls, projectName+"-staticVol"))
  73. assert.Assert(t, !strings.Contains(ls, "myvolume"))
  74. })
  75. }
  76. func TestProjectVolumeBind(t *testing.T) {
  77. if composeStandaloneMode {
  78. t.Skip()
  79. }
  80. c := NewParallelCLI(t)
  81. const projectName = "compose-e2e-project-volume-bind"
  82. t.Run("up on project volume with bind specification", func(t *testing.T) {
  83. tmpDir, err := os.MkdirTemp("", projectName)
  84. assert.NilError(t, err)
  85. defer os.RemoveAll(tmpDir) //nolint
  86. c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  87. c.RunDockerOrExitError(t, "volume", "rm", "-f", projectName+"_project-data").Assert(t, icmd.Success)
  88. cmd := c.NewCmdWithEnv([]string{"TEST_DIR=" + tmpDir},
  89. "docker", "compose", "--project-directory", "fixtures/project-volume-bind-test", "--project-name", projectName, "up", "-d")
  90. icmd.RunCmd(cmd).Assert(t, icmd.Success)
  91. defer c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  92. c.RunCmd(t, "sh", "-c", "echo SUCCESS > "+filepath.Join(tmpDir, "resultfile")).Assert(t, icmd.Success)
  93. ret := c.RunDockerOrExitError(t, "exec", "frontend", "bash", "-c", "cat /data/resultfile").Assert(t, icmd.Success)
  94. assert.Assert(t, strings.Contains(ret.Stdout(), "SUCCESS"))
  95. })
  96. }