From dfdb7f7ce2114cd7f2d56b79a30032dbbdb55e51 Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Fri, 14 Jul 2023 22:32:58 +0530 Subject: [PATCH] api: add mock http handler with fake MachineClientUpdater for tests --- pkg/crc/api/api_client_test.go | 6 +++--- pkg/crc/api/api_http_test.go | 2 +- pkg/crc/api/helpers_test.go | 13 +++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/crc/api/api_client_test.go b/pkg/crc/api/api_client_test.go index 0272725d0f..b8d9219eb4 100644 --- a/pkg/crc/api/api_client_test.go +++ b/pkg/crc/api/api_client_test.go @@ -31,7 +31,7 @@ func newTestClient() *testClient { fakeMachine := fakemachine.NewClient() config := setupNewInMemoryConfig() - ts := httptest.NewServer(NewMux(config, fakeMachine, &mockLogger{}, &mockTelemetry{})) + ts := httptest.NewServer(newMockMux(config, fakeMachine, &mockTelemetry{})) return &testClient{ apiClient.New(http.DefaultClient, ts.URL), @@ -250,7 +250,7 @@ func TestTelemetry(t *testing.T) { config := setupNewInMemoryConfig() telemetry := &mockTelemetry{} - ts := httptest.NewServer(NewMux(config, fakeMachine, &mockLogger{}, telemetry)) + ts := httptest.NewServer(newMockMux(config, fakeMachine, telemetry)) defer ts.Close() client := apiClient.New(http.DefaultClient, ts.URL) @@ -267,7 +267,7 @@ func TestPullSecret(t *testing.T) { fakeMachine := fakemachine.NewClient() config := setupNewInMemoryConfig() - ts := httptest.NewServer(NewMux(config, fakeMachine, &mockLogger{}, &mockTelemetry{})) + ts := httptest.NewServer(newMockMux(config, fakeMachine, &mockTelemetry{})) defer ts.Close() client := apiClient.New(http.DefaultClient, ts.URL) diff --git a/pkg/crc/api/api_http_test.go b/pkg/crc/api/api_http_test.go index f7aaa8e853..81242ec1e5 100644 --- a/pkg/crc/api/api_http_test.go +++ b/pkg/crc/api/api_http_test.go @@ -47,7 +47,7 @@ func newMockServer(pullSecretPath string) *mockServer { config := setupNewInMemoryConfig() _, _ = config.Set(crcConfig.PullSecretFile, pullSecretPath) - handler := NewHandler(config, fakeMachine, &mockLogger{}, &mockTelemetry{}) + handler := NewHandler(config, fakeMachine, &mockLogger{}, &mockTelemetry{}, fakeMachineClientUpdater) return &mockServer{ server: newServerWithRoutes(handler), diff --git a/pkg/crc/api/helpers_test.go b/pkg/crc/api/helpers_test.go index ac741c8a98..0fa2560807 100644 --- a/pkg/crc/api/helpers_test.go +++ b/pkg/crc/api/helpers_test.go @@ -1,9 +1,11 @@ package api import ( + "net/http" "strings" "github.com/crc-org/crc/v2/pkg/crc/config" + "github.com/crc-org/crc/v2/pkg/crc/machine" "github.com/crc-org/crc/v2/pkg/crc/preflight" ) @@ -54,3 +56,14 @@ func (m *mockTelemetry) UploadAction(action, _, _ string) error { m.actions = append(m.actions, action) return nil } + +func fakeMachineClientUpdater(_ *config.Config) machine.Client { + return nil +} + +func newMockMux(cfg *config.Config, fakeMachine machine.Client, telemetry Telemetry) http.Handler { + handler := NewHandler(cfg, fakeMachine, &mockLogger{}, telemetry, fakeMachineClientUpdater) + server := newServerWithRoutes(handler) + + return server.Handler() +}