|
| 1 | +package v1beta1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/runtime" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("OperatorPolicy", func() { |
| 15 | + var ( |
| 16 | + namespace = "default" |
| 17 | + ctx = context.Background() |
| 18 | + ) |
| 19 | + |
| 20 | + It("creates an operator policy with minimal configurations", func() { |
| 21 | + policy := OperatorPolicy{ |
| 22 | + ObjectMeta: metav1.ObjectMeta{ |
| 23 | + Name: "test-operator-policy", |
| 24 | + Namespace: namespace, |
| 25 | + }, |
| 26 | + Spec: OperatorPolicySpec{ |
| 27 | + Name: "test-operator-policy", |
| 28 | + Pattern: "^some-prefix", |
| 29 | + Definition: &runtime.RawExtension{ |
| 30 | + Raw: []byte(`{"max-length": 10}`), |
| 31 | + }, |
| 32 | + RabbitmqClusterReference: RabbitmqClusterReference{ |
| 33 | + Name: "some-cluster", |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + Expect(k8sClient.Create(ctx, &policy)).To(Succeed()) |
| 38 | + fetched := &OperatorPolicy{} |
| 39 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 40 | + Name: policy.Name, |
| 41 | + Namespace: policy.Namespace, |
| 42 | + }, fetched)).To(Succeed()) |
| 43 | + Expect(fetched.Spec.RabbitmqClusterReference).To(Equal(RabbitmqClusterReference{ |
| 44 | + Name: "some-cluster", |
| 45 | + })) |
| 46 | + Expect(fetched.Spec.Name).To(Equal("test-operator-policy")) |
| 47 | + Expect(fetched.Spec.Vhost).To(Equal("/")) |
| 48 | + Expect(fetched.Spec.Pattern).To(Equal("^some-prefix")) |
| 49 | + Expect(fetched.Spec.ApplyTo).To(Equal("queues")) |
| 50 | + Expect(fetched.Spec.Priority).To(Equal(0)) |
| 51 | + Expect(fetched.Spec.Definition.Raw).To(Equal([]byte(`{"max-length":10}`))) |
| 52 | + }) |
| 53 | + |
| 54 | + It("creates operator policy with configurations", func() { |
| 55 | + policy := OperatorPolicy{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "random-policy", |
| 58 | + Namespace: namespace, |
| 59 | + }, |
| 60 | + Spec: OperatorPolicySpec{ |
| 61 | + Name: "test-policy", |
| 62 | + Vhost: "/hello", |
| 63 | + Pattern: "*.", |
| 64 | + ApplyTo: "quorum_queues", |
| 65 | + Priority: 100, |
| 66 | + Definition: &runtime.RawExtension{ |
| 67 | + Raw: []byte(`{"max-length":10}`), |
| 68 | + }, |
| 69 | + RabbitmqClusterReference: RabbitmqClusterReference{ |
| 70 | + Name: "random-cluster", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + Expect(k8sClient.Create(ctx, &policy)).To(Succeed()) |
| 75 | + fetched := &OperatorPolicy{} |
| 76 | + Expect(k8sClient.Get(ctx, types.NamespacedName{ |
| 77 | + Name: policy.Name, |
| 78 | + Namespace: policy.Namespace, |
| 79 | + }, fetched)).To(Succeed()) |
| 80 | + |
| 81 | + Expect(fetched.Spec.Name).To(Equal("test-policy")) |
| 82 | + Expect(fetched.Spec.Vhost).To(Equal("/hello")) |
| 83 | + Expect(fetched.Spec.Pattern).To(Equal("*.")) |
| 84 | + Expect(fetched.Spec.ApplyTo).To(Equal("quorum_queues")) |
| 85 | + Expect(fetched.Spec.Priority).To(Equal(100)) |
| 86 | + Expect(fetched.Spec.RabbitmqClusterReference).To(Equal( |
| 87 | + RabbitmqClusterReference{ |
| 88 | + Name: "random-cluster", |
| 89 | + })) |
| 90 | + Expect(fetched.Spec.Definition.Raw).To(Equal([]byte(`{"max-length":10}`))) |
| 91 | + }) |
| 92 | + |
| 93 | + When("creating a policy with an invalid 'ApplyTo' value", func() { |
| 94 | + It("fails with validation errors", func() { |
| 95 | + policy := OperatorPolicy{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: "invalid", |
| 98 | + Namespace: namespace, |
| 99 | + }, |
| 100 | + Spec: OperatorPolicySpec{ |
| 101 | + Name: "test-policy", |
| 102 | + Pattern: "a-queue-name", |
| 103 | + Definition: &runtime.RawExtension{ |
| 104 | + Raw: []byte(`{"max-length":10}`), |
| 105 | + }, |
| 106 | + ApplyTo: "yo-yo", |
| 107 | + RabbitmqClusterReference: RabbitmqClusterReference{ |
| 108 | + Name: "some-cluster", |
| 109 | + }, |
| 110 | + }, |
| 111 | + } |
| 112 | + Expect(k8sClient.Create(ctx, &policy)).To(HaveOccurred()) |
| 113 | + Expect(k8sClient.Create(ctx, &policy)).To(MatchError(`OperatorPolicy.rabbitmq.com "invalid" is invalid: spec.applyTo: Unsupported value: "yo-yo": supported values: "queues", "classic_queues", "quorum_queues", "streams"`)) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | +}) |
0 commit comments