package broker import ( "reflect" "testing" ) // The mesh issues an assignment's subjects (novox/hq ADR 0160): a module alone on one machine // answers for the module and for its machine; a stateful module on two machines answers only for // each machine; one that says its instances are interchangeable answers for the module everywhere; // a holder serves its seat's verbs; and what a module may reach is resolved the same way. func TestAMembershipIsIssuedFromWhereEverythingRuns(t *testing.T) { records := Records{Assigned: map[string][]Declared{ "anchor": { {Module: "postgres", Serves: []string{"query"}, Holds: []Seat{{Name: "mesh-store", Scope: "mesh", Serves: []string{"databases", "query"}}}}, {Module: "catalog", Invokes: []string{"postgres.query", "search.find"}}, }, "home-server": { {Module: "postgres"}, {Module: "search"}, {Module: "dashboard", Invokes: []string{"postgres.query"}}, }, "laptop": {{Module: "search"}}, }, Interchangeable: map[string]bool{"search": true}} where := PlacementsOf(records, records.Interchangeable) pg := MembershipFor("anchor", records.Assigned["anchor"][0], where) if !reflect.DeepEqual(pg.Serves, []Served{{Subject: "mesh.mod.postgres.tool.{tool}.anchor"}}) { t.Fatalf("a stateful module on two machines answers only for its machine: %+v", pg.Serves) } if len(pg.Seats) != 2 || pg.Seats[0].Subject != "mesh.seat.mesh-store.tool.databases" { t.Fatalf("the holder serves the seat's verbs at the seat's subjects: %+v", pg.Seats) } if pg.Emits != "mesh.mod.postgres.event.{event}" || pg.Tools != "mesh.mod.postgres.tool.tools" { t.Fatalf("events and the tools verb: %+v", pg) } search := MembershipFor("laptop", records.Assigned["laptop"][0], where) if !reflect.DeepEqual(search.Serves, []Served{ {Subject: "mesh.mod.search.tool.{tool}.laptop"}, {Subject: "mesh.mod.search.tool.{tool}", Queue: "serve.search"}, }) { t.Fatalf("an interchangeable module answers for the module in the queue too: %+v", search.Serves) } dashboard := MembershipFor("home-server", records.Assigned["home-server"][2], where) if !reflect.DeepEqual(dashboard.Serves, []Served{ {Subject: "mesh.mod.dashboard.tool.{tool}.home-server"}, {Subject: "mesh.mod.dashboard.tool.{tool}", Queue: "serve.dashboard"}, }) { t.Fatalf("a module alone on one machine answers for the module: %+v", dashboard.Serves) } if !reflect.DeepEqual(dashboard.Reaches["postgres.query"], []string{"mesh.mod.postgres.tool.query.anchor", "mesh.mod.postgres.tool.query.home-server"}) { t.Fatalf("reaching a stateful module names each machine and no plain subject: %v", dashboard.Reaches) } catalog := MembershipFor("anchor", records.Assigned["anchor"][1], where) if !reflect.DeepEqual(catalog.Reaches["search.find"], []string{"mesh.mod.search.tool.find", "mesh.mod.search.tool.find.home-server", "mesh.mod.search.tool.find.laptop"}) { t.Fatalf("reaching an interchangeable module offers the plain subject first: %v", catalog.Reaches) } if MembershipSubject("anchor", "postgres") != "mesh.assignment.anchor.postgres" { t.Fatal("the one subject a runtime derives for itself") } } func TestAnAccountMayReadItsOwnMembershipAndNoOthers(t *testing.T) { perms, err := PermissionsFor(Principal{Kind: KindModule, Node: "anchor", Module: "postgres", PasswordHash: "x"}) if err != nil { t.Fatal(err) } has(t, perms.Subscribe, "mesh.assignment.anchor.postgres") has(t, perms.Publish, "$JS.API.DIRECT.GET.ASSIGNMENTS.mesh.assignment.anchor.postgres") hasNot(t, perms.Subscribe, "mesh.assignment.>") }