Replace InstanceConfig with DNSEntry for dnsmasq config generation
InstanceConfig was a ~50-field struct designed for Wild Cloud k8s instances,
but only 3 fields were ever read by dnsmasq (the sole consumer). The
reconciliation bridge constructed fake InstanceConfig objects from registered
domains just to satisfy this interface.
Replace with DNSEntry{Domain, IP} — a 2-field struct purpose-built for
dnsmasq. All domains get local=/ directives to prevent AAAA queries from
leaking to upstream DNS (Happy Eyeballs / RFC 8305 latency on LAN).
Removed dead code: InstanceConfig, NodeConfig, LoadCloudConfig,
SaveCloudConfig, DeepMerge, LoadMergedInstanceConfig, EnsureInstanceConfig,
instance path helpers, instanceLoadBalancerIP, GenerateInstanceConfig,
and all modular per-instance dnsmasq methods.
This commit is contained in:
@@ -5,8 +5,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Test: LoadState loads valid state
|
||||
@@ -295,187 +293,6 @@ func TestState_IsEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test: LoadCloudConfig loads instance configuration
|
||||
func TestLoadCloudConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configYAML string
|
||||
verify func(t *testing.T, config *InstanceConfig)
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "loads complete instance configuration",
|
||||
configYAML: `cloud:
|
||||
dhcpRange: "192.168.1.100,192.168.1.200"
|
||||
baseDomain: "example.com"
|
||||
domain: "home"
|
||||
internalDomain: "internal.example.com"
|
||||
cluster:
|
||||
name: "my-cluster"
|
||||
loadBalancerIp: "192.168.1.10"
|
||||
nodes:
|
||||
talos:
|
||||
version: "v1.8.0"
|
||||
activeNodes:
|
||||
- node1:
|
||||
role: "control"
|
||||
interface: "eth0"
|
||||
disk: "/dev/sda"
|
||||
`,
|
||||
verify: func(t *testing.T, config *InstanceConfig) {
|
||||
if config.Cloud.BaseDomain != "example.com" {
|
||||
t.Error("base domain not loaded correctly")
|
||||
}
|
||||
if config.Cloud.DHCPRange != "192.168.1.100,192.168.1.200" {
|
||||
t.Error("DHCP range not loaded correctly")
|
||||
}
|
||||
if config.Cluster.Name != "my-cluster" {
|
||||
t.Error("cluster name not loaded correctly")
|
||||
}
|
||||
if config.Cluster.Nodes.Talos.Version != "v1.8.0" {
|
||||
t.Error("talos version not loaded correctly")
|
||||
}
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
if err := os.WriteFile(configPath, []byte(tt.configYAML), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
|
||||
config, err := LoadCloudConfig(configPath)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
t.Fatal("config is nil")
|
||||
}
|
||||
|
||||
if tt.verify != nil {
|
||||
tt.verify(t, config)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: LoadCloudConfig error cases
|
||||
func TestLoadCloudConfig_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setupFunc func(t *testing.T) string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "non-existent file",
|
||||
setupFunc: func(t *testing.T) string {
|
||||
return filepath.Join(t.TempDir(), "nonexistent.yaml")
|
||||
},
|
||||
errContains: "reading config file",
|
||||
},
|
||||
{
|
||||
name: "invalid yaml",
|
||||
setupFunc: func(t *testing.T) string {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
content := `invalid: yaml: [[[`
|
||||
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("setup failed: %v", err)
|
||||
}
|
||||
return configPath
|
||||
},
|
||||
errContains: "parsing config file",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
configPath := tt.setupFunc(t)
|
||||
_, err := LoadCloudConfig(configPath)
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error, got nil")
|
||||
} else if !strings.Contains(err.Error(), tt.errContains) {
|
||||
t.Errorf("error %q does not contain %q", err.Error(), tt.errContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: SaveCloudConfig saves instance configuration
|
||||
func TestSaveCloudConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config *InstanceConfig
|
||||
verify func(t *testing.T, configPath string)
|
||||
}{
|
||||
{
|
||||
name: "saves instance configuration",
|
||||
config: func() *InstanceConfig {
|
||||
cfg := &InstanceConfig{}
|
||||
cfg.Cloud.BaseDomain = "example.com"
|
||||
cfg.Cloud.Domain = "home"
|
||||
return cfg
|
||||
}(),
|
||||
verify: func(t *testing.T, configPath string) {
|
||||
content, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read saved config: %v", err)
|
||||
}
|
||||
contentStr := string(content)
|
||||
if !strings.Contains(contentStr, "example.com") {
|
||||
t.Error("saved config missing base domain")
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "subdir", "config.yaml")
|
||||
|
||||
err := SaveCloudConfig(tt.config, configPath)
|
||||
if err != nil {
|
||||
t.Errorf("SaveCloudConfig failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify file exists
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
t.Errorf("config file not created: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify content can be loaded back
|
||||
loadedConfig, err := LoadCloudConfig(configPath)
|
||||
if err != nil {
|
||||
t.Errorf("failed to reload saved config: %v", err)
|
||||
} else if loadedConfig == nil {
|
||||
t.Error("loaded config is nil")
|
||||
}
|
||||
|
||||
if tt.verify != nil {
|
||||
tt.verify(t, configPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test: Round-trip save and load preserves data
|
||||
func TestState_RoundTrip(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
@@ -506,187 +323,3 @@ func TestState_RoundTrip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test: Round-trip save and load for instance config
|
||||
func TestInstanceConfig_RoundTrip(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
configPath := filepath.Join(tempDir, "config.yaml")
|
||||
|
||||
// Create instance config
|
||||
original := &InstanceConfig{}
|
||||
original.Cloud.BaseDomain = "example.com"
|
||||
original.Cloud.Domain = "home"
|
||||
original.Cluster.Name = "my-cluster"
|
||||
|
||||
// Save config
|
||||
if err := SaveCloudConfig(original, configPath); err != nil {
|
||||
t.Fatalf("SaveCloudConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Load config
|
||||
loaded, err := LoadCloudConfig(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCloudConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify fields match
|
||||
if loaded.Cloud.BaseDomain != original.Cloud.BaseDomain {
|
||||
t.Errorf("base domain mismatch: got %q, want %q", loaded.Cloud.BaseDomain, original.Cloud.BaseDomain)
|
||||
}
|
||||
if loaded.Cluster.Name != original.Cluster.Name {
|
||||
t.Errorf("cluster name mismatch: got %q, want %q", loaded.Cluster.Name, original.Cluster.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepMerge(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dst map[string]any
|
||||
src map[string]any
|
||||
expected map[string]any
|
||||
}{
|
||||
{
|
||||
name: "src overrides dst flat keys",
|
||||
dst: map[string]any{"a": "1", "b": "2"},
|
||||
src: map[string]any{"b": "3", "c": "4"},
|
||||
expected: map[string]any{"a": "1", "b": "3", "c": "4"},
|
||||
},
|
||||
{
|
||||
name: "nested maps merge recursively",
|
||||
dst: map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"central": map[string]any{"domain": "central.example.com"},
|
||||
},
|
||||
},
|
||||
src: map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"domain": "example.com",
|
||||
},
|
||||
},
|
||||
expected: map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"central": map[string]any{"domain": "central.example.com"},
|
||||
"domain": "example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "src nested key overrides dst nested key",
|
||||
dst: map[string]any{
|
||||
"cloud": map[string]any{"domain": "old.com"},
|
||||
},
|
||||
src: map[string]any{
|
||||
"cloud": map[string]any{"domain": "new.com"},
|
||||
},
|
||||
expected: map[string]any{
|
||||
"cloud": map[string]any{"domain": "new.com"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty src returns dst",
|
||||
dst: map[string]any{"a": "1"},
|
||||
src: map[string]any{},
|
||||
expected: map[string]any{"a": "1"},
|
||||
},
|
||||
{
|
||||
name: "empty dst returns src",
|
||||
dst: map[string]any{},
|
||||
src: map[string]any{"a": "1"},
|
||||
expected: map[string]any{"a": "1"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DeepMerge(tt.dst, tt.src)
|
||||
resultYAML, _ := yaml.Marshal(result)
|
||||
expectedYAML, _ := yaml.Marshal(tt.expected)
|
||||
if string(resultYAML) != string(expectedYAML) {
|
||||
t.Errorf("DeepMerge() =\n%s\nwant:\n%s", resultYAML, expectedYAML)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMergedInstanceConfig(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
instanceName := "test"
|
||||
instanceConfigDir := filepath.Join(dataDir, "instances", instanceName, "config")
|
||||
os.MkdirAll(instanceConfigDir, 0755)
|
||||
|
||||
globalState := `operator:
|
||||
email: test@example.com
|
||||
cloud:
|
||||
central:
|
||||
domain: central.example.com
|
||||
`
|
||||
instanceConfig := `cluster:
|
||||
name: test
|
||||
nodes:
|
||||
control:
|
||||
vip: 192.168.1.100
|
||||
cloud:
|
||||
domain: cloud.example.com
|
||||
`
|
||||
|
||||
os.WriteFile(filepath.Join(dataDir, "state.yaml"), []byte(globalState), 0644)
|
||||
os.WriteFile(filepath.Join(instanceConfigDir, "instance.yaml"), []byte(instanceConfig), 0644)
|
||||
|
||||
merged, err := LoadMergedInstanceConfig(dataDir, instanceName)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadMergedInstanceConfig() error: %v", err)
|
||||
}
|
||||
|
||||
cloud, ok := merged["cloud"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing cloud key in merged config")
|
||||
}
|
||||
|
||||
central, ok := cloud["central"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing cloud.central key — global state not merged")
|
||||
}
|
||||
if central["domain"] != "central.example.com" {
|
||||
t.Errorf("cloud.central.domain = %v, want central.example.com", central["domain"])
|
||||
}
|
||||
|
||||
if cloud["domain"] != "cloud.example.com" {
|
||||
t.Errorf("cloud.domain = %v, want cloud.example.com", cloud["domain"])
|
||||
}
|
||||
|
||||
cluster, ok := merged["cluster"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing cluster key — instance config not merged")
|
||||
}
|
||||
if cluster["name"] != "test" {
|
||||
t.Errorf("cluster.name = %v, want test", cluster["name"])
|
||||
}
|
||||
|
||||
operator, ok := merged["operator"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing operator key — global state not merged")
|
||||
}
|
||||
if operator["email"] != "test@example.com" {
|
||||
t.Errorf("operator.email = %v, want test@example.com", operator["email"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMergedInstanceConfig_NoGlobalState(t *testing.T) {
|
||||
dataDir := t.TempDir()
|
||||
instanceConfigDir := filepath.Join(dataDir, "instances", "test", "config")
|
||||
os.MkdirAll(instanceConfigDir, 0755)
|
||||
|
||||
os.WriteFile(filepath.Join(instanceConfigDir, "instance.yaml"), []byte("cluster:\n name: test\n"), 0644)
|
||||
|
||||
merged, err := LoadMergedInstanceConfig(dataDir, "test")
|
||||
if err != nil {
|
||||
t.Fatalf("LoadMergedInstanceConfig() error: %v", err)
|
||||
}
|
||||
|
||||
cluster, ok := merged["cluster"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatal("missing cluster key")
|
||||
}
|
||||
if cluster["name"] != "test" {
|
||||
t.Errorf("cluster.name = %v, want test", cluster["name"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user