Skip to content

Commit 64814ac

Browse files
committed
add entity source predicates
Signed-off-by: perdasilva <[email protected]>
1 parent 5ec2c4f commit 64814ac

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package predicates
2+
3+
import (
4+
"github.com/blang/semver/v4"
5+
"github.com/operator-framework/deppy/pkg/deppy/input"
6+
7+
olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity"
8+
)
9+
10+
func WithPackageName(packageName string) input.Predicate {
11+
return func(entity *input.Entity) bool {
12+
bundleEntity := olmentity.NewBundleEntity(entity)
13+
name, err := bundleEntity.PackageName()
14+
if err != nil {
15+
return false
16+
}
17+
return name == packageName
18+
}
19+
}
20+
21+
func InSemverRange(semverRange semver.Range) input.Predicate {
22+
return func(entity *input.Entity) bool {
23+
bundleEntity := olmentity.NewBundleEntity(entity)
24+
bundleVersion, err := bundleEntity.Version()
25+
if err != nil {
26+
return false
27+
}
28+
return semverRange(*bundleVersion)
29+
}
30+
}
31+
32+
func InChannel(channelName string) input.Predicate {
33+
return func(entity *input.Entity) bool {
34+
bundleEntity := olmentity.NewBundleEntity(entity)
35+
bundleChannel, err := bundleEntity.ChannelName()
36+
if err != nil {
37+
return false
38+
}
39+
return channelName == bundleChannel
40+
}
41+
}
42+
43+
func ProvidesGVK(gvk *olmentity.GVK) input.Predicate {
44+
return func(entity *input.Entity) bool {
45+
bundleEntity := olmentity.NewBundleEntity(entity)
46+
providedGVKs, err := bundleEntity.ProvidedGVKs()
47+
if err != nil {
48+
return false
49+
}
50+
for i := 0; i < len(providedGVKs); i++ {
51+
providedGVK := &providedGVKs[i]
52+
if providedGVK.String() == gvk.String() {
53+
return true
54+
}
55+
}
56+
return false
57+
}
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package predicates_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/blang/semver/v4"
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"github.com/operator-framework/deppy/pkg/deppy/input"
10+
"github.com/operator-framework/operator-registry/alpha/property"
11+
12+
olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity"
13+
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/predicates"
14+
)
15+
16+
func TestPredicates(t *testing.T) {
17+
RegisterFailHandler(Fail)
18+
RunSpecs(t, "Predicates Suite")
19+
}
20+
21+
var _ = Describe("Predicates", func() {
22+
Describe("WithPackageName", func() {
23+
It("should return true when the entity has the same package name", func() {
24+
entity := input.NewEntity("test", map[string]string{
25+
property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`,
26+
})
27+
Expect(predicates.WithPackageName("mypackage")(entity)).To(BeTrue())
28+
Expect(predicates.WithPackageName("notmypackage")(entity)).To(BeFalse())
29+
})
30+
})
31+
32+
Describe("InSemverRange", func() {
33+
It("should return true when the entity has the has version in the right range", func() {
34+
entity := input.NewEntity("test", map[string]string{
35+
property.TypePackage: `{"packageName": "mypackage", "version": "1.0.0"}`,
36+
})
37+
inRange := semver.MustParseRange(">=1.0.0")
38+
notInRange := semver.MustParseRange(">=2.0.0")
39+
Expect(predicates.InSemverRange(inRange)(entity)).To(BeTrue())
40+
Expect(predicates.InSemverRange(notInRange)(entity)).To(BeFalse())
41+
})
42+
})
43+
44+
Describe("InChannel", func() {
45+
It("should return true when the entity has the has version in the right range", func() {
46+
entity := input.NewEntity("test", map[string]string{
47+
property.TypeChannel: `{"channelName":"stable","priority":0}`,
48+
})
49+
Expect(predicates.InChannel("stable")(entity)).To(BeTrue())
50+
Expect(predicates.InChannel("unstable")(entity)).To(BeFalse())
51+
})
52+
})
53+
54+
Describe("ProvidesGVK", func() {
55+
It("should return true when the entity provides the specified gvk", func() {
56+
entity := input.NewEntity("test", map[string]string{
57+
property.TypeGVK: `[{"group":"foo.io","kind":"Foo","version":"v1"},{"group":"bar.io","kind":"Bar","version":"v1"}]`,
58+
})
59+
Expect(predicates.ProvidesGVK(&olmentity.GVK{
60+
Group: "foo.io",
61+
Version: "v1",
62+
Kind: "Foo",
63+
})(entity)).To(BeTrue())
64+
Expect(predicates.ProvidesGVK(&olmentity.GVK{
65+
Group: "baz.io",
66+
Version: "v1alpha1",
67+
Kind: "Baz",
68+
})(entity)).To(BeFalse())
69+
})
70+
})
71+
})

0 commit comments

Comments
 (0)