-
Notifications
You must be signed in to change notification settings - Fork 54
Strong type libartifact #522
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 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,63 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "go.podman.io/common/pkg/libartifact/types" | ||
| "go.podman.io/image/v5/docker/reference" | ||
| ) | ||
|
|
||
| // ArtifactReference is a fully qualified oci reference except for tag, where we add | ||
| // "latest" as the tag if tag is empty. Valid references: | ||
| // | ||
| // quay.io/podman/machine-os:latest | ||
| // quay.io/podman/machine-os | ||
| // quay.io/podman/machine-os@sha256:916ede4b2b9012f91f63100f8ba82d07ed81bf8a55d23c1503285a22a9759a1e | ||
| // | ||
| // Note: Partial sha references and digests (IDs) are not allowed. | ||
| // Note: A zero value of ArtifactReference{} is not valid because it violates | ||
| // the format promise. | ||
| // Note: repo:tag@digest are invalid. | ||
| type ArtifactReference struct { | ||
| ref reference.Named | ||
| } | ||
|
|
||
| // Name returns the full reference without the tag. | ||
| func (ar ArtifactReference) RepoName() string { | ||
| return ar.ref.Name() | ||
| } | ||
|
|
||
| // String returns the full reference. | ||
| func (ar ArtifactReference) String() string { | ||
| return ar.ref.String() | ||
| } | ||
|
|
||
| func (ar ArtifactReference) ToArtifactStoreReference() ArtifactStoreReference { | ||
| afr := ArtifactStoreReference{ | ||
| ref: &ar.ref, | ||
| } | ||
| return afr | ||
| } | ||
|
|
||
| // NewArtifactReference is a theoretical reference to an artifact. | ||
| func NewArtifactReference(input string) (ArtifactReference, error) { | ||
| named, err := stringToNamed(input) | ||
| if err != nil { | ||
| return ArtifactReference{}, err | ||
| } | ||
| return ArtifactReference{ref: named}, nil | ||
| } | ||
|
|
||
| // stringToNamed converts a string to a reference.Named. | ||
| func stringToNamed(s string) (reference.Named, error) { | ||
| named, err := reference.ParseNamed(s) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| _, isTagged := named.(reference.NamedTagged) | ||
| _, isDigested := named.(reference.Digested) | ||
|
|
||
| if isTagged && isDigested { | ||
| return nil, types.ErrTaggedAndDigested | ||
| } | ||
|
|
||
| return reference.TagNameOnly(named), 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,101 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.podman.io/common/pkg/libartifact/types" | ||
| ) | ||
|
|
||
| func TestNewArtifactReference(t *testing.T) { | ||
mtrmac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expectError bool | ||
| errorContains string | ||
| errorIs error | ||
| expectedRef string | ||
| }{ | ||
| { | ||
| name: "valid reference with tag", | ||
| input: "quay.io/podman/machine-os:5.1", | ||
| expectError: false, | ||
| expectedRef: "quay.io/podman/machine-os:5.1", | ||
| }, | ||
| { | ||
| name: "valid reference docker.io", | ||
| input: "docker.io/library/nginx:latest", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| input: "", | ||
| expectError: true, | ||
| errorContains: "invalid reference format", | ||
| }, | ||
| { | ||
| name: "malformed reference", | ||
| input: "invalid::reference", | ||
| expectError: true, | ||
| errorContains: "invalid reference format", | ||
| }, | ||
| { | ||
| name: "no tag adds latest", | ||
| input: "quay.io/machine-os/podman", | ||
| expectError: false, | ||
| expectedRef: "quay.io/machine-os/podman:latest", | ||
| }, | ||
| { | ||
| name: "valid digest", | ||
| input: "quay.io/machine-os/podman@sha256:8b96f36deaf1d2713858eebd9ef2fee9610df8452fbd083bbfa7dca66d6fcd0b", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "partial digest", | ||
| input: "quay.io/machine-os/podman@sha256:8b96f36deaf1d2", | ||
| expectError: true, | ||
| errorContains: "invalid reference format", | ||
| }, | ||
| { | ||
| name: "64-byte hex ID", | ||
| input: "84ddb405470e733d0202d6946e48fc75a7ee231337bdeb31a8579407a7052d9e", | ||
| expectError: true, | ||
| errorContains: "cannot specify 64-byte hexadecimal strings", | ||
| }, | ||
| { | ||
| name: "shortname rejected", | ||
| input: "machine-os:latest", | ||
| expectError: true, | ||
| errorContains: "repository name must be canonical", | ||
| }, | ||
| { | ||
| name: "tagged and digested", | ||
| input: "quay.io/machine-os/podman:latest@sha256:8b96f36deaf1d2713858eebd9ef2fee9610df8452fbd083bbfa7dca66d6fcd0b", | ||
| expectError: true, | ||
| errorIs: types.ErrTaggedAndDigested, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ar, err := NewArtifactReference(tt.input) | ||
|
|
||
| if tt.expectError { | ||
| assert.Error(t, err) | ||
| if len(tt.errorContains) > 0 { | ||
| assert.ErrorContains(t, err, tt.errorContains) | ||
| } | ||
| if tt.errorIs != nil { | ||
| assert.ErrorIs(t, err, tt.errorIs) | ||
| } | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, ar.ref) | ||
| if len(tt.expectedRef) > 0 { | ||
| assert.Equal(t, tt.expectedRef, ar.ref.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.