SPB Git

Repository Public

actions_test.go 5.3 KB 139 lines go

1package actions23import (4	"encoding/json"5	"strings"6	"testing"7)89func params(t *testing.T, s string) map[string]any {10	t.Helper()11	var m map[string]any12	if err := json.Unmarshal([]byte(s), &m); err != nil {13		t.Fatal(err)14	}15	return m16}1718func TestParamValidation(t *testing.T) {19	cases := []struct {20		typ, params string21		ok          bool22	}{23		{"inventory.refresh", `{}`, true},24		{"inventory.refresh", `{"x":1}`, false}, // no unknown field25		{"diagnostic.run", `{"profile":"disk"}`, true},26		{"diagnostic.run", `{}`, false},                // required27		{"diagnostic.run", `{"profile":"all"}`, false}, // enum28		{"diagnostic.run", `{"profile":7}`, false},     // type29		{"diagnostic.run", `{"profile":"disk","cmd":"id"}`, false},30		{"service.logs.tail", `{}`, true},31		{"service.logs.tail", `{"lines":500}`, true},32		{"service.logs.tail", `{"lines":501}`, false}, // range33		{"service.logs.tail", `{"lines":0}`, false},34		{"service.logs.tail", `{"lines":1.5}`, false}, // integer35		{"service.logs.tail", `{"lines":"50"}`, false},36		{"service.logs.tail", `{"lines":1e30}`, false},37		{"cache.purge", `{"cache":"pip"}`, true},38		{"cache.purge", `{"cache":"/var/cache"}`, false}, // a name, never a path39		{"cache.purge", `{"cache":"../x"}`, false},40		{"cache.purge", `{"cache":"a b"}`, false},41		{"cache.purge", `{"cache":""}`, false},42		{"cache.purge", `{"cache":"` + strings.Repeat("a", 65) + `"}`, false},43		{"node.maintenance.set", `{"enabled":true,"reason":"fan"}`, true},44		{"node.maintenance.set", `{"enabled":"yes"}`, false},45		{"node.maintenance.set", `{"reason":"fan"}`, false},46		{"node.maintenance.set", `{"enabled":true,"reason":"` + strings.Repeat("é", 501) + `"}`, false},47		{"job.run", `{"template":"disk.report","params":{"a":[1,2,{"b":null}]}}`, true},48		{"job.run", `{"template":"disk.report","params":"x"}`, false},49		{"job.run", `{"template":"Disk Report"}`, false},50		{"job.run", `{"template":"t","params":{"blob":"` + strings.Repeat("x", 17000) + `"}}`, false}, // bounded51		{"model.server.start", `{"model":"mlx-community/Qwen3-32B-4bit","port":8001,"mem_bytes":20000000000}`, true},52		{"model.server.start", `{"model":"x","port":80}`, false},53		{"model.server.start", `{"model":"x; rm -rf /"}`, false},54		{"system.reboot", `{}`, true},55		{"system.reboot", `{"force":true}`, false},56	}57	for _, c := range cases {58		typ, ok := Lookup(c.typ)59		if !ok {60			t.Fatalf("unknown type %s", c.typ)61		}62		err := typ.Params.Validate(params(t, c.params))63		if (err == nil) != c.ok {64			t.Errorf("%s %s: err=%v, want ok=%v", c.typ, c.params, err, c.ok)65		}66	}67}6869func TestCatalogConsistency(t *testing.T) {70	seen := map[string]bool{}71	for _, typ := range Catalog {72		if seen[typ.Name] {73			t.Errorf("%s listed twice", typ.Name)74		}75		seen[typ.Name] = true76		if typ.TimeoutS <= 0 || typ.TimeoutS > 3600 || len(typ.TargetKinds) == 0 || typ.Title == "" || typ.Description == "" {77			t.Errorf("%s: incomplete entry", typ.Name)78		}79		if typ.Permission() != "actions.run."+typ.Criticality {80			t.Errorf("%s: permission", typ.Name)81		}82		for name, p := range typ.Params.Props {83			if p.Type != "string" && p.Type != "integer" && p.Type != "boolean" && p.Type != "object" {84				t.Errorf("%s.%s: unsupported type %q", typ.Name, name, p.Type)85			}86			if name == "target" {87				t.Errorf("%s: 'target' is reserved for the resolved target sent to the agent", typ.Name)88			}89		}90		for _, r := range typ.Params.Required {91			if _, ok := typ.Params.Props[r]; !ok {92				t.Errorf("%s: required parameter %s is not declared", typ.Name, r)93			}94		}95		// The published schema is strict and round-trips through JSON.96		s := typ.Params.JSONSchema()97		if s["additionalProperties"] != false || s["type"] != "object" {98			t.Errorf("%s: schema not strict", typ.Name)99		}100		if _, err := json.Marshal(s); err != nil {101			t.Errorf("%s: schema: %v", typ.Name, err)102		}103		if typ.ServerSide && typ.Capability(nil) == "" {104			t.Errorf("%s: capability", typ.Name)105		}106	}107	// Flags follow the design brief.108	for name, want := range map[string][4]bool{ // step-up, justification, confirmation, approval109		"inventory.refresh": {false, false, false, false}, "service.restart": {false, false, false, false},110		"service.stop": {true, false, false, false}, "model.server.start": {true, false, false, false},111		"system.reboot": {true, true, true, true}, "system.shutdown": {true, true, true, true},112	} {113		typ, _ := Lookup(name)114		got := [4]bool{typ.RequiresStepUp(), typ.RequiresJustification(), typ.RequiresConfirmation(), typ.RequiresApproval()}115		if got != want {116			t.Errorf("%s: flags %v, want %v", name, got, want)117		}118	}119	for _, name := range []string{"deploy.release", "system.update.schedule"} {120		if typ, _ := Lookup(name); typ.Available {121			t.Errorf("%s must be catalogued but unavailable", name)122		}123	}124	if tpl, ok := TemplateOf("job.run:disk.report"); !ok || tpl != "disk.report" {125		t.Error("TemplateOf")126	}127	if _, ok := TemplateOf("job.run:../x"); ok {128		t.Error("TemplateOf accepted a malformed name")129	}130	if _, ok := TemplateOf("service.restart"); ok {131		t.Error("TemplateOf accepted a command type")132	}133	a := HashRequest(map[string]any{"b": 1, "a": map[string]any{"y": 2, "x": 1}})134	b := HashRequest(map[string]any{"a": map[string]any{"x": 1, "y": 2}, "b": 1})135	if string(a) != string(b) || string(a) == string(HashRequest(map[string]any{"b": 2})) {136		t.Error("request hash is not canonical")137	}138}139
Files main
Menu

Theme