diff --git a/test/envtest/kongpluginbinding_managed_test.go b/test/envtest/kongpluginbinding_managed_test.go index 845aaba8..d02c6c70 100644 --- a/test/envtest/kongpluginbinding_managed_test.go +++ b/test/envtest/kongpluginbinding_managed_test.go @@ -68,6 +68,17 @@ func TestKongPluginBindingManaged(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + deleteKongPluginBinding := func( + t *testing.T, + ctx context.Context, + cl client.Client, + kpb *configurationv1alpha1.KongPluginBinding, + obj client.Object, + ) { + t.Logf("delete managed kongPluginBinding %s (bound to %s), then check it gets recreated", client.ObjectKeyFromObject(kpb), client.ObjectKeyFromObject(obj)) + require.NoError(t, client.IgnoreNotFound(cl.Delete(ctx, kpb))) + } + rateLimitingkongPlugin := &configurationv1.KongPlugin{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "rate-limiting-kp-", @@ -322,8 +333,7 @@ func TestKongPluginBindingManaged(t *testing.T) { updateKongRouteStatusWithProgrammed(t, ctx, clientNamespaced, kongRoute, routeID, cp.GetKonnectStatus().GetKonnectID(), serviceID) t.Logf("waiting for 2 KongPluginBindings to be created") - kpbRouteFound := false - kpbServiceFound := false + var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name { @@ -334,13 +344,13 @@ func TestKongPluginBindingManaged(t *testing.T) { if targets.RouteReference != nil && targets.RouteReference.Name == kongRoute.Name && targets.ServiceReference == nil { - kpbRouteFound = true + kpbRoute = kpb } else if targets.RouteReference == nil && targets.ServiceReference != nil && targets.ServiceReference.Name == kongService.Name { - kpbServiceFound = true + kpbService = kpb } - return kpbRouteFound && kpbServiceFound + return kpbRoute != nil && kpbService != nil }, "2 KongPluginBindings were not created", ) @@ -356,14 +366,9 @@ func TestKongPluginBindingManaged(t *testing.T) { "KongPlugin wasn't updated to get plugin-in-use finalizer added", ) - var l configurationv1alpha1.KongPluginBindingList - require.NoError(t, clientNamespaced.List(ctx, &l)) - for _, kpb := range l.Items { - t.Logf("delete managed kongPluginBinding %s, then check it gets recreated", client.ObjectKeyFromObject(&kpb)) - require.NoError(t, client.IgnoreNotFound(clientNamespaced.Delete(ctx, &kpb))) - } + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbRoute, kongRoute) + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbService, kongService) - var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name { @@ -463,8 +468,7 @@ func TestKongPluginBindingManaged(t *testing.T) { updateKongConsumerStatusWithKonnectID(t, ctx, clientNamespaced, kongConsumer, consumerID, cp.GetKonnectStatus().GetKonnectID()) t.Logf("waiting for 2 KongPluginBindings to be created") - kpbRouteFound := false - kpbServiceFound := false + var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name { @@ -477,15 +481,15 @@ func TestKongPluginBindingManaged(t *testing.T) { targets.ConsumerReference != nil && targets.ConsumerReference.Name == kongConsumer.Name && targets.ServiceReference == nil { - kpbRouteFound = true + kpbRoute = kpb } else if targets.RouteReference == nil && targets.ServiceReference != nil && targets.ServiceReference.Name == kongService.Name && targets.ConsumerReference != nil && targets.ConsumerReference.Name == kongConsumer.Name { - kpbServiceFound = true + kpbService = kpb } - return kpbRouteFound && kpbServiceFound + return kpbRoute != nil && kpbService != nil }, "2 KongPluginBindings were not created", ) @@ -501,14 +505,9 @@ func TestKongPluginBindingManaged(t *testing.T) { "KongPlugin wasn't updated to get plugin-in-use finalizer added", ) - var l configurationv1alpha1.KongPluginBindingList - require.NoError(t, clientNamespaced.List(ctx, &l)) - for _, kpb := range l.Items { - t.Logf("delete managed kongPluginBinding %s, then check it gets recreated", client.ObjectKeyFromObject(&kpb)) - require.NoError(t, client.IgnoreNotFound(clientNamespaced.Delete(ctx, &kpb))) - } + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbRoute, kongRoute) + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbService, kongService) - var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name { @@ -668,8 +667,7 @@ func TestKongPluginBindingManaged(t *testing.T) { updateKongConsumerGroupStatusWithKonnectID(t, ctx, clientNamespaced, kongConsumerGroup, consumerGroupID, cp.GetKonnectStatus().GetKonnectID()) t.Logf("waiting for 2 KongPluginBindings to be created") - kpbRouteFound := false - kpbServiceFound := false + var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name { @@ -682,15 +680,15 @@ func TestKongPluginBindingManaged(t *testing.T) { targets.ConsumerGroupReference != nil && targets.ConsumerGroupReference.Name == kongConsumerGroup.Name && targets.ServiceReference == nil { - kpbRouteFound = true + kpbRoute = kpb } else if targets.RouteReference == nil && targets.ServiceReference != nil && targets.ServiceReference.Name == kongService.Name && targets.ConsumerGroupReference != nil && targets.ConsumerGroupReference.Name == kongConsumerGroup.Name { - kpbServiceFound = true + kpbService = kpb } - return kpbRouteFound && kpbServiceFound + return kpbRoute != nil && kpbService != nil }, "2 KongPluginBindings were not created", ) @@ -706,14 +704,9 @@ func TestKongPluginBindingManaged(t *testing.T) { "KongPlugin wasn't updated to get plugin-in-use finalizer added", ) - var l configurationv1alpha1.KongPluginBindingList - require.NoError(t, clientNamespaced.List(ctx, &l)) - for _, kpb := range l.Items { - t.Logf("delete managed kongPluginBinding %s, then check it gets recreated", client.ObjectKeyFromObject(&kpb)) - require.NoError(t, client.IgnoreNotFound(clientNamespaced.Delete(ctx, &kpb))) - } + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbRoute, kongRoute) + deleteKongPluginBinding(t, ctx, clientNamespaced, kpbService, kongService) - var kpbRoute, kpbService *configurationv1alpha1.KongPluginBinding watchFor(t, ctx, wKongPluginBinding, watch.Added, func(kpb *configurationv1alpha1.KongPluginBinding) bool { if kpb.Spec.PluginReference.Name != rateLimitingkongPlugin.Name {