Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 42cd3da

Browse files
committed
add tests
Signed-off-by: Julien Tant <[email protected]>
1 parent c2f2196 commit 42cd3da

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

local/e2e/compose/cp_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"os"
21+
"strings"
22+
"testing"
23+
24+
"gotest.tools/v3/assert"
25+
"gotest.tools/v3/icmd"
26+
27+
. "github.com/docker/compose-cli/utils/e2e"
28+
)
29+
30+
func TestCopy(t *testing.T) {
31+
c := NewParallelE2eCLI(t, binDir)
32+
33+
const projectName = "copy_e2e"
34+
35+
t.Cleanup(func() {
36+
c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "--project-name", projectName, "down")
37+
38+
os.Remove("./fixtures/cp-test/from-default.txt")
39+
os.Remove("./fixtures/cp-test/from-indexed.txt")
40+
os.RemoveAll("./fixtures/cp-test/cp-folder2")
41+
})
42+
43+
t.Run("start service", func(t *testing.T) {
44+
c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "--project-name", projectName, "up", "--scale", "nginx=5", "-d")
45+
})
46+
47+
t.Run("make sure service is running", func(t *testing.T) {
48+
res := c.RunDockerCmd("compose", "-p", projectName, "ps")
49+
res.Assert(t, icmd.Expected{Out: `nginx running`})
50+
})
51+
52+
t.Run("copy to container copies the file to the first container by default", func(t *testing.T) {
53+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/default.txt")
54+
res.Assert(t, icmd.Expected{ExitCode: 0})
55+
56+
output := c.RunDockerCmd("exec", projectName+"_nginx_1", "cat", "/tmp/default.txt").Stdout()
57+
assert.Assert(t, strings.Contains(output, `hello world`), output)
58+
59+
res = c.RunDockerOrExitError("exec", projectName+"_nginx_2", "cat", "/tmp/default.txt")
60+
res.Assert(t, icmd.Expected{ExitCode: 1})
61+
})
62+
63+
t.Run("copy to container with a given index copies the file to the given container", func(t *testing.T) {
64+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "--index=3", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/indexed.txt")
65+
res.Assert(t, icmd.Expected{ExitCode: 0})
66+
67+
output := c.RunDockerCmd("exec", projectName+"_nginx_3", "cat", "/tmp/indexed.txt").Stdout()
68+
assert.Assert(t, strings.Contains(output, `hello world`), output)
69+
70+
res = c.RunDockerOrExitError("exec", projectName+"_nginx_2", "cat", "/tmp/indexed.txt")
71+
res.Assert(t, icmd.Expected{ExitCode: 1})
72+
})
73+
74+
t.Run("copy to container with the all flag copies the file to all containers", func(t *testing.T) {
75+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "--all", "./fixtures/cp-test/cp-me.txt", "nginx:/tmp/all.txt")
76+
res.Assert(t, icmd.Expected{ExitCode: 0})
77+
78+
output := c.RunDockerCmd("exec", projectName+"_nginx_1", "cat", "/tmp/all.txt").Stdout()
79+
assert.Assert(t, strings.Contains(output, `hello world`), output)
80+
81+
output = c.RunDockerCmd("exec", projectName+"_nginx_2", "cat", "/tmp/all.txt").Stdout()
82+
assert.Assert(t, strings.Contains(output, `hello world`), output)
83+
84+
output = c.RunDockerCmd("exec", projectName+"_nginx_3", "cat", "/tmp/all.txt").Stdout()
85+
assert.Assert(t, strings.Contains(output, `hello world`), output)
86+
})
87+
88+
t.Run("copy from a container copies the file to the host from the first container by default", func(t *testing.T) {
89+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "nginx:/tmp/default.txt", "./fixtures/cp-test/from-default.txt")
90+
res.Assert(t, icmd.Expected{ExitCode: 0})
91+
92+
data, err := os.ReadFile("./fixtures/cp-test/from-default.txt")
93+
assert.NilError(t, err)
94+
assert.Equal(t, `hello world`, string(data))
95+
})
96+
97+
t.Run("copy from a container with a given index copies the file to host", func(t *testing.T) {
98+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "--index=3", "nginx:/tmp/indexed.txt", "./fixtures/cp-test/from-indexed.txt")
99+
res.Assert(t, icmd.Expected{ExitCode: 0})
100+
101+
data, err := os.ReadFile("./fixtures/cp-test/from-indexed.txt")
102+
assert.NilError(t, err)
103+
assert.Equal(t, `hello world`, string(data))
104+
})
105+
106+
t.Run("copy to and from a container also work with folder", func(t *testing.T) {
107+
res := c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "./fixtures/cp-test/cp-folder", "nginx:/tmp")
108+
res.Assert(t, icmd.Expected{ExitCode: 0})
109+
110+
output := c.RunDockerCmd("exec", projectName+"_nginx_1", "cat", "/tmp/cp-folder/cp-me.txt").Stdout()
111+
assert.Assert(t, strings.Contains(output, `hello world from folder`), output)
112+
113+
res = c.RunDockerCmd("compose", "-f", "./fixtures/cp-test/docker-compose.yml", "-p", projectName, "cp", "nginx:/tmp/cp-folder", "./fixtures/cp-test/cp-folder2")
114+
res.Assert(t, icmd.Expected{ExitCode: 0})
115+
116+
data, err := os.ReadFile("./fixtures/cp-test/cp-folder2/cp-me.txt")
117+
assert.NilError(t, err)
118+
assert.Equal(t, `hello world from folder`, string(data))
119+
})
120+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world from folder
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
nginx:
3+
image: nginx:alpine

0 commit comments

Comments
 (0)