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>
239 lines
6.9 KiB
Go
239 lines
6.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ilkermanap/hpcod/internal/model"
|
|
)
|
|
|
|
const aHash = "$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy"
|
|
|
|
// The rule the application is allowed to rely on: registering cannot
|
|
// produce a usable account, because the columns say so.
|
|
func TestANewAccountIsNotUsable(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
|
|
a, err := s.CreateAccount(ctx, "newcomer", aHash, "n@example.invalid", "a term project")
|
|
if err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
if a.State != "pending" {
|
|
t.Errorf("state = %q, want pending", a.State)
|
|
}
|
|
if a.IsAdmin {
|
|
t.Error("a new account is an administrator")
|
|
}
|
|
if a.DecidedAt != nil || a.DecidedBy != "" {
|
|
t.Errorf("a new account already has a decision: %v %q", a.DecidedAt, a.DecidedBy)
|
|
}
|
|
}
|
|
|
|
func TestUsernamesAreUnique(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
|
|
if _, err := s.CreateAccount(ctx, "newcomer", aHash, "", "x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := s.CreateAccount(ctx, "newcomer", aHash, "", "someone else entirely")
|
|
if !errors.Is(err, ErrAccountExists) {
|
|
t.Fatalf("second registration: %v, want ErrAccountExists", err)
|
|
}
|
|
}
|
|
|
|
// A plaintext password reaching the column is the failure worth catching in
|
|
// the schema, because by the time it matters the damage is done.
|
|
func TestThePasswordColumnRefusesPlaintext(t *testing.T) {
|
|
s := testStore(t)
|
|
for _, bad := range []string{"correct-horse-battery", "", "$2a$short"} {
|
|
if _, err := s.CreateAccount(context.Background(), "x"+bad, bad, "", "x"); err == nil {
|
|
t.Errorf("the database accepted %q as a password hash", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An audit trail with holes in it is not one.
|
|
func TestADecisionMustSayWhoMadeIt(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
if _, err := s.CreateAccount(ctx, "newcomer", aHash, "", "x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := s.DecideAccount(ctx, "newcomer", "active", ""); err == nil {
|
|
t.Error("an unattributed decision was accepted")
|
|
}
|
|
if _, err := s.DecideAccount(ctx, "newcomer", "sideways", "theboss"); err == nil {
|
|
t.Error("a state that is not a decision was accepted")
|
|
}
|
|
|
|
a, err := s.DecideAccount(ctx, "newcomer", "active", "theboss")
|
|
if err != nil {
|
|
t.Fatalf("approve: %v", err)
|
|
}
|
|
if !a.Active() || a.DecidedBy != "theboss" || a.DecidedAt == nil {
|
|
t.Errorf("approved account = %+v", a)
|
|
}
|
|
if a.IsAdmin {
|
|
t.Error("approving also granted administrator rights")
|
|
}
|
|
}
|
|
|
|
func TestAdminRightsAreSeparateFromApproval(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
if _, err := s.CreateAccount(ctx, "newcomer", aHash, "", "x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.DecideAccount(ctx, "newcomer", "active", "theboss"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
a, err := s.SetAdmin(ctx, "newcomer", true)
|
|
if err != nil {
|
|
t.Fatalf("grant: %v", err)
|
|
}
|
|
if !a.IsAdmin || !a.Active() {
|
|
t.Errorf("after granting: %+v", a)
|
|
}
|
|
if a, err = s.SetAdmin(ctx, "newcomer", false); err != nil || a.IsAdmin {
|
|
t.Errorf("after revoking: %+v (%v)", a, err)
|
|
}
|
|
}
|
|
|
|
// The hash comes back only from the one call that needs it, and the listing
|
|
// carries no way to reach it.
|
|
func TestTheHashIsOnlyWhereItIsNeeded(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
if _, err := s.CreateAccount(ctx, "newcomer", aHash, "", "x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, hash, err := s.AccountWithHash(ctx, "newcomer")
|
|
if err != nil {
|
|
t.Fatalf("lookup: %v", err)
|
|
}
|
|
if hash != aHash {
|
|
t.Errorf("hash = %q", hash)
|
|
}
|
|
|
|
list, err := s.Accounts(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 1 {
|
|
t.Fatalf("listed %d accounts", len(list))
|
|
}
|
|
if strings.Contains(sprintAccount(list[0]), "$2a$") {
|
|
t.Errorf("a listed account carries its hash: %s", sprintAccount(list[0]))
|
|
}
|
|
|
|
if _, _, err := s.AccountWithHash(ctx, "nobody"); !errors.Is(err, ErrNoAccount) {
|
|
t.Errorf("unknown account: %v, want ErrNoAccount", err)
|
|
}
|
|
}
|
|
|
|
func sprintAccount(a Account) string {
|
|
return a.Username + "|" + a.Email + "|" + a.Reason + "|" + a.State + "|" + a.DecidedBy
|
|
}
|
|
|
|
// Whoever came to approve should not have to hunt for the pending ones.
|
|
func TestPendingAccountsAreListedFirst(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
for _, n := range []string{"first", "second", "third"} {
|
|
if _, err := s.CreateAccount(ctx, n, aHash, "", "x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := s.DecideAccount(ctx, "first", "active", "theboss"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
list, err := s.Accounts(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 3 || list[0].State != "pending" || list[1].State != "pending" {
|
|
t.Fatalf("order = %v", []string{list[0].Username + ":" + list[0].State,
|
|
list[1].Username + ":" + list[1].State, list[2].Username + ":" + list[2].State})
|
|
}
|
|
|
|
n, err := s.PendingCount(ctx)
|
|
if err != nil || n != 2 {
|
|
t.Errorf("PendingCount = %d (%v), want 2", n, err)
|
|
}
|
|
}
|
|
|
|
// ---------- site policy ----------
|
|
|
|
// The contradiction the config loader refuses must also be impossible to
|
|
// store, or the database becomes a way around the rule.
|
|
func TestAPolicyThatReadsAsOffCannotInflate(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := s.pool.Exec(ctx, `
|
|
INSERT INTO site_policy (site, oversubscribe, cpu_overcommit, mem_overcommit, updated_by)
|
|
VALUES ('hv01', false, 6.0, 1.0, 'someone')`)
|
|
if err == nil {
|
|
t.Error("the database stored a policy that is off but still multiplies")
|
|
}
|
|
}
|
|
|
|
func TestPolicyRoundTrip(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
|
|
got, err := s.SetPolicy(ctx, model.Overcommit{Site: "hv01", Enabled: true, CPU: 6, Mem: 1.3}, "theboss")
|
|
if err != nil {
|
|
t.Fatalf("set: %v", err)
|
|
}
|
|
if !got.Enabled || got.CPU != 6 || got.Mem != 1.3 {
|
|
t.Fatalf("got %+v", got)
|
|
}
|
|
|
|
all, err := s.Policies(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := all["hv01"]; p.UpdatedBy != "theboss" || p.CPU != 6 {
|
|
t.Errorf("read back %+v", p)
|
|
}
|
|
|
|
// Setting it again replaces rather than failing on the primary key.
|
|
if _, err := s.SetPolicy(ctx, model.Overcommit{Site: "hv01", Enabled: false}, "someone"); err != nil {
|
|
t.Fatalf("second set: %v", err)
|
|
}
|
|
all, _ = s.Policies(ctx)
|
|
if p := all["hv01"]; p.Enabled || p.CPU != 1 || p.Mem != 1 {
|
|
t.Errorf("after turning it off: %+v", p)
|
|
}
|
|
}
|
|
|
|
func TestPolicyLimits(t *testing.T) {
|
|
s := testStore(t)
|
|
ctx := context.Background()
|
|
|
|
cases := map[string]model.Overcommit{
|
|
"cpu too high": {Site: "hv01", Enabled: true, CPU: 100, Mem: 1},
|
|
"mem too high": {Site: "hv01", Enabled: true, CPU: 1, Mem: 20},
|
|
"below one": {Site: "hv01", Enabled: true, CPU: 0.5, Mem: 1},
|
|
"on but no-op": {Site: "hv01", Enabled: true, CPU: 1, Mem: 1},
|
|
"no site": {Enabled: true, CPU: 2, Mem: 1},
|
|
}
|
|
for name, o := range cases {
|
|
if _, err := s.SetPolicy(ctx, o, "theboss"); err == nil {
|
|
t.Errorf("%s: accepted", name)
|
|
}
|
|
}
|
|
if _, err := s.SetPolicy(ctx, model.Overcommit{Site: "hv01", Enabled: true, CPU: 2}, ""); err == nil {
|
|
t.Error("an unattributed policy change was accepted")
|
|
}
|
|
}
|