-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathrabbitmq_plugins_test.go
252 lines (222 loc) · 8.66 KB
/
rabbitmq_plugins_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
// RabbitMQ Cluster Operator
//
// Copyright 2020 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Mozilla Public license, Version 2.0 (the "License"). You may not use this product except in compliance wit the Mozilla Public License.
//
// This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
//
package resource_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/resource"
. "github.com/rabbitmq/cluster-operator/internal/resource"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
)
var _ = Describe("RabbitMQPlugins", func() {
Context("DesiredPlugins", func() {
When("AdditionalPlugins is empty", func() {
It("returns list of required plugins", func() {
plugins := NewRabbitmqPlugins(nil)
Expect(plugins.DesiredPlugins()).To(ConsistOf([]string{"rabbitmq_peer_discovery_k8s", "rabbitmq_prometheus", "rabbitmq_management"}))
})
})
When("AdditionalPlugins are provided", func() {
It("returns a concatenated list of plugins", func() {
morePlugins := []rabbitmqv1beta1.Plugin{"rabbitmq_shovel", "my_great_plugin"}
plugins := NewRabbitmqPlugins(morePlugins)
Expect(plugins.DesiredPlugins()).To(ConsistOf([]string{"rabbitmq_peer_discovery_k8s",
"rabbitmq_prometheus",
"rabbitmq_management",
"my_great_plugin",
"rabbitmq_shovel",
}))
})
})
When("AdditionalPlugins are provided with duplicates", func() {
It("returns a unique list of plugins", func() {
morePlugins := []rabbitmqv1beta1.Plugin{"rabbitmq_management", "rabbitmq_shovel", "my_great_plugin", "rabbitmq_shovel"}
plugins := NewRabbitmqPlugins(morePlugins)
Expect(plugins.DesiredPlugins()).To(ConsistOf([]string{"rabbitmq_peer_discovery_k8s",
"rabbitmq_prometheus",
"rabbitmq_management",
"my_great_plugin",
"rabbitmq_shovel",
}))
})
})
})
Context("PluginsConfigMap", func() {
var (
instance rabbitmqv1beta1.RabbitmqCluster
configMapBuilder *resource.RabbitmqPluginsConfigMapBuilder
builder *resource.RabbitmqResourceBuilder
scheme *runtime.Scheme
)
BeforeEach(func() {
scheme = runtime.NewScheme()
Expect(rabbitmqv1beta1.AddToScheme(scheme)).To(Succeed())
Expect(defaultscheme.AddToScheme(scheme)).To(Succeed())
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: v1.ObjectMeta{
Name: "a name",
Namespace: "a namespace",
},
}
builder = &resource.RabbitmqResourceBuilder{
Instance: &instance,
Scheme: scheme,
}
configMapBuilder = builder.RabbitmqPluginsConfigMap()
})
Context("Build", func() {
var configMap *corev1.ConfigMap
BeforeEach(func() {
instance.Labels = map[string]string{
"app.kubernetes.io/foo": "bar",
"foo": "bar",
"rabbitmq": "is-great",
"foo/app.kubernetes.io": "edgecase",
}
instance.Annotations = map[string]string{
"my-annotation": "i-like-this",
"kubernetes.io/name": "i-do-not-like-this",
"kubectl.kubernetes.io/name": "i-do-not-like-this",
"k8s.io/name": "i-do-not-like-this",
"kubernetes.io/other": "i-do-not-like-this",
"kubectl.kubernetes.io/other": "i-do-not-like-this",
"k8s.io/other": "i-do-not-like-this",
}
obj, err := configMapBuilder.Build()
configMap = obj.(*corev1.ConfigMap)
Expect(err).NotTo(HaveOccurred())
})
It("generates a ConfigMap with the correct name and namespace", func() {
Expect(configMap.Name).To(Equal(builder.Instance.ChildResourceName("plugins-conf")))
Expect(configMap.Namespace).To(Equal(builder.Instance.Namespace))
})
It("adds list of default plugins", func() {
expectedEnabledPlugins := "[" +
"rabbitmq_peer_discovery_k8s," +
"rabbitmq_prometheus," +
"rabbitmq_management]."
obj, err := configMapBuilder.Build()
Expect(err).NotTo(HaveOccurred())
configMap = obj.(*corev1.ConfigMap)
Expect(configMap.Data).To(HaveKeyWithValue("enabled_plugins", expectedEnabledPlugins))
})
It("adds labels from the instance and default labels", func() {
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(6),
HaveKeyWithValue("foo", "bar"),
HaveKeyWithValue("rabbitmq", "is-great"),
HaveKeyWithValue("foo/app.kubernetes.io", "edgecase"),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Not(HaveKey("app.kubernetes.io/foo")),
))
})
It("adds annotations from the instance", func() {
Expect(configMap.Annotations).To(SatisfyAll(
HaveLen(1),
HaveKeyWithValue("my-annotation", "i-like-this"),
Not(HaveKey("kubernetes.io/name")),
Not(HaveKey("kubectl.kubernetes.io/name")),
Not(HaveKey("k8s.io/name")),
Not(HaveKey("kubernetes.io/other")),
Not(HaveKey("kubectl.kubernetes.io/other")),
Not(HaveKey("k8s.io/other")),
))
})
})
Context("Update", func() {
var configMap *corev1.ConfigMap
BeforeEach(func() {
configMap = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
},
}
})
It("sets owner reference", func() {
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbit1",
},
}
Expect(configMapBuilder.Update(configMap)).NotTo(HaveOccurred())
Expect(configMap.OwnerReferences[0].Name).To(Equal(instance.Name))
})
When("additionalPlugins are provided in instance spec", func() {
When("no previous data is present", func() {
It("creates data and sets enabled_plugins", func() {
builder.Instance.Spec.Rabbitmq.AdditionalPlugins = []rabbitmqv1beta1.Plugin{"rabbitmq_management", "rabbitmq_management", "rabbitmq_shovel", "my_great_plugin"}
expectedEnabledPlugins := "[" +
"rabbitmq_peer_discovery_k8s," +
"rabbitmq_prometheus," +
"rabbitmq_management," +
"rabbitmq_shovel," +
"my_great_plugin]."
err := configMapBuilder.Update(configMap)
Expect(err).NotTo(HaveOccurred())
Expect(configMap.Data).To(HaveKeyWithValue("enabled_plugins", expectedEnabledPlugins))
})
})
When("previous data is present", func() {
BeforeEach(func() {
configMap.Data = map[string]string{
"enabled_plugins": "[rabbitmq_peer_discovery_k8s,rabbitmq_shovel]",
}
})
It("updates enabled_plugins with unique list of default and additionalPlugins", func() {
builder.Instance.Spec.Rabbitmq.AdditionalPlugins = []rabbitmqv1beta1.Plugin{"rabbitmq_management", "rabbitmq_management", "rabbitmq_shovel", "my_great_plugin"}
expectedEnabledPlugins := "[" +
"rabbitmq_peer_discovery_k8s," +
"rabbitmq_prometheus," +
"rabbitmq_management," +
"rabbitmq_shovel," +
"my_great_plugin]."
err := configMapBuilder.Update(configMap)
Expect(err).NotTo(HaveOccurred())
Expect(configMap.Data).To(HaveKeyWithValue("enabled_plugins", expectedEnabledPlugins))
})
})
})
// ensures that we are not unnecessarily running `rabbitmq-plugins set` when CR labels are updated
It("does not update labels on the config map", func() {
configMap.Labels = map[string]string{
"app.kubernetes.io/name": instance.Name,
"app.kubernetes.io/component": "rabbitmq",
"app.kubernetes.io/part-of": "rabbitmq",
}
instance.Labels = map[string]string{
"new-label": "test",
}
Expect(configMapBuilder.Update(configMap)).To(Succeed())
Expect(configMap.Labels).To(SatisfyAll(
HaveLen(3),
HaveKeyWithValue("app.kubernetes.io/name", instance.Name),
HaveKeyWithValue("app.kubernetes.io/component", "rabbitmq"),
HaveKeyWithValue("app.kubernetes.io/part-of", "rabbitmq"),
Not(HaveKey("new-label")),
))
})
// ensures that we are not unnecessarily running `rabbitmq-plugins set` when CR annotations are updated
It("does not update annotations on the config map", func() {
instance.Annotations = map[string]string{
"new-annotation": "test",
}
Expect(configMapBuilder.Update(configMap)).To(Succeed())
Expect(configMap.Annotations).To(BeEmpty())
})
})
})
})