From e060a19c8768c9dd65d1b8dcbd6da7ec9faa1163 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Wed, 9 Jan 2019 21:35:04 +0100 Subject: Use imageParts.normalizedReference in normalizeTag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is another step to using reference values instead of strings here. CHANGES BEHAVIOR: docker.io/busybox is now normalized to docker.io/library/busybox. Signed-off-by: Miloslav Trmač --- libpod/image/image_test.go | 1 + 1 file changed, 1 insertion(+) (limited to 'libpod/image/image_test.go') diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index 2a68fd273..a85cd5386 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -268,6 +268,7 @@ func TestNormalizeTag(t *testing.T) { {"example.com/busybox:notlatest" + digestSuffix, "example.com/busybox:notlatest" + digestSuffix}, // Qualified name:tag@digest {"busybox:latest", "localhost/busybox:latest"}, // Unqualified name-only {"ns/busybox:latest", "localhost/ns/busybox:latest"}, // Unqualified with a dot-less namespace + {"docker.io/busybox:latest", "docker.io/library/busybox:latest"}, // docker.io without /library/ } { res, err := normalizeTag(c.input) if c.expected == "" { -- cgit v1.2.3-54-g00ecf From b9c0f2c987688a8e7ee2c8f4e4fbf6b1a8fee024 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Wed, 9 Jan 2019 21:38:49 +0100 Subject: Use reference.TagNameOnly instead of manually adding imageParts.tag in normalizeTag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Again, rely on the official API, instead of the suprising "suspiciousTagValueForSearch" value (set to :latest on untagged images, and :none on digested ones!) CHANGES BEHAVIOR, but the previous output of normalization of digested values was not even syntatically valid, so this can't really be worse. Still, maybe we should refuse to tag with digested references in the first place. Signed-off-by: Miloslav Trmač --- libpod/image/image.go | 7 ++----- libpod/image/image_test.go | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'libpod/image/image_test.go') diff --git a/libpod/image/image.go b/libpod/image/image.go index 2457a1c22..0b743e144 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -471,12 +471,9 @@ func normalizeTag(tag string) (string, error) { return "", err } } - tag = ref.String() // If the input does not have a tag, we need to add one (latest) - if !decomposedTag.isTagged { - tag = fmt.Sprintf("%s:%s", tag, decomposedTag.tag) - } - return tag, nil + ref = reference.TagNameOnly(ref) + return ref.String(), nil } // TagImage adds a tag to the given image diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index a85cd5386..96148c2cd 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -264,7 +264,7 @@ func TestNormalizeTag(t *testing.T) { {"#", ""}, // Clearly invalid {"example.com/busybox", "example.com/busybox:latest"}, // Qualified name-only {"example.com/busybox:notlatest", "example.com/busybox:notlatest"}, // Qualified name:tag - {"example.com/busybox" + digestSuffix, "example.com/busybox" + digestSuffix + ":none"}, // Qualified name@digest; FIXME: The result is not even syntactically valid! + {"example.com/busybox" + digestSuffix, "example.com/busybox" + digestSuffix}, // Qualified name@digest; FIXME? Should we allow tagging with a digest at all? {"example.com/busybox:notlatest" + digestSuffix, "example.com/busybox:notlatest" + digestSuffix}, // Qualified name:tag@digest {"busybox:latest", "localhost/busybox:latest"}, // Unqualified name-only {"ns/busybox:latest", "localhost/ns/busybox:latest"}, // Unqualified with a dot-less namespace -- cgit v1.2.3-54-g00ecf From f92c3ce35033a558a9c86c824a829368a472f873 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Wed, 9 Jan 2019 21:42:37 +0100 Subject: Return a reference.Named from normalizedTag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of returning a string, return a native value and convert it into the string in the caller, to make it that small bit more common to use reference types. Should not change behavior. Signed-off-by: Miloslav Trmač --- libpod/image/image.go | 18 +++++++++--------- libpod/image/image_test.go | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'libpod/image/image_test.go') diff --git a/libpod/image/image.go b/libpod/image/image.go index 0b743e144..7442f1945 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -452,42 +452,42 @@ func getImageDigest(ctx context.Context, src types.ImageReference, sc *types.Sys return "@" + digest.Hex(), nil } -// normalizeTag returns the canonical version of tag for use in Image.Names() -func normalizeTag(tag string) (string, error) { +// normalizedTag returns the canonical version of tag for use in Image.Names() +func normalizedTag(tag string) (reference.Named, error) { decomposedTag, err := decompose(tag) if err != nil { - return "", err + return nil, err } // If the input doesn't specify a registry, set the registry to localhost var ref reference.Named if !decomposedTag.hasRegistry { ref, err = decomposedTag.referenceWithRegistry(DefaultLocalRegistry) if err != nil { - return "", err + return nil, err } } else { ref, err = decomposedTag.normalizedReference() if err != nil { - return "", err + return nil, err } } // If the input does not have a tag, we need to add one (latest) ref = reference.TagNameOnly(ref) - return ref.String(), nil + return ref, nil } // TagImage adds a tag to the given image func (i *Image) TagImage(tag string) error { i.reloadImage() - tag, err := normalizeTag(tag) + ref, err := normalizedTag(tag) if err != nil { return err } tags := i.Names() - if util.StringInSlice(tag, tags) { + if util.StringInSlice(ref.String(), tags) { return nil } - tags = append(tags, tag) + tags = append(tags, ref.String()) if err := i.imageruntime.store.SetNames(i.ID(), tags); err != nil { return err } diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index 96148c2cd..077ae460e 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -257,7 +257,7 @@ func Test_stripSha256(t *testing.T) { assert.Equal(t, stripSha256("sha256:a"), "a") } -func TestNormalizeTag(t *testing.T) { +func TestNormalizedTag(t *testing.T) { const digestSuffix = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" for _, c := range []struct{ input, expected string }{ @@ -270,12 +270,12 @@ func TestNormalizeTag(t *testing.T) { {"ns/busybox:latest", "localhost/ns/busybox:latest"}, // Unqualified with a dot-less namespace {"docker.io/busybox:latest", "docker.io/library/busybox:latest"}, // docker.io without /library/ } { - res, err := normalizeTag(c.input) + res, err := normalizedTag(c.input) if c.expected == "" { assert.Error(t, err, c.input) } else { assert.NoError(t, err, c.input) - assert.Equal(t, c.expected, res) + assert.Equal(t, c.expected, res.String()) } } } -- cgit v1.2.3-54-g00ecf