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