-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathrole_binding_test.go
200 lines (180 loc) · 6 KB
/
role_binding_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
// 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 with 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/v2"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
"github.com/rabbitmq/cluster-operator/v2/internal/resource"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
)
var _ = Describe("RoleBinding", func() {
var (
roleBinding *rbacv1.RoleBinding
instance rabbitmqv1beta1.RabbitmqCluster
roleBindingBuilder *resource.RoleBindingBuilder
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: metav1.ObjectMeta{
Name: "a name",
Namespace: "a namespace",
},
}
builder = &resource.RabbitmqResourceBuilder{
Instance: &instance,
Scheme: scheme,
}
roleBindingBuilder = builder.RoleBinding()
})
Context("Build", func() {
BeforeEach(func() {
obj, err := roleBindingBuilder.Build()
roleBinding = obj.(*rbacv1.RoleBinding)
Expect(err).NotTo(HaveOccurred())
})
It("generates a correct roleBinding", func() {
Expect(roleBinding.Namespace).To(Equal(builder.Instance.Namespace))
Expect(roleBinding.Name).To(Equal(builder.Instance.ChildResourceName("server")))
})
})
Context("Update", func() {
BeforeEach(func() {
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbit-labelled",
},
}
instance.Labels = map[string]string{
"app.kubernetes.io/foo": "bar",
"foo": "bar",
"rabbitmq": "is-great",
"foo/app.kubernetes.io": "edgecase",
}
roleBinding = &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app.kubernetes.io/name": instance.Name,
"app.kubernetes.io/part-of": "rabbitmq",
"this-was-the-previous-label": "should-be-deleted",
},
},
}
err := roleBindingBuilder.Update(roleBinding)
Expect(err).NotTo(HaveOccurred())
})
It("sets owner reference", func() {
Expect(roleBinding.OwnerReferences[0].Name).To(Equal(instance.Name))
})
It("adds labels from the CR", func() {
testLabels(roleBinding.Labels)
})
It("restores the default labels", func() {
labels := roleBinding.Labels
Expect(labels["app.kubernetes.io/name"]).To(Equal(instance.Name))
Expect(labels["app.kubernetes.io/component"]).To(Equal("rabbitmq"))
Expect(labels["app.kubernetes.io/part-of"]).To(Equal("rabbitmq"))
})
It("deletes the labels that are removed from the CR", func() {
Expect(roleBinding.Labels).NotTo(HaveKey("this-was-the-previous-label"))
})
})
Context("Update with required rules", func() {
BeforeEach(func() {
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbit-rolebinding",
},
}
builder.Instance = &instance
roleBinding = &rbacv1.RoleBinding{
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "RoleRoleRole",
Name: "NameNameName",
},
Subjects: []rbacv1.Subject{
{
Kind: "AccountService",
Name: "this-account-is-not-right",
},
},
}
err := roleBindingBuilder.Update(roleBinding)
Expect(err).NotTo(HaveOccurred())
})
It("sets the required role ref and subjects", func() {
expectedRoleRef := rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: "rabbit-rolebinding-peer-discovery",
}
expectedSubjects := []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: "rabbit-rolebinding-server",
},
}
Expect(roleBinding.RoleRef).To(Equal(expectedRoleRef))
Expect(roleBinding.Subjects).To(Equal(expectedSubjects))
})
})
Context("Update with instance annotations", func() {
BeforeEach(func() {
instance = rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbit-labelled",
},
}
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",
}
roleBinding = &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"old-annotation": "old-value",
"im-here-to-stay.kubernetes.io": "for-a-while",
"kubernetes.io/name": "should-stay",
"kubectl.kubernetes.io/name": "should-stay",
"k8s.io/name": "should-stay",
},
},
}
err := roleBindingBuilder.Update(roleBinding)
Expect(err).NotTo(HaveOccurred())
})
It("updates roleBinding annotations", func() {
expectedAnnotations := map[string]string{
"my-annotation": "i-like-this",
"old-annotation": "old-value",
"im-here-to-stay.kubernetes.io": "for-a-while",
"kubernetes.io/name": "should-stay",
"kubectl.kubernetes.io/name": "should-stay",
"k8s.io/name": "should-stay",
}
Expect(roleBinding.Annotations).To(Equal(expectedAnnotations))
})
})
Context("UpdateMayRequireStsRecreate", func() {
It("returns false", func() {
Expect(roleBindingBuilder.UpdateMayRequireStsRecreate()).To(BeFalse())
})
})
})