The tool now reads every watched branch's catalog half through the seat mesh-catalog's verbs registrations and revision, as the pass's world.trunk holds them; the pass table drives p.wants, so it fails without the clean state's call; the manifest test names assigner_assignment_drop. Builds: design 47 H2 H-A-509 (and the lead's answer of review round 1)
176 lines
6.8 KiB
Go
176 lines
6.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
|
|
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
|
|
plc "mesh-delivery-assigner/internal/assignment"
|
|
)
|
|
|
|
// H-A-509 and its second addendum: a module key of a module no manifest of the trunk names, that the node key does not name,
|
|
// is dropped, by hand through assigner_assignment_drop (recorded as a hand act first) and at the pass's clean state (named in
|
|
// the journal); a key a manifest of the trunk still names, or the node key still names, stays; nothing goes while the trunk
|
|
// is empty.
|
|
func TestARetiredModulesAssignmentKeyIsDropped(t *testing.T) {
|
|
// novox places web; the key of the retired checker predates the value's version; the key of the retired old is still
|
|
// named by the node key.
|
|
values, err := mmac.AssignmentsValues(mma.Assignments{V: mmac.Version, Node: "novox", Modules: []mma.PlacedModule{{Module: "web"}, {Module: "old"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
values["novox.checker"] = []byte(`{"node":"novox"}`)
|
|
checker, web, old := "novox.checker", "novox.web", "novox.old"
|
|
trunkOf := func(names ...string) map[string]bool {
|
|
out := map[string]bool{}
|
|
for _, n := range names {
|
|
out[n] = true
|
|
}
|
|
return out
|
|
}
|
|
copyOf := func() map[string][]byte {
|
|
out := map[string][]byte{}
|
|
for k, v := range values {
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
t.Run("by hand", func(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, module string
|
|
trunk map[string]bool
|
|
refuse error
|
|
dropped bool
|
|
}{
|
|
{"a retired module's key", "checker", trunkOf("web"), nil, true},
|
|
{"a manifest of the trunk names it", "web", trunkOf("web"), nil, false},
|
|
{"the node key still names it", "old", trunkOf("web"), nil, false},
|
|
{"the trunk is empty", "checker", trunkOf(), nil, false},
|
|
{"no key there", "gone", trunkOf("web"), nil, false},
|
|
{"not recorded", "checker", trunkOf("web"), errors.New("no responders"), false},
|
|
} {
|
|
st := memStore{mma.StateAssignments: &memKV{values: copyOf()}}
|
|
var asked []string
|
|
ask := func(verb string, body any) (json.RawMessage, error) {
|
|
raw, _ := json.Marshal(body)
|
|
asked = append(asked, verb+" "+string(raw))
|
|
return json.RawMessage(`{}`), c.refuse
|
|
}
|
|
trunk := func() (map[string]bool, error) { return c.trunk, nil }
|
|
out, err := dropAssignment(st, ask, trunk, "novox", c.module, "retired", "person.jochen")
|
|
key := "novox." + c.module
|
|
_, there := st[mma.StateAssignments].values[key]
|
|
switch {
|
|
case c.dropped && (err != nil || there || len(asked) != 1 || !strings.Contains(asked[0], assignmentDropTool+" "+key)):
|
|
t.Errorf("%s: %v, %v; the key left %v, asked %v", c.name, out, err, there, asked)
|
|
case !c.dropped && err == nil:
|
|
t.Errorf("%s: %v, want it refused", c.name, out)
|
|
case len(st[mma.StateAssignments].values) != len(values)-btoi(c.dropped):
|
|
t.Errorf("%s: %d keys left, want %d", c.name, len(st[mma.StateAssignments].values), len(values)-btoi(c.dropped))
|
|
}
|
|
}
|
|
if _, err := dropAssignment(memStore{mma.StateAssignments: &memKV{values: copyOf()}}, nil, func() (map[string]bool, error) { return trunkOf("web"), nil },
|
|
"novox", "checker", "", "person.jochen"); err == nil {
|
|
t.Error("a drop with no why is not refused")
|
|
}
|
|
})
|
|
|
|
// The tool judges against every watched branch's half, as the pass does (the lead's answer of review round 1): the branches
|
|
// of the registrations, each read page by page; a branch with none yet holds none.
|
|
t.Run("every watched branch", func(t *testing.T) {
|
|
call := func(verb string, body any) (json.RawMessage, error) {
|
|
switch verb {
|
|
case meshcatalogclient.RegistrationsVerb:
|
|
return json.Marshal(meshcatalog.RegistrationsAnswer{Registrations: []meshcatalog.Registration{{Module: "web", Branches: []string{"main", "feature"}}, {Module: "db", Branches: []string{"fresh"}}}})
|
|
case meshcatalogclient.RevisionVerb:
|
|
in := body.(meshcatalog.RevisionInput)
|
|
switch {
|
|
case in.Branch == "main" && in.After == "":
|
|
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "web"}}, Next: "web"})
|
|
case in.Branch == "main":
|
|
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "db"}}})
|
|
case in.Branch == "feature":
|
|
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{}, Entries: []meshcatalog.CatalogEntry{{Module: "new"}}})
|
|
}
|
|
return json.Marshal(meshcatalog.RevisionAnswer{NoneYet: true})
|
|
}
|
|
return nil, errors.New("no such verb " + verb)
|
|
}
|
|
got, err := watchedModules(call)()
|
|
if err != nil || len(got) != 3 || !got["web"] || !got["db"] || !got["new"] {
|
|
t.Errorf("watchedModules = %v, %v; want web, db and new", got, err)
|
|
}
|
|
})
|
|
|
|
t.Run("at the pass", func(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
trunk []string
|
|
gone []string
|
|
}{
|
|
{"a retired module's key goes, one the node key names stays", []string{"web"}, []string{checker}},
|
|
{"the trunk is empty", nil, nil},
|
|
} {
|
|
kv := &memKV{values: copyOf()}
|
|
p := newPasser(kvSet{plc.AssignmentsBucket(): kv}, time.Now())
|
|
var said []string
|
|
p.logf = func(f string, a ...any) { said = append(said, f) }
|
|
for k, v := range kv.values {
|
|
p.w.put(plc.AssignmentsBucket(), k, v)
|
|
}
|
|
// web is held by a watched branch that is not the trunk's first: the pass judges against every watched branch's half.
|
|
for _, n := range c.trunk {
|
|
mod, _ := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Fp2: fpTest, Repository: "novox/" + n, Commit: strings.Repeat("1", 40), Path: meshcatalogclient.RootPath})
|
|
putModuleRaw(p.w, "feature", n, mod)
|
|
}
|
|
for range 2 {
|
|
p.wants(context.Background())
|
|
}
|
|
for _, key := range []string{checker, web, old, "novox.node"} {
|
|
_, there := kv.values[key]
|
|
if want := !contains(c.gone, key); there != want {
|
|
t.Errorf("%s: %s left = %v, want %v", c.name, key, there, want)
|
|
}
|
|
}
|
|
if dropped, left := countSaid(said, "is dropped"), countSaid(said, "is left"); len(c.gone) > 0 && (dropped != 1 || left != 1) {
|
|
t.Errorf("%s: said dropped %d and left %d times, want once each: %v", c.name, dropped, left, said)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func btoi(b bool) int {
|
|
if b {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func contains(list []string, s string) bool {
|
|
for _, x := range list {
|
|
if x == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func countSaid(said []string, words string) int {
|
|
n := 0
|
|
for _, s := range said {
|
|
if strings.Contains(s, words) {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|