Rewrite the service registration model per the architecture plan:
Model changes:
- Domain is the unique key (removed Name field)
- Removed ExtraDomains — each domain is its own registration
- Removed SourceNode, BackendStatic, ReachOff
- Added Subdomains bool for wildcard matching control
- Files stored as <domain_with_underscores>.yaml
- API routes: /services/{domain:.+} (regex for dots in path)
- Added DeregisterBySource for cleanup before re-registration
Reconciliation changes:
- No ExtraDomains iteration — each service IS one domain
- reach:internal → sets InternalDomain (generates local=/)
- reach:public → sets Domain (no local=/)
- tcp-passthrough → DNS points to backend IP
- http → DNS points to Central IP
All 22 tests pass (9 manager + 8 handler + 5 config/cert).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
237 lines
6.0 KiB
Go
237 lines
6.0 KiB
Go
package services
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestRegisterAndGet(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
svc := Service{
|
|
Domain: "my-api.payne.io",
|
|
Source: "wild-works",
|
|
Backend: Backend{
|
|
Address: "192.168.8.60:9001",
|
|
Type: BackendHTTP,
|
|
},
|
|
Reach: ReachInternal,
|
|
}
|
|
|
|
if err := mgr.Register(svc); err != nil {
|
|
t.Fatalf("Register failed: %v", err)
|
|
}
|
|
|
|
got, err := mgr.Get("my-api.payne.io")
|
|
if err != nil {
|
|
t.Fatalf("Get failed: %v", err)
|
|
}
|
|
|
|
if got.Domain != "my-api.payne.io" {
|
|
t.Errorf("expected domain my-api.payne.io, got %s", got.Domain)
|
|
}
|
|
if got.Backend.Address != "192.168.8.60:9001" {
|
|
t.Errorf("expected backend 192.168.8.60:9001, got %s", got.Backend.Address)
|
|
}
|
|
if got.TLS != TLSTerminate {
|
|
t.Errorf("expected TLS terminate (default for http), got %s", got.TLS)
|
|
}
|
|
if got.Source != "wild-works" {
|
|
t.Errorf("expected source wild-works, got %s", got.Source)
|
|
}
|
|
}
|
|
|
|
func TestRegisterTCPPassthrough(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
svc := Service{
|
|
Domain: "cloud.payne.io",
|
|
Source: "wild-cloud",
|
|
Backend: Backend{
|
|
Address: "192.168.8.240:443",
|
|
Type: BackendTCPPassthrough,
|
|
},
|
|
Subdomains: true,
|
|
Reach: ReachPublic,
|
|
}
|
|
|
|
if err := mgr.Register(svc); err != nil {
|
|
t.Fatalf("Register failed: %v", err)
|
|
}
|
|
|
|
got, err := mgr.Get("cloud.payne.io")
|
|
if err != nil {
|
|
t.Fatalf("Get failed: %v", err)
|
|
}
|
|
|
|
if got.TLS != TLSPassthrough {
|
|
t.Errorf("expected TLS passthrough for tcp-passthrough, got %s", got.TLS)
|
|
}
|
|
if !got.Subdomains {
|
|
t.Error("expected subdomains=true")
|
|
}
|
|
if got.Reach != ReachPublic {
|
|
t.Errorf("expected reach public, got %s", got.Reach)
|
|
}
|
|
}
|
|
|
|
func TestListServices(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
for _, domain := range []string{"a.example.com", "b.example.com", "c.example.com"} {
|
|
if err := mgr.Register(Service{
|
|
Domain: domain,
|
|
Source: "test",
|
|
Backend: Backend{Address: "127.0.0.1:8080", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
}); err != nil {
|
|
t.Fatalf("Register %s failed: %v", domain, err)
|
|
}
|
|
}
|
|
|
|
svcs, err := mgr.List()
|
|
if err != nil {
|
|
t.Fatalf("List failed: %v", err)
|
|
}
|
|
if len(svcs) != 3 {
|
|
t.Errorf("expected 3 services, got %d", len(svcs))
|
|
}
|
|
}
|
|
|
|
func TestDeregister(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
if err := mgr.Register(Service{
|
|
Domain: "temp.example.com",
|
|
Source: "test",
|
|
Backend: Backend{Address: "127.0.0.1:8080", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
}); err != nil {
|
|
t.Fatalf("Register failed: %v", err)
|
|
}
|
|
|
|
if err := mgr.Deregister("temp.example.com"); err != nil {
|
|
t.Fatalf("Deregister failed: %v", err)
|
|
}
|
|
|
|
if _, err := mgr.Get("temp.example.com"); err == nil {
|
|
t.Error("expected error after deregister, got nil")
|
|
}
|
|
}
|
|
|
|
func TestDeregisterBySource(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
// Register 3 services from wild-cloud with same backend
|
|
for _, domain := range []string{"cloud.payne.io", "internal.cloud.payne.io", "payne.io"} {
|
|
mgr.Register(Service{
|
|
Domain: domain,
|
|
Source: "wild-cloud",
|
|
Backend: Backend{Address: "192.168.8.240:443", Type: BackendTCPPassthrough},
|
|
Reach: ReachPublic,
|
|
})
|
|
}
|
|
// Register 1 service from wild-works (different source)
|
|
mgr.Register(Service{
|
|
Domain: "my-api.payne.io",
|
|
Source: "wild-works",
|
|
Backend: Backend{Address: "127.0.0.1:9001", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
})
|
|
|
|
// Deregister all wild-cloud services with backend 192.168.8.240:443
|
|
if err := mgr.DeregisterBySource("wild-cloud", "192.168.8.240:443"); err != nil {
|
|
t.Fatalf("DeregisterBySource failed: %v", err)
|
|
}
|
|
|
|
svcs, _ := mgr.List()
|
|
if len(svcs) != 1 {
|
|
t.Errorf("expected 1 remaining service, got %d", len(svcs))
|
|
}
|
|
if svcs[0].Domain != "my-api.payne.io" {
|
|
t.Errorf("expected wild-works service to remain, got %s", svcs[0].Domain)
|
|
}
|
|
}
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
mgr.Register(Service{
|
|
Domain: "updatable.example.com",
|
|
Source: "test",
|
|
Backend: Backend{Address: "127.0.0.1:8080", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
})
|
|
|
|
if err := mgr.Update("updatable.example.com", map[string]any{
|
|
"reach": "public",
|
|
"subdomains": true,
|
|
}); err != nil {
|
|
t.Fatalf("Update failed: %v", err)
|
|
}
|
|
|
|
got, _ := mgr.Get("updatable.example.com")
|
|
if got.Reach != ReachPublic {
|
|
t.Errorf("expected reach public after update, got %s", got.Reach)
|
|
}
|
|
if !got.Subdomains {
|
|
t.Error("expected subdomains=true after update")
|
|
}
|
|
}
|
|
|
|
func TestRegisterValidation(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
// Missing domain
|
|
if err := mgr.Register(Service{Backend: Backend{Address: "127.0.0.1:80", Type: BackendHTTP}, Reach: ReachInternal}); err == nil {
|
|
t.Error("expected error for missing domain")
|
|
}
|
|
|
|
// Missing backend address
|
|
if err := mgr.Register(Service{Domain: "x.com", Backend: Backend{Type: BackendHTTP}, Reach: ReachInternal}); err == nil {
|
|
t.Error("expected error for missing backend address")
|
|
}
|
|
|
|
// Missing backend type
|
|
if err := mgr.Register(Service{Domain: "x.com", Backend: Backend{Address: "127.0.0.1:80"}, Reach: ReachInternal}); err == nil {
|
|
t.Error("expected error for missing backend type")
|
|
}
|
|
|
|
// Missing reach
|
|
if err := mgr.Register(Service{Domain: "x.com", Backend: Backend{Address: "127.0.0.1:80", Type: BackendHTTP}}); err == nil {
|
|
t.Error("expected error for missing reach")
|
|
}
|
|
}
|
|
|
|
func TestDefaultSource(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
mgr.Register(Service{
|
|
Domain: "test.com",
|
|
Backend: Backend{Address: "127.0.0.1:80", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
})
|
|
|
|
got, _ := mgr.Get("test.com")
|
|
if got.Source != "manual" {
|
|
t.Errorf("expected default source 'manual', got %s", got.Source)
|
|
}
|
|
}
|
|
|
|
func TestDomainToFilename(t *testing.T) {
|
|
tests := []struct {
|
|
domain string
|
|
expected string
|
|
}{
|
|
{"cloud.payne.io", "cloud_payne_io.yaml"},
|
|
{"internal.cloud.payne.io", "internal_cloud_payne_io.yaml"},
|
|
{"payne.io", "payne_io.yaml"},
|
|
{"example.com", "example_com.yaml"},
|
|
}
|
|
for _, tt := range tests {
|
|
got := domainToFilename(tt.domain)
|
|
if got != tt.expected {
|
|
t.Errorf("domainToFilename(%q) = %q, want %q", tt.domain, got, tt.expected)
|
|
}
|
|
}
|
|
}
|