package cloudinit import ( "os" "path/filepath" "strings" "testing" ) func TestApplyUpdatesEmptyConfigSkipsWrite(t *testing.T) { confPath := filepath.Join(t.TempDir(), "update.conf") cfg := &Config{} // Updates block default-zero if err := ApplyUpdates(cfg, confPath); err != nil { t.Fatalf("apply: %v", err) } if _, err := os.Stat(confPath); !os.IsNotExist(err) { t.Errorf("expected no file when cloud-init Updates is empty, got %v", err) } } func TestApplyUpdatesAllFields(t *testing.T) { confPath := filepath.Join(t.TempDir(), "update.conf") cfg := &Config{Updates: UpdatesConfig{ Server: "https://updates.example.com", Channel: "stable", MaintenanceWindow: "03:00-05:00", PubKey: "/etc/kubesolo/pub.hex", }} if err := ApplyUpdates(cfg, confPath); err != nil { t.Fatalf("apply: %v", err) } data, err := os.ReadFile(confPath) if err != nil { t.Fatalf("read: %v", err) } out := string(data) wants := []string{ "server = https://updates.example.com", "channel = stable", "maintenance_window = 03:00-05:00", "pubkey = /etc/kubesolo/pub.hex", } for _, w := range wants { if !strings.Contains(out, w) { t.Errorf("update.conf missing %q in output:\n%s", w, out) } } } func TestApplyUpdatesPartialFields(t *testing.T) { // Only server set — others should be omitted from the file, not written // as blank values. confPath := filepath.Join(t.TempDir(), "update.conf") cfg := &Config{Updates: UpdatesConfig{Server: "https://x.example.com"}} if err := ApplyUpdates(cfg, confPath); err != nil { t.Fatalf("apply: %v", err) } data, _ := os.ReadFile(confPath) out := string(data) if !strings.Contains(out, "server = https://x.example.com") { t.Errorf("missing server line:\n%s", out) } for _, unwanted := range []string{"channel = ", "maintenance_window = ", "pubkey = "} { if strings.Contains(out, unwanted) { t.Errorf("unexpected empty line %q present in:\n%s", unwanted, out) } } } func TestApplyUpdatesCreatesParentDir(t *testing.T) { // /etc/kubesolo may not exist on first boot before cloud-init runs. confPath := filepath.Join(t.TempDir(), "nested", "kubesolo", "update.conf") cfg := &Config{Updates: UpdatesConfig{Server: "https://x"}} if err := ApplyUpdates(cfg, confPath); err != nil { t.Fatalf("apply: %v", err) } if _, err := os.Stat(confPath); err != nil { t.Errorf("file not created: %v", err) } }