package health import ( "testing" "time" ) func TestStatusIsHealthy(t *testing.T) { // Helper for the new 6-field Status: all-true except the named one. allBut := func(field string) Status { s := Status{ Containerd: true, APIServer: true, NodeReady: true, KubeSystemReady: true, ProbeURL: true, DiskWritable: true, } switch field { case "Containerd": s.Containerd = false case "APIServer": s.APIServer = false case "NodeReady": s.NodeReady = false case "KubeSystemReady": s.KubeSystemReady = false case "ProbeURL": s.ProbeURL = false case "DiskWritable": s.DiskWritable = false } return s } tests := []struct { name string status Status wantHealth bool }{ {"all healthy", allBut(""), true}, {"containerd down", allBut("Containerd"), false}, {"apiserver down", allBut("APIServer"), false}, {"node not ready", allBut("NodeReady"), false}, {"kube-system not ready", allBut("KubeSystemReady"), false}, {"probe URL failed", allBut("ProbeURL"), false}, {"disk not writable", allBut("DiskWritable"), false}, {"all down", Status{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.status.IsHealthy(); got != tt.wantHealth { t.Errorf("IsHealthy() = %v, want %v", got, tt.wantHealth) } }) } } func TestNewChecker(t *testing.T) { // Test defaults c := NewChecker("", "", 0) if c.kubeconfigPath != "/var/lib/kubesolo/pki/admin/admin.kubeconfig" { t.Errorf("unexpected default kubeconfig: %s", c.kubeconfigPath) } if c.apiServerAddr != "127.0.0.1:6443" { t.Errorf("unexpected default apiserver addr: %s", c.apiServerAddr) } if c.timeout != 120*time.Second { t.Errorf("unexpected default timeout: %v", c.timeout) } // Test custom values c = NewChecker("/custom/kubeconfig", "10.0.0.1:6443", 30*time.Second) if c.kubeconfigPath != "/custom/kubeconfig" { t.Errorf("expected custom kubeconfig, got %s", c.kubeconfigPath) } if c.apiServerAddr != "10.0.0.1:6443" { t.Errorf("expected custom addr, got %s", c.apiServerAddr) } if c.timeout != 30*time.Second { t.Errorf("expected 30s timeout, got %v", c.timeout) } } func TestStatusMessage(t *testing.T) { s := &Status{ Containerd: true, APIServer: true, NodeReady: true, Message: "all checks passed", } if s.Message != "all checks passed" { t.Errorf("unexpected message: %s", s.Message) } }