Skip to content
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

libimage: Untag should error for non existent name #1701

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion libimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ func (i *Image) Tag(name string) error {
return i.reload()
}

// to have some symmetry with the errors from containers/storage.
var errTagUnknown = errors.New("tag not known")
Copy link
Member

@rhatdan rhatdan Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be exported in libimage/define?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don´t think we need to. I cannot think of a scenario where users shouldn't error out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is c/common so no and it just brings back the error removed in 465dd3e.

I don't see the need to export it unless someone wants to match on this specific error but for now this is not the case and we can always export it later if needed


// TODO (@vrothberg) - `docker rmi sha256:` will remove the digest from the
// image. However, that's something containers storage does not support.
var errUntagDigest = errors.New("untag by digest not supported")
Expand All @@ -601,7 +604,7 @@ func (i *Image) Untag(name string) error {

ref, err := NormalizeName(name)
if err != nil {
return fmt.Errorf("normalizing name %q: %w", name, err)
return err
}

// FIXME: this is breaking Podman CI but must be re-enabled once
Expand All @@ -616,6 +619,19 @@ func (i *Image) Untag(name string) error {

name = ref.String()

foundName := false
for _, n := range i.Names() {
if n == name {
foundName = true
break
}
}
// Return an error if the name is not found, the c/storage
// RemoveNames() API does not create one if no match is found.
if !foundName {
return fmt.Errorf("%s: %w", name, errTagUnknown)
}

logrus.Debugf("Untagging %q from image %s", ref.String(), i.ID())
if i.runtime.eventChannel != nil {
defer i.runtime.writeEvent(&Event{ID: i.ID(), Name: name, Time: time.Now(), Type: EventTypeImageUntag})
Expand Down
23 changes: 12 additions & 11 deletions libimage/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,24 +361,25 @@ func TestUntag(t *testing.T) {
for _, test := range []struct {
tag string
untag string
expectError bool
expectError string
}{
{"foo", "foo", false},
{"foo", "foo:latest", false},
{"foo", "localhost/foo", false},
{"foo", "localhost/foo:latest", false},
{"quay.io/image/foo", "quay.io/image/foo", false},
{"foo", "doNotExist", true},
{"foo", digest, true},
{"foo", "foo", ""},
{"foo", "foo:latest", ""},
{"foo", "localhost/foo", ""},
{"foo", "localhost/foo:latest", ""},
{"quay.io/image/foo", "quay.io/image/foo", ""},
{"foo", "upperCase", "normalizing name \"upperCase\": repository name must be lowercase"},
{"foo", "donotexist", "localhost/donotexist:latest: tag not known"},
{"foo", digest, digest + ": untag by digest not supported"},
// {"foo", "foo@" + digest, false},
// {"foo", "localhost/foo@" + digest, false},
} {
err := image.Tag(test.tag)
require.NoError(t, err, "tag should have succeeded: %v", test)

err = image.Untag(test.untag)
if test.expectError {
require.Error(t, err, "untag should have failed: %v", test)
if test.expectError != "" {
require.EqualError(t, err, test.expectError, "untag should have failed: %v", test)
continue
}
require.NoError(t, err, "untag should have succeedded: %v", test)
Expand All @@ -388,7 +389,7 @@ func TestUntag(t *testing.T) {

// Check for specific error.
err := image.Untag(digest)
require.True(t, errors.Is(err, errUntagDigest), "check for specific digest error")
require.ErrorIs(t, err, errUntagDigest, "check for specific digest error")
}

func getImageAndRuntime(t *testing.T) (*Runtime, *Image, func()) {
Expand Down