From 54d3b49c23cd5fcf5e75c236c30d5f570d6caa38 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Sat, 13 Jan 2024 15:21:58 +0100 Subject: [PATCH 1/3] chore: fix jetstream flaky test Signed-off-by: Jorge Turrado --- pkg/scalers/nats_jetstream_scaler_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/scalers/nats_jetstream_scaler_test.go b/pkg/scalers/nats_jetstream_scaler_test.go index a30fa59f854..7ede15dd12a 100644 --- a/pkg/scalers/nats_jetstream_scaler_test.go +++ b/pkg/scalers/nats_jetstream_scaler_test.go @@ -287,10 +287,6 @@ func TestNATSJetStreamGetMetrics(t *testing.T) { tr := http.DefaultTransport.(*http.Transport).Clone() srv := natsMockHTTPJetStreamServer(t, mockResponseJSON) - defer func() { - srv.Close() - http.DefaultTransport = tr - }() ctx := context.Background() meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}) @@ -313,6 +309,7 @@ func TestNATSJetStreamGetMetrics(t *testing.T) { t.Errorf("Expected error for '%s' but got success %s", mockResponse.name, mockResponse.metadata.metadataTestData.authParams["natsServerMonitoringEndpoint"]) } srv.Close() + http.DefaultTransport = tr } } From 1bded29527489506e40ec91a03af1bcca2dbe6d6 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Sat, 13 Jan 2024 17:02:42 +0100 Subject: [PATCH 2/3] use specific instances instead of default client Signed-off-by: Jorge Turrado --- pkg/scalers/nats_jetstream_scaler_test.go | 35 +++++++++++------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/pkg/scalers/nats_jetstream_scaler_test.go b/pkg/scalers/nats_jetstream_scaler_test.go index 7ede15dd12a..6b3bcd00a58 100644 --- a/pkg/scalers/nats_jetstream_scaler_test.go +++ b/pkg/scalers/nats_jetstream_scaler_test.go @@ -234,8 +234,7 @@ func TestNATSJetStreamIsActive(t *testing.T) { t.Fatal("Could not parse mock response struct:", err) } - srv := natsMockHTTPJetStreamServer(t, mockResponseJSON) - defer srv.Close() + client, srv := natsMockHTTPJetStreamServer(t, mockResponseJSON) ctx := context.Background() meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}) @@ -246,7 +245,7 @@ func TestNATSJetStreamIsActive(t *testing.T) { mockJetStreamScaler := natsJetStreamScaler{ stream: nil, metadata: meta, - httpClient: http.DefaultClient, + httpClient: client, logger: InitializeLogger(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}, "nats_jetstream_scaler"), } @@ -285,8 +284,7 @@ func TestNATSJetStreamGetMetrics(t *testing.T) { t.Fatal("Could not parse mock response struct:", err) } - tr := http.DefaultTransport.(*http.Transport).Clone() - srv := natsMockHTTPJetStreamServer(t, mockResponseJSON) + client, srv := natsMockHTTPJetStreamServer(t, mockResponseJSON) ctx := context.Background() meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}) @@ -297,7 +295,7 @@ func TestNATSJetStreamGetMetrics(t *testing.T) { mockJetStreamScaler := natsJetStreamScaler{ stream: nil, metadata: meta, - httpClient: http.DefaultClient, + httpClient: client, logger: InitializeLogger(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}, "nats_jetstream_scaler"), } @@ -309,18 +307,20 @@ func TestNATSJetStreamGetMetrics(t *testing.T) { t.Errorf("Expected error for '%s' but got success %s", mockResponse.name, mockResponse.metadata.metadataTestData.authParams["natsServerMonitoringEndpoint"]) } srv.Close() - http.DefaultTransport = tr } } -func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) *httptest.Server { +func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) (*http.Client, *httptest.Server) { dialer := &net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, } // redirect leader.localhost for the clustered test - http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { + client := &http.Client{ + Transport: &http.Transport{}, + } + client.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { if strings.HasSuffix(addr, ".localhost:8222") { addr = "127.0.0.1:8222" } @@ -364,16 +364,13 @@ func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) *httptes srv.Listener = l srv.Start() - return srv + return client, srv } func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) { - tr := http.DefaultTransport.(*http.Transport).Clone() - - invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) + client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) defer func() { invalidJSONServer.Close() - http.DefaultTransport = tr }() ctx := context.Background() @@ -385,7 +382,7 @@ func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) { mockJetStreamScaler := natsJetStreamScaler{ stream: nil, metadata: meta, - httpClient: http.DefaultClient, + httpClient: client, logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"), } @@ -396,7 +393,7 @@ func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) { } func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) { - invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) + client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) defer invalidJSONServer.Close() meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}) @@ -407,7 +404,7 @@ func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) { mockJetStreamScaler := natsJetStreamScaler{ stream: nil, metadata: meta, - httpClient: http.DefaultClient, + httpClient: client, logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"), } @@ -420,7 +417,7 @@ func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) { } func TestNATSJetStreamGetNATSJetstreamServerURL(t *testing.T) { - invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) + client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`)) defer invalidJSONServer.Close() meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}) @@ -431,7 +428,7 @@ func TestNATSJetStreamGetNATSJetstreamServerURL(t *testing.T) { mockJetStreamScaler := natsJetStreamScaler{ stream: nil, metadata: meta, - httpClient: http.DefaultClient, + httpClient: client, logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"), } From c35f4de98553722946d58806e7f0378c9f7b16ff Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Sat, 13 Jan 2024 21:43:20 +0100 Subject: [PATCH 3/3] Update mock responses Signed-off-by: Jorge Turrado --- pkg/scalers/nats_jetstream_scaler_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/scalers/nats_jetstream_scaler_test.go b/pkg/scalers/nats_jetstream_scaler_test.go index 6b3bcd00a58..c73029f106f 100644 --- a/pkg/scalers/nats_jetstream_scaler_test.go +++ b/pkg/scalers/nats_jetstream_scaler_test.go @@ -222,9 +222,7 @@ var testNATSJetStreamMockResponses = []parseNATSJetStreamMockResponsesTestData{ } var testNATSJetStreamServerMockResponses = map[string][]byte{ - "not-leader-1.localhost:8222": []byte(`{"server_name": "not-leader-1", "cluster": {"urls": ["leader.localhost.nats.svc:8222", "not-leader-2.localhost.nats.svc:8222"]}}`), - "not-leader-2.localhost:8222": []byte(`{"server_name": "not-leader-2", "cluster": {"urls": ["leader.localhost.nats.svc:8222", "not-leader-1.localhost.nats.svc:8222"]}}`), - "leader.localhost:8222": []byte(`{"server_name": "leader", "cluster": {"urls": ["not-leader-1.localhost.nats.svc:8222", "not-leader-2.localhost.nats.svc:8222"]}}`), + "localhost:8222": []byte(`{"server_name": "leader", "cluster": {"urls": ["leader.localhost.nats.svc:8222","not-leader-1.localhost.nats.svc:8222", "not-leader-2.localhost.nats.svc:8222"]}}`), } func TestNATSJetStreamIsActive(t *testing.T) {