-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathreconcile_cli_test.go
258 lines (232 loc) · 9.59 KB
/
reconcile_cli_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package controllers_test
import (
"time"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
)
var _ = Describe("Reconcile CLI", func() {
var (
cluster *rabbitmqv1beta1.RabbitmqCluster
annotations map[string]string
defaultNamespace = "default"
)
BeforeEach(func() {
annotations = map[string]string{}
})
AfterEach(func() {
Expect(client.Delete(ctx, cluster)).To(Succeed())
waitForClusterDeletion(ctx, cluster, client)
})
When("cluster is created", func() {
var sts *appsv1.StatefulSet
BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbitmq-feature-flags",
Namespace: defaultNamespace,
},
}
Expect(client.Create(ctx, cluster)).To(Succeed())
waitForClusterCreation(ctx, cluster, client)
})
It("enables all feature flags", func() {
By("annotating that StatefulSet got created", func() {
sts = statefulSet(ctx, cluster)
value := sts.ObjectMeta.Annotations["rabbitmq.com/createdAt"]
_, err := time.Parse(time.RFC3339, value)
Expect(err).NotTo(HaveOccurred(), "annotation rabbitmq.com/createdAt was not a valid RFC3339 timestamp")
})
By("enabling all feature flags once all Pods are up, and removing the annotation", func() {
sts.Status.Replicas = 1
sts.Status.ReadyReplicas = 1
Expect(client.Status().Update(ctx, sts)).To(Succeed())
Eventually(func() map[string]string {
Expect(client.Get(ctx, types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.ChildResourceName("server")}, sts)).To(Succeed())
return sts.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/createdAt"))
Expect(fakeExecutor.ExecutedCommands()).To(ContainElement(command{"bash", "-c",
"rabbitmqctl enable_feature_flag all"}))
})
})
})
When("the cluster is configured to run post-deploy steps", func() {
BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbitmq-three",
Namespace: defaultNamespace,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
Replicas: pointer.Int32(3),
},
}
Expect(client.Create(ctx, cluster)).To(Succeed())
waitForClusterCreation(ctx, cluster, client)
})
When("the cluster is updated", func() {
var sts *appsv1.StatefulSet
BeforeEach(func() {
sts = statefulSet(ctx, cluster)
sts.Status.Replicas = 3
sts.Status.CurrentReplicas = 2
sts.Status.CurrentRevision = "some-old-revision"
sts.Status.UpdatedReplicas = 1
sts.Status.UpdateRevision = "some-new-revision"
Expect(client.Status().Update(ctx, sts)).To(Succeed())
})
It("triggers the controller to run rabbitmq-queues rebalance all", func() {
By("setting an annotation on the CR", func() {
Eventually(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
Expect(client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)).To(Succeed())
annotations = rmq.ObjectMeta.Annotations
return annotations
}, 5).Should(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
_, err := time.Parse(time.RFC3339, annotations["rabbitmq.com/queueRebalanceNeededAt"])
Expect(err).NotTo(HaveOccurred(), "annotation rabbitmq.com/queueRebalanceNeededAt was not a valid RFC3339 timestamp")
})
By("not removing the annotation when all replicas are updated but not yet ready", func() {
sts.Status.CurrentReplicas = 3
sts.Status.CurrentRevision = "some-new-revision"
sts.Status.UpdatedReplicas = 3
sts.Status.UpdateRevision = "some-new-revision"
sts.Status.ReadyReplicas = 2
Expect(client.Status().Update(ctx, sts)).To(Succeed())
Eventually(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
annotations = rmq.ObjectMeta.Annotations
return annotations
}, 5).Should(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"}))
_, err := time.Parse(time.RFC3339, annotations["rabbitmq.com/queueRebalanceNeededAt"])
Expect(err).NotTo(HaveOccurred(), "Annotation rabbitmq.com/queueRebalanceNeededAt was not a valid RFC3339 timestamp")
})
By("removing the annotation once all Pods are up, and triggering the queue rebalance", func() {
sts.Status.ReadyReplicas = 3
Expect(client.Status().Update(ctx, sts)).To(Succeed())
Eventually(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
return rmq.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
Expect(fakeExecutor.ExecutedCommands()).To(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"}))
})
})
})
})
When("the cluster is not configured to run post-deploy steps", func() {
BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbitmq-three-no-post-deploy",
Namespace: defaultNamespace,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
Replicas: pointer.Int32(3),
SkipPostDeploySteps: true,
},
}
Expect(client.Create(ctx, cluster)).To(Succeed())
waitForClusterCreation(ctx, cluster, client)
})
When("the cluster is updated", func() {
var sts *appsv1.StatefulSet
BeforeEach(func() {
sts = statefulSet(ctx, cluster)
sts.Status.Replicas = 3
sts.Status.CurrentReplicas = 2
sts.Status.CurrentRevision = "some-old-revision"
sts.Status.UpdatedReplicas = 1
sts.Status.UpdateRevision = "some-new-revision"
Expect(client.Status().Update(ctx, sts)).To(Succeed())
})
It("does not trigger the controller to run rabbitmq-queues rebalance all", func() {
By("never setting the annotation on the CR", func() {
Consistently(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
return rmq.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
})
By("not running the rebalance command once all nodes are up", func() {
sts.Status.CurrentReplicas = 3
sts.Status.CurrentRevision = "some-new-revision"
sts.Status.UpdatedReplicas = 3
sts.Status.UpdateRevision = "some-new-revision"
sts.Status.ReadyReplicas = 3
Expect(client.Status().Update(ctx, sts)).To(Succeed())
Consistently(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
return rmq.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"}))
})
})
})
})
When("the cluster is a single node cluster", func() {
BeforeEach(func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbitmq-one-rebalance",
Namespace: defaultNamespace,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
Replicas: pointer.Int32(1),
SkipPostDeploySteps: false,
},
}
Expect(client.Create(ctx, cluster)).To(Succeed())
waitForClusterCreation(ctx, cluster, client)
})
When("the cluster is updated", func() {
var sts *appsv1.StatefulSet
BeforeEach(func() {
sts = statefulSet(ctx, cluster)
sts.Status.Replicas = 1
sts.Status.CurrentReplicas = 1
sts.Status.CurrentRevision = "some-old-revision"
sts.Status.UpdatedReplicas = 0
sts.Status.UpdateRevision = "some-new-revision"
sts.Status.ReadyReplicas = 0
Expect(client.Status().Update(ctx, sts)).To(Succeed())
})
It("does not trigger the controller to run rabbitmq-queues rebalance all", func() {
By("never setting the annotation on the CR", func() {
Consistently(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
return rmq.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
})
By("not running the rebalance command once all nodes are up", func() {
sts.Status.CurrentReplicas = 1
sts.Status.CurrentRevision = "some-new-revision"
sts.Status.UpdatedReplicas = 1
sts.Status.UpdateRevision = "some-new-revision"
sts.Status.ReadyReplicas = 1
Expect(client.Status().Update(ctx, sts)).To(Succeed())
Consistently(func() map[string]string {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
Expect(err).To(BeNil())
return rmq.ObjectMeta.Annotations
}, 5).ShouldNot(HaveKey("rabbitmq.com/queueRebalanceNeededAt"))
Expect(fakeExecutor.ExecutedCommands()).NotTo(ContainElement(command{"sh", "-c", "rabbitmq-queues rebalance all"}))
})
})
})
})
})