|
| 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