-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_cli_commit_test.go
193 lines (149 loc) · 6.1 KB
/
docker_cli_commit_test.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"strings"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
func (s *DockerSuite) TestCommitAfterContainerIsDone(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
out, _ = dockerCmd(c, "commit", cleanedContainerID)
cleanedImageID := strings.TrimSpace(out)
dockerCmd(c, "inspect", cleanedImageID)
}
func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
out, _ = dockerCmd(c, "commit", "-p=false", cleanedContainerID)
cleanedImageID := strings.TrimSpace(out)
dockerCmd(c, "inspect", cleanedImageID)
}
//test commit a paused container should not unpause it after commit
func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
testRequires(c, DaemonIsLinux)
defer unpauseAllContainers()
out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "pause", cleanedContainerID)
out, _ = dockerCmd(c, "commit", cleanedContainerID)
out, err := inspectField(cleanedContainerID, "State.Paused")
c.Assert(err, checker.IsNil, check.Commentf("%s", out))
// commit should not unpause a paused container
c.Assert(out, checker.Contains, "true")
}
func (s *DockerSuite) TestCommitNewFile(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
imageID, _ := dockerCmd(c, "commit", "commiter")
imageID = strings.TrimSpace(imageID)
out, _ := dockerCmd(c, "run", imageID, "cat", "/foo")
actual := strings.TrimSpace(out)
c.Assert(actual, checker.Equals, "koye")
}
func (s *DockerSuite) TestCommitHardlink(c *check.C) {
testRequires(c, DaemonIsLinux)
firstOutput, _ := dockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
inode := chunks[0]
chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
imageID, _ := dockerCmd(c, "commit", "hardlinks", "hardlinks")
imageID = strings.TrimSpace(imageID)
secondOutput, _ := dockerCmd(c, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
inode = chunks[0]
chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
c.Assert(chunks[1], checker.Contains, chunks[0], check.Commentf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:]))
}
func (s *DockerSuite) TestCommitTTY(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
imageID, _ := dockerCmd(c, "commit", "tty", "ttytest")
imageID = strings.TrimSpace(imageID)
dockerCmd(c, "run", "ttytest", "/bin/ls")
}
func (s *DockerSuite) TestCommitWithHostBindMount(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
imageID, _ := dockerCmd(c, "commit", "bind-commit", "bindtest")
imageID = strings.TrimSpace(imageID)
dockerCmd(c, "run", "bindtest", "true")
}
func (s *DockerSuite) TestCommitChange(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "--name", "test", "busybox", "true")
imageID, _ := dockerCmd(c, "commit",
"--change", "EXPOSE 8080",
"--change", "ENV DEBUG true",
"--change", "ENV test 1",
"--change", "ENV PATH /foo",
"--change", "LABEL foo bar",
"--change", "CMD [\"/bin/sh\"]",
"--change", "WORKDIR /opt",
"--change", "ENTRYPOINT [\"/bin/sh\"]",
"--change", "USER testuser",
"--change", "VOLUME /var/lib/docker",
"--change", "ONBUILD /usr/local/bin/python-build --dir /app/src",
"test", "test-commit")
imageID = strings.TrimSpace(imageID)
expected := map[string]string{
"Config.ExposedPorts": "map[8080/tcp:{}]",
"Config.Env": "[DEBUG=true test=1 PATH=/foo]",
"Config.Labels": "map[foo:bar]",
"Config.Cmd": "{[/bin/sh]}",
"Config.WorkingDir": "/opt",
"Config.Entrypoint": "{[/bin/sh]}",
"Config.User": "testuser",
"Config.Volumes": "map[/var/lib/docker:{}]",
"Config.OnBuild": "[/usr/local/bin/python-build --dir /app/src]",
}
for conf, value := range expected {
res, err := inspectField(imageID, conf)
c.Assert(err, checker.IsNil, check.Commentf("%s('%s')", conf, res))
if res != value {
c.Errorf("%s('%s'), expected %s", conf, res, value)
}
}
}
// TODO: commit --run is deprecated, remove this once --run is removed
func (s *DockerSuite) TestCommitMergeConfigRun(c *check.C) {
testRequires(c, DaemonIsLinux)
name := "commit-test"
out, _ := dockerCmd(c, "run", "-d", "-e=FOO=bar", "busybox", "/bin/sh", "-c", "echo testing > /tmp/foo")
id := strings.TrimSpace(out)
dockerCmd(c, "commit", `--run={"Cmd": ["cat", "/tmp/foo"]}`, id, "commit-test")
out, _ = dockerCmd(c, "run", "--name", name, "commit-test")
//run config in committed container was not merged
c.Assert(strings.TrimSpace(out), checker.Equals, "testing")
type cfg struct {
Env []string
Cmd []string
}
config1 := cfg{}
err := inspectFieldAndMarshall(id, "Config", &config1)
c.Assert(err, checker.IsNil)
config2 := cfg{}
err = inspectFieldAndMarshall(name, "Config", &config2)
c.Assert(err, checker.IsNil)
// Env has at least PATH loaded as well here, so let's just grab the FOO one
var env1, env2 string
for _, e := range config1.Env {
if strings.HasPrefix(e, "FOO") {
env1 = e
break
}
}
for _, e := range config2.Env {
if strings.HasPrefix(e, "FOO") {
env2 = e
break
}
}
if len(config1.Env) != len(config2.Env) || env1 != env2 && env2 != "" {
c.Fatalf("expected envs to match: %v - %v", config1.Env, config2.Env)
}
}