Placement knew how to pick the node with the GPU. It did not know which GPUs were already taken, in two separate ways. Device.AllocatedTo was read in three places and written in none, so every discovery reported every card as free. Two clusters provisioned in sequence would both be assigned the same GPU; the second VM fails to start, hours after the portal promised the hardware. Ownership now comes from the hypervisor, which also accounts for guests created outside hpcod. Two details that are easy to get wrong and are covered by tests: a stopped guest still owns its card, because the assignment lives in the config and not in the running process; and ownership is per slot, not per function, so a guest holding 01:00.0 has the whole card. Measured against the live site. A stopped VM was given the Quadro K620: discovery went from alloc='' to alloc='vm 9099 (hpcod-devtest)', the card dropped out of the schedulable set, and a GPU request went from being rejected for want of cores to "gpu: none present (need 1)". Destroying the VM put it back. The ConnectX-4 Lx on the same host is classified roce, not infiniband, and stays unschedulable for want of agent confirmation - which is the correct answer for that card. The second way is the window the hypervisor cannot see. A cluster takes minutes to build, and until its VMs exist the cards it was promised still look free. So a reservation is written when the cluster is allocated, in the same transaction as its address, and the planner reads both. The partial unique index on live devices enforces one holder per card exactly as the cluster_id index does for addresses. Allocation now takes the whole placement rather than a site name, because storing an address without a record of where the cluster runs creates a cluster nothing will ever release. A device conflict therefore rolls the address back too - tested by checking that the next allocation gets the number the failed one would have consumed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
117 lines
4.1 KiB
Go
117 lines
4.1 KiB
Go
package proxmox
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// testServer answers the listed paths with the given JSON and 404s anything
|
|
// else, so a test that reaches for an endpoint it did not declare fails
|
|
// rather than quietly seeing an empty result.
|
|
func testServer(t *testing.T, routes map[string]string) *client {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
body, ok := routes[r.URL.Path]
|
|
if !ok {
|
|
t.Errorf("unexpected request for %s", r.URL.Path)
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(body))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
c, err := newClient(srv.URL, "u@pve!t=x", true, 5*time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func TestHostPCISlots(t *testing.T) {
|
|
mappings := map[string][]string{
|
|
"gpu": {"hv01/0000:01:00", "pve2/0000:81:00"},
|
|
}
|
|
|
|
cases := []struct {
|
|
name string
|
|
value string
|
|
node string
|
|
want []string
|
|
}{
|
|
{"full address", "0000:01:00.0", "hv01", []string{"0000:01:00"}},
|
|
{"short address", "01:00.0", "hv01", []string{"0000:01:00"}},
|
|
{"slot only, no function", "0000:01:00", "hv01", []string{"0000:01:00"}},
|
|
{"with options", "0000:01:00,pcie=1,x-vga=1", "hv01", []string{"0000:01:00"}},
|
|
{"two devices", "0000:01:00.0;0000:02:00.0", "hv01", []string{"0000:01:00", "0000:02:00"}},
|
|
{"both functions of one card collapse", "0000:01:00.0;0000:01:00.1", "hv01", []string{"0000:01:00"}},
|
|
{"uppercase", "0000:0A:00.0", "hv01", []string{"0000:0a:00"}},
|
|
|
|
// A mapping resolves differently per node, which is the whole point
|
|
// of mappings: the same name is a different card on each hypervisor.
|
|
{"mapping on hv01", "mapping=gpu,pcie=1", "hv01", []string{"0000:01:00"}},
|
|
{"mapping on pve2", "mapping=gpu,pcie=1", "pve2", []string{"0000:81:00"}},
|
|
{"mapping absent on this node", "mapping=gpu", "hv03", nil},
|
|
{"unknown mapping", "mapping=nosuch", "hv01", nil},
|
|
|
|
{"garbage", "not-a-pci-id", "hv01", nil},
|
|
{"empty", "", "hv01", nil},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
got := hostPCISlots(c.value, c.node, mappings)
|
|
if !reflect.DeepEqual(got, c.want) {
|
|
t.Errorf("hostPCISlots(%q, %q) = %v, want %v", c.value, c.node, got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The mapping list arrives as a flat option string per entry; a mapping that
|
|
// names several nodes has to stay separable by node.
|
|
func TestPCIMappingsParseOptionStrings(t *testing.T) {
|
|
srv := testServer(t, map[string]string{
|
|
"/api2/json/cluster/mapping/pci": `{"data":[{"id":"gpu","map":[
|
|
"id=10de:13bb,iommugroup=47,node=hv01,path=0000:01:00.0",
|
|
"id=10de:13bb,iommugroup=12,node=pve2,path=0000:81:00.0"]},
|
|
{"id":"broken","map":["id=10de:13bb,iommugroup=9"]}]}`,
|
|
})
|
|
p := &Provider{cli: srv}
|
|
|
|
got := p.pciMappings(t.Context())
|
|
want := map[string][]string{"gpu": {"hv01/0000:01:00", "pve2/0000:81:00"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("pciMappings() = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// The finding this whole file exists for: a device held by a guest must stop
|
|
// being offered, and a stopped guest holds its device just as firmly as a
|
|
// running one.
|
|
func TestDeviceOwnershipIncludesStoppedGuests(t *testing.T) {
|
|
srv := testServer(t, map[string]string{
|
|
"/api2/json/cluster/resources": `{"data":[
|
|
{"type":"qemu","node":"hv01","vmid":100,"name":"running-one","status":"running"},
|
|
{"type":"qemu","node":"hv01","vmid":101,"name":"stopped-one","status":"stopped"},
|
|
{"type":"lxc","node":"hv01","vmid":102,"name":"a-container","status":"running"}]}`,
|
|
"/api2/json/cluster/mapping/pci": `{"data":[]}`,
|
|
"/api2/json/nodes/hv01/qemu/100/config": `{"data":{"name":"running-one","hostpci0":"0000:01:00,pcie=1"}}`,
|
|
"/api2/json/nodes/hv01/qemu/101/config": `{"data":{"name":"stopped-one","hostpci0":"0000:07:00.0"}}`,
|
|
})
|
|
p := &Provider{cli: srv}
|
|
|
|
got := p.deviceOwners(t.Context())
|
|
want := map[string]string{
|
|
"hv01/0000:01:00": "vm 100 (running-one)",
|
|
"hv01/0000:07:00": "vm 101 (stopped-one)",
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("deviceOwners() = %v, want %v", got, want)
|
|
}
|
|
}
|