Files
ilkerandClaude Opus 5 9ad16290bf hpcod: oversubscription is a policy you can change, not a line in a file
It was enabled - 168 cores offered from 28, 655 GiB from 504 - but only
because I edited sites.yaml on the server and restarted the service. So
changing it needed a shell, an editor and a restart, which means it needed
whoever had those rather than whoever had the judgement. It is a judgement
about how much of the hardware to promise, it moves with load, and it
belongs where the person making it can reach it.

It is now a row per site, set from the Sites page by an administrator, and
it takes effect on the next discovery sweep rather than the next restart.

The ratio is applied after discovery instead of inside the provider, which
is what makes that possible - and because it recomputes from the physical
core and memory counts rather than from whatever Cores already held,
applying it twice gives the same answer as applying it once. Without that
the provider's own default would compound with it.

The file stays as the default. A site with no row shows what the catalogue
gave it, read back from the numbers rather than claimed, and says so: "from
the site catalogue - nobody has set this" against "set by ilker". One
authority at a time, and the page says which.

The rules the config loader already enforced are enforced here too, in the
database rather than in the handler: a policy that reads as off cannot
still multiply, and turning it off resets the ratios instead of leaving
numbers behind that would take effect again for whoever turns it back on.
Memory is capped at 8x where CPU is capped at 64x, because oversubscribed
cores make jobs slow and oversubscribed memory makes them die.

Also fixed a test that was crying wolf: the public-page leak check matched
bare substrings, so adding a "label" rule to the stylesheet looked like the
tenant name "lab" appearing on the front door. It matches word boundaries
now - a test that raises false alarms gets weakened until it stops catching
anything.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 12:54:24 +02:00

208 lines
7.1 KiB
Go

