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
6 changes: 0 additions & 6 deletions pkg/services/registry_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ func (s *RegistryService) SetAddress(address string) error {
return nil
}

// GetHostname returns the hostname for the registry service, removing the last domain part
func (s *RegistryService) GetHostname() string {
tld := s.configHandler.GetString("dns.domain", "test")
return getBasename(s.GetName()) + "." + tld
}

// =============================================================================
// Private Methods
// =============================================================================
Expand Down
114 changes: 87 additions & 27 deletions pkg/services/registry_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestRegistryService_NewRegistryService(t *testing.T) {
service := NewRegistryService(mocks.Injector)
service.shims = mocks.Shims
service.Initialize()

return service, mocks
}

Expand Down Expand Up @@ -56,7 +55,6 @@ func TestRegistryService_GetComposeConfig(t *testing.T) {
service.shims = mocks.Shims
service.Initialize()
service.SetName("registry")

return service, mocks
}

Expand Down Expand Up @@ -199,7 +197,6 @@ func TestRegistryService_SetAddress(t *testing.T) {
setup := func(t *testing.T) (*RegistryService, *Mocks) {
t.Helper()
mocks := setupMocks(t)

// Load initial config
configYAML := `
apiVersion: v1alpha1
Expand All @@ -216,16 +213,13 @@ contexts:
if err := mocks.ConfigHandler.LoadConfigString(configYAML); err != nil {
t.Fatalf("Failed to load config: %v", err)
}

service := NewRegistryService(mocks.Injector)
service.shims = mocks.Shims
service.Initialize()
service.SetName("registry")

// Reset package-level variables
registryNextPort = constants.REGISTRY_DEFAULT_HOST_PORT + 1
localRegistry = nil

return service, mocks
}

Expand Down Expand Up @@ -542,42 +536,110 @@ contexts:

func TestRegistryService_GetHostname(t *testing.T) {
setup := func(t *testing.T) (*RegistryService, *Mocks) {
t.Helper()
mocks := setupMocks(t)
mockConfigHandler := config.NewMockConfigHandler()
mockConfigHandler.GetStringFunc = func(key string, defaultValue ...string) string {
if key == "dns.domain" {
return "test"
}
return defaultValue[0]
}
mocks := setupMocks(t, &SetupOptions{
ConfigHandler: mockConfigHandler,
})
service := NewRegistryService(mocks.Injector)
service.shims = mocks.Shims
service.Initialize()

return service, mocks
}

t.Run("Success", func(t *testing.T) {
t.Run("SimpleName", func(t *testing.T) {
// Given a registry service with a simple name
service, _ := setup(t)
service.SetName("test-registry")
service.SetName("registry")

// When getting the hostname
name := service.GetHostname()

hostname := service.GetHostname()
if hostname != "test-registry.test" {
t.Errorf("GetHostname() = %v, want %v", hostname, "test-registry.test")
// Then it should return the name with the TLD
expected := "registry.test"
if name != expected {
t.Errorf("expected hostname %q, got %q", expected, name)
}
})

t.Run("LocalRegistry", func(t *testing.T) {
t.Run("DomainName", func(t *testing.T) {
// Given a registry service with a domain name
service, _ := setup(t)
service.SetName("local-registry")
service.SetName("gcr.io")

// When getting the hostname
name := service.GetHostname()

hostname := service.GetHostname()
if hostname != "local-registry.test" {
t.Errorf("GetHostname() = %v, want %v", hostname, "local-registry.test")
// Then it should return the name with the last part replaced by TLD
expected := "gcr.test"
if name != expected {
t.Errorf("expected hostname %q, got %q", expected, name)
}
})

t.Run("RemoteRegistry", func(t *testing.T) {
t.Run("MultiPartDomain", func(t *testing.T) {
// Given a registry service with a multi-part domain name
service, _ := setup(t)
service.SetName("remote-registry")
service.SetName("registry.k8s.io")

// When getting the hostname
name := service.GetHostname()

hostname := service.GetHostname()
if hostname != "remote-registry.test" {
t.Errorf("GetHostname() = %v, want %v", hostname, "remote-registry.test")
// Then it should return the name with the last part replaced by TLD
expected := "registry.k8s.test"
if name != expected {
t.Errorf("expected hostname %q, got %q", expected, name)
}
})

t.Run("EmptyName", func(t *testing.T) {
// Given a registry service with no name
service, _ := setup(t)
service.SetName("")

// When getting the hostname
name := service.GetHostname()

// Then it should return an empty string
if name != "" {
t.Errorf("expected empty hostname, got %q", name)
}
})
}

func TestRegistryService_GetContainerName(t *testing.T) {
setup := func(t *testing.T) (*RegistryService, *Mocks) {
mockConfigHandler := config.NewMockConfigHandler()
mockConfigHandler.GetStringFunc = func(key string, defaultValue ...string) string {
if key == "dns.domain" {
return "test"
}
return defaultValue[0]
}
mocks := setupMocks(t, &SetupOptions{
ConfigHandler: mockConfigHandler,
})
service := NewRegistryService(mocks.Injector)
service.Initialize()
return service, mocks
}

t.Run("DomainName", func(t *testing.T) {
// Given a registry service with a domain name
service, _ := setup(t)
service.SetName("gcr.io")

// When getting the container name
name := service.GetContainerName()

// Then it should return the name with the last part replaced by TLD
expected := "gcr.test"
if name != expected {
t.Errorf("expected container name %q, got %q", expected, name)
}
})
}
Expand All @@ -590,7 +652,6 @@ func TestRegistryService_GetName(t *testing.T) {
service.shims = mocks.Shims
service.Initialize()
service.SetName("registry")

return service, mocks
}

Expand All @@ -612,7 +673,6 @@ func TestRegistryService_SupportsWildcard(t *testing.T) {
service.shims = mocks.Shims
service.Initialize()
service.SetName("registry")

return service, mocks
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/windsorcli/cli/pkg/config"
Expand Down Expand Up @@ -110,10 +111,9 @@ func (s *BaseService) GetName() string {
return s.name
}

// GetContainerName returns the container name with the "windsor-" prefix and without the DNS domain
// GetContainerName returns the container name with the DNS domain
func (s *BaseService) GetContainerName() string {
contextName := s.configHandler.GetContext()
return fmt.Sprintf("windsor-%s-%s", contextName, s.name)
return s.GetHostname()
}

// =============================================================================
Expand All @@ -131,11 +131,17 @@ func (s *BaseService) SupportsWildcard() bool {
return false
}

// GetHostname returns the hostname for the service with the configured TLD
// GetHostname returns the hostname for the service, handling domain names specially
func (s *BaseService) GetHostname() string {
if s.name == "" {
return ""
}
tld := s.configHandler.GetString("dns.domain", "test")

if strings.Contains(s.name, ".") {
parts := strings.Split(s.name, ".")
return strings.Join(parts[:len(parts)-1], ".") + "." + tld
}

return s.name + "." + tld
}
48 changes: 48 additions & 0 deletions pkg/services/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,51 @@ func TestBaseService_SupportsWildcard(t *testing.T) {
}
})
}

func TestBaseService_GetContainerName(t *testing.T) {
setup := func(t *testing.T) (*BaseService, *Mocks) {
mockConfigHandler := config.NewMockConfigHandler()
mockConfigHandler.GetStringFunc = func(key string, defaultValue ...string) string {
if key == "dns.domain" {
return "test"
}
return defaultValue[0]
}
mocks := setupMocks(t, &SetupOptions{
ConfigHandler: mockConfigHandler,
})
service := NewBaseService(mocks.Injector)
service.shims = mocks.Shims
service.Initialize()
return service, mocks
}

t.Run("SimpleName", func(t *testing.T) {
// Given a service with a simple name
service, _ := setup(t)
service.SetName("dns")

// When getting the container name
name := service.GetContainerName()

// Then it should return the name with the TLD
expected := "dns.test"
if name != expected {
t.Errorf("expected container name %q, got %q", expected, name)
}
})

t.Run("EmptyName", func(t *testing.T) {
// Given a service with no name
service, _ := setup(t)
service.SetName("")

// When getting the container name
name := service.GetContainerName()

// Then it should return an empty string
if name != "" {
t.Errorf("expected empty container name, got %q", name)
}
})
}
2 changes: 1 addition & 1 deletion pkg/services/talos_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ contexts:
}

// And the container name should use the fallback name with context prefix
expectedContainerName := "windsor-mock-context-controlplane"
expectedContainerName := "controlplane.test"
if config.Services[0].ContainerName != expectedContainerName {
t.Errorf("expected container name %s, got %s", expectedContainerName, config.Services[0].ContainerName)
}
Expand Down
Loading