-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix and extend label access functions (#484)
- Loading branch information
1 parent
79a0fc5
commit 651f3eb
Showing
3 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
v1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" | ||
. "github.com/open-component-model/ocm/pkg/testutils" | ||
) | ||
|
||
var _ = Describe("labels", func() { | ||
Context("access", func() { | ||
It("modififies label set", func() { | ||
labels := v1.Labels{} | ||
|
||
MustBeSuccessful(labels.Set("l1", "v1")) | ||
MustBeSuccessful(labels.Set("l2", "v2", v1.WithSigning())) | ||
MustBeSuccessful(labels.Set("l3", "v3", v1.WithSigning(), v1.WithVersion("v10"))) | ||
MustBeSuccessful(labels.Set("l4", "v4", v1.WithVersion("v11"))) | ||
|
||
Expect(labels).To(ConsistOf( | ||
v1.Label{ | ||
Name: "l1", | ||
Value: []byte(`"v1"`), | ||
}, | ||
v1.Label{ | ||
Name: "l2", | ||
Value: []byte(`"v2"`), | ||
Signing: true, | ||
}, | ||
v1.Label{ | ||
Name: "l3", | ||
Value: []byte(`"v3"`), | ||
Signing: true, | ||
Version: "v10", | ||
}, | ||
v1.Label{ | ||
Name: "l4", | ||
Value: []byte(`"v4"`), | ||
Version: "v11", | ||
}, | ||
)) | ||
|
||
Expect(labels.GetDef("l3")).To(Equal(&v1.Label{ | ||
Name: "l3", | ||
Value: []byte(`"v3"`), | ||
Signing: true, | ||
Version: "v10", | ||
})) | ||
|
||
Expect(labels.GetIndex("l3")).To(Equal(2)) | ||
Expect(labels.GetIndex("lx")).To(Equal(-1)) | ||
data, ok := labels.Get("l3") | ||
Expect(ok).To(BeTrue()) | ||
Expect(data).To(Equal([]byte(`"v3"`))) | ||
var str string | ||
Expect(labels.GetValue("l3", &str)).To(BeTrue()) | ||
Expect(str).To(Equal("v3")) | ||
|
||
// Modify | ||
|
||
labels.Set("l4", "modl4", v1.WithVersion("v100")) | ||
Expect(labels).To(ConsistOf( | ||
v1.Label{ | ||
Name: "l1", | ||
Value: []byte(`"v1"`), | ||
}, | ||
v1.Label{ | ||
Name: "l2", | ||
Value: []byte(`"v2"`), | ||
Signing: true, | ||
}, | ||
v1.Label{ | ||
Name: "l3", | ||
Value: []byte(`"v3"`), | ||
Signing: true, | ||
Version: "v10", | ||
}, | ||
v1.Label{ | ||
Name: "l4", | ||
Value: []byte(`"modl4"`), | ||
Version: "v100", | ||
}, | ||
)) | ||
Expect(labels.GetDef("l4")).To(Equal(&v1.Label{ | ||
Name: "l4", | ||
Value: []byte(`"modl4"`), | ||
Version: "v100", | ||
})) | ||
}) | ||
|
||
It("handles complex values", func() { | ||
var labels v1.Labels | ||
|
||
var nested v1.Labels | ||
nested.Set("label", "value", v1.WithSigning()) | ||
meta := &v1.ObjectMeta{ | ||
Name: "value", | ||
Version: "v1.0.0", | ||
Labels: nested, | ||
Provider: v1.Provider{ | ||
Name: "acme.org", | ||
Labels: nested, | ||
}, | ||
CreationTime: nil, | ||
} | ||
|
||
labels.Set("l1", meta) | ||
Expect(labels).To(ConsistOf( | ||
v1.Label{ | ||
Name: "l1", | ||
Value: []byte(`{"name":"value","version":"v1.0.0","labels":[{"name":"label","value":"value","signing":true}],"provider":{"name":"acme.org","labels":[{"name":"label","value":"value","signing":true}]}}`), | ||
}, | ||
)) | ||
var get v1.ObjectMeta | ||
Expect(labels.GetValue("l1", &get)).To(BeTrue()) | ||
Expect(&get).To(Equal(meta)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Component Descriptor Meta Test Suite") | ||
} |