Skip to content

Commit

Permalink
api: add mock http handler with fake MachineClientUpdater for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anjannath committed Oct 11, 2023
1 parent 819350d commit dfdb7f7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/api/api_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions pkg/crc/api/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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()
}

0 comments on commit dfdb7f7

Please sign in to comment.