1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package main
import (
"bufio"
"compress/gzip"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"github.com/docker/docker/integration-cli/cli"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
type DockerCLIImportSuite struct {
ds *DockerSuite
}
func (s *DockerCLIImportSuite) TearDownTest(c *testing.T) {
s.ds.TearDownTest(c)
}
func (s *DockerCLIImportSuite) OnTimeout(c *testing.T) {
s.ds.OnTimeout(c)
}
func (s *DockerCLIImportSuite) TestImportDisplay(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
cleanedContainerID := strings.TrimSpace(out)
out, err := RunCommandPipelineWithOutput(
exec.Command(dockerBinary, "export", cleanedContainerID),
exec.Command(dockerBinary, "import", "-"),
)
assert.NilError(c, err)
assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
image := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "--rm", image, "true")
assert.Equal(c, out, "", "command output should've been nothing.")
}
func (s *DockerCLIImportSuite) TestImportBadURL(c *testing.T) {
out, _, err := dockerCmdWithError("import", "https://nosuchdomain.invalid/bad")
assert.Assert(c, err != nil, "import was supposed to fail but didn't")
// Depending on your system you can get either of these errors
if !strings.Contains(out, "dial tcp") &&
!strings.Contains(out, "ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header") &&
!strings.Contains(out, "Error processing tar file") {
c.Fatalf("expected an error msg but didn't get one.\nErr: %v\nOut: %v", err, out)
}
}
func (s *DockerCLIImportSuite) TestImportFile(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
temporaryFile, err := os.CreateTemp("", "exportImportTest")
assert.Assert(c, err == nil, "failed to create temporary file")
defer os.Remove(temporaryFile.Name())
icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "export", "test-import"},
Stdout: bufio.NewWriter(temporaryFile),
}).Assert(c, icmd.Success)
out, _ := dockerCmd(c, "import", temporaryFile.Name())
assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
image := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "--rm", image, "true")
assert.Equal(c, out, "", "command output should've been nothing.")
}
func (s *DockerCLIImportSuite) TestImportGzipped(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
temporaryFile, err := os.CreateTemp("", "exportImportTest")
assert.Assert(c, err == nil, "failed to create temporary file")
defer os.Remove(temporaryFile.Name())
w := gzip.NewWriter(temporaryFile)
icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "export", "test-import"},
Stdout: w,
}).Assert(c, icmd.Success)
assert.Assert(c, w.Close() == nil, "failed to close gzip writer")
temporaryFile.Close()
out, _ := dockerCmd(c, "import", temporaryFile.Name())
assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
image := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "--rm", image, "true")
assert.Equal(c, out, "", "command output should've been nothing.")
}
func (s *DockerCLIImportSuite) TestImportFileWithMessage(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
temporaryFile, err := os.CreateTemp("", "exportImportTest")
assert.Assert(c, err == nil, "failed to create temporary file")
defer os.Remove(temporaryFile.Name())
icmd.RunCmd(icmd.Cmd{
Command: []string{dockerBinary, "export", "test-import"},
Stdout: bufio.NewWriter(temporaryFile),
}).Assert(c, icmd.Success)
message := "Testing commit message"
out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
assert.Assert(c, strings.Count(out, "\n") == 1, "display is expected 1 '\\n' but didn't")
image := strings.TrimSpace(out)
out, _ = dockerCmd(c, "history", image)
split := strings.Split(out, "\n")
assert.Equal(c, len(split), 3, "expected 3 lines from image history")
r := regexp.MustCompile(`[\s]{2,}`)
split = r.Split(split[1], -1)
assert.Equal(c, message, split[3], "didn't get expected value in commit message")
out, _ = dockerCmd(c, "run", "--rm", image, "true")
assert.Equal(c, out, "", "command output should've been nothing")
}
func (s *DockerCLIImportSuite) TestImportFileNonExistentFile(c *testing.T) {
_, _, err := dockerCmdWithError("import", "example.com/myImage.tar")
assert.Assert(c, err != nil, "import non-existing file must failed")
}
func (s *DockerCLIImportSuite) TestImportWithQuotedChanges(c *testing.T) {
testRequires(c, DaemonIsLinux)
cli.DockerCmd(c, "run", "--name", "test-import", "busybox", "true")
temporaryFile, err := os.CreateTemp("", "exportImportTest")
assert.Assert(c, err == nil, "failed to create temporary file")
defer os.Remove(temporaryFile.Name())
cli.Docker(cli.Args("export", "test-import"), cli.WithStdout(bufio.NewWriter(temporaryFile))).Assert(c, icmd.Success)
result := cli.DockerCmd(c, "import", "-c", `ENTRYPOINT ["/bin/sh", "-c"]`, temporaryFile.Name())
image := strings.TrimSpace(result.Stdout())
result = cli.DockerCmd(c, "run", "--rm", image, "true")
result.Assert(c, icmd.Expected{Out: icmd.None})
}
|