From 0f9048dd7e3670b723d9cf901aaca361cc78139f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Fri, 4 Oct 2024 15:03:36 +0200 Subject: [PATCH] tests: fix HTTPRoute update in integration test --- pkg/utils/test/predicates.go | 23 +++++++++++++++++++ .../test_kongplugininstallation.go | 20 ++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/pkg/utils/test/predicates.go b/pkg/utils/test/predicates.go index 018cbc8f8..41cd7a8c5 100644 --- a/pkg/utils/test/predicates.go +++ b/pkg/utils/test/predicates.go @@ -551,6 +551,29 @@ func DataPlaneUpdateEventually(t *testing.T, ctx context.Context, dataplaneNN ty } } +// HTTPRouteUpdateEventually is a helper function for tests that returns a function +// that can be used to update the HTTPRoute. +// Should be used in conjunction with require.Eventually or assert.Eventually. +func HTTPRouteUpdateEventually(t *testing.T, ctx context.Context, httpRouteNN types.NamespacedName, clients K8sClients, updateFunc func(*gatewayv1.HTTPRoute)) func() bool { + return func() bool { + cl := clients.GatewayClient.GatewayV1().HTTPRoutes(httpRouteNN.Namespace) + dp, err := cl.Get(ctx, httpRouteNN.Name, metav1.GetOptions{}) + if err != nil { + t.Logf("error getting HTTPRoute: %v", err) + return false + } + + updateFunc(dp) + + _, err = cl.Update(ctx, dp, metav1.UpdateOptions{}) + if err != nil { + t.Logf("error updating HTTPRoute: %v", err) + return false + } + return true + } +} + // ControlPlaneUpdateEventually is a helper function for tests that returns a function // that can be used to update the ControlPlane. // Should be used in conjunction with require.Eventually or assert.Eventually. diff --git a/test/integration/test_kongplugininstallation.go b/test/integration/test_kongplugininstallation.go index 3946a9cfa..6cc4a0389 100644 --- a/test/integration/test_kongplugininstallation.go +++ b/test/integration/test_kongplugininstallation.go @@ -306,6 +306,8 @@ func attachKPI(t *testing.T, gatewayConfigNN k8stypes.NamespacedName, kpiNN k8st } func attachKongPluginBasedOnKPIToRoute(t *testing.T, cleaner *clusters.Cleaner, httpRouteNN, kpiNN k8stypes.NamespacedName) { + t.Helper() + kongPluginName := kpiNN.Name + "-plugin" // To have it in the same namespace as the HTTPRoute to which it is attached. kongPluginNamespace := httpRouteNN.Namespace @@ -320,16 +322,16 @@ func attachKongPluginBasedOnKPIToRoute(t *testing.T, cleaner *clusters.Cleaner, require.NoError(t, err) cleaner.Add(&kongPlugin) - t.Logf("attaching KongPlugin to GatewayConfiguration") - // Update httpRoute with KongPlugin - httpRoute, err := GetClients().GatewayClient.GatewayV1().HTTPRoutes(httpRouteNN.Namespace).Get(GetCtx(), httpRouteNN.Name, metav1.GetOptions{}) - require.NoError(t, err) - const kpAnnotation = "konghq.com/plugins" - httpRoute.Annotations[kpAnnotation] = strings.Join( - append(strings.Split(httpRoute.Annotations[kpAnnotation], ","), kongPluginName), ",", + t.Logf("attaching KongPlugin %s to HTTPRoute %s", kongPluginName, httpRouteNN) + require.Eventually(t, + testutils.HTTPRouteUpdateEventually(t, GetCtx(), httpRouteNN, clients, func(h *gatewayv1.HTTPRoute) { + const kpAnnotation = "konghq.com/plugins" + h.Annotations[kpAnnotation] = strings.Join( + append(strings.Split(h.Annotations[kpAnnotation], ","), kongPluginName), ",", + ) + }), + time.Minute, 250*time.Millisecond, ) - _, err = GetClients().GatewayClient.GatewayV1().HTTPRoutes(httpRouteNN.Namespace).Update(GetCtx(), httpRoute, metav1.UpdateOptions{}) - require.NoError(t, err) } func checkDataPlaneStatus(