github.com/quay/claircore@v1.5.28/ruby/packagescanner_test.go (about) 1 package ruby_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/quay/zlog" 9 10 "github.com/quay/claircore" 11 "github.com/quay/claircore/ruby" 12 "github.com/quay/claircore/test" 13 ) 14 15 func TestScanRemote(t *testing.T) { 16 ctx := zlog.Test(context.Background(), t) 17 18 type testcase struct { 19 Name string 20 Ref test.LayerRef 21 Total int 22 Subset []*claircore.Package 23 } 24 table := []testcase{ 25 { 26 // From the ruby-3.2.1-rake tag. 27 Name: "Rake", 28 Ref: test.LayerRef{ 29 Registry: "quay.io", 30 Name: "projectquay/clair-fixtures", 31 Digest: "sha256:f5da3e7f188aa027cf5c8111497d2abda6339d44eb0e7bff04647198ddfd87c1", 32 }, 33 Total: 6, 34 Subset: []*claircore.Package{ 35 { 36 Name: "bar", 37 Version: "0.0.2", 38 Kind: claircore.BINARY, 39 PackageDB: "ruby:usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/bar-0.0.2.gemspec", 40 Filepath: "usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/bar-0.0.2.gemspec", 41 RepositoryHint: "rubygems", 42 }, 43 { 44 Name: "bundler", 45 Version: "2.4.7", 46 Kind: claircore.BINARY, 47 PackageDB: "ruby:usr/local/lib/ruby/gems/3.2.0/specifications/default/bundler-2.4.7.gemspec", 48 Filepath: "usr/local/lib/ruby/gems/3.2.0/specifications/default/bundler-2.4.7.gemspec", 49 RepositoryHint: "rubygems", 50 }, 51 { 52 Name: "foo", 53 Version: "0.0.1", 54 Kind: claircore.BINARY, 55 PackageDB: "ruby:usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec", 56 Filepath: "usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec", 57 RepositoryHint: "rubygems", 58 }, 59 { 60 Name: "rake", 61 Version: "13.0.6", 62 Kind: claircore.BINARY, 63 PackageDB: "ruby:usr/local/bundle/specifications/rake-13.0.6.gemspec", 64 Filepath: "usr/local/bundle/specifications/rake-13.0.6.gemspec", 65 RepositoryHint: "rubygems", 66 }, 67 { 68 Name: "rubyforge", 69 Version: "0.0.1", 70 Kind: claircore.BINARY, 71 PackageDB: "ruby:usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/rubyforge-0.0.1.gemspec", 72 Filepath: "usr/local/bundle/gems/rubygems-update-3.4.7/test/rubygems/specifications/rubyforge-0.0.1.gemspec", 73 RepositoryHint: "rubygems", 74 }, 75 { 76 Name: "rubygems-update", 77 Version: "3.4.7", 78 Kind: claircore.BINARY, 79 PackageDB: "ruby:usr/local/bundle/specifications/rubygems-update-3.4.7.gemspec", 80 Filepath: "usr/local/bundle/specifications/rubygems-update-3.4.7.gemspec", 81 RepositoryHint: "rubygems", 82 }, 83 }, 84 }, 85 { 86 // From the "ruby-3.2.1-rails" tag. 87 Name: "Rails", 88 Ref: test.LayerRef{ 89 Registry: "quay.io", 90 Name: "projectquay/clair-fixtures", 91 Digest: "sha256:8cf469d41e77c8c4aaa2191f42f55b9758f77c640f2f7015b799b26458903f9b", 92 }, 93 Total: 35, 94 Subset: []*claircore.Package{ 95 { 96 Name: "actioncable", 97 Version: "7.0.4.3", 98 Kind: claircore.BINARY, 99 PackageDB: "ruby:usr/local/bundle/specifications/actioncable-7.0.4.3.gemspec", 100 Filepath: "usr/local/bundle/specifications/actioncable-7.0.4.3.gemspec", 101 RepositoryHint: "rubygems", 102 }, 103 { 104 Name: "actionpack", 105 Version: "7.0.4.3", 106 Kind: claircore.BINARY, 107 PackageDB: "ruby:usr/local/bundle/specifications/actionpack-7.0.4.3.gemspec", 108 Filepath: "usr/local/bundle/specifications/actionpack-7.0.4.3.gemspec", 109 RepositoryHint: "rubygems", 110 }, 111 { 112 Name: "concurrent-ruby", 113 Version: "1.2.2", 114 Kind: claircore.BINARY, 115 PackageDB: "ruby:usr/local/bundle/specifications/concurrent-ruby-1.2.2.gemspec", 116 Filepath: "usr/local/bundle/specifications/concurrent-ruby-1.2.2.gemspec", 117 RepositoryHint: "rubygems", 118 }, 119 { 120 Name: "rails", 121 Version: "7.0.4.3", 122 Kind: claircore.BINARY, 123 PackageDB: "ruby:usr/local/bundle/specifications/rails-7.0.4.3.gemspec", 124 Filepath: "usr/local/bundle/specifications/rails-7.0.4.3.gemspec", 125 RepositoryHint: "rubygems", 126 }, 127 { 128 Name: "rails-html-sanitizer", 129 Version: "1.5.0", 130 Kind: claircore.BINARY, 131 PackageDB: "ruby:usr/local/bundle/specifications/rails-html-sanitizer-1.5.0.gemspec", 132 Filepath: "usr/local/bundle/specifications/rails-html-sanitizer-1.5.0.gemspec", 133 RepositoryHint: "rubygems", 134 }, 135 }, 136 }, 137 } 138 139 for _, tc := range table { 140 t.Run(tc.Name, func(t *testing.T) { 141 ctx := zlog.Test(ctx, t) 142 l := test.RealizeLayer(ctx, t, tc.Ref) 143 var scanner ruby.Scanner 144 145 got, err := scanner.Scan(ctx, l) 146 if err != nil { 147 t.Error(err) 148 } 149 if !cmp.Equal(len(got), tc.Total) { 150 t.Error(cmp.Diff(len(got), tc.Total)) 151 } 152 153 gotMap := make(map[string]*claircore.Package, len(got)) 154 for _, pkg := range got { 155 gotMap[pkg.Name] = pkg 156 } 157 158 for _, pkg := range tc.Subset { 159 gotPkg, exists := gotMap[pkg.Name] 160 if !exists { 161 t.Errorf("did not find %s", pkg.Name) 162 } 163 164 if !cmp.Equal(gotPkg, pkg) { 165 t.Error(cmp.Diff(gotPkg, pkg)) 166 } 167 } 168 }) 169 } 170 }