Skip to content

Commit

Permalink
tests: fix envtest KongPluginBindingManaged (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Dec 23, 2024
1 parent eaa360c commit e4df7ca
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions test/envtest/kongpluginbinding_managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-",
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
)
Expand All @@ -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 {
Expand Down

0 comments on commit e4df7ca

Please sign in to comment.