Skip to content

Commit

Permalink
fix version info for OCI refs (#1078)
Browse files Browse the repository at this point in the history
<!-- markdownlint-disable MD041 -->
#### 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
<!--
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

---------

Co-authored-by: Jakob Möller <contact@jakob-moeller.com>
  • Loading branch information
mandelsoft and jakobmoellerdev authored Nov 12, 2024
1 parent 782970c commit a1890c2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 16 deletions.
22 changes: 21 additions & 1 deletion api/oci/ociutils/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func ParseVersion(vers string) (*ArtVersion, error) {
Digest: &dig,
}, nil
}
if vers == "" {
return &ArtVersion{}, nil
}
return &ArtVersion{
Tag: &vers,
}, nil
Expand All @@ -50,7 +53,7 @@ type ArtVersion struct {
}

func (v *ArtVersion) VersionSpec() string {
if v != nil {
if v == nil {
return ""
}

Expand Down Expand Up @@ -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"
}
70 changes: 70 additions & 0 deletions api/oci/ociutils/ref_test.go
Original file line number Diff line number Diff line change
@@ -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,
}),
)
})
13 changes: 13 additions & 0 deletions api/oci/ociutils/suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
10 changes: 0 additions & 10 deletions api/oci/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""
}
Expand Down
8 changes: 4 additions & 4 deletions api/oci/testhelper/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
Expand Down

0 comments on commit a1890c2

Please sign in to comment.