-
Notifications
You must be signed in to change notification settings - Fork 91
all: account for language package overwrites #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Package language implements structs and functions common between | ||
| // programming language indexing implementations. | ||
| package language | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/indexer" | ||
| ) | ||
|
|
||
| var _ indexer.Coalescer = (*coalescer)(nil) | ||
|
|
||
| type coalescer struct{} | ||
|
|
||
| // NewCoalescer returns a new common programming language coalescer. | ||
| func NewCoalescer(_ context.Context) (indexer.Coalescer, error) { | ||
| return &coalescer{}, nil | ||
| } | ||
|
|
||
| // Coalesce implements [indexer.Coalescer]. | ||
crozzy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // | ||
| // Image builders may opt to update language-packages instead of deleting and recreating them | ||
| // (as in, there may or may not be a whiteout file to make it clear the package was updated). | ||
| // This function ensures both scenarios are supported. | ||
| func (c *coalescer) Coalesce(_ context.Context, ls []*indexer.LayerArtifacts) (*claircore.IndexReport, error) { | ||
| ir := &claircore.IndexReport{ | ||
| Environments: map[string][]*claircore.Environment{}, | ||
| Packages: map[string]*claircore.Package{}, | ||
| Repositories: map[string]*claircore.Repository{}, | ||
| } | ||
| // Similar to ir.Packages, except instead of mapping | ||
| // id -> package, it maps packageDB -> package. | ||
| // For language packages, it is possible the | ||
| // packageDB is overwritten between subsequent layers. | ||
| packages := make(map[string]*claircore.Package) | ||
| for i := len(ls) - 1; i >= 0; i-- { | ||
| l := ls[i] | ||
| // If we didn't find at least one repo in this layer | ||
| // no point searching for packages. | ||
| if len(l.Repos) == 0 { | ||
| continue | ||
| } | ||
| rs := make([]string, len(l.Repos)) | ||
| for i, r := range l.Repos { | ||
| rs[i] = r.ID | ||
| ir.Repositories[r.ID] = r | ||
| } | ||
| for _, pkg := range l.Pkgs { | ||
| if seen, exists := packages[pkg.PackageDB]; exists { | ||
| // If the package was renamed or has a different version in a higher (previously seen) layer, | ||
| // then this is considered a different package. | ||
| // In that case, ignore the original package in the lower (this) layer. | ||
| if pkg.Name != seen.Name || pkg.Version != seen.Version { | ||
| continue | ||
| } | ||
| // The name and version are the same, so delete the entry related to the higher (previously seen) | ||
| // layer, as this package was likely introduced in the lower (this) layer. | ||
| delete(ir.Packages, seen.ID) | ||
| delete(ir.Environments, seen.ID) | ||
| } | ||
| packages[pkg.PackageDB] = pkg | ||
| ir.Packages[pkg.ID] = pkg | ||
| ir.Environments[pkg.ID] = []*claircore.Environment{ | ||
| { | ||
| PackageDB: pkg.PackageDB, | ||
| IntroducedIn: l.Hash, | ||
| RepositoryIDs: rs, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
| return ir, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| package language | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/quay/zlog" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/indexer" | ||
| "github.com/quay/claircore/test" | ||
| ) | ||
|
|
||
| func TestCoalescer(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := zlog.Test(context.Background(), t) | ||
| coalescer := &coalescer{} | ||
| pkgs := test.GenUniquePackages(6) | ||
| repo := []*claircore.Repository{{ | ||
| Name: "npm", | ||
| URI: "https://www.npmjs.com/", | ||
| }} | ||
| layerArtifacts := []*indexer.LayerArtifacts{ | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs[:1], | ||
| }, | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs[:2], | ||
| }, | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs[:3], | ||
| Repos: repo, | ||
| }, | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs[:4], | ||
| }, | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs[:5], | ||
| Repos: repo, | ||
| }, | ||
| { | ||
| Hash: test.RandomSHA256Digest(t), | ||
| Pkgs: pkgs, | ||
| }, | ||
| } | ||
| ir, err := coalescer.Coalesce(ctx, layerArtifacts) | ||
| if err != nil { | ||
| t.Fatalf("received error from coalesce method: %v", err) | ||
| } | ||
| // Expect 0-5 to have gotten associated with the repository. | ||
| for i := range pkgs { | ||
| es, ok := ir.Environments[strconv.Itoa(i)] | ||
| if !ok && i == 5 { | ||
| // Left out the last package. | ||
| continue | ||
| } | ||
| e := es[0] | ||
| if len(e.RepositoryIDs) == 0 { | ||
| t.Error("expected some repositories") | ||
| } | ||
| for _, id := range e.RepositoryIDs { | ||
| r := ir.Repositories[id] | ||
| if got, want := r.Name, "npm"; got != want { | ||
| t.Errorf("got: %q, want: %q", got, want) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCoalescerPackageOverwrite(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := zlog.Test(context.Background(), t) | ||
| coalescer := &coalescer{} | ||
| repo := []*claircore.Repository{{ | ||
| Name: "npm", | ||
| URI: "https://www.npmjs.com/", | ||
| }} | ||
| hashes := []claircore.Digest{ | ||
| test.RandomSHA256Digest(t), | ||
| test.RandomSHA256Digest(t), | ||
| test.RandomSHA256Digest(t), | ||
| test.RandomSHA256Digest(t), | ||
| } | ||
| layerArtifacts := []*indexer.LayerArtifacts{ | ||
| { | ||
| Hash: hashes[0], | ||
| Pkgs: []*claircore.Package{ | ||
| { | ||
| ID: "0", | ||
| Name: "semver", | ||
| Version: "7.3.8", | ||
| PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", | ||
| }, | ||
| }, | ||
| Repos: repo, | ||
| }, | ||
| { | ||
| Hash: hashes[1], | ||
| }, | ||
| { | ||
| Hash: hashes[2], | ||
| Pkgs: []*claircore.Package{ | ||
| { | ||
| ID: "1", | ||
| Name: "semver", | ||
| Version: "7.5.2", | ||
| PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", | ||
| }, | ||
| }, | ||
| Repos: repo, | ||
| }, | ||
| { | ||
| Hash: hashes[3], | ||
| Pkgs: []*claircore.Package{ | ||
| { | ||
| ID: "2", | ||
| Name: "semver", | ||
| Version: "7.5.2", | ||
| PackageDB: "nodejs:usr/local/lib/node_modules/npm/node_modules/semver/package.json", | ||
| }, | ||
| }, | ||
| Repos: repo, | ||
| }, | ||
| } | ||
| ir, err := coalescer.Coalesce(ctx, layerArtifacts) | ||
| if err != nil { | ||
| t.Fatalf("received error from coalesce method: %v", err) | ||
| } | ||
| if len(ir.Packages) != 1 { | ||
| t.Fatalf("unexpected number of packages: %d != %d", len(ir.Packages), 1) | ||
| } | ||
| pkg, exists := ir.Packages["1"] | ||
| if !exists { | ||
| t.Fatal("expected package does not exist") | ||
| } | ||
| if pkg.Version != "7.5.2" { | ||
| t.Fatalf("unexpected version: %s != %s", pkg.Version, "7.5.2") | ||
| } | ||
| envs, exists := ir.Environments["1"] | ||
| if !exists { | ||
| t.Fatal("expected environments do not exist") | ||
| } | ||
| if len(envs) != 1 { | ||
| t.Fatalf("unexpected number of envionments: %d != %d", len(envs), 1) | ||
| } | ||
| if envs[0].IntroducedIn.String() != hashes[2].String() { | ||
| t.Fatalf("unexpected introducedIn: %s != %s", envs[0].IntroducedIn.String(), hashes[2].String()) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.