@@ -15,6 +15,7 @@ import (
1515"sigs.k8s.io/controller-runtime/pkg/config"
1616"sigs.k8s.io/controller-runtime/pkg/metrics/server"
1717
18+ rabbithole "github.com/michaelklishin/rabbit-hole/v3"
1819. "github.com/onsi/ginkgo/v2"
1920. "github.com/onsi/gomega"
2021. "github.com/onsi/gomega/gstruct"
@@ -176,6 +177,11 @@ var _ = Describe("UserController", func() {
176177Status : "201 Created" ,
177178StatusCode : http .StatusCreated ,
178179}, nil )
180+ fakeRabbitMQClient .GetUserLimitsReturns (nil , rabbithole.ErrorResponse {
181+ StatusCode : 404 ,
182+ Message : "Object Not Found" ,
183+ Reason : "Not Found" ,
184+ })
179185})
180186
181187It ("should create the user limits" , func () {
@@ -203,6 +209,64 @@ var _ = Describe("UserController", func() {
203209Expect (userLimitsValues ).To (HaveKeyWithValue ("max-channels" , (int (channels ))))
204210})
205211})
212+
213+ When ("the user already has existing limits that differ from the new limits" , func () {
214+ BeforeEach (func () {
215+ userName = "test-changed-user-limits"
216+ connections = 5
217+ userLimits = topology.UserLimits {
218+ Connections : & connections ,
219+ Channels : nil ,
220+ }
221+ var userLimitsInfo []rabbithole.UserLimitsInfo
222+ userLimitsInfo = append (userLimitsInfo , rabbithole.UserLimitsInfo {
223+ User : userName ,
224+ Value : rabbithole.UserLimitsValues {"max-channels" : 10 , "max-connections" : 3 },
225+ })
226+ fakeRabbitMQClient .PutUserReturns (& http.Response {
227+ Status : "201 Created" ,
228+ StatusCode : http .StatusCreated ,
229+ }, nil )
230+ fakeRabbitMQClient .PutUserLimitsReturns (& http.Response {
231+ Status : "201 Created" ,
232+ StatusCode : http .StatusCreated ,
233+ }, nil )
234+ fakeRabbitMQClient .GetUserLimitsReturns (userLimitsInfo , nil )
235+ fakeRabbitMQClient .DeleteUserLimitsReturns (& http.Response {
236+ Status : "204 No Content" ,
237+ StatusCode : http .StatusNoContent ,
238+ }, nil )
239+ })
240+
241+ It ("should update the existing user limit and delete the unused old limit" , func () {
242+ Expect (k8sClient .Create (ctx , & user )).To (Succeed ())
243+ Eventually (func () []topology.Condition {
244+ _ = k8sClient .Get (
245+ ctx ,
246+ types.NamespacedName {Name : user .Name , Namespace : user .Namespace },
247+ & user ,
248+ )
249+
250+ return user .Status .Conditions
251+ }).
252+ Within (statusEventsUpdateTimeout ).
253+ WithPolling (time .Second ).
254+ Should (ContainElement (MatchFields (IgnoreExtras , Fields {
255+ "Type" : Equal (topology .ConditionType ("Ready" )),
256+ "Reason" : Equal ("SuccessfulCreateOrUpdate" ),
257+ "Status" : Equal (corev1 .ConditionTrue ),
258+ })))
259+ By ("calling DeleteUserLimits with the unused old user limits" )
260+ Expect (fakeRabbitMQClient .DeleteUserLimitsCallCount ()).To (BeNumerically (">" , 0 ))
261+ _ , userLimits := fakeRabbitMQClient .DeleteUserLimitsArgsForCall (0 )
262+ Expect (userLimits ).To (HaveLen (1 ))
263+ Expect (userLimits ).To (ContainElement ("max-channels" ))
264+ By ("calling PutUserLimits with the correct new user limits" )
265+ Expect (fakeRabbitMQClient .PutUserLimitsCallCount ()).To (BeNumerically (">" , 0 ))
266+ _ , userLimitsValues := fakeRabbitMQClient .PutUserLimitsArgsForCall (0 )
267+ Expect (userLimitsValues ).To (HaveKeyWithValue ("max-connections" , int (connections )))
268+ })
269+ })
206270})
207271
208272When ("deleting a user" , func () {
0 commit comments