From a99acc5121cc424c9269ebe9e21e2b6db4ebe660 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Krienb=C3=BChl?= Date: Tue, 6 May 2025 14:12:21 +0200 Subject: [PATCH] Wait for namespaces to be removed at the end of tests This fixes a rare set of errors that would occur, because an earlier tests namespace was not completely removed yet. This is relevant for some resources that are used across namespaces (i.e, Floating IPs). --- pkg/internal/integration/main_test.go | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/internal/integration/main_test.go b/pkg/internal/integration/main_test.go index 3de90ad..f9f4e6a 100644 --- a/pkg/internal/integration/main_test.go +++ b/pkg/internal/integration/main_test.go @@ -16,7 +16,9 @@ import ( "github.com/stretchr/testify/suite" "golang.org/x/oauth2" v1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" ) @@ -181,6 +183,39 @@ func (s *IntegrationTestSuite) TearDownTest() { errors++ } + // Wait up to two minutes for the namespace to be deleted + timeout := 120 * time.Second + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + err = wait.PollUntilContextCancel(ctx, 1*time.Second, true, + func(ctx context.Context) (bool, error) { + _, err := s.k8s.CoreV1().Namespaces().Get( + ctx, + s.ns, + metav1.GetOptions{}, + ) + + // Not found, we are done + if k8serrors.IsNotFound(err) { + return true, nil + } + + // Another error, fail + if err != nil { + return false, err + } + + // Found, try again + return false, nil + }) + + if err != nil { + s.T().Logf("took too long to delete namespace %s: %s", s.ns, err) + errors++ + } + if errors > 0 { panic(fmt.Sprintf("failed cleanup test: %d errors", errors)) }