From 3c554fff5d33c6661ad28f285ccbfd21262521e1 Mon Sep 17 00:00:00 2001 From: Charles Sibbald Date: Thu, 16 Jan 2025 21:59:57 +0200 Subject: [PATCH] fix: clashing httptest ports --- pkg/http/server_test.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/http/server_test.go b/pkg/http/server_test.go index c5f7d7368f..66dc37dfbb 100644 --- a/pkg/http/server_test.go +++ b/pkg/http/server_test.go @@ -8,6 +8,7 @@ import ( "io" "log" "math/rand/v2" + "net" "net/http" "os" "testing" @@ -17,6 +18,15 @@ import ( wegohttp "github.com/weaveworks/weave-gitops/pkg/http" ) +func portInUse(port int) bool { + conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port)) + if err != nil { + return false + } + conn.Close() + return true +} + func TestMultiServerStartReturnsImmediatelyWithClosedContext(t *testing.T) { g := NewGomegaWithT(t) srv := wegohttp.MultiServer{ @@ -46,7 +56,8 @@ func TestMultiServerServesOverBothProtocols(t *testing.T) { httpPort := rand.N(49151-1024) + 1024 // #nosec G404 httpsPort := rand.N(49151-1024) + 1024 // #nosec G404 - for httpPort == httpsPort { + for httpPort == httpsPort || portInUse(httpPort) || portInUse(httpsPort) { + httpPort = rand.N(49151-1024) + 1024 // #nosec G404 httpsPort = rand.N(49151-1024) + 1024 // #nosec G404 }