From 31d3114a1f0a5769393b3aef453516c3922806ad Mon Sep 17 00:00:00 2001 From: Geoffrey Beausire Date: Thu, 23 Nov 2023 18:28:11 +0100 Subject: [PATCH] Fix flaky test when reconciler start before previous one still up between test, the reconciler is stopped and started again. Stop is not instant but we started a new instance right away. Since it bind a port, the new one crashed because the old one didn't release it yet. Instead we wait for the manager to be properly down before starting a new one --- .../controller/nodedisruption_controller_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/controller/nodedisruption_controller_test.go b/internal/controller/nodedisruption_controller_test.go index c41688a..0bf4401 100644 --- a/internal/controller/nodedisruption_controller_test.go +++ b/internal/controller/nodedisruption_controller_test.go @@ -59,7 +59,7 @@ func clearAllNodeDisruptionRessources() { } -func startReconcilerWithConfig(config NodeDisruptionReconcilerConfig) (cancelFn context.CancelFunc) { +func startReconcilerWithConfig(config NodeDisruptionReconcilerConfig) context.CancelFunc { k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ MetricsBindAddress: "127.0.0.1:8081", PprofBindAddress: "127.0.0.1:8082", @@ -83,14 +83,22 @@ func startReconcilerWithConfig(config NodeDisruptionReconcilerConfig) (cancelFn }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) - managerCtx, cancelFn := context.WithCancel(context.Background()) + managerCtx, cancel := context.WithCancel(context.Background()) + + shutdownChan := make(chan bool, 1) go func() { defer GinkgoRecover() err = k8sManager.Start(managerCtx) Expect(err).ToNot(HaveOccurred(), "failed to run manager") + shutdownChan <- true }() - return cancelFn + + return func() { + cancel() + // Ensure the manager is actually stopped to avoid starting a new manager too early + <-shutdownChan + } } func startDummyHTTPServer(handle http.HandlerFunc, listenAddr string) (cancelFn func()) {