Files
mesh-host/internal/compose/rest_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

316 lines
16 KiB
Go

package compose
import (
"strings"
"testing"
"git.novox.be/novox/mesh-networking/modules/mesh-wireguard/go/overlay"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// objects and files are the provider and consumer of mesh-controller's facts_test.go aMeshWithSecrets (cmd/mesh-controller at
// 6c2a8d62): a module providing s3-bucket at mesh scope with an identity bound, and one requiring it.
func objects() catalogue.Manifest {
return catalogue.Manifest{Module: "objects", Version: "1",
Provides: []catalogue.Offer{{Name: "s3-bucket", Scope: catalogue.ScopeMesh,
Identity: &catalogue.OfferIdentity{Max: 20, In: "an S3 access key"}}},
Receives: map[string]string{"s3-bucket": "/var/lib/mesh/objects/mesh.json"}}
}
func files() catalogue.Manifest {
return catalogue.Manifest{Module: "files", Version: "1", Requires: []string{"s3-bucket"},
Resources: []map[string]any{{"id": "config", "type": "file", "path": "/etc/files/config.json",
"mode": "0600", "content": "{}", "merge": "json"}}}
}
// threeNodes is aMesh and a third node `ace` running a database, so the mesh has three nodes that each offer something:
// anchor objects, ace a database, laptop a consumer of the first.
func threeNodes(t *testing.T) Input {
t.Helper()
in := aMesh(t)
db := catalogue.Manifest{Module: "pg", Version: "1", Provides: catalogue.FromAnywhere("postgres-database")}
in.Catalogue["objects"], in.Catalogue["files"], in.Catalogue["pg"] = objects(), files(), db
anchor, laptop := nodeOf(t, &in, "anchor"), nodeOf(t, &in, "laptop")
anchor.Assigned = append(anchor.Assigned, "objects")
laptop.Assigned = append(laptop.Assigned, "files")
in.Nodes = append(in.Nodes, Node{Name: "ace", Place: Place{Site: "there", Address: "10.77.0.3"},
Assigned: []string{"pg"}, Capabilities: map[string]bool{}})
return in
}
func providers(w catalogue.World, provision string) []string {
var out []string
for _, p := range w.Offered[provision] {
out = append(out, p.Node+"/"+p.Module)
}
return out
}
// 1361/AC1: three nodes each offering a provision; the rest of the mesh, for one of them, offers every other node's and none of its
// own, and holds what they hold.
func testTheRestOfTheMeshOffersEveryOtherNodesProvisionsAndNoneOfTheExcluded(t *testing.T) {
in := threeNodes(t)
w, passes, left, err := TheRestOfTheMesh(in, "laptop")
if err != nil || len(left) != 0 || len(passes.Passes) != 0 {
t.Fatalf("%v %v %+v", err, left, passes)
}
if got := providers(w, "s3-bucket"); len(got) != 1 || got[0] != "anchor/objects" {
t.Errorf("s3-bucket is offered by %v", got)
}
if got := providers(w, "postgres-database"); len(got) != 1 || got[0] != "ace/pg" {
t.Errorf("postgres-database is offered by %v", got)
}
if p := w.Offered["s3-bucket"][0]; p.At != "anchor.internal" {
t.Errorf("anchor is at %q on the private network", p.At)
}
// Excluding anchor takes its offer away and keeps the others'.
w, _, _, err = TheRestOfTheMesh(in, "anchor")
if err != nil {
t.Fatal(err)
}
if got := providers(w, "s3-bucket"); len(got) != 0 {
t.Errorf("the excluded node's offer is in the world: %v", got)
}
if got := providers(w, "postgres-database"); len(got) != 1 {
t.Errorf("another node's offer is missing: %v", got)
}
// With nobody excluded, every offer is there, and everyone's claims are held.
w, _, _, err = TheRestOfTheMesh(in, "")
if err != nil || len(providers(w, "s3-bucket")) != 1 || len(providers(w, "postgres-database")) != 1 {
t.Fatalf("%v %v", providers(w, "s3-bucket"), err)
}
// Each machine is resolved with its own pins, as its plan is: the world is the same for any pin of the excluded node.
nodeOf(t, &in, "laptop").Pins = map[string]catalogue.Chosen{"s3-bucket": {Node: "anchor", Module: "objects"}}
if _, _, _, err := TheRestOfTheMesh(in, "laptop"); err != nil {
t.Fatal(err)
}
}
// 1361/AC2: a node whose assigned set does not resolve is left out and named with today's reason, and the others still offer.
func testANodeWhoseSetDoesNotResolveIsLeftOutAndTheOthersStillOffer(t *testing.T) {
in := threeNodes(t)
rivalOne := catalogue.Manifest{Module: "rival-one", Version: "1", Resources: []map[string]any{{"id": "r", "type": "directory", "path": "/var/lib/the-same", "mode": "0700"}}}
rivalTwo := catalogue.Manifest{Module: "rival-two", Version: "1", Resources: []map[string]any{{"id": "r", "type": "directory", "path": "/var/lib/the-same", "mode": "0700"}}}
in.Catalogue["rival-one"], in.Catalogue["rival-two"] = rivalOne, rivalTwo
ace := nodeOf(t, &in, "ace")
ace.Assigned = append(ace.Assigned, "rival-one", "rival-two")
w, _, left, err := TheRestOfTheMesh(in, "laptop")
if err != nil {
t.Fatal(err)
}
if len(left) == 0 {
t.Fatal("a node whose set does not resolve was not left out")
}
for _, l := range left {
if l.Node != "ace" || !strings.Contains(l.String(), "ace is left out of the rest of the mesh: it does not resolve: ") {
t.Errorf("left out %q", l.String())
}
}
if got := providers(w, "s3-bucket"); len(got) != 1 {
t.Errorf("the others do not offer: %v", got)
}
if got := providers(w, "postgres-database"); len(got) != 0 {
t.Errorf("the node left out offers: %v", got)
}
// Said for the whole-mesh view in both passes (as the controller says it), and for a view with one machine excluded in the first only.
_, _, whole, _ := TheRestOfTheMesh(in, "")
if len(whole) != 2 {
t.Errorf("the whole-mesh view says %d line(s), want the two passes'", len(whole))
}
if len(left) != 1 {
t.Errorf("a view with one excluded says %d line(s), want the first pass's", len(left))
}
}
// 1361/AC3 and AC4 (mesh-controller seat_pass_test.go TestACompositionPassesTheSeatAndWritesOneHandActByTheController, in
// the part that is not a write): a seat held once whose holder on record is assigned nowhere and one claimant: the seat
// passes to that claimant in the World, and is returned as a pass; Input's holdings are unchanged.
func testASeatWhoseHolderIsAssignedNowhereGoesToItsOnlyClaimant(t *testing.T) {
withSeat(t)
mk := func() Input {
return Input{
Catalogue: map[string]catalogue.Manifest{"gate": aBrokerClaiming("gate")},
Nodes: []Node{{Name: "novox", Place: Place{Site: "here"}, Assigned: []string{"gate"}}},
Holdings: []catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "novox", Module: "old-broker", Site: "here"}},
}
}
in := mk()
w, passes, _, err := TheRestOfTheMesh(in, "")
if err != nil {
t.Fatal(err)
}
if len(w.Holdings) != 1 || w.Holdings[0].Module != "gate" || w.Holdings[0].Node != "novox" {
t.Fatalf("the holdings after the composition do not name the gate: %+v", w.Holdings)
}
if len(passes.Passes) != 1 {
t.Fatalf("passes=%d, want one", len(passes.Passes))
}
p := passes.Passes[0]
if p.Seat.Name != "mesh-broker" || p.From.Module != "old-broker" || p.To.Module != "gate" || p.To.Node != "novox" {
t.Errorf("the pass is %+v", p)
}
unchanged(t, mk, func(in Input) {
if _, _, _, err := TheRestOfTheMesh(in, ""); err != nil {
t.Fatal(err)
}
})
}
// 1317/AC2 (seat_pass_test.go TestACompositionWithTwoClaimantsRecordsNoneAndSaysSo): two claimants, no pass, the refusal said with
// both and the remedy. 1317/AC3 and AC4 (TestACompositionChangesNothingWhileTheHolderIsAssignedOrNobodyClaims): nothing changes while
// the holder is assigned, nor with no claimant.
func testNoSeatIsPassedForTwoClaimantsAnAssignedHolderOrNoClaimant(t *testing.T) {
withSeat(t)
held := []catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "novox", Module: "old-broker"}}
shelf := map[string]catalogue.Manifest{"gate": aBrokerClaiming("gate"), "other": aBrokerClaiming("other"), "old-broker": aBrokerClaiming("old-broker")}
_, passes := passSeatsOn(held, []catalogue.Assignment{{Node: "novox", Module: "gate"}, {Node: "ace", Module: "other"}}, nil, entryReader{shelf: shelf})
said := strings.Join(passes.Said, "\n")
if len(passes.Passes) != 0 || !strings.Contains(said, "gate") || !strings.Contains(said, "other") ||
!strings.Contains(said, "seat mesh-broker --to") {
t.Fatalf("passes %+v said %q", passes.Passes, said)
}
got, passes := passSeatsOn(held, []catalogue.Assignment{{Node: "novox", Module: "old-broker"}, {Node: "novox", Module: "gate"}}, nil, entryReader{shelf: shelf})
if len(passes.Passes) != 0 || len(got) != 1 || got[0].Module != "old-broker" {
t.Fatalf("holder assigned: %+v %+v", got, passes)
}
got, passes = passSeatsOn(held, nil, nil, entryReader{shelf: shelf})
if len(passes.Passes) != 0 || len(got) != 1 || got[0].Module != "old-broker" {
t.Fatalf("no claimant: %+v %+v", got, passes)
}
}
// seat_pass_test.go TestAHolderAssignedButNotOnTheShelfIsNotTakenForGone.
func testAHolderAssignedButNotOnTheShelfIsNotTakenForGone(t *testing.T) {
withSeat(t)
as := (&composer{}).assignmentsOf(map[string][]string{"novox": {"gate", "old-broker"}})
passes, _ := catalogue.SeatsToPassOn(
[]catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "novox", Module: "old-broker"}}, as, nil,
entryReader{shelf: map[string]catalogue.Manifest{"gate": aBrokerClaiming("gate")}})
if len(passes) != 0 {
t.Fatalf("a holder missing from the shelf lost its seat: %+v", passes)
}
// Design 47 G §199 (test 56): the build a node runs is the one it said in its running event, the trunk's entry counting
// only for a node that said nothing. The trunk's old-broker still claims the seat; the one the away node runs does not.
for name, c := range map[string]struct {
heard Heard
passTo string
}{
"the away node runs a build that no longer claims the seat": {Heard{Fingerprint: "f1", Manifest: catalogue.Manifest{Module: "old-broker"}, Read: true}, "gate"},
"the away node says it stopped it": {Heard{Stopped: true}, "gate"},
"the away node's build cannot be read": {Heard{Fingerprint: "f1"}, ""},
"the away node runs a build that claims the seat": {Heard{Fingerprint: "f1", Manifest: aBrokerClaiming("old-broker"), Read: true}, ""},
} {
in := Input{
Catalogue: map[string]catalogue.Manifest{"old-broker": aBrokerClaiming("old-broker"), "gate": aBrokerClaiming("gate")},
Nodes: []Node{{Name: "away", Assigned: []string{"old-broker"}, Heard: map[string]Heard{"old-broker": c.heard}},
{Name: "novox", Assigned: []string{"gate"}}},
Holdings: []catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "away", Module: "old-broker"}},
}
_, got, _, err := TheRestOfTheMesh(in, "")
if err != nil || (c.passTo == "") != (len(got.Passes) == 0) || (c.passTo != "" && got.Passes[0].To.Module != c.passTo) {
t.Errorf("%s: %v %+v", name, err, got)
}
}
}
// The not-resolving node of a view with a holder on record: holdings reach the first pass (novox/hq ADR 0131), so a node running
// the next holder beside the current one is not refused as a second holder.
func testAHolderOnRecordSettlesTheSeatInTheRestOfTheMesh(t *testing.T) {
withSeat(t)
in := Input{
Catalogue: map[string]catalogue.Manifest{"old-broker": aBrokerClaiming("old-broker"), "gate": aBrokerClaiming("gate")},
Nodes: []Node{
{Name: "novox", Assigned: []string{"old-broker", "gate"}},
{Name: "ace", Assigned: []string{}}},
Holdings: []catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "novox", Module: "old-broker"}},
}
_, passes, left, err := TheRestOfTheMesh(in, "ace")
if err != nil || len(left) != 0 || len(passes.Passes) != 0 {
t.Fatalf("%v %v %+v", err, left, passes)
}
in.Holdings = nil
// Nothing on record: the second claimant is held, not the node left out, and holds nothing in the view (issue 1966).
w, _, left, err := TheRestOfTheMesh(in, "ace")
if err != nil || len(left) != 0 || len(w.Held) != 1 || w.Held[0].Module != "old-broker" {
t.Fatalf("two claimants with nothing on record: %v %v %+v", err, left, w.Held)
}
}
// Each machine is resolved in the second pass with its own pins, as its plan is (novox/hq issue 188): a pin to a provider that
// does not answer is its own refusal, so the machine is left out for it; the same machine without the pin is not.
func testEachNodeIsResolvedWithItsOwnPinsInTheRestOfTheMesh(t *testing.T) {
in := threeNodes(t)
in.Nodes = append(in.Nodes, Node{Name: "newt", Place: Place{Site: "there", Address: "10.77.0.4"}, Assigned: []string{overlay.Name, "files"}, Capabilities: map[string]bool{}})
_, _, left, err := TheRestOfTheMesh(in, "")
if err != nil || len(left) != 0 {
t.Fatalf("without a pin the consumer is left out: %v %v", left, err)
}
nodeOf(t, &in, "newt").Pins = map[string]catalogue.Chosen{"s3-bucket": {Node: "ace", Module: "nothing"}}
_, _, left, err = TheRestOfTheMesh(in, "")
if err != nil || len(left) != 1 || left[0].Node != "newt" || !strings.Contains(left[0].String(), "was told to get \"s3-bucket\" from") {
t.Fatalf("a pin to a provider that does not answer did not leave the consumer out: %v %v", left, err)
}
}
// ADR 0660: a holder on record that is still assigned, whose manifest no longer claims the seat, has left it; the seat is
// composed with the one claimant, the pass is returned and said, and Input's holdings (the record) are not written.
func testAHolderWhoseManifestDroppedTheSeatGoesToItsOnlyClaimant(t *testing.T) {
withSeat(t)
mk := func(claimants ...string) Input {
cat := map[string]catalogue.Manifest{"old-broker": {Module: "old-broker"}}
assigned := []string{"old-broker"}
for _, c := range claimants {
cat[c] = aBrokerClaiming(c)
assigned = append(assigned, c)
}
return Input{Catalogue: cat,
Nodes: []Node{{Name: "novox", Place: Place{Site: "here"}, Assigned: assigned}},
Holdings: []catalogue.Held{{Claim: "mesh-broker", Scope: catalogue.ScopeMesh, Node: "novox", Module: "old-broker", Site: "here"}}}
}
in := mk("gate")
w, passes, _, err := TheRestOfTheMesh(in, "")
if err != nil || len(passes.Passes) != 1 || passes.Passes[0].To.Module != "gate" || passes.Passes[0].From.Module != "old-broker" {
t.Fatalf("%v %+v", err, passes)
}
if len(w.Holdings) != 1 || w.Holdings[0].Module != "gate" {
t.Fatalf("the composition does not hold the pass: %+v", w.Holdings)
}
unchanged(t, func() Input { return mk("gate") }, func(in Input) {
if _, _, _, err := TheRestOfTheMesh(in, ""); err != nil {
t.Fatal(err)
}
})
_, passes, _, err = TheRestOfTheMesh(mk("gate", "other"), "")
said := strings.Join(passes.Said, "\n")
if err != nil || len(passes.Passes) != 0 || !strings.Contains(said, "gate") || !strings.Contains(said, "other") ||
!strings.Contains(said, "seat mesh-broker --to <node>/<module>") {
t.Fatalf("two claimants: %v %+v", err, passes)
}
// The build the holder's node runs still claims the seat, though the trunk dropped it: the holder keeps it (ADR 0660 §2).
running := mk("gate")
running.Nodes[0].Heard = map[string]Heard{"old-broker": {Fingerprint: "f", Manifest: aBrokerClaiming("old-broker"), Read: true}}
_, passes, _, err = TheRestOfTheMesh(running, "")
if err != nil || len(passes.Passes) != 0 || len(passes.Said) != 0 {
t.Fatalf("a holder whose running build claims the seat lost it: %v %+v", err, passes)
}
// And a pass is said in the composition's answer, not recorded.
var d Declared
d, _, err = Compose(mk("gate"), "novox")
said = strings.Join(d.Said, "\n")
if err != nil || !strings.Contains(said, "mesh-broker: old-broker on novox") || !strings.Contains(said, "held by gate on novox, not recorded") {
t.Fatalf("the answer does not say the pass: %v %q", err, said)
}
}
// TestRest runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestRest(t *testing.T) {
t.Run("TheRestOfTheMeshOffersEveryOtherNodesProvisionsAndNoneOfTheExcluded", testTheRestOfTheMeshOffersEveryOtherNodesProvisionsAndNoneOfTheExcluded)
t.Run("ANodeWhoseSetDoesNotResolveIsLeftOutAndTheOthersStillOffer", testANodeWhoseSetDoesNotResolveIsLeftOutAndTheOthersStillOffer)
t.Run("ASeatWhoseHolderIsAssignedNowhereGoesToItsOnlyClaimant", testASeatWhoseHolderIsAssignedNowhereGoesToItsOnlyClaimant)
t.Run("NoSeatIsPassedForTwoClaimantsAnAssignedHolderOrNoClaimant", testNoSeatIsPassedForTwoClaimantsAnAssignedHolderOrNoClaimant)
t.Run("AHolderAssignedButNotOnTheShelfIsNotTakenForGone", testAHolderAssignedButNotOnTheShelfIsNotTakenForGone)
t.Run("AHolderOnRecordSettlesTheSeatInTheRestOfTheMesh", testAHolderOnRecordSettlesTheSeatInTheRestOfTheMesh)
t.Run("EachNodeIsResolvedWithItsOwnPinsInTheRestOfTheMesh", testEachNodeIsResolvedWithItsOwnPinsInTheRestOfTheMesh)
t.Run("AHolderWhoseManifestDroppedTheSeatGoesToItsOnlyClaimant", testAHolderWhoseManifestDroppedTheSeatGoesToItsOnlyClaimant)
}