|
| 1 | +package language |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/quay/zlog" |
| 9 | + |
| 10 | + "github.com/quay/claircore" |
| 11 | + "github.com/quay/claircore/indexer" |
| 12 | + "github.com/quay/claircore/test" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCoalescer(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + ctx := zlog.Test(context.Background(), t) |
| 18 | + coalescer := &coalescer{} |
| 19 | + pkgs := test.GenUniquePackages(6) |
| 20 | + repo := []*claircore.Repository{{ |
| 21 | + Name: "npm", |
| 22 | + URI: "https://www.npmjs.com/", |
| 23 | + }} |
| 24 | + layerArtifacts := []*indexer.LayerArtifacts{ |
| 25 | + { |
| 26 | + Hash: test.RandomSHA256Digest(t), |
| 27 | + Pkgs: pkgs[:1], |
| 28 | + }, |
| 29 | + { |
| 30 | + Hash: test.RandomSHA256Digest(t), |
| 31 | + Pkgs: pkgs[:2], |
| 32 | + }, |
| 33 | + { |
| 34 | + Hash: test.RandomSHA256Digest(t), |
| 35 | + Pkgs: pkgs[:3], |
| 36 | + Repos: repo, |
| 37 | + }, |
| 38 | + { |
| 39 | + Hash: test.RandomSHA256Digest(t), |
| 40 | + Pkgs: pkgs[:4], |
| 41 | + }, |
| 42 | + { |
| 43 | + Hash: test.RandomSHA256Digest(t), |
| 44 | + Pkgs: pkgs[:5], |
| 45 | + Repos: repo, |
| 46 | + }, |
| 47 | + { |
| 48 | + Hash: test.RandomSHA256Digest(t), |
| 49 | + Pkgs: pkgs, |
| 50 | + }, |
| 51 | + } |
| 52 | + ir, err := coalescer.Coalesce(ctx, layerArtifacts) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("received error from coalesce method: %v", err) |
| 55 | + } |
| 56 | + // Expect 0-5 to have gotten associated with the repository. |
| 57 | + for i := range pkgs { |
| 58 | + es, ok := ir.Environments[strconv.Itoa(i)] |
| 59 | + if !ok && i == 5 { |
| 60 | + // Left out the last package. |
| 61 | + continue |
| 62 | + } |
| 63 | + e := es[0] |
| 64 | + if len(e.RepositoryIDs) == 0 { |
| 65 | + t.Error("expected some repositories") |
| 66 | + } |
| 67 | + for _, id := range e.RepositoryIDs { |
| 68 | + r := ir.Repositories[id] |
| 69 | + if got, want := r.Name, "npm"; got != want { |
| 70 | + t.Errorf("got: %q, want: %q", got, want) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestCoalescer_package_overwrite(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + ctx := zlog.Test(context.Background(), t) |
| 79 | + coalescer := &coalescer{} |
| 80 | + repo := []*claircore.Repository{{ |
| 81 | + Name: "npm", |
| 82 | + URI: "https://www.npmjs.com/", |
| 83 | + }} |
| 84 | + hashes := []claircore.Digest{ |
| 85 | + test.RandomSHA256Digest(t), |
| 86 | + test.RandomSHA256Digest(t), |
| 87 | + test.RandomSHA256Digest(t), |
| 88 | + test.RandomSHA256Digest(t), |
| 89 | + } |
| 90 | + layerArtifacts := []*indexer.LayerArtifacts{ |
| 91 | + { |
| 92 | + Hash: hashes[0], |
| 93 | + Pkgs: []*claircore.Package{ |
| 94 | + { |
| 95 | + ID: "0", |
| 96 | + Name: "semver", |
| 97 | + Version: "7.3.8", |
| 98 | + PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", |
| 99 | + }, |
| 100 | + }, |
| 101 | + Repos: repo, |
| 102 | + }, |
| 103 | + { |
| 104 | + Hash: hashes[1], |
| 105 | + }, |
| 106 | + { |
| 107 | + Hash: hashes[2], |
| 108 | + Pkgs: []*claircore.Package{ |
| 109 | + { |
| 110 | + ID: "1", |
| 111 | + Name: "semver", |
| 112 | + Version: "7.5.2", |
| 113 | + PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", |
| 114 | + }, |
| 115 | + }, |
| 116 | + Repos: repo, |
| 117 | + }, |
| 118 | + { |
| 119 | + Hash: hashes[3], |
| 120 | + Pkgs: []*claircore.Package{ |
| 121 | + { |
| 122 | + ID: "2", |
| 123 | + Name: "semver", |
| 124 | + Version: "7.5.2", |
| 125 | + PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", |
| 126 | + }, |
| 127 | + }, |
| 128 | + Repos: repo, |
| 129 | + }, |
| 130 | + } |
| 131 | + ir, err := coalescer.Coalesce(ctx, layerArtifacts) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("received error from coalesce method: %v", err) |
| 134 | + } |
| 135 | + if len(ir.Packages) != 1 { |
| 136 | + t.Fatalf("unexpected number of packages: %d != %d", len(ir.Packages), 1) |
| 137 | + } |
| 138 | + pkg, exists := ir.Packages["1"] |
| 139 | + if !exists { |
| 140 | + t.Fatal("expected package does not exist") |
| 141 | + } |
| 142 | + if pkg.Version != "7.5.2" { |
| 143 | + t.Fatalf("unexpected version: %s != %s", pkg.Version, "7.5.2") |
| 144 | + } |
| 145 | + envs, exists := ir.Environments["1"] |
| 146 | + if !exists { |
| 147 | + t.Fatal("expected environments do not exist") |
| 148 | + } |
| 149 | + if len(envs) != 1 { |
| 150 | + t.Fatalf("unexpected number of envionments: %d != %d", len(envs), 1) |
| 151 | + } |
| 152 | + if envs[0].IntroducedIn.String() != hashes[2].String() { |
| 153 | + t.Fatalf("unexpected introducedIn: %s != %s", envs[0].IntroducedIn.String(), hashes[2].String()) |
| 154 | + } |
| 155 | +} |
0 commit comments