github.com/quay/claircore@v1.5.28/rpm/packagescanner_test.go (about) 1 package rpm 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/quay/zlog" 11 12 "github.com/quay/claircore" 13 "github.com/quay/claircore/test" 14 "github.com/quay/claircore/test/rpmtest" 15 ) 16 17 func TestScan(t *testing.T) { 18 ctx := zlog.Test(context.Background(), t) 19 20 a := test.NewCachedArena(t) 21 t.Cleanup(func() { 22 if err := a.Close(ctx); err != nil { 23 t.Error(err) 24 } 25 }) 26 27 tt := []PackageTestcase{ 28 { 29 Name: "BDB", 30 ManifestFile: "bdb.rpm-manifest.json", 31 // Some forgotten CentOS layer. 32 Ref: test.LayerRef{ 33 Registry: "docker.io", 34 Name: "library/centos", 35 Digest: `sha256:729ec3a6ada3a6d26faca9b4779a037231f1762f759ef34c08bdd61bf52cd704`, 36 }, 37 }, 38 { 39 Name: "SQLite", 40 ManifestFile: "sqlite.rpm-manifest.json", 41 // Layer from registry.access.redhat.com/ubi9/ubi@sha256:f14f9eb6c5ec6ea9664b54ea8816462e11abc096954d59eac536b64873d908f2 42 Ref: test.LayerRef{ 43 Registry: "registry.access.redhat.com", 44 Name: "ubi9/ubi", 45 Digest: `sha256:6505b024e539929a1909c8820535221fe70806ce5467b5f30aef8d45a4a97da7`, 46 }, 47 }, 48 { 49 Name: "NodeJS", 50 ManifestFile: "nodejs.rpm-manifest.json", 51 // Layer from registry.access.redhat.com/ubi9/nodejs-18@sha256:1ff5080686736cbab820ec560873c59bd80659a2b2f8d8f4e379301a910e5d54 52 Ref: test.LayerRef{ 53 Registry: "registry.access.redhat.com", 54 Name: "ubi9/nodejs-18", 55 Digest: `sha256:1ae06b64755052cef4c32979aded82a18f664c66fa7b50a6d2924afac2849c6e`, 56 }, 57 }, 58 } 59 for _, tc := range tt { 60 t.Run(tc.Name, tc.Run(ctx, a)) 61 } 62 } 63 64 type PackageTestcase struct { 65 Name string 66 ManifestFile string 67 Ref test.LayerRef 68 } 69 70 func (tc PackageTestcase) Run(ctx context.Context, a *test.CachedArena) func(*testing.T) { 71 s := &Scanner{} 72 return func(t *testing.T) { 73 t.Parallel() 74 ctx := zlog.Test(ctx, t) 75 a.LoadLayerFromRegistry(ctx, t, tc.Ref) 76 wf, err := os.Open(filepath.Join("testdata/", tc.ManifestFile)) 77 if err != nil { 78 t.Fatal(err) 79 } 80 t.Cleanup(func() { 81 if err := wf.Close(); err != nil { 82 t.Error(err) 83 } 84 }) 85 want := rpmtest.PackagesFromRPMManifest(t, wf) 86 r := a.Realizer(ctx).(*test.CachedRealizer) 87 t.Cleanup(func() { 88 if err := r.Close(); err != nil { 89 t.Error(err) 90 } 91 }) 92 ls, err := r.RealizeDescriptions(ctx, []claircore.LayerDescription{ 93 { 94 Digest: tc.Ref.Digest, 95 URI: "http://example.com", 96 MediaType: test.MediaType, 97 Headers: make(map[string][]string), 98 }, 99 }) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 got, err := s.Scan(ctx, &ls[0]) 105 if err != nil { 106 t.Error(err) 107 } 108 t.Logf("found %d packages", len(got)) 109 if !cmp.Equal(got, want, rpmtest.Options) { 110 t.Error(cmp.Diff(got, want, rpmtest.Options)) 111 } 112 } 113 } 114 115 func TestLayer(t *testing.T) { 116 ctx := context.Background() 117 ents, err := os.ReadDir(`testdata/layers`) 118 if err != nil { 119 t.Fatal(err) 120 } 121 var s Scanner 122 desc := claircore.LayerDescription{ 123 Digest: test.RandomSHA256Digest(t).String(), 124 URI: "file:///dev/null", 125 MediaType: test.MediaType, 126 Headers: make(map[string][]string), 127 } 128 129 for _, e := range ents { 130 n := e.Name() 131 if n == ".gitignore" || n == "README.md" { 132 continue 133 } 134 t.Run(n, func(t *testing.T) { 135 ctx := zlog.Test(ctx, t) 136 f, err := os.Open(filepath.Join(`testdata/layers`, n)) 137 if err != nil { 138 t.Fatal(err) 139 } 140 t.Cleanup(func() { 141 if err := f.Close(); err != nil { 142 t.Error(err) 143 } 144 }) 145 var l claircore.Layer 146 if err := l.Init(ctx, &desc, f); err != nil { 147 t.Fatal(err) 148 } 149 t.Cleanup(func() { 150 if err := l.Close(); err != nil { 151 t.Error(err) 152 } 153 }) 154 155 got, err := s.Scan(ctx, &l) 156 if err != nil { 157 t.Error(err) 158 } 159 t.Logf("found %d packages", len(got)) 160 }) 161 } 162 }