From a1890c261fc2a5ffb58ee1590662b04a752fa9a0 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Tue, 12 Nov 2024 17:02:10 +0100 Subject: [PATCH] fix version info for OCI refs (#1078) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What this PR does / why we need it PR #1033 introduced a split of the OCI version info from the repository part. The method `Version` checks the inverted condition for an existing version inf. #### Which issue(s) this PR fixes --------- Co-authored-by: Jakob Möller --- api/oci/ociutils/ref.go | 22 +++++- api/oci/ociutils/ref_test.go | 70 +++++++++++++++++++ api/oci/ociutils/suite_test.go | 13 ++++ api/oci/ref.go | 10 --- api/oci/testhelper/manifests.go | 8 +-- .../relativeociref/method_test.go | 2 +- 6 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 api/oci/ociutils/ref_test.go create mode 100644 api/oci/ociutils/suite_test.go diff --git a/api/oci/ociutils/ref.go b/api/oci/ociutils/ref.go index 5d05921777..b419e30a3d 100644 --- a/api/oci/ociutils/ref.go +++ b/api/oci/ociutils/ref.go @@ -31,6 +31,9 @@ func ParseVersion(vers string) (*ArtVersion, error) { Digest: &dig, }, nil } + if vers == "" { + return &ArtVersion{}, nil + } return &ArtVersion{ Tag: &vers, }, nil @@ -50,7 +53,7 @@ type ArtVersion struct { } func (v *ArtVersion) VersionSpec() string { - if v != nil { + if v == nil { return "" } @@ -94,3 +97,20 @@ func (v *ArtVersion) GetTag() string { } return "" } + +func (v *ArtVersion) GetDigest() digest.Digest { + if v != nil && v.Digest != nil { + return *v.Digest + } + return "" +} + +func (r *ArtVersion) Version() string { + if r.Digest != nil { + return "@" + string(*r.Digest) + } + if r.Tag != nil { + return *r.Tag + } + return "latest" +} diff --git a/api/oci/ociutils/ref_test.go b/api/oci/ociutils/ref_test.go new file mode 100644 index 0000000000..1e5075447a --- /dev/null +++ b/api/oci/ociutils/ref_test.go @@ -0,0 +1,70 @@ +package ociutils_test + +import ( + . "github.com/mandelsoft/goutils/testutils" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/opencontainers/go-digest" + "ocm.software/ocm/api/oci/ociutils" + "ocm.software/ocm/api/oci/testhelper" +) + +var _ = Describe("Ref Test Environment", func() { + dig := "sha256:" + testhelper.H_OCIARCHMANIFEST1 + + type expect struct { + yaml string + versionSpec string + isVersion bool + version string + isTag bool + tag string + isDigested bool + digest string + } + + DescribeTable("parsing", func(src string, e expect) { + v := Must(ociutils.ParseVersion(src)) + Expect(v).NotTo(BeNil()) + Expect(v).To(YAMLEqual(e.yaml)) + Expect(v.VersionSpec()).To(Equal(e.versionSpec)) + Expect(v.IsVersion()).To(Equal(e.isVersion)) + Expect(v.Version()).To(Equal(e.version)) + Expect(v.IsTagged()).To(Equal(e.isTag)) + Expect(v.GetTag()).To(Equal(e.tag)) + Expect(v.IsDigested()).To(Equal(e.isDigested)) + Expect(v.GetDigest()).To(Equal(digest.Digest(e.digest))) + }, + Entry("empty", "", expect{ + yaml: "{}", + versionSpec: "latest", + version: "latest", + }), + Entry("tag", "tag", expect{ + yaml: "{\"tag\":\"tag\"}", + versionSpec: "tag", + isVersion: true, + version: "tag", + isTag: true, + tag: "tag", + }), + Entry("digest", "@"+dig, expect{ + yaml: "{\"digest\":\"" + dig + "\"}", + versionSpec: "@" + dig, + isVersion: true, + version: "@" + dig, + isDigested: true, + digest: dig, + }), + Entry("tag@digest", "tag@"+dig, expect{ + yaml: "{\"tag\":\"tag\",\"digest\":\"" + dig + "\"}", + versionSpec: "tag@" + dig, + isVersion: true, + version: "@" + dig, + isTag: true, + tag: "tag", + isDigested: true, + digest: dig, + }), + ) +}) diff --git a/api/oci/ociutils/suite_test.go b/api/oci/ociutils/suite_test.go new file mode 100644 index 0000000000..bf4c1257f7 --- /dev/null +++ b/api/oci/ociutils/suite_test.go @@ -0,0 +1,13 @@ +package ociutils_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "OCI Utils Test Suite") +} diff --git a/api/oci/ref.go b/api/oci/ref.go index 20186d78c9..0d64f407b0 100644 --- a/api/oci/ref.go +++ b/api/oci/ref.go @@ -272,16 +272,6 @@ type ArtSpec struct { ArtVersion `json:",inline"` } -func (r *ArtSpec) Version() string { - if r.Digest != nil { - return "@" + string(*r.Digest) - } - if r.Tag != nil { - return *r.Tag - } - return "latest" -} - func (r *ArtSpec) IsRegistry() bool { return r.Repository == "" } diff --git a/api/oci/testhelper/manifests.go b/api/oci/testhelper/manifests.go index 44b92c12cd..a6506db65a 100644 --- a/api/oci/testhelper/manifests.go +++ b/api/oci/testhelper/manifests.go @@ -64,8 +64,8 @@ func OCIArtifactResource1(env *builder.Builder, name string, host string, funcs const ( D_OCIMANIFEST1 = "0c4abdb72cf59cb4b77f4aacb4775f9f546ebc3face189b2224a966c8826ca9f" - H_OCIARCHMANIFEST1 = "818fb6a69a5f55e8b3dbc921a61fdd000b9445a745b587ba753a811b02426326" - // H_OCIARCHMANIFEST1 = "b0692bcec00e0a875b6b280f3209d6776f3eca128adcb7e81e82fd32127c0c62". + H_OCIARCHMANIFEST1 = "b0692bcec00e0a875b6b280f3209d6776f3eca128adcb7e81e82fd32127c0c62" + // H_OCIARCHMANIFEST1 = "818fb6a69a5f55e8b3dbc921a61fdd000b9445a745b587ba753a811b02426326". ) var DS_OCIMANIFEST1 = &metav1.DigestSpec{ @@ -124,8 +124,8 @@ func OCIManifest2For(env *builder.Builder, ns, tag string, nested ...func()) (*a const ( D_OCIMANIFEST2 = "c2d2dca275c33c1270dea6168a002d67c0e98780d7a54960758139ae19984bd7" - H_OCIARCHMANIFEST2 = "2aaf6f8857dcbfa04a72fb98dd53f649b46e5d81aa4fb17330df74b0ffc30839" - // H_OCIARCHMANIFEST2 = "cb85cd58b10e36343971691abbfe40200cb645c6e95f0bdabd111a30cf794708". + H_OCIARCHMANIFEST2 = "cb85cd58b10e36343971691abbfe40200cb645c6e95f0bdabd111a30cf794708" + // H_OCIARCHMANIFEST2 = "2aaf6f8857dcbfa04a72fb98dd53f649b46e5d81aa4fb17330df74b0ffc30839". ) func HashManifest2(fmt string) string { diff --git a/api/ocm/extensions/accessmethods/relativeociref/method_test.go b/api/ocm/extensions/accessmethods/relativeociref/method_test.go index 2e589770c0..4634480fc9 100644 --- a/api/ocm/extensions/accessmethods/relativeociref/method_test.go +++ b/api/ocm/extensions/accessmethods/relativeociref/method_test.go @@ -68,7 +68,7 @@ var _ = Describe("Method", func() { return m.Close() }) data := Must(m.Get()) - Expect(len(data)).To(Equal(630)) + Expect(len(data)).To(Equal(628)) Expect(accspeccpi.GetAccessMethodImplementation(m).(blobaccess.DigestSource).Digest().String()).To(Equal("sha256:0c4abdb72cf59cb4b77f4aacb4775f9f546ebc3face189b2224a966c8826ca9f")) Expect(utils.GetOCIArtifactRef(env, res)).To(Equal("ocm/value:v2.0")) })