volumes_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "strings"
  17. "testing"
  18. "time"
  19. "gotest.tools/v3/assert"
  20. )
  21. func TestLocalComposeVolume(t *testing.T) {
  22. c := NewParallelE2eCLI(t, binDir)
  23. const projectName = "compose-e2e-volume"
  24. t.Run("up with build and no image name, volume", func(t *testing.T) {
  25. // ensure local test run does not reuse previously build image
  26. c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
  27. c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
  28. c.RunDockerOrExitError("volume", "rm", "myvolume")
  29. c.RunDockerCmd("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "-d")
  30. })
  31. t.Run("access bind mount data", func(t *testing.T) {
  32. output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
  33. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  34. })
  35. t.Run("check container volume specs", func(t *testing.T) {
  36. res := c.RunDockerCmd("inspect", "compose-e2e-volume-nginx2-1", "--format", "{{ json .Mounts }}")
  37. output := res.Stdout()
  38. // nolint
  39. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"z","RW":true,"Propagation":""`), output)
  40. assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
  41. })
  42. t.Run("check config content", func(t *testing.T) {
  43. output := c.RunDockerCmd("exec", "compose-e2e-volume-nginx2-1", "cat", "/myconfig").Stdout()
  44. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  45. })
  46. t.Run("check secrets content", func(t *testing.T) {
  47. output := c.RunDockerCmd("exec", "compose-e2e-volume-nginx2-1", "cat", "/run/secrets/mysecret").Stdout()
  48. assert.Assert(t, strings.Contains(output, `Hello from Nginx container`), output)
  49. })
  50. t.Run("check container bind-mounts specs", func(t *testing.T) {
  51. res := c.RunDockerCmd("inspect", "compose-e2e-volume-nginx-1", "--format", "{{ json .Mounts }}")
  52. output := res.Stdout()
  53. // nolint
  54. assert.Assert(t, strings.Contains(output, `"Type":"bind"`))
  55. assert.Assert(t, strings.Contains(output, `"Destination":"/usr/share/nginx/html"`))
  56. })
  57. t.Run("should inherit anonymous volumes", func(t *testing.T) {
  58. c.RunDockerOrExitError("exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  59. c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "-d")
  60. c.RunDockerOrExitError("exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  61. })
  62. t.Run("should renew anonymous volumes", func(t *testing.T) {
  63. c.RunDockerOrExitError("exec", "compose-e2e-volume-nginx2-1", "touch", "/usr/src/app/node_modules/test")
  64. c.RunDockerOrExitError("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "--force-recreate", "--renew-anon-volumes", "-d")
  65. c.RunDockerOrExitError("exec", "compose-e2e-volume-nginx2-1", "ls", "/usr/src/app/node_modules/test")
  66. })
  67. t.Run("cleanup volume project", func(t *testing.T) {
  68. c.RunDockerCmd("compose", "--project-name", projectName, "down", "--volumes")
  69. res := c.RunDockerCmd("volume", "ls")
  70. assert.Assert(t, !strings.Contains(res.Stdout(), projectName+"_staticVol"))
  71. assert.Assert(t, !strings.Contains(res.Stdout(), "myvolume"))
  72. })
  73. }