88 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
99 "io"
1010 "k8s.io/apimachinery/pkg/labels"
11+ "k8s.io/utils/ptr"
1112 "net/http"
1213 "time"
1314
@@ -16,6 +17,7 @@ import (
1617 "sigs.k8s.io/controller-runtime/pkg/cache"
1718 runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
1819 "sigs.k8s.io/controller-runtime/pkg/config"
20+ k "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
1921 "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2022
2123 rabbithole "github.com/michaelklishin/rabbit-hole/v3"
@@ -105,6 +107,15 @@ var _ = Describe("UserController", func() {
105107 }
106108 }
107109
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+
108119 AfterEach (func () {
109120 managerCancel ()
110121 // Sad workaround to avoid controllers racing for the reconciliation of other's
@@ -188,6 +199,11 @@ var _ = Describe("UserController", func() {
188199 })
189200
190201 Context ("user limits" , func () {
202+ AfterEach (func () {
203+ userLimits .Connections = nil
204+ userLimits .Channels = nil
205+ })
206+
191207 When ("the user has limits defined" , func () {
192208 BeforeEach (func () {
193209 userName = "test-user-limits"
@@ -422,4 +438,39 @@ var _ = Describe("UserController", func() {
422438 })
423439 })
424440 })
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+ })
425476})
0 commit comments