Issue 1585, S19: the strict parser, every manifest problem function and the judge's tables (192 declarations, about 5,200 lines) and their 244 tests leave; composition keeps its own refusals and the helpers it uses. The catalogue's tests decode the manifests they build. ParseManifest, ParseManifestWith, ParseManifestForInputs, CatalogueProblems and the rest are gone; mesh-delivery-indexer/judge holds them. Claude-Session: https://claude.ai/code/session_01NcbgfJ9Gum47zkFKWRaovL
164 lines
6.9 KiB
Go
164 lines
6.9 KiB
Go
package catalogue
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/validate"
|
|
)
|
|
|
|
// A module says how each long-running resource is ready (novox/hq ADR 0240 rule 2, to-be 48 §8): `module
|
|
// check` refuses each part out of its bounds, each endpoint named by a port or an address, a tool the
|
|
// module does not serve, and a tool check alone — a test per refusal.
|
|
|
|
const healthDigest = "@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
// healthManifest is a module of one web container, one running service and one step, with the health
|
|
// given on the container (or on the resource named by on).
|
|
func healthManifest(t *testing.T, on string, health map[string]any, more ...map[string]any) []byte {
|
|
t.Helper()
|
|
resources := []map[string]any{
|
|
{"id": "server", "type": "container", "name": "app-server", "image": "registry.example/app" + healthDigest,
|
|
"ports": []any{"8080"}},
|
|
{"id": "daemon", "type": "service", "unit": "app.service", "state": "running"},
|
|
{"id": "seed", "type": "container", "name": "app-seed", "image": "registry.example/app" + healthDigest,
|
|
"run-once": true},
|
|
{"id": "sweep", "type": "container", "name": "app-sweep", "image": "registry.example/app" + healthDigest,
|
|
"schedule": "0 3 * * *"},
|
|
}
|
|
resources = append(resources, more...)
|
|
for _, r := range resources {
|
|
if r["id"] == on && health != nil {
|
|
r["health"] = health
|
|
}
|
|
}
|
|
m := map[string]any{"module": "app", "requires": []any{"postgres-database"}, "tools": []any{"app_status"},
|
|
"listens": []any{
|
|
map[string]any{"name": "web", "port": 8080, "from": "mesh"},
|
|
map[string]any{"name": "beacon", "port": 9999, "protocol": "udp", "from": "mesh"},
|
|
},
|
|
"resources": resources}
|
|
raw, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func TestAWellFormedHealthIsAccepted(t *testing.T) {
|
|
for _, h := range []map[string]any{
|
|
{"kind": "http", "endpoint": "web", "path": "/healthz", "status": 200, "body": "ok", "needs": "postgres-database"},
|
|
{"kind": "tcp", "endpoint": "web", "interval": "10s", "timeout": "2s", "looks": 2, "grace": "0s"},
|
|
{"kind": "runtime"},
|
|
{"kind": "exec", "command": "pg_isready -q", "grace": "4m", "looks": 2, "interval": "30s"},
|
|
} {
|
|
if _, err := decodeForTest(healthManifest(t, "server", h)); err != nil {
|
|
t.Errorf("%v was refused: %v", h, err)
|
|
}
|
|
}
|
|
if _, err := decodeForTest(healthManifest(t, "daemon", map[string]any{"kind": "unit"})); err != nil {
|
|
t.Errorf("a service's own readiness was refused: %v", err)
|
|
}
|
|
// A tool beside a check of another kind on the module.
|
|
raw := healthManifest(t, "server", map[string]any{"kind": "http", "endpoint": "web"},
|
|
map[string]any{"id": "admin", "type": "service", "unit": "app-admin.service", "state": "running",
|
|
"health": map[string]any{"kind": "tool", "tool": "app_status"}})
|
|
if _, err := decodeForTest(raw); err != nil {
|
|
t.Errorf("a tool check beside an http check was refused: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestHealthIsComposedAsThePortThisMachineGaveTheEndpoint(t *testing.T) {
|
|
m, err := decodeForTest(healthManifest(t, "server", map[string]any{"kind": "http", "endpoint": "web", "path": "/healthz",
|
|
"needs": "postgres-database"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
compose := func(with Rendering) map[string]any {
|
|
out, err := Resolution{Node: "laptop", Modules: []Manifest{m}}.Declaration(with)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
at := indexOfID(out, "app.server")
|
|
if at < 0 {
|
|
t.Fatal("the container was lost")
|
|
}
|
|
return out[at]
|
|
}
|
|
// An engine older than the field is not sent it: it would refuse the whole declaration.
|
|
if h, sent := compose(Rendering{Ports: map[string]map[int]int{"app": {8080: 31001}}})["health"]; sent {
|
|
t.Fatalf("health was sent to an engine that does not read it: %v", h)
|
|
}
|
|
got := compose(Rendering{ReadsHealth: true, Ports: map[string]map[int]int{"app": {8080: 31001}}})["health"].(map[string]any)
|
|
if got["port"] != 31001 || got["endpoint"] != "web" || got["path"] != "/healthz" || got["needs"] != "postgres-database" {
|
|
t.Errorf("composed as %v", got)
|
|
}
|
|
if got["interval"] != "30s" || got["timeout"] != "5s" || got["looks"] != 3 || got["grace"] != "1m0s" {
|
|
t.Errorf("the defaults were not written out: %v", got)
|
|
}
|
|
// The port this machine gives the endpoint moves, and the check moves with it.
|
|
moved := compose(Rendering{ReadsHealth: true, Ports: map[string]map[int]int{"app": {8080: 31002}}})["health"].(map[string]any)
|
|
if moved["port"] != 31002 {
|
|
t.Errorf("after the port moved the check still dials %v", moved["port"])
|
|
}
|
|
// And the catalogue's manifest is untouched by composing it.
|
|
if _, ok := m.Resources[0]["health"].(map[string]any)["port"]; ok {
|
|
t.Error("composing wrote the port into the catalogue's own manifest")
|
|
}
|
|
}
|
|
|
|
func TestTheUndeclaredAreTheLongRunningWithoutHealth(t *testing.T) {
|
|
m, err := decodeForTest(healthManifest(t, "server", map[string]any{"kind": "runtime"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(Undeclared(m), ","); got != "daemon" {
|
|
t.Errorf("undeclared: %q; the step and the schedule are not long-running, the server declares", got)
|
|
}
|
|
}
|
|
|
|
func TestATooledHealthIsGrantedToTheEngine(t *testing.T) {
|
|
raw := healthManifest(t, "server", map[string]any{"kind": "http", "endpoint": "web"},
|
|
map[string]any{"id": "admin", "type": "service", "unit": "app-admin.service", "state": "running",
|
|
"health": map[string]any{"kind": "tool", "tool": "app_status"}})
|
|
m, err := decodeForTest(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := HealthChecks(m); len(got) != 1 || got[0] != "app.app_status" {
|
|
t.Errorf("checks %v", got)
|
|
}
|
|
}
|
|
|
|
// What the controller composes the node-engine takes: every kind, composed for an engine that reads it,
|
|
// passes the engine's own validator (mesh-host/validate) — one set of words on both sides.
|
|
func TestEveryComposedHealthIsOneTheNodeEngineTakes(t *testing.T) {
|
|
for _, h := range []map[string]any{
|
|
{"kind": "http", "endpoint": "web", "path": "/healthz", "status": 200, "body": "ok", "needs": "postgres-database"},
|
|
{"kind": "tcp", "endpoint": "web", "interval": "10s", "timeout": "2s", "looks": 2, "grace": "0s"},
|
|
{"kind": "runtime"},
|
|
{"kind": "exec", "command": "pg_isready -q"},
|
|
} {
|
|
m, err := decodeForTest(healthManifest(t, "server", h,
|
|
map[string]any{"id": "admin", "type": "service", "unit": "app-admin.service", "state": "running",
|
|
"health": map[string]any{"kind": "tool", "tool": "app_status"}}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.Resources[1]["health"] = map[string]any{"kind": "unit"}
|
|
out, err := Resolution{Node: "laptop", Modules: []Manifest{m}}.Declaration(Rendering{ReadsHealth: true,
|
|
Ports: map[string]map[int]int{"app": {8080: 31001}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, err := json.Marshal(map[string]any{"declaration": validate.Version, "resources": out})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if problems := validate.Declaration(body); len(problems) > 0 {
|
|
t.Errorf("%v composed into something the node-engine refuses: %v", h, problems)
|
|
}
|
|
}
|
|
}
|