@@ -26,6 +26,7 @@ import (
2626 "github.com/kong/kong-operator/test/helpers"
2727 "github.com/kong/kong-operator/test/helpers/conditions"
2828 "github.com/kong/kong-operator/test/helpers/deploy"
29+ "github.com/kong/kong-operator/test/helpers/eventually"
2930)
3031
3132const (
@@ -244,11 +245,7 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
244245 t .Cleanup (deleteObjectAndWaitForDeletionFn (t , cp .DeepCopy ()))
245246
246247 t .Logf ("Waiting for Konnect ID to be assigned to ControlPlane %s/%s" , cp .Namespace , cp .Name )
247- require .EventuallyWithT (t , func (t * assert.CollectT ) {
248- err := GetClients ().MgrClient .Get (GetCtx (), types.NamespacedName {Name : cp .Name , Namespace : cp .Namespace }, cp )
249- require .NoError (t , err )
250- assertKonnectEntityProgrammed (t , cp )
251- }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick )
248+ cp = eventually .KonnectEntityGetsProgrammed (t , ctx , clientNamespaced , cp )
252249
253250 cpKonnectID := cp .GetKonnectID ()
254251
@@ -277,6 +274,7 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
277274 require .NotNil (t , resp .Plugin , "Should get a non-nil plugin in the response" )
278275 globalRequestTransformerPlugin := resp .Plugin
279276 require .NotNil (t , globalRequestTransformerPlugin .ID , "Should receive a non-nil plugin ID" )
277+ globalPluginID := * globalRequestTransformerPlugin .ID
280278
281279 buf , err := json .Marshal (globalRequestTransformerPlugin .Config )
282280 require .NoError (t , err , "Should marshal plugin configuration in JSON successfully" )
@@ -300,48 +298,35 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
300298 PluginReference : configurationv1alpha1.PluginRef {
301299 Name : kongPluginReqTransformer .Name ,
302300 },
303- Adopt : & commonv1alpha1.AdoptOptions {
304- From : commonv1alpha1 .AdoptSourceKonnect ,
305- Mode : commonv1alpha1 .AdoptModeOverride ,
306- Konnect : & commonv1alpha1.AdoptKonnectOptions {
307- ID : * globalRequestTransformerPlugin .ID ,
308- },
309- },
310301 Scope : configurationv1alpha1 .KongPluginBindingScopeGlobalInControlPlane ,
311302 },
312- }, deploy .WithKonnectNamespacedRefControlPlaneRef (cp ))
303+ },
304+ deploy .WithKonnectNamespacedRefControlPlaneRef (cp ),
305+ deploy .WithKonnectAdoptOptions [* configurationv1alpha1.KongPluginBinding ](commonv1alpha1 .AdoptModeOverride , globalPluginID ),
306+ )
313307 t .Cleanup (deleteObjectAndWaitForDeletionFn (t , kpbGlobal .DeepCopy ()))
314308
315309 t .Log ("Waiting for KongPluginBinding to be programmed and set Konnect ID" )
316- require .EventuallyWithT (t , func (collect * assert.CollectT ) {
317- err := clientNamespaced .Get (GetCtx (), client .ObjectKeyFromObject (kpbGlobal ), kpbGlobal )
318- require .NoError (t , err )
319-
320- assertKonnectEntityProgrammed (collect , kpbGlobal )
321- assert .Equalf (collect , * globalRequestTransformerPlugin .ID , kpbGlobal .GetKonnectID (),
322- "KongPluginBinding should set Konnect ID %s as the adopted plugin in status" ,
323- * globalRequestTransformerPlugin .ID ,
324- )
325- }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick ,
326- "Did not see KongPluginBinding set Konnect ID and Programmed condition to True" ,
310+ eventually .KonnectEntityGetsProgrammed (
311+ t , ctx , clientNamespaced , kpbGlobal ,
312+ func (t * assert.CollectT , kpb * configurationv1alpha1.KongPluginBinding ) {
313+ require .Equalf (t , globalPluginID , kpb .GetKonnectID (),
314+ "KongPluginBinding %s/%s should set Konnect ID %s as the adopted plugin in status" ,
315+ kpb .Namespace , kpb .Name , globalPluginID ,
316+ )
317+ },
327318 )
328319
329320 t .Log ("Creating a KongService to attach plugins to" )
330321 ks := deploy .KongService (t , GetCtx (), clientNamespaced , deploy .WithKonnectNamespacedRefControlPlaneRef (cp ))
331322 t .Cleanup (deleteObjectAndWaitForDeletionFn (t , ks .DeepCopy ()))
332323
333324 t .Log ("Waiting for the KongService to get a Konnect ID" )
334- var serviceKonnectID string
335- require .EventuallyWithT (t , func (collect * assert.CollectT ) {
336- err := clientNamespaced .Get (GetCtx (), client .ObjectKeyFromObject (ks ), ks )
337- require .NoError (t , err )
338-
339- assertKonnectEntityProgrammed (collect , ks )
340- assert .NotEmpty (collect , ks .GetKonnectID ())
341- serviceKonnectID = ks .GetKonnectID ()
342- }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick ,
343- "Did not see KongService set Konnect ID and Programmed condition to True" ,
325+ ks = eventually .KonnectEntityGetsProgrammed (
326+ t , ctx , clientNamespaced , ks ,
344327 )
328+ require .NotEmpty (t , ks .GetKonnectID (), "KongService should get Konnect ID when programmed" )
329+ serviceKonnectID := ks .GetKonnectID ()
345330
346331 t .Log ("Creating a plugin by SDK attached to the service for adopting" )
347332 resp , err = sdk .GetPluginSDK ().CreatePlugin (
@@ -364,59 +349,32 @@ func TestKonnectEntityAdoption_Plugin(t *testing.T) {
364349 require .NotNil (t , resp .Plugin , "Should get a non-nil plugin in the response" )
365350 serviceResponseTransformerPlugin := resp .Plugin
366351 require .NotNil (t , serviceResponseTransformerPlugin .ID , "Should receive a non-nil plugin ID" )
367-
368- buf , err = json .Marshal (serviceResponseTransformerPlugin .Config )
369- require .NoError (t , err , "Should marshal plugin configuration in JSON successfully" )
352+ pluginServiceID := * serviceResponseTransformerPlugin .ID
370353
371354 t .Log ("Creating a KongPlugin and a KongPluginBinding for adopting the plugin" )
372- kongPluginResponseTransformer := & configurationv1.KongPlugin {
373- ObjectMeta : metav1.ObjectMeta {
374- Namespace : ns .Name ,
375- Name : "kongplugin-service-response-transformer" ,
376- },
377- PluginName : "response-transformer" ,
378- Config : apiextensionsv1.JSON {
379- Raw : buf ,
380- },
381- }
382- require .NoError (t , clientNamespaced .Create (GetCtx (), kongPluginResponseTransformer ))
355+ kongPluginResponseTransformer := deploy .ResponseTransformerPlugin (t , ctx , clientNamespaced )
383356 t .Cleanup (deleteObjectAndWaitForDeletionFn (t , kongPluginResponseTransformer .DeepCopy ()))
384357
385358 kpbService := deploy .KongPluginBinding (t , GetCtx (), clientNamespaced , & configurationv1alpha1.KongPluginBinding {
386359 Spec : configurationv1alpha1.KongPluginBindingSpec {
387360 PluginReference : configurationv1alpha1.PluginRef {
388361 Name : kongPluginResponseTransformer .Name ,
389362 },
390- Adopt : & commonv1alpha1.AdoptOptions {
391- From : commonv1alpha1 .AdoptSourceKonnect ,
392- Mode : commonv1alpha1 .AdoptModeOverride ,
393- Konnect : & commonv1alpha1.AdoptKonnectOptions {
394- ID : * serviceResponseTransformerPlugin .ID ,
395- },
396- },
397- Scope : configurationv1alpha1 .KongPluginBindingScopeOnlyTargets ,
398- Targets : & configurationv1alpha1.KongPluginBindingTargets {
399- ServiceReference : & configurationv1alpha1.TargetRefWithGroupKind {
400- Name : ks .Name ,
401- Kind : "KongService" ,
402- Group : configurationv1alpha1 .GroupVersion .Group ,
403- },
404- },
405363 },
406- }, deploy .WithKonnectNamespacedRefControlPlaneRef (cp ))
364+ },
365+ deploy .WithKonnectNamespacedRefControlPlaneRef (cp ),
366+ deploy .WithKonnectAdoptOptions [* configurationv1alpha1.KongPluginBinding ](commonv1alpha1 .AdoptModeOverride , pluginServiceID ),
367+ deploy .WithKongPluginBindingTarget (ks ),
368+ )
407369 t .Cleanup (deleteObjectAndWaitForDeletionFn (t , kpbService .DeepCopy ()))
408370
409371 t .Log ("Waiting for KongPluginBinding to be programmed and set Konnect ID" )
410- require .EventuallyWithT (t , func (collect * assert.CollectT ) {
411- err := clientNamespaced .Get (GetCtx (), client .ObjectKeyFromObject (kpbService ), kpbService )
412- require .NoError (t , err )
413-
414- assertKonnectEntityProgrammed (collect , kpbService )
415- assert .Equalf (collect , * serviceResponseTransformerPlugin .ID , kpbService .GetKonnectID (),
416- "KongPluginBinding should set Konnect ID %s as the adopted plugin in status" ,
417- * serviceResponseTransformerPlugin .ID ,
418- )
419- }, testutils .ObjectUpdateTimeout , testutils .ObjectUpdateTick ,
420- "Did not see KongPluginBinding set Konnect ID and Programmed condition to True" ,
372+ eventually .KonnectEntityGetsProgrammed (t , ctx , clientNamespaced , kpbService ,
373+ func (t * assert.CollectT , kpb * configurationv1alpha1.KongPluginBinding ) {
374+ require .Equalf (t , pluginServiceID , kpb .GetKonnectID (),
375+ "KongPluginBinding %s/%s should set Konnect ID %s as the adopted plugin in status" ,
376+ kpbService .Namespace , kpbService .Name , pluginServiceID ,
377+ )
378+ },
421379 )
422380}
0 commit comments