Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/workstation/network/colima_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
// =============================================================================

func TestColimaNetworkManager_AssignIPs(t *testing.T) {
setup := func(t *testing.T) (*ColimaNetworkManager, *Mocks) {
setup := func(t *testing.T) (*ColimaNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewColimaNetworkManager(mocks.Runtime, mocks.SSHClient, mocks.SecureShell, mocks.NetworkInterfaceProvider)
manager.shims = mocks.Shims
return manager, mocks
Expand All @@ -42,9 +42,9 @@ func TestColimaNetworkManager_AssignIPs(t *testing.T) {
}

func TestColimaNetworkManager_ConfigureGuest(t *testing.T) {
setup := func(t *testing.T) (*ColimaNetworkManager, *Mocks) {
setup := func(t *testing.T) (*ColimaNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewColimaNetworkManager(mocks.Runtime, mocks.SSHClient, mocks.SecureShell, mocks.NetworkInterfaceProvider)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down Expand Up @@ -300,9 +300,9 @@ func TestColimaNetworkManager_ConfigureGuest(t *testing.T) {
}

func TestColimaNetworkManager_getHostIP(t *testing.T) {
setup := func(t *testing.T) (*ColimaNetworkManager, *Mocks) {
setup := func(t *testing.T) (*ColimaNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewColimaNetworkManager(mocks.Runtime, mocks.SSHClient, mocks.SecureShell, mocks.NetworkInterfaceProvider)
manager.AssignIPs([]services.Service{})
return manager, mocks
Expand Down
8 changes: 4 additions & 4 deletions pkg/workstation/network/darwin_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
// =============================================================================

func TestDarwinNetworkManager_ConfigureHostRoute(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down Expand Up @@ -164,9 +164,9 @@ func TestDarwinNetworkManager_ConfigureHostRoute(t *testing.T) {
}

func TestDarwinNetworkManager_ConfigureDNS(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down
8 changes: 4 additions & 4 deletions pkg/workstation/network/linux_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
// =============================================================================

func TestLinuxNetworkManager_ConfigureHostRoute(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down Expand Up @@ -166,9 +166,9 @@ func TestLinuxNetworkManager_ConfigureHostRoute(t *testing.T) {
}

func TestLinuxNetworkManager_ConfigureDNS(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down
55 changes: 21 additions & 34 deletions pkg/workstation/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Test Setup
// =============================================================================

type Mocks struct {
type NetworkTestMocks struct {
Runtime *runtime.Runtime
ConfigHandler config.ConfigHandler
Shell *shell.MockShell
Expand All @@ -29,14 +29,7 @@ type Mocks struct {
Shims *Shims
}

type SetupOptions struct {
ConfigHandler config.ConfigHandler
ConfigStr string
}

func setupShims(t *testing.T) *Shims {
t.Helper()

func setupDefaultShims() *Shims {
return &Shims{
Stat: func(path string) (os.FileInfo, error) { return nil, nil },
WriteFile: func(path string, data []byte, perm os.FileMode) error { return nil },
Expand All @@ -46,7 +39,7 @@ func setupShims(t *testing.T) *Shims {
}
}

func setupMocks(t *testing.T, opts ...*SetupOptions) *Mocks {
func setupNetworkMocks(t *testing.T, opts ...func(*NetworkTestMocks)) *NetworkTestMocks {
t.Helper()

// Store original directory and create temp dir
Expand Down Expand Up @@ -77,13 +70,8 @@ func setupMocks(t *testing.T, opts ...*SetupOptions) *Mocks {
return tmpDir, nil
}

// Create config handler if not provided
var configHandler config.ConfigHandler
if len(opts) > 0 && opts[0].ConfigHandler != nil {
configHandler = opts[0].ConfigHandler
} else {
configHandler = config.NewConfigHandler(mockShell)
}
// Create config handler
configHandler := config.NewConfigHandler(mockShell)

configYAML := `
version: v1alpha1
Expand All @@ -101,13 +89,6 @@ contexts:
t.Fatalf("Failed to load config: %v", err)
}

// Load optional config if provided
if len(opts) > 0 && opts[0].ConfigStr != "" {
if err := configHandler.LoadConfigString(opts[0].ConfigStr); err != nil {
t.Fatalf("Failed to load config string: %v", err)
}
}

// Configure mock shell functions
mockShell.ExecFunc = func(command string, args ...string) (string, error) {
return "", nil
Expand Down Expand Up @@ -192,19 +173,24 @@ contexts:
}

// Create mocks struct with references to the same instances
mocks := &Mocks{
mocks := &NetworkTestMocks{
Runtime: rt,
ConfigHandler: configHandler,
Shell: mockShell,
SecureShell: mockSecureShell,
SSHClient: mockSSHClient,
NetworkInterfaceProvider: mockNetworkInterfaceProvider,
Services: []*services.MockService{mockService1, mockService2},
Shims: setupShims(t),
Shims: setupDefaultShims(),
}

configHandler.SetContext("mock-context")

// Apply any overrides
for _, opt := range opts {
opt(mocks)
}

return mocks
}

Expand Down Expand Up @@ -235,9 +221,9 @@ func TestNetworkManager_NewNetworkManager(t *testing.T) {
// =============================================================================

func TestNetworkManager_AssignIPs(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
return manager, mocks
Expand Down Expand Up @@ -358,8 +344,9 @@ func TestNetworkManager_AssignIPs(t *testing.T) {
}

// Setup with mock config handler
mocks := setupMocks(t, &SetupOptions{
ConfigHandler: mockConfigHandler,
mocks := setupNetworkMocks(t, func(m *NetworkTestMocks) {
m.ConfigHandler = mockConfigHandler
m.Runtime.ConfigHandler = mockConfigHandler
})
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
Expand Down Expand Up @@ -408,9 +395,9 @@ func TestNetworkManager_AssignIPs(t *testing.T) {
}

func TestNetworkManager_ConfigureGuest(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
return manager, mocks
Expand All @@ -431,9 +418,9 @@ func TestNetworkManager_ConfigureGuest(t *testing.T) {
}

func TestNetworkManager_assignIPAddresses(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
return manager, mocks
Expand Down
8 changes: 4 additions & 4 deletions pkg/workstation/network/windows_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
// =============================================================================

func TestWindowsNetworkManager_ConfigureHostRoute(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down Expand Up @@ -139,9 +139,9 @@ func TestWindowsNetworkManager_ConfigureHostRoute(t *testing.T) {
}

func TestWindowsNetworkManager_ConfigureDNS(t *testing.T) {
setup := func(t *testing.T) (*BaseNetworkManager, *Mocks) {
setup := func(t *testing.T) (*BaseNetworkManager, *NetworkTestMocks) {
t.Helper()
mocks := setupMocks(t)
mocks := setupNetworkMocks(t)
manager := NewBaseNetworkManager(mocks.Runtime)
manager.shims = mocks.Shims
manager.AssignIPs([]services.Service{})
Expand Down
32 changes: 16 additions & 16 deletions pkg/workstation/services/dns_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
// =============================================================================

// setupDnsMocks creates and returns mock components for DNS service tests
func setupDnsMocks(t *testing.T, opts ...*SetupOptions) *Mocks {
func setupDnsMocks(t *testing.T, opts ...func(*ServicesTestMocks)) *ServicesTestMocks {
t.Helper()

// Create base mocks using setupMocks
mocks := setupMocks(t, opts...)
// Create base mocks using setupServicesMocks
mocks := setupServicesMocks(t, opts...)

// Set up shell project root
mocks.Shell.GetProjectRootFunc = func() (string, error) {
Expand All @@ -34,7 +34,7 @@ func setupDnsMocks(t *testing.T, opts ...*SetupOptions) *Mocks {
// =============================================================================

func TestNewDNSService(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand All @@ -59,7 +59,7 @@ func TestNewDNSService(t *testing.T) {
// =============================================================================

func TestDNSService_Initialize(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand All @@ -80,7 +80,7 @@ func TestDNSService_Initialize(t *testing.T) {
}

func TestDNSService_SetAddress(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down Expand Up @@ -114,8 +114,9 @@ func TestDNSService_SetAddress(t *testing.T) {
mockConfigHandler.SetFunc = func(key string, value any) error {
return fmt.Errorf("mocked error setting address")
}
mocks := setupDnsMocks(t, &SetupOptions{
ConfigHandler: mockConfigHandler,
mocks := setupDnsMocks(t, func(m *ServicesTestMocks) {
m.ConfigHandler = mockConfigHandler
m.Runtime.ConfigHandler = mockConfigHandler
})
service := NewDNSService(mocks.Runtime)
service.shims = mocks.Shims
Expand All @@ -136,7 +137,7 @@ func TestDNSService_SetAddress(t *testing.T) {
}

func TestDNSService_GetComposeConfig(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestDNSService_GetComposeConfig(t *testing.T) {
}

func TestDNSService_WriteConfig(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down Expand Up @@ -595,7 +596,6 @@ func TestDNSService_WriteConfig(t *testing.T) {
}
})


t.Run("SuccessRemovingCorefileDirectory", func(t *testing.T) {
// Given a DNSService with mock components
service, mocks := setup(t)
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestDNSService_WriteConfig(t *testing.T) {
}

func TestDNSService_SetName(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand All @@ -667,7 +667,7 @@ func TestDNSService_SetName(t *testing.T) {
}

func TestDNSService_GetName(t *testing.T) {
setupSuccess := func(t *testing.T) (*DNSService, *Mocks) {
setupSuccess := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand All @@ -677,7 +677,7 @@ func TestDNSService_GetName(t *testing.T) {
return service, mocks
}

setupError := func(t *testing.T) (*DNSService, *Mocks) {
setupError := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestDNSService_GetName(t *testing.T) {
}

func TestDNSService_GetHostname(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down Expand Up @@ -764,7 +764,7 @@ func TestDNSService_GetHostname(t *testing.T) {
}

func TestDNSService_SupportsWildcard(t *testing.T) {
setup := func(t *testing.T) (*DNSService, *Mocks) {
setup := func(t *testing.T) (*DNSService, *ServicesTestMocks) {
t.Helper()
mocks := setupDnsMocks(t)
service := NewDNSService(mocks.Runtime)
Expand Down
Loading
Loading