cp_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "os"
  16. "strings"
  17. "testing"
  18. "gotest.tools/v3/assert"
  19. "gotest.tools/v3/icmd"
  20. )
  21. func TestCopy(t *testing.T) {
  22. c := NewParallelE2eCLI(t, binDir)
  23. const projectName = "copy_e2e"
  24. t.Cleanup(func() {
  25. c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "--project-name", projectName, "down")
  26. os.Remove("./fixtures/cp-test/from-default.txt") //nolint:errcheck
  27. os.Remove("./fixtures/cp-test/from-indexed.txt") //nolint:errcheck
  28. os.RemoveAll("./fixtures/cp-test/cp-folder2") //nolint:errcheck
  29. })
  30. t.Run("start service", func(t *testing.T) {
  31. c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "--project-name", projectName, "up", "--scale", "nginx=5", "-d")
  32. })
  33. t.Run("make sure service is running", func(t *testing.T) {
  34. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  35. res.Assert(t, icmd.Expected{Out: `nginx running`})
  36. })
  37. t.Run("copy to container copies the file to the first container by default", func(t *testing.T) {
  38. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/default.txt")
  39. res.Assert(t, icmd.Expected{ExitCode: 0})
  40. output := c.RunDockerCmd("exec", projectName+"-nginx-1", "cat", "/tmp/default.txt").Stdout()
  41. assert.Assert(t, strings.Contains(output, `hello world`), output)
  42. res = c.RunDockerOrExitError("exec", projectName+"_nginx_2", "cat", "/tmp/default.txt")
  43. res.Assert(t, icmd.Expected{ExitCode: 1})
  44. })
  45. t.Run("copy to container with a given index copies the file to the given container", func(t *testing.T) {
  46. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "--index=3", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/indexed.txt")
  47. res.Assert(t, icmd.Expected{ExitCode: 0})
  48. output := c.RunDockerCmd("exec", projectName+"-nginx-3", "cat", "/tmp/indexed.txt").Stdout()
  49. assert.Assert(t, strings.Contains(output, `hello world`), output)
  50. res = c.RunDockerOrExitError("exec", projectName+"-nginx-2", "cat", "/tmp/indexed.txt")
  51. res.Assert(t, icmd.Expected{ExitCode: 1})
  52. })
  53. t.Run("copy to container with the all flag copies the file to all containers", func(t *testing.T) {
  54. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "--all", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/all.txt")
  55. res.Assert(t, icmd.Expected{ExitCode: 0})
  56. output := c.RunDockerCmd("exec", projectName+"-nginx-1", "cat", "/tmp/all.txt").Stdout()
  57. assert.Assert(t, strings.Contains(output, `hello world`), output)
  58. output = c.RunDockerCmd("exec", projectName+"-nginx-2", "cat", "/tmp/all.txt").Stdout()
  59. assert.Assert(t, strings.Contains(output, `hello world`), output)
  60. output = c.RunDockerCmd("exec", projectName+"-nginx-3", "cat", "/tmp/all.txt").Stdout()
  61. assert.Assert(t, strings.Contains(output, `hello world`), output)
  62. })
  63. t.Run("copy from a container copies the file to the host from the first container by default", func(t *testing.T) {
  64. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "nginx:/tmp/default.txt", "./fixtures/cp-test/from-default.txt")
  65. res.Assert(t, icmd.Expected{ExitCode: 0})
  66. data, err := os.ReadFile("./fixtures/cp-test/from-default.txt")
  67. assert.NilError(t, err)
  68. assert.Equal(t, `hello world`, string(data))
  69. })
  70. t.Run("copy from a container with a given index copies the file to host", func(t *testing.T) {
  71. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "--index=3", "nginx:/tmp/indexed.txt", "./fixtures/cp-test/from-indexed.txt")
  72. res.Assert(t, icmd.Expected{ExitCode: 0})
  73. data, err := os.ReadFile("./fixtures/cp-test/from-indexed.txt")
  74. assert.NilError(t, err)
  75. assert.Equal(t, `hello world`, string(data))
  76. })
  77. t.Run("copy to and from a container also work with folder", func(t *testing.T) {
  78. res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "./fixtures/cp-test/cp-folder", "nginx:/tmp")
  79. res.Assert(t, icmd.Expected{ExitCode: 0})
  80. output := c.RunDockerCmd("exec", projectName+"-nginx-1", "cat", "/tmp/cp-folder/cp-me.txt").Stdout()
  81. assert.Assert(t, strings.Contains(output, `hello world from folder`), output)
  82. res = c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "nginx:/tmp/cp-folder", "./fixtures/cp-test/cp-folder2")
  83. res.Assert(t, icmd.Expected{ExitCode: 0})
  84. data, err := os.ReadFile("./fixtures/cp-test/cp-folder2/cp-me.txt")
  85. assert.NilError(t, err)
  86. assert.Equal(t, `hello world from folder`, string(data))
  87. })
  88. }