Files
gpu-broker/internal/server/server_test.go
T
2026-11-11 13:57:36 -07:00

128 lines
4.0 KiB
Go

package server
import (
"context"
"net/http/httptest"
"strings"
"testing"
"time"
"gitea.godemperordoge.org/ziadarafat/gpu-broker/internal/api"
"gitea.godemperordoge.org/ziadarafat/gpu-broker/internal/sched"
)
func ptr[T any](v T) *T { return &v }
// start runs the broker's HTTP API over a real scheduler (real clock, fast ticks).
func start(t *testing.T, used0, used1 int) (*api.ClientWithResponses, *sched.Scheduler) {
t.Helper()
s := sched.New(sched.Config{ReserveMB: map[int]int{0: 1500}, LoadWindow: time.Minute, LeaseTTL: time.Minute}, time.Now)
s.Observe([]sched.GPU{{Index: 0, TotalMB: 6144, UsedMB: used0}, {Index: 1, TotalMB: 8192, UsedMB: used1}})
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go Tick(ctx, s, 20*time.Millisecond)
srv := httptest.NewServer(Handler(s, time.Minute))
t.Cleanup(srv.Close)
c, err := api.NewClientWithResponses(srv.URL)
if err != nil {
t.Fatal(err)
}
return c, s
}
func TestLeaseIsGrantedRenewedAndReleased(t *testing.T) {
c, s := start(t, 0, 0)
ctx := context.Background()
r, err := c.AcquireLeaseWithResponse(ctx, api.AcquireLeaseJSONRequestBody{
Client: "genres", Priority: api.Interactive, VramMb: 7000, Prefer: ptr([]int{1}),
})
if err != nil || r.JSON200 == nil {
t.Fatalf("%v %s", err, r.Body)
}
g := r.JSON200
if g.Device != api.Gpu || g.Gpu != 1 || g.LeaseId == nil {
t.Fatalf("grant %+v", g)
}
if rr, _ := c.RenewLeaseWithResponse(ctx, *g.LeaseId); rr.StatusCode() != 204 {
t.Fatalf("renew %d", rr.StatusCode())
}
st, _ := c.StatusWithResponse(ctx)
if len(st.JSON200.Leases) != 1 || st.JSON200.Leases[0].Client != "genres" {
t.Fatalf("status %+v", st.JSON200)
}
if rel, _ := c.ReleaseLeaseWithResponse(ctx, *g.LeaseId); rel.StatusCode() != 204 {
t.Fatalf("release %d", rel.StatusCode())
}
if len(s.Status().Leases) != 0 {
t.Fatal("lease still there")
}
}
func TestBusyGPUsWithFallbackAnswerCPU(t *testing.T) {
c, _ := start(t, 6144, 8192)
r, err := c.AcquireLeaseWithResponse(context.Background(), api.AcquireLeaseJSONRequestBody{
Client: "genres", Priority: api.Batch, VramMb: 3000, MaxWaitS: ptr(1), CpuFallback: ptr(true),
})
if err != nil || r.JSON200 == nil || r.JSON200.Device != api.Cpu || r.JSON200.Gpu != -1 {
t.Fatalf("%v %s", err, r.Body)
}
}
func TestBusyGPUsWithoutFallbackAre503(t *testing.T) {
c, _ := start(t, 6144, 8192)
r, _ := c.AcquireLeaseWithResponse(context.Background(), api.AcquireLeaseJSONRequestBody{
Client: "llama", Priority: api.Interactive, VramMb: 3000, MaxWaitS: ptr(1),
})
if r.StatusCode() != 503 || !strings.Contains(string(r.Body), "no GPU became free") {
t.Fatalf("%d %s", r.StatusCode(), r.Body)
}
}
func TestTooBigWithoutFallbackIs413(t *testing.T) {
c, _ := start(t, 0, 0)
r, _ := c.AcquireLeaseWithResponse(context.Background(), api.AcquireLeaseJSONRequestBody{
Client: "ollama", Priority: api.Interactive, VramMb: 17000,
})
if r.StatusCode() != 413 {
t.Fatalf("%d %s", r.StatusCode(), r.Body)
}
}
func TestRenewingAnUnknownLeaseIs404(t *testing.T) {
c, _ := start(t, 0, 0)
if r, _ := c.RenewLeaseWithResponse(context.Background(), "nope"); r.StatusCode() != 404 {
t.Fatalf("%d", r.StatusCode())
}
}
func TestHealthWantsASample(t *testing.T) {
c, _ := start(t, 0, 0)
r, _ := c.HealthWithResponse(context.Background())
if r.JSON200 == nil || r.JSON200.Status != api.Ok {
t.Fatalf("%s", r.Body)
}
}
func TestMetricsShowFreeVRAMPerGPU(t *testing.T) {
_, s := start(t, 1000, 2000)
body := Metrics(s)
for _, want := range []string{
`gpu_broker_vram_free_mb{gpu="0"} 3644`,
`gpu_broker_vram_free_mb{gpu="1"} 6192`,
"gpu_broker_leases 0",
"gpu_broker_waiting 0",
} {
if !strings.Contains(body, want) {
t.Errorf("metrics missing %q:\n%s", want, body)
}
}
}
func TestEmptyListsAreArraysNotNull(t *testing.T) {
c, _ := start(t, 0, 0)
r, _ := c.StatusWithResponse(context.Background())
if !strings.Contains(string(r.Body), `"leases":[]`) || !strings.Contains(string(r.Body), `"waiting":[]`) {
t.Fatalf("%s", r.Body)
}
}