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

fix: fix versioned GetIdentity methods #1247

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions api/ocm/compdesc/componentdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ func (o *ElementMeta) AddExtraIdentity(identity metav1.Identity) {

// GetIdentity returns the identity of the object.
func (o *ElementMeta) GetIdentity(accessor ElementListAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity := o.GetExtraIdentity()
identity[SystemIdentityName] = o.Name
if identity.Get(SystemIdentityVersion) == "" && accessor != nil {
found := false
Expand Down Expand Up @@ -266,7 +263,7 @@ func (o *ElementMeta) GetRawIdentity() metav1.Identity {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
if o.Version != "" {
if _, ok := identity[SystemIdentityVersion]; !ok && o.Version != "" {
identity[SystemIdentityVersion] = o.Version
}
return identity
Expand All @@ -279,8 +276,9 @@ func (o *ElementMeta) GetMatchBaseIdentity() metav1.Identity {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
identity[SystemIdentityVersion] = o.Version

if _, ok := identity[SystemIdentityVersion]; !ok {
identity[SystemIdentityVersion] = o.Version
}
return identity
}

Expand Down
57 changes: 57 additions & 0 deletions api/ocm/compdesc/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package compdesc_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"ocm.software/ocm/api/ocm/compdesc"
metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
)

var _ = Describe("Extra Identity Test Environment", func() {
It("handles complete defaulting", func() {
resources := compdesc.Resources{
compdesc.Resource{
ResourceMeta: compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "res",
Version: "v1",
},
},
},
compdesc.Resource{
ResourceMeta: compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "res",
Version: "v2",
},
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v2")))
})

It("handles partial defaulting", func() {
resources := compdesc.Resources{
compdesc.Resource{
ResourceMeta: compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "res",
Version: "v1",
ExtraIdentity: metav1.NewExtraIdentity(compdesc.SystemIdentityVersion, "v1"),
},
},
},
compdesc.Resource{
ResourceMeta: compdesc.ResourceMeta{
ElementMeta: compdesc.ElementMeta{
Name: "res",
Version: "v2",
},
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", compdesc.SystemIdentityVersion, "v2")))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,33 @@ func (o *ElementMeta) SetExtraIdentity(identity metav1.Identity) {
o.ExtraIdentity = identity
}

// GetExtraIdentity gets the extra identity of the object.
func (o *ElementMeta) GetExtraIdentity() metav1.Identity {
if o.ExtraIdentity == nil {
return metav1.Identity{}
}
return o.ExtraIdentity.Copy()
}

// GetIdentity returns the identity of the object.
func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity := o.GetExtraIdentity()
identity[SystemIdentityName] = o.Name
if accessor != nil {
if identity.Get(SystemIdentityVersion) == "" && accessor != nil {
found := false
l := accessor.Len()
for i := 0; i < l; i++ {
m := accessor.Get(i).GetMeta()
if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version
break
if m.GetName() == o.Name {
mid := m.GetExtraIdentity()
mid.Remove(SystemIdentityVersion)
if mid.Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version
break
}
found = true
}
found = true
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions api/ocm/compdesc/versions/ocm.software/v3alpha1/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v3alpha1_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
me "ocm.software/ocm/api/ocm/compdesc/versions/ocm.software/v3alpha1"
)

var _ = Describe("Extra Identity Test Environment", func() {
It("handles complete defaulting", func() {
resources := me.Resources{
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v1",
},
},
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v2",
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2")))
})

It("handles partial defaulting", func() {
resources := me.Resources{
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v1",
ExtraIdentity: metav1.NewExtraIdentity(me.SystemIdentityVersion, "v1"),
},
},
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v2",
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2")))
})
})
13 changes: 13 additions & 0 deletions api/ocm/compdesc/versions/ocm.software/v3alpha1/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v3alpha1_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "v3alpha1 Test Suite")
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v3alpha1_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
Expand All @@ -17,11 +15,6 @@ import (
"ocm.software/ocm/api/utils/runtime"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "V2 Test Suite")
}

var _ = Describe("Validation", func() {
testutils.TestCompName(jsonscheme.ResourcesComponentDescriptorOcmV3SchemaYamlBytes())

Expand Down
31 changes: 20 additions & 11 deletions api/ocm/compdesc/versions/v2/componentdescriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,33 @@ func (o *ElementMeta) SetExtraIdentity(identity metav1.Identity) {
o.ExtraIdentity = identity
}

// GetExtraIdentity gets the extra identity of the object.
func (o *ElementMeta) GetExtraIdentity() metav1.Identity {
if o.ExtraIdentity == nil {
return metav1.Identity{}
}
return o.ExtraIdentity.Copy()
}

// GetIdentity returns the identity of the object.
func (o *ElementMeta) GetIdentity(accessor ElementAccessor) metav1.Identity {
identity := o.ExtraIdentity.Copy()
if identity == nil {
identity = metav1.Identity{}
}
identity := o.GetExtraIdentity()
identity[SystemIdentityName] = o.Name
if accessor != nil {
if identity.Get(SystemIdentityVersion) == "" && accessor != nil {
found := false
l := accessor.Len()
for i := 0; i < l; i++ {
m := accessor.Get(i).GetMeta()
if m.Name == o.Name && m.ExtraIdentity.Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version
break
if m.GetName() == o.Name {
mid := m.GetExtraIdentity()
mid.Remove(SystemIdentityVersion)
if mid.Equals(o.ExtraIdentity) {
if found {
identity[SystemIdentityVersion] = o.Version
break
}
found = true
}
found = true
}
}
}
Expand All @@ -209,7 +218,7 @@ func (o *ElementMeta) GetRawIdentity() metav1.Identity {
identity = metav1.Identity{}
}
identity[SystemIdentityName] = o.Name
if o.Version != "" {
if _, ok := identity[SystemIdentityVersion]; !ok && o.Version != "" {
identity[SystemIdentityVersion] = o.Version
}
return identity
Expand Down
50 changes: 50 additions & 0 deletions api/ocm/compdesc/versions/v2/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v2_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
me "ocm.software/ocm/api/ocm/compdesc/versions/v2"
)

var _ = Describe("Extra Identity Test Environment", func() {
It("handles complete defaulting", func() {
resources := me.Resources{
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v1",
},
},
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v2",
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2")))
})

It("handles partial defaulting", func() {
resources := me.Resources{
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v1",
ExtraIdentity: metav1.NewExtraIdentity(me.SystemIdentityVersion, "v1"),
},
},
me.Resource{
ElementMeta: me.ElementMeta{
Name: "res",
Version: "v2",
},
},
}
Expect(resources[0].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v1")))
Expect(resources[1].GetIdentity(resources)).To(Equal(metav1.NewIdentity("res", me.SystemIdentityVersion, "v2")))
})
})
13 changes: 13 additions & 0 deletions api/ocm/compdesc/versions/v2/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package v2_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "v2 Test Suite")
}
6 changes: 0 additions & 6 deletions api/ocm/compdesc/versions/v2/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"strings"
"testing"

. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -22,11 +21,6 @@ import (
"ocm.software/ocm/api/utils/runtime"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "V2 Test Suite")
}

var _ = Describe("Validation", func() {
testutils.TestCompName(jsonscheme.ResourcesComponentDescriptorV2SchemaYamlBytes())

Expand Down
Loading