package reconciler import ( "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" "gitea.mynodes.xyz/ilker/llmgw/internal/control/inventory" ) func TestHeadroomIsWhatPlacementWouldDoNext(t *testing.T) { // Two 8-GPU nodes and a 4-GPU profile: four replicas in all, one placed. h := newHarness(t, testModel(1), gpuNode("1", 8), gpuNode("2", 8)) h.r.Reconcile(t.Context()) h.markReady() got, ok := h.r.Headroom("llama", 10) if !ok { t.Fatal("the model was not found") } if got.Current != 1 || got.More != 3 || got.Limited { t.Fatalf("headroom %+v, expected 1 placed and room for 3 more", got) } // Spread first: the next one goes to the node without one. if first := got.Nodes[0][0]; first == h.r.Replicas()[0].NodeIDs()[0] { t.Errorf("the next replica would stack on %s beside the first", first) } if len(got.Blocked) == 0 || !strings.Contains(strings.Join(got.Blocked, "\n"), "GPU") { t.Errorf("no reason given for the fifth: %v", got.Blocked) } // Tried on a copy: nothing was taken from the real inventory. if free := len(h.inv.FreeGPUs("1")) + len(h.inv.FreeGPUs("2")); free != 12 { t.Errorf("%d GPUs free after asking, expected the 12 the one replica leaves", free) } capped, _ := h.r.Headroom("llama", 2) if capped.More != 2 || !capped.Limited || capped.Blocked != nil { t.Errorf("asked for at most 2: %+v", capped) } if _, ok := h.r.Headroom("nope", 5); ok { t.Error("an unknown model has headroom") } } func TestHeadroomCountsMemoryAlreadyClaimed(t *testing.T) { cpu := func(id string) inventory.Node { return inventory.Node{ID: id, MgmtAddr: "10.0.0." + id, State: inventory.Ready, MemoryTotalMB: 16 * 1024} } m := catalog.Model{Name: "small", Source: "/m.gguf", Scaling: catalog.Scaling{Min: 1, Max: 1}, Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp", MinMemoryGB: 6}}} h := newHarness(t, m, cpu("a"), cpu("b")) h.r.Reconcile(t.Context()) // 16 GB each at 6 GB a replica: two per node, one of them taken. got, _ := h.r.Headroom("small", 10) if got.Current != 1 || got.More != 3 { t.Errorf("headroom %+v, expected room for 3 more", got) } } func TestReservedCoresAreTheEnginesThreadsAndBoundTheRoom(t *testing.T) { cpu := inventory.Node{ID: "a", MgmtAddr: "10.0.0.1", State: inventory.Ready, MemoryTotalMB: 64 * 1024, CPUCount: 8} m := catalog.Model{Name: "coder", Source: "/m.gguf", Scaling: catalog.Scaling{Min: 1, Max: 1}, Profiles: []catalog.Profile{{Name: "cpu", Engine: "llamacpp", MinMemoryGB: 6, CPUCores: 3}}} h := newHarness(t, m, cpu) h.r.Reconcile(t.Context()) started := h.disp.startedShards() if len(started) != 1 || started[0].Spec.Threads != 3 { t.Fatalf("the engine was not told its 3 cores: %+v", started) } if a := h.inv.Allocations("a"); len(a) != 1 || a[0].CPUCores != 3 { t.Errorf("the cores were not reserved: %+v", a) } // Memory would allow nine more; cores allow one: 8 - 3 = 5, then 2. got, _ := h.r.Headroom("coder", 10) if got.More != 1 { t.Errorf("room for %d more, expected 1 by cores: %+v", got.More, got) } }