package config import ( "os" "path/filepath" "testing" ) func writeConf(t *testing.T, content string) string { t.Helper() path := filepath.Join(t.TempDir(), "update.conf") if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatalf("seed: %v", err) } return path } func TestLoadMissingReturnsEmptyConfig(t *testing.T) { c, err := Load(filepath.Join(t.TempDir(), "does-not-exist.conf")) if err != nil { t.Fatalf("unexpected error: %v", err) } if c == nil { t.Fatal("Load returned nil config") } if c.Server != "" || c.Channel != "" || c.MaintenanceWindow != "" || c.PubKey != "" { t.Errorf("expected empty config, got %+v", c) } } func TestLoadAllFields(t *testing.T) { path := writeConf(t, `# comment line server = https://updates.example.com channel = stable maintenance_window = 03:00-05:00 pubkey = /etc/kubesolo/pub.hex `) c, err := Load(path) if err != nil { t.Fatalf("load: %v", err) } if c.Server != "https://updates.example.com" { t.Errorf("server: got %q", c.Server) } if c.Channel != "stable" { t.Errorf("channel: got %q", c.Channel) } if c.MaintenanceWindow != "03:00-05:00" { t.Errorf("maintenance_window: got %q", c.MaintenanceWindow) } if c.PubKey != "/etc/kubesolo/pub.hex" { t.Errorf("pubkey: got %q", c.PubKey) } } func TestLoadIgnoresUnknownKeys(t *testing.T) { // Unknown keys must not be an error — supports forward-compat config // fields added by newer agent versions. path := writeConf(t, `server = https://x future_field = whatever channel = beta `) c, err := Load(path) if err != nil { t.Fatalf("load: %v", err) } if c.Server != "https://x" { t.Errorf("server: got %q", c.Server) } if c.Channel != "beta" { t.Errorf("channel: got %q", c.Channel) } } func TestLoadStripsWhitespace(t *testing.T) { path := writeConf(t, " server = https://example \n channel=stable\n") c, err := Load(path) if err != nil { t.Fatalf("load: %v", err) } if c.Server != "https://example" { t.Errorf("server: got %q (whitespace not stripped?)", c.Server) } if c.Channel != "stable" { t.Errorf("channel: got %q", c.Channel) } } func TestLoadIgnoresBlankAndCommentLines(t *testing.T) { path := writeConf(t, ` # this is a comment server = https://example # indented comment channel = stable `) c, err := Load(path) if err != nil { t.Fatalf("load: %v", err) } if c.Server != "https://example" { t.Errorf("server: got %q", c.Server) } } func TestLoadRejectsMissingEquals(t *testing.T) { // "noEqualsHere" with no '=' is a syntax error worth surfacing — likely // indicates a corrupted config file. path := writeConf(t, `server = https://example noEqualsHere `) _, err := Load(path) if err == nil { t.Error("expected error on malformed line, got nil") } }