Tests added (13 new): - HAProxy ACL ordering: L7 before L4 wildcard, L4 exact before L4 wildcard - HAProxy subdomains: false=exact only, true=wildcard+exact - Reconciliation integration: HAProxy config from services, DNS config from services, Central config domain injection, correct DNS target IPs - dnsmasq: reach:internal has local=/, reach:public does not - Services: idempotent registration, no duplicates in list Bug fix: - Run initial reconciliation on startup so Central's own domain (from config) gets HAProxy + DNS routes even with zero registered services. Previously, Central only got routes after the first service was registered, causing 503 on fresh restart. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
327 lines
8.2 KiB
Go
327 lines
8.2 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 TestRegisterIdempotent(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
svc := Service{
|
|
Domain: "idempotent.example.com",
|
|
Source: "test",
|
|
Backend: Backend{Address: "127.0.0.1:8080", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
}
|
|
|
|
// Register twice with same domain
|
|
if err := mgr.Register(svc); err != nil {
|
|
t.Fatalf("first Register failed: %v", err)
|
|
}
|
|
|
|
// Update the backend address on second registration
|
|
svc.Backend.Address = "127.0.0.1:9090"
|
|
if err := mgr.Register(svc); err != nil {
|
|
t.Fatalf("second Register failed: %v", err)
|
|
}
|
|
|
|
// Should still have only one service
|
|
svcs, err := mgr.List()
|
|
if err != nil {
|
|
t.Fatalf("List failed: %v", err)
|
|
}
|
|
if len(svcs) != 1 {
|
|
t.Errorf("expected 1 service after idempotent register, got %d", len(svcs))
|
|
}
|
|
|
|
// Should have the updated address
|
|
got, err := mgr.Get("idempotent.example.com")
|
|
if err != nil {
|
|
t.Fatalf("Get failed: %v", err)
|
|
}
|
|
if got.Backend.Address != "127.0.0.1:9090" {
|
|
t.Errorf("expected updated backend address 127.0.0.1:9090, got %s", got.Backend.Address)
|
|
}
|
|
}
|
|
|
|
func TestListNoDuplicates(t *testing.T) {
|
|
mgr := NewManager(t.TempDir())
|
|
|
|
domains := []string{
|
|
"alpha.example.com",
|
|
"beta.example.com",
|
|
"gamma.example.com",
|
|
}
|
|
|
|
// Register each domain
|
|
for _, domain := range domains {
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Re-register one domain to verify no duplication
|
|
if err := mgr.Register(Service{
|
|
Domain: "beta.example.com",
|
|
Source: "test",
|
|
Backend: Backend{Address: "127.0.0.1:9090", Type: BackendHTTP},
|
|
Reach: ReachInternal,
|
|
}); err != nil {
|
|
t.Fatalf("Re-register beta failed: %v", err)
|
|
}
|
|
|
|
svcs, err := mgr.List()
|
|
if err != nil {
|
|
t.Fatalf("List failed: %v", err)
|
|
}
|
|
|
|
if len(svcs) != 3 {
|
|
t.Errorf("expected 3 unique services, got %d", len(svcs))
|
|
}
|
|
|
|
// Verify no duplicate domains
|
|
seen := make(map[string]bool)
|
|
for _, svc := range svcs {
|
|
if seen[svc.Domain] {
|
|
t.Errorf("duplicate domain in List(): %s", svc.Domain)
|
|
}
|
|
seen[svc.Domain] = true
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|