Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions pkg/api/projects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package api

import "testing"

func TestGetProjectDashboards_KnownProject(t *testing.T) {

Check failure on line 5 in pkg/api/projects_test.go

View workflow job for this annotation

GitHub Actions / pr-check

TestGetProjectDashboards_KnownProject redeclared in this block

Check failure on line 5 in pkg/api/projects_test.go

View workflow job for this annotation

GitHub Actions / go test ./...

TestGetProjectDashboards_KnownProject redeclared in this block
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)
}
}
}
Comment on lines +1 to +27

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)
}
})
}
}
100 changes: 100 additions & 0 deletions pkg/api/server_runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package api

import (
"testing"
)

Comment on lines +1 to +6
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")
}
}
Comment on lines +84 to +100
Loading