github.com/quay/claircore@v1.5.28/whiteout/scanner_test.go (about) 1 package whiteout 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/quay/zlog" 9 10 "github.com/quay/claircore" 11 "github.com/quay/claircore/test" 12 ) 13 14 var testScanner = &Scanner{} 15 16 // CheckCount is a helper to check the number of whiteouts returned from the 17 // layer in "name". 18 func checkCount(t testing.TB, name string, ct int) { 19 t.Helper() 20 ctx := zlog.Test(context.Background(), t) 21 lf, err := os.Open(name) 22 if err != nil { 23 t.Fatal(err) 24 } 25 t.Cleanup(func() { 26 if err := lf.Close(); err != nil { 27 t.Error(err) 28 } 29 }) 30 var l claircore.Layer 31 err = l.Init(ctx, &claircore.LayerDescription{ 32 Digest: `sha256:25fd87072f39aaebd1ee24dca825e61d9f5a0f87966c01551d31a4d8d79d37d8`, 33 URI: "file:///dev/null", 34 MediaType: test.MediaType, 35 Headers: make(map[string][]string), 36 }, lf) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 files, err := testScanner.Scan(ctx, &l) 42 if err != nil { 43 t.Error(err) 44 } 45 if got, want := len(files), ct; got != want { 46 t.Errorf("checking length, got: %d, want: %d", got, want) 47 } 48 for _, f := range files { 49 t.Logf("got whiteout file %q", f.Path) 50 } 51 } 52 53 func TestWhiteout(t *testing.T) { 54 t.Run("Basic", func(t *testing.T) { 55 t.Parallel() 56 // files in whiteout.layer 57 // etc/ 58 // etc/.wh.profile 59 checkCount(t, `testdata/whiteout.layer`, 1) 60 }) 61 t.Run("Advanced", func(t *testing.T) { 62 t.Parallel() 63 // files in whiteout_advanced.layer 64 // a/ 65 // a/.wh.a_file.txt 66 // .wh.a_root_file.txt 67 // .wh.b 68 checkCount(t, `testdata/whiteout_advanced.layer`, 3) 69 }) 70 t.Run("Opaque", func(t *testing.T) { 71 t.Parallel() 72 // files in whiteout_opaque.layer 73 // a/ 74 // a/.wh..wh..opq 75 // b/ 76 // b/not.a.wh..wh..opq 77 checkCount(t, `testdata/whiteout_opaque.layer`, 1) 78 }) 79 }