Debian Codegraph @

dockerclient/archive_test.go

raw
1 package dockerclient
2
3 import (
4 "archive/tar"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "reflect"
11 "sort"
12 "testing"
13
14 "go.podman.io/storage/pkg/archive"
15 )
16
17 type testDirectoryCheck map[string]bool
18
19 func (c testDirectoryCheck) IsDirectory(path string) (bool, error) {
20 if c == nil {
21 return false, nil
22 }
23
24 isDir, ok := c[path]
25 if !ok {
26 return false, fmt.Errorf("no path defined for %s", path)
27 }
28 return isDir, nil
29 }
30
31 type archiveGenerator struct {
32 Headers []*tar.Header
33 }
34
35 func newArchiveGenerator() *archiveGenerator {
36 return &archiveGenerator{}
37 }
38
39 func typeflagAsString(b byte) string {
40 switch b {
41 case tar.TypeDir:
42 return "dir"
43 case tar.TypeReg:
44 return "reg"
45 case tar.TypeRegA:
46 return "rega"
47 default:
48 return fmt.Sprintf("%d", b)
49 }
50 }
51
52 func (g *archiveGenerator) String() string {
53 s := "["
54 for i := range g.Headers {
55 if i > 0 {
56 s += ", "
57 }
58 s += fmt.Sprintf("{%q: %v}", g.Headers[i].Name, typeflagAsString(g.Headers[i].Typeflag))
59 }
60 s += "]"
61 return s
62 }
63
64 func (g *archiveGenerator) File(name string) *archiveGenerator {
65 g.Headers = append(g.Headers, &tar.Header{Name: name, Typeflag: tar.TypeReg, Size: 1})
66 return g
67 }
68
69 func (g *archiveGenerator) Dir(name string) *archiveGenerator {
70 g.Headers = append(g.Headers, &tar.Header{Name: name, Typeflag: tar.TypeDir})
71 return g
72 }
73
74 func (g *archiveGenerator) Reader() io.Reader {
75 pr, pw := io.Pipe()
76 go func() {
77 err := func() error {
78 w := tar.NewWriter(pw)
79 for _, h := range g.Headers {
80 if err := w.WriteHeader(h); err != nil {
81 return err
82 }
83 if h.Typeflag&tar.TypeDir == tar.TypeDir {
84 continue
85 }
86 for i := int64(0); i < h.Size; i++ {
87 if _, err := w.Write([]byte{byte(i)}); err != nil {
88 return err
89 }
90 }
91 }
92 return w.Close()
93 }()
94 pw.CloseWithError(err)
95 }()
96 return pr
97 }
98
99 func Test_archiveFromFile(t *testing.T) {
100 f, err := os.CreateTemp("", "test-tar")
101 if err != nil {
102 t.Fatal(err)
103 }
104 rc, err := archive.TarWithOptions("testdata/dir", &archive.TarOptions{})
105 if err != nil {
106 t.Fatal(err)
107 }
108 if _, err := io.Copy(f, rc); err != nil {
109 t.Fatal(err)
110 }
111 if err := f.Close(); err != nil {
112 t.Fatal(err)
113 }
114 defer os.Remove(f.Name())
115
116 testArchive := f.Name()
117 testCases := []struct {
118 file string
119 gen *archiveGenerator
120 src string
121 closeErr error
122 dst string
123 excludes []string
124 expect []string
125 check map[string]bool
126 }{
127 {
128 file: testArchive,
129 src: "/*",
130 dst: "test",
131 expect: []string{
132 "test/Dockerfile",
133 "test/file",
134 "test/subdir",
135 "test/subdir/file2",
136 },
137 },
138 {
139 file: testArchive,
140 src: ".",
141 dst: "test",
142 expect: []string{
143 "test/Dockerfile",
144 "test/file",
145 "test/subdir",
146 "test/subdir/file2",
147 },
148 },
149 {
150 file: testArchive,
151 src: "fil?",
152 dst: "test",
153 expect: []string{
154 "test/file",
155 },
156 },
157 {
158 file: testArchive,
159 src: "fil?",
160 dst: "",
161 expect: []string{
162 "file",
163 },
164 },
165 {
166 file: testArchive,
167 src: "subdir",
168 dst: "",
169 expect: []string{
170 "file2",
171 },
172 },
173 {
174 file: testArchive,
175 src: "subdir/",
176 dst: "",
177 expect: []string{
178 "file2",
179 },
180 },
181 {
182 file: testArchive,
183 src: "subdir/",
184 dst: "test/",
185 expect: []string{
186 "test",
187 "test/file2",
188 },
189 },
190 {
191 file: testArchive,
192 src: "subdir/file?",
193 dst: "test/",
194 expect: []string{
195 "test/file2",
196 },
197 },
198 {
199 file: testArchive,
200 src: "subdi?",
201 dst: "test",
202 expect: []string{
203 "test/subdir",
204 "test/subdir/file2",
205 },
206 },
207 {
208 file: testArchive,
209 src: "subdi?",
210 dst: "test/",
211 expect: []string{
212 "test/subdir",
213 "test/subdir/file2",
214 },
215 },
216 {
217 file: testArchive,
218 src: "subdi?",
219 dst: "test/",
220 excludes: []string{"**/file*"},
221 expect: []string{
222 "test/subdir",
223 },
224 },
225 {
226 file: testArchive,
227 src: ".",
228 dst: "",
229 excludes: []string{"unknown"},
230 expect: []string{
231 "Dockerfile",
232 "file",
233 "subdir",
234 "subdir/file2",
235 },
236 },
237 {
238 file: testArchive,
239 src: ".",
240 dst: "",
241 excludes: []string{"subdir"},
242 expect: []string{
243 "Dockerfile",
244 "file",
245 },
246 },
247 {
248 file: testArchive,
249 src: ".",
250 dst: "",
251 excludes: []string{"file"},
252 expect: []string{
253 "Dockerfile",
254 "subdir",
255 "subdir/file2",
256 },
257 },
258 {
259 file: testArchive,
260 src: ".",
261 dst: "",
262 excludes: []string{"*/file2"},
263 expect: []string{
264 "Dockerfile",
265 "file",
266 "subdir",
267 },
268 },
269 {
270 file: testArchive,
271 src: "subdir/no-such-file",
272 closeErr: os.ErrNotExist,
273 },
274 }
275 for i := range testCases {
276 testCase := testCases[i]
277 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
278 r, c, err := archiveFromFile(
279 testCase.file,
280 testCase.src,
281 testCase.dst,
282 testCase.excludes,
283 testDirectoryCheck(testCase.check),
284 )
285 if err != nil {
286 t.Fatal(err)
287 }
288 tr := tar.NewReader(r)
289 var found []string
290 for {
291 h, err := tr.Next()
292 if err != nil {
293 if err == io.EOF {
294 break
295 }
296 t.Fatal(err)
297 }
298 found = append(found, h.Name)
299 }
300 closeErr := c.Close()
301 if !errors.Is(closeErr, testCase.closeErr) {
302 t.Fatalf("expected error %v, got %v", testCase.closeErr, closeErr)
303 }
304 sort.Strings(found)
305 if !reflect.DeepEqual(testCase.expect, found) {
306 t.Errorf("unexpected files:\n%v\n%v", testCase.expect, found)
307 }
308 })
309 }
310 }
311
312 func Test_archiveFromContainer(t *testing.T) {
313 testCases := []struct {
314 gen *archiveGenerator
315 src string
316 closeErr error
317 dst string
318 excludes []string
319 expect []string
320 path string
321 check map[string]bool
322 }{
323 {
324 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
325 src: "/*",
326 dst: "test",
327 check: map[string]bool{"test": true},
328 path: "/",
329 expect: []string{
330 "test/file",
331 "test/test",
332 "test/test/file2",
333 },
334 },
335 {
336 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
337 src: "/",
338 dst: "test",
339 path: "/",
340 expect: []string{
341 "test/file",
342 "test/test",
343 "test/test/file2",
344 },
345 },
346 {
347 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
348 src: ".",
349 dst: "test",
350 path: ".",
351 expect: []string{
352 "test/file",
353 "test/test",
354 "test/test/file2",
355 },
356 },
357 {
358 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
359 src: ".",
360 dst: "test/",
361 path: ".",
362 expect: []string{
363 "test/file",
364 "test/test",
365 "test/test/file2",
366 },
367 },
368 {
369 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
370 src: ".",
371 dst: "/test",
372 path: ".",
373 expect: []string{
374 "/test/file",
375 "/test/test",
376 "/test/test/file2",
377 },
378 },
379 {
380 gen: newArchiveGenerator().File("file").Dir("test").File("test/file2"),
381 src: ".",
382 dst: "/test/",
383 path: ".",
384 expect: []string{
385 "/test/file",
386 "/test/test",
387 "/test/test/file2",
388 },
389 },
390 {
391 gen: newArchiveGenerator().File("b/file").Dir("b/test").File("b/test/file2"),
392 src: "/a/b/",
393 dst: "/b",
394 path: "/a/b",
395 expect: []string{
396 "/b/file",
397 "/b/test",
398 "/b/test/file2",
399 },
400 },
401 {
402 gen: newArchiveGenerator().File("/b/file").Dir("/b/test").File("/b/test/file2"),
403 src: "/a/b/*",
404 dst: "/b",
405 check: map[string]bool{"/b": true},
406 path: "/a/b",
407 expect: []string{
408 "/b/file",
409 "/b/test",
410 "/b/test/file2",
411 },
412 },
413
414 // DownloadFromContainer returns tar archive paths prefixed with a slash when
415 // the base directory is the root
416 {
417 gen: newArchiveGenerator().File("/a").Dir("/b").File("/b/1"),
418 src: "/a",
419 dst: "/",
420 path: "/",
421 expect: []string{
422 "/a",
423 },
424 },
425 {
426 gen: newArchiveGenerator().File("/a").Dir("/b").File("/b/1"),
427 src: "/a",
428 dst: "/a",
429 path: "/",
430 expect: []string{
431 "/a",
432 },
433 },
434 {
435 gen: newArchiveGenerator().Dir("b/").File("b/1").File("b/2"),
436 src: "/a/b/",
437 dst: "/b/",
438 path: "/a/b",
439 expect: []string{
440 "/b",
441 "/b/1",
442 "/b/2",
443 },
444 },
445 {
446 gen: newArchiveGenerator().Dir("").File("b"),
447 src: "/a/b",
448 closeErr: os.ErrNotExist,
449 dst: "/a",
450 path: "/a",
451 expect: nil,
452 },
453 {
454 gen: newArchiveGenerator().File("b"),
455 src: "/a/b",
456 closeErr: os.ErrNotExist,
457 dst: "/a",
458 check: map[string]bool{"/a": true},
459 path: "/a",
460 expect: nil,
461 },
462 {
463 gen: newArchiveGenerator().Dir("a/").File("a/b"),
464 src: "/a/b",
465 dst: "/a",
466 path: "/a",
467 expect: []string{
468 "/a",
469 },
470 },
471 {
472 gen: newArchiveGenerator().Dir("./a").File("./a/b"),
473 src: "a",
474 dst: "/a",
475 path: ".",
476 expect: []string{"/a/b"},
477 },
478 {
479 gen: newArchiveGenerator().Dir("/a").File("/a/b"),
480 src: "/a/c",
481 path: "/a",
482 closeErr: os.ErrNotExist,
483 },
484 {
485 gen: newArchiveGenerator().Dir("/a").File("/a/b"),
486 src: "/a/c*",
487 path: "/a",
488 closeErr: os.ErrNotExist,
489 },
490 }
491 for i := range testCases {
492 testCase := testCases[i]
493 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
494 rc, path, err := archiveFromContainer(
495 testCase.gen.Reader(),
496 testCase.src,
497 testCase.dst,
498 testCase.excludes,
499 testDirectoryCheck(testCase.check),
500 func(pw *io.PipeWriter) {
501 _, err := io.Copy(pw, testCase.gen.Reader())
502 pw.CloseWithError(err)
503 },
504 false,
505 )
506 if err != nil {
507 t.Fatal(err)
508 }
509 if filepath.Clean(path) != testCase.path {
510 t.Errorf("unexpected path for root of archive: %q != expected value %q", filepath.Clean(path), testCase.path)
511 }
512 tr := tar.NewReader(rc)
513 var found []string
514 for {
515 h, err := tr.Next()
516 if err != nil {
517 if err == io.EOF {
518 break
519 }
520 t.Fatal(err)
521 }
522 found = append(found, h.Name)
523 }
524 closeErr := rc.Close()
525 if !errors.Is(closeErr, testCase.closeErr) {
526 t.Fatalf("expected error %v, got %v", testCase.closeErr, closeErr)
527 }
528 sort.Strings(found)
529 if !reflect.DeepEqual(testCase.expect, found) {
530 t.Errorf("from %q to %q with content: %v\nunexpected files:\nexpected: %v\nfound: %v", testCase.src, testCase.dst, testCase.gen, testCase.expect, found)
531 }
532 })
533 }
534 }