package manifest import ( "bytes" "encoding/json" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-teller/tellerevents" "io/fs" "os" "path/filepath" "reflect" "slices" "strings" "testing" ) // The controller's manifest reader is mesh-controller internal/catalogue (Manifest, manifest.go: Consumes :521, // Invokes :590, Streams :613, Reads :659; StreamDeclaration, streams.go), an internal package of another repository. // These tests decode the manifest strictly into the fields those types name (DisallowUnknownFields) and hold the // rules of issue 1240 (ADR 0437 §4). The merge gate's module check proves the controller takes the manifest. type state struct { Name string `json:"name"` History int `json:"history,omitempty"` } type stream struct { Name string `json:"name"` Events []string `json:"events"` MaxAgeDays int `json:"max-age-days"` MaxMiB int `json:"max-mib"` } type artifact struct { Name string `json:"name"` Kind string `json:"kind"` Language string `json:"language"` System string `json:"system"` From string `json:"from"` Binary string `json:"binary"` Loads []string `json:"loads"` } type manifest struct { Module string `json:"module"` Version string `json:"version"` Slug string `json:"slug"` Tools []string `json:"tools"` Emits []string `json:"emits"` Uses []string `json:"uses"` State []state `json:"state"` Reads []string `json:"reads"` Invokes []string `json:"invokes"` Consumes []string `json:"consumes"` Streams []stream `json:"streams"` OwnSecrets map[string]string `json:"own-secrets"` SecretsOwner string `json:"secrets-owner"` Resources []map[string]any `json:"resources"` Requires []require `json:"requires"` Build struct { Artifacts []artifact `json:"artifacts"` } `json:"build"` } func load(t *testing.T) (manifest, map[string]json.RawMessage) { t.Helper() raw, err := os.ReadFile("module.json") if err != nil { t.Fatal(err) } var m manifest dec := json.NewDecoder(bytes.NewReader(raw)) dec.DisallowUnknownFields() if err := dec.Decode(&m); err != nil { t.Fatalf("the controller's reader refuses a key this manifest has: %v", err) } var top map[string]json.RawMessage if err := json.Unmarshal(raw, &top); err != nil { t.Fatal(err) } return m, top } func sorted(s []string) []string { s = slices.Clone(s) slices.Sort(s) return s } func TestManifestDeclarations(t *testing.T) { // 1240/AC1 m, top := load(t) if m.Module != "mesh-delivery-teller" { t.Errorf("module = %q", m.Module) } // answers is the teller's answer to each ask; asked is what it asked of the router, which condition it raised and which // ask key and revision each ask was made at (issue 1343); ends is the ends asked by hand with teller_end_ask (issue 774). // first-views is where its first view of the assigner's asks stands and findings its condition past the bound (ADR 0615). if want := []state{{Name: "answers", History: 5}, {Name: "asked", History: 1}, {Name: "ends", History: 1}, {Name: "first-views", History: 1}, {Name: "widens-said", History: 1}, {Name: "teller-marks", History: 1}}; !reflect.DeepEqual(m.State, want) { t.Errorf("state = %+v, want %+v", m.State, want) } // design 47 G §653, A-172: the teller reads no other module's state: the assigner's asks and the view's answers are the // owners' states, asked by their read tools and followed by their views families (ADR 0615 §1, DLV-M3; A-175). if len(m.Reads) != 0 { t.Errorf("reads = %v, want none", m.Reads) } // Its own states answers and teller-marks are said as its views family views.answers, read by teller_read_answers (A-175, A-178). // The join: its first view asked again by hand is a hand act, recorded through the seat (views.StatusTools). if want := []string{"mesh-delivery-assigner.assigner_read_asks", "mesh-delivery-view.view_read_answers", "seat:mesh-hand-acts.hand-act", "seat:mesh-catalog.registrations", "seat:mesh-broker.show", "seat:operator-channel.notify", // A-126: who holds the seat operator-channel, whose state asks holds the router's record of its asks (operatorask.Bucket). "seat:mesh-module-assigner.assignments"}; !reflect.DeepEqual(m.Invokes, want) { t.Errorf("invokes = %v, want %v", m.Invokes, want) } // design 47 G §335 (TL1): the widening of a pipeline end is told through the seat operator-channel and kept in widens-said. // design 47 G §387: the keeper's ask for the findings is heard, and the finding stream is declared (§383). if want := []string{"mesh-conditions.findings-asked", "mesh-delivery-assigner.views.asks", "mesh-delivery-view.views.answers", "mesh-catalog.registration-changed", "mesh-pipeline-runner.pipeline-ended", "mesh-delivery.recovery-merged", "mesh-delivery.record-opened", "mesh-broker.recreated", "mesh-broker.dropped"}; !reflect.DeepEqual(m.Consumes, want) { t.Errorf("consumes = %v, want %v", m.Consumes, want) } if want := []string{"operator-channel"}; !reflect.DeepEqual(sorted(m.Uses), want) { t.Errorf("uses = %v, want %v", m.Uses, want) } if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" { t.Errorf("own-secrets = %v, want exactly broker", m.OwnSecrets) } if m.SecretsOwner != "mesh-delivery-teller" { t.Errorf("secrets-owner = %q", m.SecretsOwner) } // 774: teller_end_ask, served by its tools bundle through the state ends; the process serves none. if want := []string{"teller_end_ask", "teller_answers", "teller_read_answers", "teller_first_views", "teller_first_view_again", "teller_widens_said"}; !reflect.DeepEqual(m.Tools, want) { t.Errorf("tools = %v, want %v", m.Tools, want) } // Its two events: each answer it writes (ADR 0596 §2), and an assigner ask ended at the channel without an answer, heard by // the assigner (issue 1966, tellerevents). if want := []string{"answered", tellerevents.AskEnded, "views.answers"}; !reflect.DeepEqual(m.Emits, want) { t.Errorf("emits = %v, want %v", m.Emits, want) } // design 47 G §383, §387: the stream -findings, 7 days and 16 MiB; §368: version is MAJOR.MINOR. if len(m.Streams) != 0 { t.Errorf("streams = %+v: the finding stream is a require now", m.Streams) } if want := findingRequires(1); !reflect.DeepEqual(m.Requires, want) { t.Errorf("requires = %+v, want %+v", m.Requires, want) } if m.Version != "0.1" { t.Errorf("version = %q, want 0.1", m.Version) } // No bus or seat (ADR 0437 §4). for _, k := range []string{"logs", "claims", "provides", "defines-seats", "seat", "seats", "bus"} { if _, ok := top[k]; ok { t.Errorf("the manifest declares %q: the teller has none", k) } } } func TestManifestProcess(t *testing.T) { // 1240/AC1 m, _ := load(t) var procs []map[string]any for _, r := range m.Resources { if r["type"] == "process" { procs = append(procs, r) } if r["type"] == "tool" || r["type"] == "seat" { t.Errorf("resource %v: the teller serves no tool and holds no seat", r) } } if len(procs) != 1 { t.Fatalf("%d process resources, want one", len(procs)) } p := procs[0] if p["artifact"] != "program" || !reflect.DeepEqual(p["run"], []any{"./mesh-delivery-teller"}) { t.Errorf("process = %v", p) } env, _ := p["env"].(map[string]any) if len(env) != 1 || env["MESH_BROKER_FILE"] != "${dir:state}/broker" { t.Errorf("env = %v", p["env"]) } if _, bus := p["bus"]; bus { t.Errorf("the process declares a bus: it runs on its own broker credential") } if len(m.Build.Artifacts) != 2 { t.Fatalf("artifacts = %+v, want the program and the tools", m.Build.Artifacts) } a := m.Build.Artifacts[0] if st, err := os.Stat(a.From); err != nil || !st.IsDir() || a.Binary != "mesh-delivery-teller" || !strings.HasSuffix(a.From, a.Binary) { t.Errorf("artifact = %+v", a) } if a := m.Build.Artifacts[1]; a.Name != "tools" || a.Kind != "bundle" || a.Binary != "mesh-delivery-teller-tools" || !reflect.DeepEqual(a.Loads, []string{"mesh-delivery-teller-tools"}) { t.Errorf("tools artifact = %+v", a) // 774 } else if st, err := os.Stat(a.From); err != nil || !st.IsDir() { t.Errorf("the tools' source %s: %v", a.From, err) } } func TestServesNoRequestSubjectAndHoldsNoTicker(t *testing.T) { // 1240/AC3 (ADR 0437 §8) forbidden := []string{"Subscribe(", "QueueSubscribe", "ChanSubscribe", "Respond(", "RespondMsg(", "time.NewTicker", "time.Tick", "time.NewTimer", "time.AfterFunc", "time.After(", "time.Sleep"} // The teller hears its own `decided` event through its durable consumer, pulled (decided.go): it holds no subscription. var files int for _, root := range []string{"cmd", "internal"} { err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { if os.IsNotExist(err) { return fs.SkipDir } if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { return err } files++ src, err := os.ReadFile(path) if err != nil { return err } for _, f := range forbidden { if strings.Contains(string(src), f) { t.Errorf("%s holds %s: the teller serves no request subject and holds no timer", path, f) } } return nil }) if err != nil { t.Fatal(err) } } if files < 1 { t.Fatalf("read %d source files, want the program's", files) } } // Issue 1242: no delivery part hand-writes its bus connection. A part's own credential, its pinned dial, the subjects of its // streams and its durable consumer come from mesh-sdk's process package (ReadBrokerAccount, Account.Dial, Account.EventConsumer, // ModuleStreamSubject); a part whose main.go dials with nats.Connect, pins with its own certificate check or binds the // consumer name itself is refused here. mesh-grants is outside it until its own seats change (issue 1945) lands. func TestNoPartHandWritesItsBusConnection(t *testing.T) { mains, err := filepath.Glob("../mesh-delivery-*/cmd/*/*.go") if err != nil || len(mains) == 0 { t.Fatalf("no delivery part found: %v", err) } for _, f := range mains { if strings.HasSuffix(f, "_test.go") { continue } raw, err := os.ReadFile(f) if err != nil { t.Fatal(err) } for _, banned := range []string{"nats.Connect(", "VerifyPeerCertificate", `cred.Node+"_"+cred.Module`, `"MESH_BROKER_FILE"`} { if bytes.Contains(raw, []byte(banned)) { t.Errorf("%s hand-writes the bus connection (%s): use mesh-sdk's process.ReadBrokerAccount, Account.Dial, Account.EventConsumer or ModuleStreamSubject", f, banned) } } } } // require is one entry of the manifest's requires: a provision the module asks for by a template (ADR 0595), which the // controller's reader takes. type require struct { Name string `json:"name"` Storage string `json:"storage"` Template string `json:"template"` Options map[string]any `json:"options"` } // findingRequires is the findings state and the finding stream a module asks for by their templates (design 47 G // §383, §387; A-30): 7 days and 16 MiB, the findings state keeping the given history. func findingRequires(history float64) []require { return []require{ {Name: "findings", Storage: "key-value", Template: "state", Options: map[string]any{"history": history}}, {Name: "finding-events", Storage: "stream", Template: "events", Options: map[string]any{ "events": []any{"finding-written.*", "finding-cleared.*", "findings-stated.*"}, "max-age-days": float64(7), "max-mib": float64(16)}}, } }