|
| 1 | +// Copyright 2025 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package reflectjoborder |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "encoding/json" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/api/common_info" |
| 14 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/api/podgroup_info" |
| 15 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/conf" |
| 16 | + "github.com/NVIDIA/KAI-scheduler/pkg/scheduler/framework" |
| 17 | +) |
| 18 | + |
| 19 | +func TestJobOrderPlugin_OnSessionOpen(t *testing.T) { |
| 20 | + ssn := &framework.Session{ |
| 21 | + PodGroupInfos: map[common_info.PodGroupID]*podgroup_info.PodGroupInfo{ |
| 22 | + "pg1": {UID: "pg1", Priority: 5, Queue: "q1"}, |
| 23 | + "pg2": {UID: "pg2", Priority: 2, Queue: "q2"}, |
| 24 | + }, |
| 25 | + Config: &conf.SchedulerConfiguration{ |
| 26 | + QueueDepthPerAction: map[string]int{"Allocate": 10}, |
| 27 | + }, |
| 28 | + } |
| 29 | + plugin := &JobOrderPlugin{} |
| 30 | + plugin.OnSessionOpen(ssn) |
| 31 | + |
| 32 | + if plugin.ReflectJobOrder == nil { |
| 33 | + t.Fatalf("ReflectJobOrder should be initialized") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Test serveJobs returns correct JSON and status when ReflectJobOrder is set |
| 38 | +func TestServeJobs_ReflectJobOrderReady(t *testing.T) { |
| 39 | + plugin := &JobOrderPlugin{ |
| 40 | + ReflectJobOrder: &ReflectJobOrder{ |
| 41 | + GlobalOrder: []JobOrder{{ID: "pg1", Priority: 10}}, |
| 42 | + QueueOrder: map[common_info.QueueID][]JobOrder{"q1": {{ID: "pg1", Priority: 10}}}, |
| 43 | + }, |
| 44 | + } |
| 45 | + req := httptest.NewRequest(http.MethodGet, "/get-job-order", nil) |
| 46 | + rr := httptest.NewRecorder() |
| 47 | + plugin.serveJobs(rr, req) |
| 48 | + if rr.Code != http.StatusOK { |
| 49 | + t.Errorf("Expected HTTP 200 OK, got %d", rr.Code) |
| 50 | + } |
| 51 | + var resp ReflectJobOrder |
| 52 | + if err := json.NewDecoder(bytes.NewReader(rr.Body.Bytes())).Decode(&resp); err != nil { |
| 53 | + t.Fatalf("Failed to decode serveJobs response: %v", err) |
| 54 | + } |
| 55 | + if len(resp.GlobalOrder) != 1 || resp.GlobalOrder[0].Priority != 10 { |
| 56 | + t.Errorf("Unexpected response json: %+v", resp) |
| 57 | + } |
| 58 | + if len(resp.QueueOrder) != 1 { |
| 59 | + t.Errorf("Expected 1 queue, got %d", len(resp.QueueOrder)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Test serveJobs returns 503 if ReflectJobOrder is nil |
| 64 | +func TestServeJobs_ReflectJobOrderNotReady(t *testing.T) { |
| 65 | + plugin := &JobOrderPlugin{ReflectJobOrder: nil} |
| 66 | + req := httptest.NewRequest(http.MethodGet, "/get-job-order", nil) |
| 67 | + rr := httptest.NewRecorder() |
| 68 | + plugin.serveJobs(rr, req) |
| 69 | + if rr.Code != http.StatusServiceUnavailable { |
| 70 | + t.Errorf("Expected HTTP 503, got %d", rr.Code) |
| 71 | + } |
| 72 | + if !bytes.Contains(rr.Body.Bytes(), []byte("Job order data not ready")) { |
| 73 | + t.Errorf("Expected error message in body, got: %s", rr.Body.String()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Test serveJobs handles encoding error gracefully |
| 78 | +type brokenWriter struct{ http.ResponseWriter } |
| 79 | + |
| 80 | +func (b *brokenWriter) Write(_ []byte) (int, error) { return 0, errEncode } |
| 81 | + |
| 82 | +var errEncode = &encodeError{"forced encode error"} |
| 83 | + |
| 84 | +type encodeError struct{ msg string } |
| 85 | + |
| 86 | +func (e *encodeError) Error() string { return e.msg } |
| 87 | + |
| 88 | +func TestServeJobs_EncodeError(t *testing.T) { |
| 89 | + plugin := &JobOrderPlugin{ReflectJobOrder: &ReflectJobOrder{}} |
| 90 | + req := httptest.NewRequest(http.MethodGet, "/get-job-order", nil) |
| 91 | + rr := httptest.NewRecorder() |
| 92 | + bw := &brokenWriter{rr} |
| 93 | + plugin.serveJobs(bw, req) |
| 94 | + // Should write 500 error |
| 95 | + if rr.Code != http.StatusInternalServerError { |
| 96 | + t.Errorf("Expected HTTP 500, got %d", rr.Code) |
| 97 | + } |
| 98 | +} |
0 commit comments