package resolution import ( "encoding/json" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) 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 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 mcat.Rendering) map[string]any { out, err := Declaration(mcat.Resolution{Node: "laptop", Modules: []mcat.Manifest{m}}, 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(mcat.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(mcat.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(mcat.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") } } // 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"} if _, err := Declaration((mcat.Resolution{Node: "laptop", Modules: []mcat.Manifest{m}}), mcat.Rendering{ReadsHealth: true, Ports: map[string]map[int]int{"app": {8080: 31001}}}); err != nil { t.Fatal(err) } } }