Files
mesh-delivery/modules/mesh-grants/internal/grants/processbus_test.go
T
jschoubben 7044bf5024 A-340 process-bus-consumes: a process hears only what its module consumes, and is granted exactly that
The judge refuses a process's bus consumes its module does not consume (beside V5). The grants part gives such a process the subjects of those events, its own durable <node>_<module>_<process> on EVENTS and on each kept seat event's stream, and their consumer grants, and makes those consumers, as G1 gives the publish.

Builds: design 47 G A-340, A-339; design 42 zfs-owns-zfs V5, G1

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 09:16:25 +01:00

99 lines
4.6 KiB
Go

package grants
import (
"encoding/json"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"mesh-grants/internal/buscompose"
)
// Design 42 zfs-owns-zfs, A-38 (G1): a process whose bus names an event of the events stream its module asks for in
// `requires` is granted publish on that stream's subjects, and one naming a state asked for there the reads and writes of
// its bucket; a name in the module's own `emits` is granted as before, and what its bus does not name is granted nothing.
func TestAProcessBusNamesWhatItsModuleAsksFor(t *testing.T) {
var m catalogue.Manifest
raw := `{"module":"zfs","version":"1","emits":["scrub.requested"],"requires":[` +
`{"name":"events","storage":"stream","template":"events","options":{"events":["config.applied","config.refused"]}},` +
`{"name":"applied","storage":"key-value","template":"state"},` +
`{"name":"findings","storage":"key-value","template":"state"}],"resources":[` +
`{"id":"zfs-keeper","type":"process","run":["./zfs"],"user":"zfs-keeper","bus":` +
`{"emits":["config.applied","scrub.requested"],"state":["applied"]}}]}`
if err := json.Unmarshal([]byte(raw), &m); err != nil {
t.Fatal(err)
}
users, err := buscompose.Users(buscompose.Records{Nodes: []string{"ace"}, Assigned: map[string][]buscompose.Declared{
"ace": {{Module: "zfs", Emits: m.EmitsAll(), Processes: processesOf(m)}}}})
if err != nil {
t.Fatal(err)
}
var perms buscompose.Permissions
for _, u := range users {
if u.Kind == buscompose.KindProcess {
if perms, err = buscompose.PermissionsFor(u); err != nil {
t.Fatal(err)
}
}
}
for subject, want := range map[string]bool{
"mesh.bus.zfs.events.config.applied": true,
"mesh.mod.zfs.event.config.applied": false,
"mesh.mod.zfs.event.scrub.requested": true,
"$KV.zfs_applied.pool.tank": true,
"$KV.zfs_findings.pool.tank": false,
"mesh.bus.zfs.findings.config.applied": false,
} {
if got := buscompose.MayPublish(perms, subject); got != want {
t.Errorf("publish on %s: %v, want %v", subject, got, want)
}
}
}
// Design 47 G A-340: a process whose bus consumes an event its module consumes hears exactly that event: a kept seat event
// on its own durable on the event's stream, granted its subject and its consumer's subjects, and nothing it does not name.
func TestAProcessBusHearsWhatItsModuleConsumes(t *testing.T) {
var m catalogue.Manifest
raw := `{"module":"zfs","version":"1","consumes":["mesh-conditions.findings-asked"],"resources":[` +
`{"id":"keeper","type":"process","run":["./zfs"],"user":"zfs-keeper","bus":{"consumes":["mesh-conditions.findings-asked"]}},` +
`{"id":"mute","type":"process","run":["./zfs"],"user":"zfs-mute","bus":{"state":["applied"]}}]}`
if err := json.Unmarshal([]byte(raw), &m); err != nil {
t.Fatal(err)
}
const stream, subject = "SEAT_mesh-conditions_findings-asked", "mesh.seat.mesh-conditions.event.findings-asked"
users, err := buscompose.Users(buscompose.Records{Nodes: []string{"ace"},
Kept: []buscompose.KeptEvent{{Seat: "mesh-conditions", Event: "findings-asked", Stream: stream, Filter: subject}},
Assigned: map[string][]buscompose.Declared{"ace": {{Module: "zfs", Consumes: m.Consumes, Processes: processesOf(m)}}}})
if err != nil {
t.Fatal(err)
}
perms := map[string]buscompose.Permissions{}
for _, u := range users {
if u.Kind == buscompose.KindProcess {
if perms[u.Process], err = buscompose.PermissionsFor(u); err != nil {
t.Fatal(err)
}
}
}
if !buscompose.MaySubscribe(perms["keeper"], subject) || buscompose.MaySubscribe(perms["mute"], subject) {
t.Errorf("the hear of %s: keeper %v, mute %v; want the keeper alone", subject,
buscompose.MaySubscribe(perms["keeper"], subject), buscompose.MaySubscribe(perms["mute"], subject))
}
durable := "ace_zfs_keeper"
for _, s := range []string{"$JS.API.CONSUMER.MSG.NEXT." + stream + "." + durable, "$JS.ACK." + stream + "." + durable + ".1"} {
if !buscompose.MayPublish(perms["keeper"], s) || buscompose.MayPublish(perms["mute"], s) {
t.Errorf("publish on %s: keeper %v, mute %v; want the keeper alone", s,
buscompose.MayPublish(perms["keeper"], s), buscompose.MayPublish(perms["mute"], s))
}
}
var made []string
for _, c := range buscompose.AccountConsumers(users) {
if c.Module == "zfs" && strings.HasPrefix(c.Consumer.Name, "ace_zfs_") { // the processes' own, not the module's
made = append(made, c.Consumer.Stream+"/"+c.Consumer.Name+"/"+strings.Join(c.Consumer.Filters, ","))
}
}
if want := stream + "/" + durable + "/" + subject; len(made) != 1 || made[0] != want {
t.Errorf("the consumers made for zfs's processes are %v, want %s alone", made, want)
}
}