From 1d8efeae5f6bd56f25661534ae90d26a639af1e1 Mon Sep 17 00:00:00 2001 From: Paul Jouvanceau Date: Mon, 31 Aug 2026 11:19:01 +0200 Subject: [PATCH] fix: use dynamic ports in sgcp tests to avoid port 1215 conflict --- util/sgcp/api_test.go | 17 ++++------------- util/sgcp/auth_test.go | 16 +++++----------- util/sgcp/file_test.go | 14 +++----------- 3 files changed, 12 insertions(+), 35 deletions(-) diff --git a/util/sgcp/api_test.go b/util/sgcp/api_test.go index e545b007f..22bbd4cd2 100644 --- a/util/sgcp/api_test.go +++ b/util/sgcp/api_test.go @@ -3,7 +3,6 @@ package sgcp import ( "context" "encoding/json" - "net" "net/http" "net/http/httptest" "strings" @@ -49,16 +48,7 @@ func TestDo(t *testing.T) { } }) - ts := httptest.NewUnstartedServer(handler) - - ln, err := net.Listen("tcp", "127.0.0.1:1215") - if err != nil { - t.Fatal(err) - } - - ts.EnableHTTP2 = true - ts.Listener = ln - ts.StartTLS() + ts := httptest.NewTLSServer(handler) defer ts.Close() client := ts.Client() @@ -73,11 +63,12 @@ func TestDo(t *testing.T) { ctx := context.Background() // First request should hit the server - code, data1, err := api.do(ctx, "GET", "https://127.0.0.1:1215/foo/bar", nil, "scope1", "scope2") + url := ts.URL + "/foo/bar" + code, data1, err := api.do(ctx, "GET", url, nil, "scope1", "scope2") require.Equal(t, 200, code) require.NoError(t, err) t.Logf("First request result: %s", string(data1)) - assert.Equal(t, string([]byte(`{"test":["test"]}`)), strings.TrimSuffix(string(data1), "\n")) + assert.Equal(t, `{"test":["test"]}`, strings.TrimSuffix(string(data1), "\n")) } // TestCheckStatusCode tests the status code checking diff --git a/util/sgcp/auth_test.go b/util/sgcp/auth_test.go index 9218af6ff..bbb8c5aaf 100644 --- a/util/sgcp/auth_test.go +++ b/util/sgcp/auth_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/base64" "encoding/json" - "net" "net/http" "net/http/httptest" "strings" @@ -18,6 +17,8 @@ import ( func TestTokenFactory(t *testing.T) { defer Setup(t)() + t.Setenv("OSVC_VAR_DIR", t.TempDir()) + cfg := GetConfig() require.NotNil(t, cfg) @@ -59,18 +60,11 @@ func TestTokenFactory(t *testing.T) { w.WriteHeader(http.StatusNotFound) }) - ts := httptest.NewUnstartedServer(handler) - - ln, err := net.Listen("tcp", "127.0.0.1:1215") - if err != nil { - t.Fatal(err) - } - - ts.EnableHTTP2 = true - ts.Listener = ln - ts.StartTLS() + ts := httptest.NewTLSServer(handler) defer ts.Close() + cfg.Auth.BaseURL = ts.URL + "/auth" + authInfo := &AuthInfo{ AccountID: "account1", ClientID: "clientid1", diff --git a/util/sgcp/file_test.go b/util/sgcp/file_test.go index b419b3b2f..dff623753 100644 --- a/util/sgcp/file_test.go +++ b/util/sgcp/file_test.go @@ -3,7 +3,6 @@ package sgcp import ( "context" "encoding/json" - "net" "net/http" "net/http/httptest" "testing" @@ -66,18 +65,11 @@ func TestGetFilesystem(t *testing.T) { w.WriteHeader(http.StatusNotFound) }) - ts := httptest.NewUnstartedServer(handler) - - ln, err := net.Listen("tcp", "127.0.0.1:1215") - if err != nil { - t.Fatal(err) - } - - ts.EnableHTTP2 = true - ts.Listener = ln - ts.StartTLS() + ts := httptest.NewTLSServer(handler) defer ts.Close() + cfg.Files.BaseURL = ts.URL + "/file" + client := ts.Client() log := plog.NewDefaultLogger() tk := &TTkBuilder{}