8
8
"github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
9
9
"io"
10
10
"k8s.io/apimachinery/pkg/labels"
11
+ "k8s.io/utils/ptr"
11
12
"net/http"
12
13
"time"
13
14
@@ -16,6 +17,7 @@ import (
16
17
"sigs.k8s.io/controller-runtime/pkg/cache"
17
18
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
18
19
"sigs.k8s.io/controller-runtime/pkg/config"
20
+ k "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
19
21
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
20
22
21
23
rabbithole "github.com/michaelklishin/rabbit-hole/v3"
@@ -105,6 +107,15 @@ var _ = Describe("UserController", func() {
105
107
}
106
108
}
107
109
110
+ objectStatus := func () []topology.Condition {
111
+ _ = k8sClient .Get (
112
+ ctx ,
113
+ types.NamespacedName {Name : user .Name , Namespace : user .Namespace },
114
+ & user ,
115
+ )
116
+ return user .Status .Conditions
117
+ }
118
+
108
119
AfterEach (func () {
109
120
managerCancel ()
110
121
// Sad workaround to avoid controllers racing for the reconciliation of other's
@@ -188,6 +199,11 @@ var _ = Describe("UserController", func() {
188
199
})
189
200
190
201
Context ("user limits" , func () {
202
+ AfterEach (func () {
203
+ userLimits .Connections = nil
204
+ userLimits .Channels = nil
205
+ })
206
+
191
207
When ("the user has limits defined" , func () {
192
208
BeforeEach (func () {
193
209
userName = "test-user-limits"
@@ -422,4 +438,39 @@ var _ = Describe("UserController", func() {
422
438
})
423
439
})
424
440
})
441
+
442
+ It ("sets an owner reference and does not block owner deletion" , func () {
443
+ userName = "test-owner-reference"
444
+ initialiseUser ()
445
+ user .Labels = map [string ]string {"test" : userName }
446
+ fakeRabbitMQClient .PutUserReturns (& http.Response {Status : "201 Created" , StatusCode : http .StatusCreated }, nil )
447
+ initialiseManager ("test" , userName )
448
+
449
+ Expect (k8sClient .Create (ctx , & user )).To (Succeed ())
450
+ Eventually (objectStatus ).
451
+ Within (statusEventsUpdateTimeout ).
452
+ WithPolling (time .Second ).
453
+ Should (ContainElement (MatchFields (IgnoreExtras , Fields {
454
+ "Type" : Equal (topology .ConditionType ("Ready" )),
455
+ "Reason" : Equal ("SuccessfulCreateOrUpdate" ),
456
+ "Status" : Equal (corev1 .ConditionTrue ),
457
+ })), "User should have been created and have a True Ready condition" )
458
+
459
+ generatedSecret := & corev1.Secret {ObjectMeta : metav1.ObjectMeta {Name : user .Name + "-user-credentials" , Namespace : user .Namespace }}
460
+ Eventually (k .Get (generatedSecret )).
461
+ Within (10 * time .Second ).
462
+ Should (Succeed ())
463
+
464
+ idFn := func (e any ) string {
465
+ ownRef := e .(metav1.OwnerReference )
466
+ return ownRef .Kind
467
+ }
468
+ Expect (generatedSecret .OwnerReferences ).To (MatchElements (idFn , IgnoreExtras ,
469
+ Elements {
470
+ "User" : MatchFields (IgnoreExtras , Fields {
471
+ "BlockOwnerDeletion" : Equal (ptr .To (false )),
472
+ }),
473
+ },
474
+ ))
475
+ })
425
476
})
0 commit comments