placement.Plan(sites, request, weights) is a pure function - no I/O, no cloud - which is what makes the decision reproducible, unit-testable against synthetic multi-site fixtures, and usable as a dry-run endpoint later. hpcod plan can run against live sites or a saved discover -json snapshot. Filter then score. Filtering eliminates the impossible and records why for each site; scoring ranks the survivors with capability_fit weighted highest, because scarce hardware must be kept for requests that need it. A plain CPU job placed on the only InfiniBand site is a scheduling failure even though it fits, and the tests assert exactly that. The refusals carry the design's central distinction. A site is told apart as "none present", "present but unconfirmed - the fabric has not been verified by a node agent", and "present but not passthrough-safe (shared IOMMU group)". Those are three different problems needing three different responses: buy hardware, run the agent, fix the topology. Reporting them all as "no capacity" would waste an operator's afternoon. Requests state usable memory; the portal adds the in-RAM rootfs reserve on top, since a tenant allocated exactly what it asked for would OOM under its own job. Packing prefers hosts already in use by the tenant so whole hypervisors stay free for later requests. Verified against real hv01 inventory, where the request for 4 GPUs and InfiniBand is refused with "gpu: need 4, only 1 usable" and "infiniband: none present" - correct, because the ConnectX-4 Lx cards are RoCE and the curated map knows it. Also: an invalid lease now names the field instead of surfacing a bare "invalid duration", which a test caught.
85 lines
2.4 KiB
Go
85 lines
2.4 KiB
Go
package spec
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseAndDefaults(t *testing.T) {
|
|
r, err := Parse([]byte(`
|
|
name: t1
|
|
lease: 14d
|
|
head: { cores: 4, memory_gb: 16 }
|
|
compute:
|
|
count: 4
|
|
cores: 8
|
|
memory_gb: 32
|
|
require:
|
|
- capability: gpu # count omitted, should default to 1
|
|
- capability: infiniband
|
|
count: 2
|
|
`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Compute.Require[0].Count != 1 {
|
|
t.Errorf("omitted count = %d, want default 1", r.Compute.Require[0].Count)
|
|
}
|
|
if r.Compute.Require[1].Count != 2 {
|
|
t.Errorf("explicit count lost: %d", r.Compute.Require[1].Count)
|
|
}
|
|
d, err := r.LeaseDuration()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := 14 * 24 * time.Hour; d != want {
|
|
t.Errorf("lease = %v, want %v", d, want)
|
|
}
|
|
}
|
|
|
|
// Requests state usable memory; the in-RAM rootfs is added by the portal.
|
|
// A tenant that asked for 32 GiB and got 32 GiB allocated would OOM under its
|
|
// own job, so this is not cosmetic.
|
|
func TestRootfsReserveAddedToRequestedMemory(t *testing.T) {
|
|
r := Request{Compute: ComputeSpec{MemoryGB: 32}}
|
|
if got, want := r.MemoryPerComputeGB(), 32+RootfsReserveGB; got != want {
|
|
t.Errorf("allocated %d GiB, want %d", got, want)
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsNonsense(t *testing.T) {
|
|
cases := map[string]Request{
|
|
"no name": {Compute: ComputeSpec{Count: 1, Cores: 1, MemoryGB: 1}, Head: NodeSpec{Cores: 1, MemoryGB: 1}},
|
|
"zero compute": {Name: "x", Compute: ComputeSpec{Count: 0, Cores: 1, MemoryGB: 1}, Head: NodeSpec{Cores: 1, MemoryGB: 1}},
|
|
"no head cores": {Name: "x", Compute: ComputeSpec{Count: 1, Cores: 1, MemoryGB: 1}},
|
|
"bad capability": {Name: "x", Compute: ComputeSpec{Count: 1, Cores: 1, MemoryGB: 1, Require: []Constraint{{Capability: "quantum"}}}, Head: NodeSpec{Cores: 1, MemoryGB: 1}},
|
|
}
|
|
for name, r := range cases {
|
|
if err := r.Validate(); err == nil {
|
|
t.Errorf("%s: accepted an invalid request", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateAcceptsGoodRequest(t *testing.T) {
|
|
r := Request{
|
|
Name: "ok", Head: NodeSpec{Cores: 4, MemoryGB: 16},
|
|
Compute: ComputeSpec{Count: 2, Cores: 8, MemoryGB: 32},
|
|
}
|
|
if err := r.Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestBadLeaseIsReported(t *testing.T) {
|
|
r := Request{
|
|
Name: "x", Lease: "fortnight", Head: NodeSpec{Cores: 1, MemoryGB: 1},
|
|
Compute: ComputeSpec{Count: 1, Cores: 1, MemoryGB: 1},
|
|
}
|
|
err := r.Validate()
|
|
if err == nil || !strings.Contains(err.Error(), "lease") {
|
|
t.Errorf("want a lease error, got %v", err)
|
|
}
|
|
}
|