The runner consumes mesh-catalog.version-released and writes its version into the mover of that module in the pipeline of the source revision built, on the branch the version was released for (the registration's first watched branch), by compare-and-set, so pipeline-ended names the version released. Unit tests 80 to 81. Builds: design 47 H §746 (the released version per mover); lead answers H-A-52, H-A-62 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
345 lines
16 KiB
Go
345 lines
16 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
|
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
type memStore map[string]struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}
|
|
|
|
func (m memStore) Get(key string) (seat.Pipeline, uint64, error) {
|
|
e, ok := m[key]
|
|
if !ok {
|
|
return seat.Pipeline{}, 0, errors.New("no such key")
|
|
}
|
|
// A read is a copy, as one from the bus is: a change not written is not seen by the next read.
|
|
e.p.Steps = append([]seat.StepResult(nil), e.p.Steps...)
|
|
e.p.Movers = append([]seat.Mover(nil), e.p.Movers...)
|
|
return e.p, e.rev, nil
|
|
}
|
|
|
|
func (m memStore) Update(key string, p seat.Pipeline, rev uint64) (uint64, error) {
|
|
if m[key].rev != rev {
|
|
return 0, ErrChanged
|
|
}
|
|
m[key] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{p, rev + 1}
|
|
return rev + 1, nil
|
|
}
|
|
|
|
func (m memStore) Keys(filter string) ([]string, error) {
|
|
var out []string
|
|
for k := range m {
|
|
if ok, _ := matchKey(filter, k); ok {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func matchKey(filter, key string) (bool, error) {
|
|
f, k := strings.Split(filter, "."), strings.Split(key, ".")
|
|
if len(f) != len(k) {
|
|
return false, nil
|
|
}
|
|
for i := range f {
|
|
if f[i] != "*" && f[i] != k[i] {
|
|
return false, nil
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
type askRecorder struct{ asked []oc.Ask }
|
|
|
|
func (a *askRecorder) Ask(x oc.Ask) error { a.asked = append(a.asked, x); return nil }
|
|
|
|
const pkey = "main.5"
|
|
|
|
// fixture is a pipeline of app with a required lint asked of node-build-go, an optional unit asked of the same seat and an
|
|
// approval step waiting, and a hearer over it.
|
|
func fixture() (*Hearer, memStore, *[]string, *askRecorder, map[string]seat.KeptResult, map[string]string) {
|
|
store := memStore{pkey: {p: seat.Pipeline{V: 1, Pipeline: pkey, Branch: "main", Source: 5,
|
|
Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{
|
|
{Module: "app", Artifact: "program", Step: seat.StepResultStepLint, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "a1", Seat: nodebuildgo.Seat},
|
|
{Module: "app", Artifact: "program", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusOptional, Outcome: seat.StepResultOutcomeRunning, Ask: "a2", Seat: nodebuildgo.Seat},
|
|
{Module: "app", Artifact: "program", Step: seat.StepResultStepApproval, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeWaiting}}, Movers: []seat.Mover{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}, rev: 1}}
|
|
said := &[]string{}
|
|
asker := &askRecorder{}
|
|
kept := map[string]seat.KeptResult{}
|
|
outs := map[string]string{}
|
|
h := &Hearer{Store: store, Name: "mesh-pipeline-runner", Asker: asker,
|
|
Say: func(subject string, _ any) error {
|
|
*said = append(*said, subject[strings.Index(subject, ".event.")+7:])
|
|
return nil
|
|
},
|
|
Log: func(seat.LogEntry) error { return nil },
|
|
Keep: func(k string, v seat.KeptResult) error { kept[k] = v; return nil },
|
|
Output: func(k string, d []byte) error { outs[k] = string(d); return nil },
|
|
Now: func() time.Time { return time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) }}
|
|
return h, store, said, asker, kept, outs
|
|
}
|
|
|
|
func ended(ask, step, outcome string) StepEnded {
|
|
return StepEnded{ID: ask, Ask: ask, Node: "n1", Pipeline: pkey, Module: "app", Commit: "c1", Fingerprint: "src2:aa", Step: step, Outcome: outcome}
|
|
}
|
|
|
|
var goSeat = StepSeat{Seat: nodebuildgo.Seat, Prefix: "mesh.seat.node-build-go.event.step-ended."}
|
|
|
|
// A step's end is taken only as the runner asked it (design 47 G §24; ADR 0666 §21).
|
|
func TestStepEndedIsTakenOnlyAsAsked(t *testing.T) {
|
|
h, store, _, _, _, _ := fixture()
|
|
other := ended("a1", "lint", "passed")
|
|
other.Fingerprint = "src2:bb"
|
|
wrongNode := ended("a1", "lint", "passed")
|
|
wrongNode.Node = "n2"
|
|
unknownPipeline := ended("a1", "lint", "passed")
|
|
unknownPipeline.Pipeline = "main.9"
|
|
for name, c := range map[string]struct {
|
|
subject string
|
|
ev StepEnded
|
|
}{
|
|
"a subject of another seat": {"mesh.seat.node-build-node.event.step-ended.n1", ended("a1", "lint", "passed")},
|
|
"a body naming another node": {goSeat.Prefix + "n1", wrongNode},
|
|
"an unknown pipeline": {goSeat.Prefix + "n1", unknownPipeline},
|
|
"an ask nobody made": {goSeat.Prefix + "n1", ended("zz", "lint", "passed")},
|
|
"another fingerprint": {goSeat.Prefix + "n1", other},
|
|
"another step": {goSeat.Prefix + "n1", ended("a1", "unit", "passed")},
|
|
"an outcome step-ended lacks": {goSeat.Prefix + "n1", ended("a1", "lint", "waiting")},
|
|
} {
|
|
if _, err := h.OnStepEnded(goSeat, c.subject, c.ev); !errors.Is(err, ErrRefused) {
|
|
t.Errorf("%s: err %v, want a refusal", name, err)
|
|
}
|
|
}
|
|
if store[pkey].p.Steps[0].Outcome != seat.StepResultOutcomeRunning {
|
|
t.Error("a refused end changed the pipeline")
|
|
}
|
|
// R2/AC1 (design 47 G §24, §69): the end of another asker's step (the assigner's composer build, which names no
|
|
// pipeline) is not the runner's: it writes and says nothing, read before any pipeline, and is acknowledged.
|
|
theirs := ended("b7", "build", "passed")
|
|
theirs.Asker, theirs.Pipeline = "mesh-delivery-assigner", ""
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", theirs); err != nil {
|
|
t.Errorf("another asker's step end: err %v, want it taken as nothing", err)
|
|
}
|
|
if store[pkey].p.Steps[0].Outcome != seat.StepResultOutcomeRunning || store[pkey].rev != 1 {
|
|
t.Error("another asker's step end changed the pipeline")
|
|
}
|
|
// The seat the step was asked of is the only one that ends it.
|
|
if _, err := h.OnStepEnded(StepSeat{Seat: "node-build-node", Prefix: "mesh.seat.node-build-node.event.step-ended."}, "mesh.seat.node-build-node.event.step-ended.n1", ended("a1", "lint", "passed")); !errors.Is(err, ErrRefused) {
|
|
t.Errorf("another seat's holder ended the step: %v", err)
|
|
}
|
|
}
|
|
|
|
// A taken end is written, kept and said; the pipeline ends once every step ended (§24, §27).
|
|
func TestStepEndedEndsThePipeline(t *testing.T) {
|
|
h, store, said, _, kept, outs := fixture()
|
|
h.Asker = nil
|
|
lint := ended("a1", "lint", "passed")
|
|
lint.Output = "all clean"
|
|
unit := ended("a2", "unit", "failed")
|
|
unit.Kept = []nodebuildgo.KeptResult{{Digest: "sha256:d1", Level: "unit", Package: "app/x"}}
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", lint); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := store[pkey].p; p.Outcome != seat.PipelineOutcomeRunning || p.Steps[0].Node != "n1" || p.Steps[0].Output == "" {
|
|
t.Fatalf("after the lint: %+v", p)
|
|
}
|
|
if len(outs) != 1 {
|
|
t.Errorf("outputs kept %v", outs)
|
|
}
|
|
// Approval waits with no asker, so the pipeline does not end on the last test step alone.
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", unit); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p := store[pkey].p; p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Fatalf("a pipeline with its approval waiting ended: %+v", p)
|
|
}
|
|
if k := kept[seat.KeptByLevelDigestKey("unit", keys.EscapeName("sha256:d1"))]; k.Package != "app/x" || k.Pipeline != pkey {
|
|
t.Errorf("kept %+v", kept)
|
|
}
|
|
// A step heard again takes nothing.
|
|
n := len(*said)
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", unit); err != nil || len(*said) != n {
|
|
t.Errorf("a second end said %v (%v)", (*said)[n:], err)
|
|
}
|
|
}
|
|
|
|
// The approval step is asked last, only when every other required step passed, and ended by its warrant (§26, §37).
|
|
func TestApprovalIsAskedLastAndEndedByItsWarrant(t *testing.T) {
|
|
h, store, said, asker, _, _ := fixture()
|
|
var record *oc.Warrant
|
|
h.Where = func(string) (string, *oc.Warrant, error) { return "chosen", record, nil }
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "passed")); err != nil || len(asker.asked) != 0 {
|
|
t.Fatalf("asked %d before the unit step ended (%v)", len(asker.asked), err)
|
|
}
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a2", "unit", "failed")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// An optional step that failed holds nothing: the approval is asked.
|
|
if len(asker.asked) != 1 || asker.asked[0].About != "app.main" {
|
|
t.Fatalf("asked %+v", asker.asked)
|
|
}
|
|
a := asker.asked[0]
|
|
if got := (*said)[len(*said)-1]; !strings.HasPrefix(got, "wait-held.main.5") {
|
|
t.Errorf("the wait was said as %q", got)
|
|
}
|
|
warrant := func(option, outcome string) oc.Warrant {
|
|
return oc.Warrant{Asker: "mesh-pipeline-runner", Ask: a.ID, About: a.About, Outcome: outcome, Option: option, Level: operatorchannelclient.Approve,
|
|
AskDigest: operatorchannelclient.Digest(a), At: operatorchannelclient.FormatTime(time.Date(2026, 11, 11, 13, 0, 0, 0, time.UTC)),
|
|
By: &oc.Person{Who: operatorchannelclient.Operator, Identity: "op", Kind: "telegram", Verified: "linked"}}
|
|
}
|
|
// The event only wakes the runner (A-89): an event that says approve while the router's record holds none takes nothing.
|
|
event := warrant(OptionApprove, oc.WarrantOutcomeChosen)
|
|
if _, err := h.OnDecided(event); !errors.Is(err, ErrNoRecord) || store[pkey].p.Steps[2].Outcome != seat.StepResultOutcomeRunning {
|
|
t.Errorf("an event with no record was taken: %v", err)
|
|
}
|
|
// A record whose warrant is for another ask's digest is not taken; an event of another asker neither.
|
|
bad := warrant(OptionApprove, oc.WarrantOutcomeChosen)
|
|
bad.AskDigest = "sha256:other"
|
|
record = &bad
|
|
if _, err := h.OnDecided(event); err == nil || store[pkey].p.Steps[2].Outcome != seat.StepResultOutcomeRunning {
|
|
t.Errorf("a warrant for another ask was taken: %v", err)
|
|
}
|
|
notMine := event
|
|
notMine.Asker = "someone"
|
|
if _, err := h.OnDecided(notMine); !errors.Is(err, ErrRefused) {
|
|
t.Errorf("another asker's event: %v", err)
|
|
}
|
|
// The record says decline although the event body says approve: the record decides.
|
|
decline := warrant(OptionDecline, oc.WarrantOutcomeChosen)
|
|
record = &decline
|
|
if p, err := h.OnDecided(event); err != nil || p.Steps[2].Outcome != seat.StepResultOutcomeDeclined {
|
|
t.Fatalf("the record said decline: %+v (%v)", p.Steps[2], err)
|
|
}
|
|
// (the declined pipeline above is a fresh fixture's: approve it on a new one)
|
|
h, store, said, asker, _, _ = fixture()
|
|
h.Where = func(string) (string, *oc.Warrant, error) { return "chosen", record, nil }
|
|
for _, s := range []StepEnded{ended("a1", "lint", "passed"), ended("a2", "unit", "failed")} {
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
a = asker.asked[0]
|
|
approve := warrant(OptionApprove, oc.WarrantOutcomeChosen)
|
|
record = &approve
|
|
p, err := h.OnDecided(event)
|
|
if err != nil || p.Steps[2].Outcome != seat.StepResultOutcomePassed || p.Outcome != seat.PipelineOutcomePassed || p.EndCount != 1 {
|
|
t.Fatalf("approved: %+v (%v)", p, err)
|
|
}
|
|
if !strings.HasPrefix((*said)[len(*said)-1], "pipeline-ended.main.5") {
|
|
t.Errorf("said %v", *said)
|
|
}
|
|
// A pipeline that ended takes no second decision.
|
|
record = &decline
|
|
if p, err := h.OnDecided(event); err != nil || p.Steps[2].Outcome != seat.StepResultOutcomePassed {
|
|
t.Errorf("a second warrant changed it: %+v (%v)", p.Steps[2], err)
|
|
}
|
|
}
|
|
|
|
// A decline, an expiry or a replacement ends the approval step declined or superseded, and a failed required step leaves it
|
|
// not asked with the pipeline failed (§26, §37).
|
|
func TestApprovalEnds(t *testing.T) {
|
|
for name, c := range map[string]struct {
|
|
outcome, option, want string
|
|
}{
|
|
"declined": {oc.WarrantOutcomeChosen, OptionDecline, seat.StepResultOutcomeDeclined},
|
|
"expired": {oc.WarrantOutcomeExpired, "", seat.StepResultOutcomeDeclined},
|
|
"refused": {oc.WarrantOutcomeRefused, "", seat.StepResultOutcomeDeclined},
|
|
"superseded": {oc.WarrantOutcomeReplaced, "", seat.StepResultOutcomeSuperseded},
|
|
} {
|
|
h, _, _, asker, _, _ := fixture()
|
|
for _, s := range []StepEnded{ended("a1", "lint", "passed"), ended("a2", "unit", "passed")} {
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
a := asker.asked[0]
|
|
rec := oc.Warrant{Asker: h.Name, Ask: a.ID, About: a.About, Outcome: c.outcome, Option: c.option, Level: operatorchannelclient.Approve,
|
|
AskDigest: operatorchannelclient.Digest(a), At: operatorchannelclient.FormatTime(time.Date(2026, 11, 11, 13, 0, 0, 0, time.UTC)),
|
|
By: &oc.Person{Who: operatorchannelclient.Operator, Identity: "op", Kind: "telegram", Verified: "linked"}}
|
|
h.Where = func(string) (string, *oc.Warrant, error) { return c.outcome, &rec, nil }
|
|
p, err := h.OnDecided(oc.Warrant{Asker: h.Name, Ask: a.ID, About: a.About})
|
|
if err != nil || p.Steps[2].Outcome != c.want || p.Outcome != seat.PipelineOutcomeFailed {
|
|
t.Errorf("%s: step %q, pipeline %q (%v)", name, p.Steps[2].Outcome, p.Outcome, err)
|
|
}
|
|
}
|
|
h, store, _, asker, _, _ := fixture()
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "failed")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a2", "unit", "passed")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := store[pkey].p
|
|
if len(asker.asked) != 0 || p.Steps[2].Outcome != seat.StepResultOutcomeNotAsked || p.Outcome != seat.PipelineOutcomeFailed || !strings.Contains(p.Why, "lint") {
|
|
t.Errorf("asked %d; %+v", len(asker.asked), p)
|
|
}
|
|
}
|
|
|
|
// An optional step still waiting (a code review nobody has given) does not hold a pipeline's end; a required one does
|
|
// (design 47 G A-104).
|
|
func TestAnOptionalStepStillWaitingHoldsNoEnd(t *testing.T) {
|
|
now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
|
|
for name, c := range map[string]struct {
|
|
review string
|
|
ends bool
|
|
}{"optional review waiting": {seat.StepResultStatusOptional, true}, "required review waiting": {seat.StepResultStatusRequired, false}} {
|
|
p := seat.Pipeline{Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{
|
|
{Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
|
|
{Step: seat.StepResultStepReview, Status: c.review, Outcome: seat.StepResultOutcomeWaiting},
|
|
{Step: seat.StepResultStepApproval, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
|
|
}}
|
|
if got := endPipeline(&p, now); got != c.ends {
|
|
t.Errorf("%s: ended = %v, want %v", name, got, c.ends)
|
|
}
|
|
if c.ends && p.Outcome != seat.PipelineOutcomePassed {
|
|
t.Errorf("%s: outcome %s", name, p.Outcome)
|
|
}
|
|
}
|
|
}
|
|
|
|
type askerFunc func(accept, id string, body any) error
|
|
|
|
func (f askerFunc) AskStep(accept, id string, body any) error { return f(accept, id, body) }
|
|
|
|
// A recovery's steps are asked on the build seats' stream delivery, ahead of the first round; the seats with no such stream keep
|
|
// their own accept subject; every other commit's steps go on check (design 47 G §86, §415).
|
|
func TestARecoverysStepsAreAskedOnTheStreamDelivery(t *testing.T) {
|
|
asks := []Asked{{Seat: nodebuildgo.Seat, ID: "s1", Ask: 1}, {Seat: wants.Seat, ID: "s2", Ask: 2}}
|
|
for _, c := range []struct {
|
|
recovery bool
|
|
want string
|
|
}{{true, "mesh.seat.node-build-go.accept.delivery.mesh-pipeline-runner," + wants.AcceptSubjectStep}, {false, "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner," + wants.AcceptSubjectStep}} {
|
|
var to []string
|
|
asker := askerFunc(func(accept, _ string, _ any) error { to = append(to, accept); return nil })
|
|
if err := askAll(asker, "k", asks, func(string) bool { return c.recovery }); err != nil || strings.Join(to, ",") != c.want {
|
|
t.Errorf("recovery %v: asked on %v (%v), want %s", c.recovery, to, err, c.want)
|
|
}
|
|
}
|
|
// Starter.recovered reads the commit; a failed read asks on check and says so.
|
|
var said []string
|
|
s := &Starter{Logf: func(f string, a ...any) { said = append(said, f) }, Recovered: func(c string) (bool, error) {
|
|
if c == "bad" {
|
|
return false, errors.New("unread")
|
|
}
|
|
return c == "rec", nil
|
|
}}
|
|
if !s.recovered("k", "rec") || s.recovered("k", "other") || s.recovered("k", "bad") || len(said) != 1 {
|
|
t.Errorf("recovered: said %v", said)
|
|
}
|
|
}
|