package httpapi
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/ilkermanap/hpcod/internal/model"
)
func siteWithHardware() []model.Site {
return []model.Site{{
ID: "hv01", Reachable: true, Healthy: true,
Nodes: []model.Node{{
Name: "hv01", Online: true,
PhysicalCores: 28, PhysicalMemBytes: 504 << 30,
// What the provider already applied from the catalogue.
Cores: 56, MemBytes: 504 << 30,
}},
}}
}
// The property that makes applying policy after discovery safe: it
// recomputes from the hardware, so running it twice is running it once.
func TestApplyOvercommitIsIdempotent(t *testing.T) {
sites := siteWithHardware()
p := map[string]model.Overcommit{"hv01": {Site: "hv01", Enabled: true, CPU: 6, Mem: 1.3}}
model.ApplyOvercommit(sites, p)
first := sites[0].Nodes[0].Cores
model.ApplyOvercommit(sites, p)
model.ApplyOvercommit(sites, p)
if first != 168 {
t.Errorf("cores = %d, want 168 (28 x 6)", first)
}
if got := sites[0].Nodes[0].Cores; got != first {
t.Errorf("applying twice compounded: %d then %d", first, got)
}
}
func TestPolicyOffReturnsTheHardware(t *testing.T) {
sites := siteWithHardware()
model.ApplyOvercommit(sites, map[string]model.Overcommit{
// Ratios left behind but the switch is off: they must not apply.
"hv01": {Site: "hv01", Enabled: false, CPU: 6, Mem: 1.3},
})
n := sites[0].Nodes[0]
if n.Cores != 28 || n.MemBytes != 504<<30 {
t.Errorf("off should offer the hardware: %d cores, %d bytes", n.Cores, n.MemBytes)
}
}
// A node that never reported its hardware must be left alone rather than
// have capacity invented for it.
func TestApplyOvercommitLeavesUnknownHardwareAlone(t *testing.T) {
sites := []model.Site{{ID: "hv01", Nodes: []model.Node{{Name: "x", Cores: 99, MemBytes: 7}}}}
model.ApplyOvercommit(sites, map[string]model.Overcommit{
"hv01": {Site: "hv01", Enabled: true, CPU: 6, Mem: 2},
})
if n := sites[0].Nodes[0]; n.Cores != 99 || n.MemBytes != 7 {
t.Errorf("a node with no physical figures was rewritten: %+v", n)
}
}
func TestOnlyAnAdminCanChangeThePolicy(t *testing.T) {
f := &fakeStore{}
s := testServerWithPages(t, f)
s.cache.set(siteWithHardware())
form := url.Values{"site": {"hv01"}, "enabled": {"on"}, "cpu": {"6"}, "mem": {"1.3"}}
r := httptest.NewRequest(http.MethodPost, "/admin/site-policy", strings.NewReader(form.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r = r.WithContext(withUser(r.Context(), "someone", false))
w := httptest.NewRecorder()
s.setSitePolicy(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("a non-admin got %d, want 404", w.Code)
}
if len(f.policies) != 0 {
t.Errorf("the policy changed anyway: %v", f.policies)
}
}
func TestAnAdminSetsThePolicyAndItTakesEffectAtOnce(t *testing.T) {
f := &fakeStore{}
s := testServerWithPages(t, f)
s.cache.set(siteWithHardware())
form := url.Values{"site": {"hv01"}, "enabled": {"on"}, "cpu": {"6"}, "mem": {"1.3"}}
r := httptest.NewRequest(http.MethodPost, "/admin/site-policy", strings.NewReader(form.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r = r.WithContext(withUser(r.Context(), "theboss", true))
w := httptest.NewRecorder()
s.setSitePolicy(w, r)
if w.Code != http.StatusSeeOther {
t.Fatalf("status %d", w.Code)
}
got := f.policies["hv01"]
if !got.Enabled || got.CPU != 6 || got.Mem != 1.3 || got.UpdatedBy != "theboss" {
t.Errorf("recorded %+v", got)
}
// The cache must already show it, or an operator reasonably concludes
// the change did not take.
sites, _ := s.cache.get()
if c := sites[0].Nodes[0].Cores; c != 168 {
t.Errorf("the cached inventory still offers %d cores, want 168", c)
}
}
// Turning it off must not leave the ratios behind, waiting to take effect
// again for whoever turns it back on.
func TestTurningItOffResetsTheRatios(t *testing.T) {
f := &fakeStore{}
s := testServerWithPages(t, f)
s.cache.set(siteWithHardware())
post := func(form url.Values) {
r := httptest.NewRequest(http.MethodPost, "/admin/site-policy", strings.NewReader(form.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r = r.WithContext(withUser(r.Context(), "theboss", true))
s.setSitePolicy(httptest.NewRecorder(), r)
}
post(url.Values{"site": {"hv01"}, "enabled": {"on"}, "cpu": {"6"}, "mem": {"1.3"}})
post(url.Values{"site": {"hv01"}, "cpu": {"6"}, "mem": {"1.3"}}) // checkbox unchecked
got := f.policies["hv01"]
if got.Enabled || got.CPU != 1 || got.Mem != 1 {
t.Errorf("after turning it off: %+v", got)
}
sites, _ := s.cache.get()
if c := sites[0].Nodes[0].Cores; c != 28 {
t.Errorf("the estate still offers %d cores, want the hardware's 28", c)
}
}
// A typo in the site name would record a policy that applies to nothing
// and look like it worked.
func TestAnUnknownSiteIsRefused(t *testing.T) {
f := &fakeStore{}
s := testServerWithPages(t, f)
s.cache.set(siteWithHardware())
form := url.Values{"site": {"pve11"}, "enabled": {"on"}, "cpu": {"6"}, "mem": {"1.3"}}
r := httptest.NewRequest(http.MethodPost, "/admin/site-policy", strings.NewReader(form.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r = r.WithContext(withUser(r.Context(), "theboss", true))
w := httptest.NewRecorder()
s.setSitePolicy(w, r)
if w.Code != http.StatusBadRequest {
t.Errorf("status %d, want 400", w.Code)
}
if len(f.policies) != 0 {
t.Errorf("a policy was recorded for a site that does not exist: %v", f.policies)
}
}
// With no row recorded, the page has to show what is actually in effect -
// which is whatever the site catalogue said - not a 1.0 it cannot support.
func TestThePageReadsBackTheCatalogueDefault(t *testing.T) {
s := testServerWithPages(t, &fakeStore{})
s.cache.set(siteWithHardware()) // 28 physical, 56 offered: the file said x2
got := s.sitePolicies(httptest.NewRequest(http.MethodGet, "/sites", nil))
if len(got) != 1 {
t.Fatalf("got %d sites", len(got))
}
v := got[0]
if !v.FromFile {
t.Error("a site with no recorded policy is not marked as coming from the file")
}
if v.CPU < 1.99 || v.CPU > 2.01 || !v.Enabled {
t.Errorf("read back cpu=%.2f enabled=%v, want about 2.0 and enabled", v.CPU, v.Enabled)
}
if v.PhysicalCores != 28 || v.OfferedCores != 56 {
t.Errorf("hardware %d, offered %d", v.PhysicalCores, v.OfferedCores)
}
}
// The control must not be rendered for someone who cannot use it.
func TestTheFormIsOnlyShownToAdministrators(t *testing.T) {
s := testServerWithPages(t, &fakeStore{})
s.cache.set(siteWithHardware())
r := httptest.NewRequest(http.MethodGet, "/sites", nil)
r = r.WithContext(withUser(r.Context(), "someone", false))
w := httptest.NewRecorder()
s.pageSites(w, r)
if strings.Contains(w.Body.String(), "/admin/site-policy") {
t.Error("a non-admin was shown the policy form")
}
if !strings.Contains(w.Body.String(), "Only an administrator") {
t.Error("the page does not say why there is no control")
}
w = httptest.NewRecorder()
s.pageSites(w, adminRequest(http.MethodGet, "/sites", nil))
if !strings.Contains(w.Body.String(), "/admin/site-policy") {
t.Error("an administrator was not shown the policy form")
}
}