(t *testing.T)
| 26 | ) |
| 27 | |
| 28 | func TestCopy(t *testing.T) { |
| 29 | c := NewParallelCLI(t) |
| 30 | |
| 31 | const projectName = "copy_e2e" |
| 32 | |
| 33 | t.Cleanup(func() { |
| 34 | c.RunDockerComposeCmd(t, "-f", "./fixtures/cp-test/compose.yaml", "--project-name", projectName, "down") |
| 35 | |
| 36 | os.Remove("./fixtures/cp-test/from-default.txt") //nolint:errcheck |
| 37 | os.Remove("./fixtures/cp-test/from-indexed.txt") //nolint:errcheck |
| 38 | os.RemoveAll("./fixtures/cp-test/cp-folder2") //nolint:errcheck |
| 39 | }) |
| 40 | |
| 41 | t.Run("start service", func(t *testing.T) { |
| 42 | c.RunDockerComposeCmd(t, "-f", "./fixtures/cp-test/compose.yaml", "--project-name", projectName, "up", |
| 43 | "--scale", "nginx=5", "-d") |
| 44 | }) |
| 45 | |
| 46 | t.Run("make sure service is running", func(t *testing.T) { |
| 47 | res := c.RunDockerComposeCmd(t, "-p", projectName, "ps") |
| 48 | assertServiceStatus(t, projectName, "nginx", "Up", res.Stdout()) |
| 49 | }) |
| 50 | |
| 51 | t.Run("copy to container copies the file to the all containers by default", func(t *testing.T) { |
| 52 | res := c.RunDockerComposeCmd(t, "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", |
| 53 | "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/default.txt") |
| 54 | res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 55 | |
| 56 | output := c.RunDockerCmd(t, "exec", projectName+"-nginx-1", "cat", "/tmp/default.txt").Stdout() |
| 57 | assert.Assert(t, strings.Contains(output, `hello world`), output) |
| 58 | |
| 59 | output = c.RunDockerCmd(t, "exec", projectName+"-nginx-2", "cat", "/tmp/default.txt").Stdout() |
| 60 | assert.Assert(t, strings.Contains(output, `hello world`), output) |
| 61 | |
| 62 | output = c.RunDockerCmd(t, "exec", projectName+"-nginx-3", "cat", "/tmp/default.txt").Stdout() |
| 63 | assert.Assert(t, strings.Contains(output, `hello world`), output) |
| 64 | }) |
| 65 | |
| 66 | t.Run("copy to container with a given index copies the file to the given container", func(t *testing.T) { |
| 67 | res := c.RunDockerComposeCmd(t, "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", "--index=3", |
| 68 | "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/indexed.txt") |
| 69 | res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 70 | |
| 71 | output := c.RunDockerCmd(t, "exec", projectName+"-nginx-3", "cat", "/tmp/indexed.txt").Stdout() |
| 72 | assert.Assert(t, strings.Contains(output, `hello world`), output) |
| 73 | |
| 74 | res = c.RunDockerOrExitError(t, "exec", projectName+"-nginx-2", "cat", "/tmp/indexed.txt") |
| 75 | res.Assert(t, icmd.Expected{ExitCode: 1}) |
| 76 | }) |
| 77 | |
| 78 | t.Run("copy from a container copies the file to the host from the first container by default", func(t *testing.T) { |
| 79 | res := c.RunDockerComposeCmd(t, "-f", "./fixtures/cp-test/compose.yaml", "-p", projectName, "cp", |
| 80 | "nginx:/tmp/default.txt", "./fixtures/cp-test/from-default.txt") |
| 81 | res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 82 | |
| 83 | data, err := os.ReadFile("./fixtures/cp-test/from-default.txt") |
| 84 | assert.NilError(t, err) |
| 85 | assert.Equal(t, `hello world`, string(data)) |
nothing calls this directly
no test coverage detected