github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/test/integration/package_deduplication_test.go (about) 1 //go:build !arm64 2 3 package integration 4 5 import ( 6 "fmt" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/metasources/buildx/buildx/pkg" 13 "github.com/metasources/buildx/buildx/source" 14 ) 15 16 func TestPackageDeduplication(t *testing.T) { 17 tests := []struct { 18 scope source.Scope 19 packageCount int 20 instanceCount map[string]int 21 locationCount map[string]int 22 }{ 23 { 24 scope: source.AllLayersScope, 25 packageCount: 174, // without deduplication this would be 618 26 instanceCount: map[string]int{ 27 "basesystem": 1, 28 "wget": 1, 29 "curl": 2, // upgraded in the image 30 "vsftpd": 1, 31 "httpd": 2, // rpm, binary 32 }, 33 locationCount: map[string]int{ 34 "basesystem-10.0-7.el7.centos": 4, 35 "curl-7.29.0-59.el7": 1, // from base image 36 "curl-7.29.0-59.el7_9.1": 3, // upgrade 37 "wget-1.14-18.el7_6.1": 3, 38 "vsftpd-3.0.2-29.el7_9": 2, 39 "httpd-2.4.6-97.el7.centos.5": 1, 40 "httpd-2.4.6": 1, // binary 41 }, 42 }, 43 { 44 scope: source.SquashedScope, 45 packageCount: 172, 46 instanceCount: map[string]int{ 47 "basesystem": 1, 48 "wget": 1, 49 "curl": 1, // upgraded, but the most recent 50 "vsftpd": 1, 51 "httpd": 2, // rpm, binary 52 }, 53 locationCount: map[string]int{ 54 "basesystem-10.0-7.el7.centos": 1, 55 "curl-7.29.0-59.el7_9.1": 1, // upgrade 56 "wget-1.14-18.el7_6.1": 1, 57 "vsftpd-3.0.2-29.el7_9": 1, 58 "httpd-2.4.6-97.el7.centos.5": 1, 59 "httpd-2.4.6": 1, // binary 60 }, 61 }, 62 } 63 64 for _, tt := range tests { 65 t.Run(string(tt.scope), func(t *testing.T) { 66 sbom, _ := catalogFixtureImage(t, "image-vertical-package-dups", tt.scope, nil) 67 68 for _, p := range sbom.Artifacts.PackageCatalog.Sorted() { 69 if p.Type == pkg.BinaryPkg { 70 assert.NotEmpty(t, p.Name) 71 } 72 } 73 74 assert.Equal(t, tt.packageCount, sbom.Artifacts.PackageCatalog.PackageCount()) 75 for name, expectedInstanceCount := range tt.instanceCount { 76 pkgs := sbom.Artifacts.PackageCatalog.PackagesByName(name) 77 78 // with multiple packages with the same name, something is wrong (or this is the wrong fixture) 79 require.Len(t, pkgs, expectedInstanceCount) 80 for _, p := range pkgs { 81 nameVersion := fmt.Sprintf("%s-%s", name, p.Version) 82 expectedLocationCount, ok := tt.locationCount[nameVersion] 83 if !ok { 84 t.Fatalf("missing name-version: %s", nameVersion) 85 } 86 87 // we should see merged locations (assumption, there was 1 location for each package) 88 assert.Len(t, p.Locations.ToSlice(), expectedLocationCount) 89 90 // all paths should match 91 assert.Len(t, p.Locations.CoordinateSet().Paths(), 1) 92 } 93 } 94 95 }) 96 } 97 }