forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_cli_save_load_test.go
259 lines (198 loc) · 9.21 KB
/
docker_cli_save_load_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
// save a repo and try to load it using stdout
func TestSaveAndLoadRepoStdout(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
cleanedContainerID := stripTrailingCharacters(out)
repoName := "foobar-save-load-test"
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
out, _, err = runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
out, _, err = runCommandWithOutput(commitCmd)
errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
saveCmdTemplate := `%v save %v > /tmp/foobar-save-load-test.tar`
saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
deleteImages(repoName)
loadCmdFinal := `cat /tmp/foobar-save-load-test.tar | docker load`
loadCmd := exec.Command("bash", "-c", loadCmdFinal)
out, _, err = runCommandWithOutput(loadCmd)
errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
if before != after {
t.Fatalf("inspect is not the same after a save / load")
}
deleteContainer(cleanedContainerID)
deleteImages(repoName)
os.Remove("/tmp/foobar-save-load-test.tar")
logDone("save - save a repo using stdout")
logDone("load - load a repo using stdout")
}
func TestSaveSingleTag(t *testing.T) {
repoName := "foobar-save-single-tag-test"
tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
tagCmd := exec.Command("bash", "-c", tagCmdFinal)
out, _, err := runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
idCmd := exec.Command("bash", "-c", idCmdFinal)
out, _, err = runCommandWithOutput(idCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
cleanedImageID := stripTrailingCharacters(out)
saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo with image ID and 'repositories' file: %v %v", out, err))
deleteImages(repoName)
logDone("save - save a specific image:tag")
}
func TestSaveImageId(t *testing.T) {
repoName := "foobar-save-image-id-test"
tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
tagCmd := exec.Command("bash", "-c", tagCmdFinal)
out, _, err := runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
out, _, err = runCommandWithOutput(idLongCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
cleanedLongImageID := stripTrailingCharacters(out)
idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
out, _, err = runCommandWithOutput(idShortCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo short ID: %v %v", out, err))
cleanedShortImageID := stripTrailingCharacters(out)
saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo with image ID: %v %v", out, err))
deleteImages(repoName)
logDone("save - save a image by ID")
}
// save a repo and try to load it using flags
func TestSaveAndLoadRepoFlags(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("failed to create a container: %v %v", out, err))
cleanedContainerID := stripTrailingCharacters(out)
repoName := "foobar-save-load-test"
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
out, _, err = runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("output should've been a container id: %v %v", cleanedContainerID, err))
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, repoName)
out, _, err = runCommandWithOutput(commitCmd)
errorOut(err, t, fmt.Sprintf("failed to commit container: %v %v", out, err))
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("the repo should exist before saving it: %v %v", before, err))
saveCmdTemplate := `%v save -o /tmp/foobar-save-load-test.tar %v`
saveCmdFinal := fmt.Sprintf(saveCmdTemplate, dockerBinary, repoName)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo: %v %v", out, err))
deleteImages(repoName)
loadCmdFinal := `docker load -i /tmp/foobar-save-load-test.tar`
loadCmd := exec.Command("bash", "-c", loadCmdFinal)
out, _, err = runCommandWithOutput(loadCmd)
errorOut(err, t, fmt.Sprintf("failed to load repo: %v %v", out, err))
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("the repo should exist after loading it: %v %v", after, err))
if before != after {
t.Fatalf("inspect is not the same after a save / load")
}
deleteContainer(cleanedContainerID)
deleteImages(repoName)
os.Remove("/tmp/foobar-save-load-test.tar")
logDone("save - save a repo using -o")
logDone("load - load a repo using -i")
}
func TestSaveMultipleNames(t *testing.T) {
repoName := "foobar-save-multi-name-test"
// Make one image
tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v-one:latest", dockerBinary, repoName)
tagCmd := exec.Command("bash", "-c", tagCmdFinal)
out, _, err := runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
// Make two images
tagCmdFinal = fmt.Sprintf("%v tag scratch:latest %v-two:latest", dockerBinary, repoName)
tagCmd = exec.Command("bash", "-c", tagCmdFinal)
out, _, err = runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
saveCmdFinal := fmt.Sprintf("%v save %v-one %v-two:latest | tar xO repositories | grep -q -E '(-one|-two)'", dockerBinary, repoName, repoName)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save multiple repos: %v %v", out, err))
deleteImages(repoName)
logDone("save - save by multiple names")
}
// Issue #6722 #5892 ensure directories are included in changes
func TestSaveDirectoryPermissions(t *testing.T) {
layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
name := "save-directory-permissions"
tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
os.Mkdir(extractionDirectory, 0777)
if err != nil {
t.Errorf("failed to create temporary directory: %s", err)
}
defer os.RemoveAll(tmpDir)
defer deleteImages(name)
_, err = buildImage(name,
`FROM busybox
RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
true)
if err != nil {
t.Fatal(err)
}
saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err := runCommandWithOutput(saveCmd)
if err != nil {
t.Errorf("failed to save and extract image: %s", out)
}
dirs, err := ioutil.ReadDir(extractionDirectory)
if err != nil {
t.Errorf("failed to get a listing of the layer directories: %s", err)
}
found := false
for _, entry := range dirs {
if entry.IsDir() {
layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
f, err := os.Open(layerPath)
if err != nil {
t.Fatalf("failed to open %s: %s", layerPath, err)
}
entries, err := ListTar(f)
if err != nil {
t.Fatalf("encountered error while listing tar entries: %s", err)
}
if reflect.DeepEqual(entries, layerEntries) {
found = true
}
}
}
if !found {
t.Fatalf("failed to find the layer with the right content listing")
}
logDone("save - ensure directories exist in exported layers")
}