package catalogue import ( "fmt" "strings" "testing" ) func passSeat(t *testing.T) { t.Helper() was := Seats() t.Cleanup(func() { UseSeats(was) }) UseSeats([]Seat{ {Name: "mesh-broker", Scope: ScopeMesh, Delivers: "mesh-bus", Decision: "test"}, {Name: "mesh-dns-resolver", Scope: ScopeMesh, Replicated: true, Decision: "test"}, }) } func broker(module string) Manifest { return Manifest{Module: module, Provides: []Offer{{Name: "mesh-bus", Scope: ScopeMesh}}, Claims: []Claim{{Name: "mesh-broker", Scope: ScopeMesh}}} } // A module that still runs but no longer claims the seat. func dropped(module string) Manifest { return Manifest{Module: module} } func recorded(node, module string) []Held { return []Held{{Claim: "mesh-broker", Scope: ScopeMesh, Node: node, Module: module}} } // asg is a case's assignment in the earlier form: a node with the manifest of the build it runs, which the harness // hears as a fingerprint and serves through a reader of entries; Unread is an entry the reader does not answer. type asg struct { Node string Manifest Manifest Unread bool // Silent is a node that said no `running`: the module's entry on its branch is taken. Stopped says it stopped. Silent, Stopped bool } type entriesFixture struct{ byFP, byBranch map[string]Manifest } func (e entriesFixture) ByFingerprint(fp string) (Manifest, bool) { m, ok := e.byFP[fp]; return m, ok } func (e entriesFixture) OnBranch(module, branch string) (Manifest, bool) { m, ok := e.byBranch[module+"@"+branch] return m, ok } func passOn(rec []Held, as []asg) ([]SeatPass, []string) { e := entriesFixture{map[string]Manifest{}, map[string]Manifest{}} var assignments []Assignment heard := map[string]Heard{} for i, a := range as { fp := fmt.Sprintf("fp%d", i) assignments = append(assignments, Assignment{Node: a.Node, Module: a.Manifest.Module, Branch: "trunk"}) switch { case a.Stopped: heard[a.Node+"/"+a.Manifest.Module] = Heard{Stopped: true} case a.Silent: if !a.Unread { e.byBranch[a.Manifest.Module+"@trunk"] = a.Manifest } default: heard[a.Node+"/"+a.Manifest.Module] = Heard{Fingerprint: fp} if !a.Unread { e.byFP[fp] = a.Manifest } } } return SeatsToPassOn(rec, assignments, func(node, module string) (Heard, bool) { h, ok := heard[node+"/"+module] return h, ok }, e) } // novox/hq ADR 0660 "How it is checked", and ADR 0449 §4's original case. func TestSeatsToPassOn(t *testing.T) { passSeat(t) rec := recorded("novox", "old") cases := []struct { name string rec []Held as []asg to string // "node/module" of the one pass, "" for none saidW []string }{ {"holder assigned nowhere, one claimant (ADR 0449 §4)", rec, []asg{{Node: "novox", Manifest: broker("new")}}, "novox/new", nil}, {"holder assigned but its manifest dropped the claim", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "novox", Manifest: broker("new")}}, "novox/new", nil}, {"holder claims but cannot hold the seat", rec, []asg{{Node: "novox", Manifest: Manifest{Module: "old", Claims: []Claim{{Name: "mesh-broker", Scope: ScopeMesh}}}}, {Node: "ace", Manifest: broker("new")}}, "ace/new", nil}, {"the running build still claims: the holder keeps the seat", rec, []asg{{Node: "novox", Manifest: broker("old")}, {Node: "novox", Manifest: broker("new")}}, "", nil}, {"one of the holder's two nodes still claims it", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "ace", Manifest: broker("old")}, {Node: "g14", Manifest: broker("new")}}, "", nil}, {"holder's manifest unread: still assigned", rec, []asg{{Node: "novox", Manifest: Manifest{Module: "old"}, Unread: true}, {Node: "novox", Manifest: broker("new")}}, "", []string{"no entry is answered for old on novox"}}, {"an unread assignment is no claimant", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "ace", Manifest: Manifest{Module: "new"}, Unread: true}}, "", []string{"no entry is answered for new on ace"}}, {"a claimant whose running manifest does not claim is not counted", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "ace", Manifest: dropped("new")}}, "", nil}, {"no claimant keeps the record", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "novox", Manifest: dropped("other")}}, "", nil}, {"holder gone, no claimant keeps the record", rec, nil, "", nil}, {"one module on two nodes is two claimants: none passed, said", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "ace", Manifest: broker("new")}, {Node: "novox", Manifest: broker("new")}}, "", []string{"old", "new on ace", "new on novox", "seat mesh-broker --to /"}}, {"two claimants, holder gone: said", rec, []asg{{Node: "novox", Manifest: broker("a")}, {Node: "ace", Manifest: broker("b")}}, "", []string{"a on novox", "b on ace", "seat mesh-broker --to /"}}, {"a seat with two holders on record is not read", append(rec[:1:1], Held{Claim: "mesh-broker", Scope: ScopeMesh, Node: "ace", Module: "x"}), []asg{{Node: "novox", Manifest: broker("new")}}, "", nil}, // Design 47 G §459, §199, §404. {"nothing heard: the module's entry on its branch is taken", rec, []asg{{Node: "novox", Manifest: dropped("old"), Silent: true}, {Node: "ace", Manifest: broker("new"), Silent: true}}, "ace/new", nil}, {"nothing heard of the holder: its entry on the branch still claims, so it keeps the seat", rec, []asg{{Node: "novox", Manifest: broker("old"), Silent: true}, {Node: "ace", Manifest: broker("new")}}, "", nil}, {"a stopped holder passes no seat to itself: the one claimant takes it", rec, []asg{{Node: "novox", Manifest: broker("old"), Stopped: true}, {Node: "ace", Manifest: broker("new")}}, "ace/new", nil}, {"a stopped module claims nothing", rec, []asg{{Node: "novox", Manifest: dropped("old")}, {Node: "ace", Manifest: broker("new"), Stopped: true}}, "", nil}, {"an entry not answered keeps the holder on record and says it", rec, []asg{{Node: "novox", Manifest: dropped("old"), Unread: true}, {Node: "ace", Manifest: broker("new")}}, "", []string{"no entry is answered for old on novox (fp0)"}}, {"a replicated seat is never passed", []Held{{Claim: "mesh-dns-resolver", Scope: ScopeMesh, Node: "novox", Module: "gone"}}, []asg{{Node: "novox", Manifest: Manifest{Module: "r", Claims: []Claim{{Name: "mesh-dns-resolver", Scope: ScopeMesh}}}}}, "", nil}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { passes, said := passOn(c.rec, c.as) if c.to == "" && len(passes) != 0 || c.to != "" && len(passes) != 1 { t.Fatalf("passes=%+v said=%v", passes, said) } if c.to != "" { p := passes[0] if p.Seat.Name != "mesh-broker" || p.From.Module != "old" || p.To.Node+"/"+p.To.Module != c.to { t.Fatalf("the pass is %+v", p) } } if (len(c.saidW) == 0) != (len(said) == 0) { t.Fatalf("said=%v, want %v", said, c.saidW) } for _, w := range c.saidW { if !strings.Contains(said[0], w) { t.Fatalf("the answer does not say %q: %s", w, said[0]) } } }) } }