diff --git a/pkg/api/projects_test.go b/pkg/api/projects_test.go new file mode 100644 index 0000000000..b7f8019914 --- /dev/null +++ b/pkg/api/projects_test.go @@ -0,0 +1,103 @@ +package api + +import "testing" + +func TestGetProjectDashboards_KnownProject(t *testing.T) { +dashboards := getProjectDashboards("kubestellar") +if dashboards == nil { +t.Fatal("expected non-nil dashboard list for 'kubestellar' project") +} +if len(dashboards) == 0 { +t.Fatal("expected non-empty dashboard list for 'kubestellar' project") +} +// Verify key dashboards are present +required := []string{"dashboard", "clusters", "compliance", "security", "workloads"} +for _, r := range required { +found := false +for _, d := range dashboards { +if d == r { +found = true +break +} +} +if !found { +t.Errorf("expected dashboard %q in kubestellar presets, not found", r) +} +} +} + +func TestGetProjectDashboards_UnknownProject(t *testing.T) { +dashboards := getProjectDashboards("nonexistent-project") +if dashboards != nil { +t.Errorf("expected nil for unknown project, got %v", dashboards) +} +} + +func TestGetProjectDashboards_EmptyString(t *testing.T) { +dashboards := getProjectDashboards("") +if dashboards != nil { +t.Errorf("expected nil for empty project name, got %v", dashboards) +} +} + +func TestIsProjectEnabled(t *testing.T) { +tests := []struct { +name string +activeProject string +project string +want bool +}{ +{ +name: "wildcard always matches", +activeProject: "kubestellar", +project: "*", +want: true, +}, +{ +name: "wildcard matches empty active project", +activeProject: "", +project: "*", +want: true, +}, +{ +name: "exact match", +activeProject: "kubestellar", +project: "kubestellar", +want: true, +}, +{ +name: "mismatch", +activeProject: "kubestellar", +project: "other-project", +want: false, +}, +{ +name: "empty active project with non-wildcard", +activeProject: "", +project: "kubestellar", +want: false, +}, +{ +name: "both empty matches", +activeProject: "", +project: "", +want: true, +}, +{ +name: "case sensitive mismatch", +activeProject: "KubeStellar", +project: "kubestellar", +want: false, +}, +} + +for _, tt := range tests { +t.Run(tt.name, func(t *testing.T) { +got := isProjectEnabled(tt.activeProject, tt.project) +if got != tt.want { +t.Errorf("isProjectEnabled(%q, %q) = %v, want %v", +tt.activeProject, tt.project, got, tt.want) +} +}) +} +} diff --git a/pkg/api/server_runtime_test.go b/pkg/api/server_runtime_test.go new file mode 100644 index 0000000000..39e15ecf4a --- /dev/null +++ b/pkg/api/server_runtime_test.go @@ -0,0 +1,100 @@ +package api + +import ( +"testing" +) + +func TestNewServerLifecycle(t *testing.T) { +lc := newServerLifecycle(nil) +if lc == nil { +t.Fatal("expected non-nil serverLifecycle") +} +if lc.done == nil { +t.Error("expected done channel to be initialized") +} +if lc.loadingSrv != nil { +t.Error("expected loadingSrv to be nil when passed nil") +} +} + +func TestNewAuthRuntime(t *testing.T) { +ar := newAuthRuntime() +if ar == nil { +t.Fatal("expected non-nil authRuntime") +} +if ar.handler != nil { +t.Error("expected handler to be nil initially") +} +} + +func TestNewBackgroundServices(t *testing.T) { +bg := newBackgroundServices() +if bg == nil { +t.Fatal("expected non-nil backgroundServices") +} +if bg.gpuUtilWorker != nil { +t.Error("expected gpuUtilWorker to be nil initially") +} +} + +func TestNewQuantumWorkloadCache(t *testing.T) { +qc := newQuantumWorkloadCache() +if qc == nil { +t.Fatal("expected non-nil quantumWorkloadCache") +} +if qc.available { +t.Error("expected available to be false initially") +} +if !qc.refreshedAt.IsZero() { +t.Error("expected refreshedAt to be zero initially") +} +} + +func TestQuantumWorkloadCache_DisabledByEnv(t *testing.T) { +t.Setenv("QUANTUM_WORKLOAD_DISABLED", "true") +t.Setenv("QUANTUM_WORKLOAD_RUNNING", "") +qc := newQuantumWorkloadCache() +if qc.isRunning(nil) { +t.Error("expected isRunning=false when QUANTUM_WORKLOAD_DISABLED=true") +} +} + +func TestQuantumWorkloadCache_ForcedRunningByEnv(t *testing.T) { +t.Setenv("QUANTUM_WORKLOAD_DISABLED", "") +t.Setenv("QUANTUM_WORKLOAD_RUNNING", "true") +qc := newQuantumWorkloadCache() +if !qc.isRunning(nil) { +t.Error("expected isRunning=true when QUANTUM_WORKLOAD_RUNNING=true") +} +} + +func TestQuantumWorkloadCache_NilClient(t *testing.T) { +t.Setenv("QUANTUM_WORKLOAD_DISABLED", "") +t.Setenv("QUANTUM_WORKLOAD_RUNNING", "") +qc := newQuantumWorkloadCache() +if qc.isRunning(nil) { +t.Error("expected isRunning=false with nil k8s client") +} +// After calling isRunning, refreshedAt should be updated (cache populated) +if qc.refreshedAt.IsZero() { +t.Error("expected refreshedAt to be set after isRunning call") +} +} + +func TestQuantumWorkloadCache_CacheTTL(t *testing.T) { +t.Setenv("QUANTUM_WORKLOAD_DISABLED", "") +t.Setenv("QUANTUM_WORKLOAD_RUNNING", "") +qc := newQuantumWorkloadCache() + +// First call populates cache +result1 := qc.isRunning(nil) +if result1 { +t.Error("expected false with nil client") +} + +// Second call should use cache (refreshedAt is recent) +result2 := qc.isRunning(nil) +if result2 != result1 { +t.Error("expected cached result to match first result") +} +}