Files
mesh-delivery/modules/mesh-delivery-view/internal/view/pipelines_test.go
T
jschoubben 7a32a8b783 view: keep the pipeline runner's pipelines and serve view_pipelines
The view hears the seat mesh-pipeline-runner's pipeline-started, step-started, step-ended and pipeline-ended and the build seats' step-progress, keeps each pipeline whole in its own state pipelines (newest 500) and the latest progress per running ask in pipeline-progress (200), and view_pipelines reads them.

Builds: the lead's handover of 2026-11-16 (part 1 of the delivery view through its own pipeline); design 47 G, Pipelines per module (no milestone line cited by the handover)
2026-11-16 21:37:07 +01:00

198 lines
8.7 KiB
Go

package view
import (
"encoding/json"
"errors"
"fmt"
"sort"
"testing"
"time"
)
// memStore is a bucket in memory with a revision per key; failPuts makes that many puts fail as a moved key does.
type memStore struct {
vals map[string][]byte
revs map[string]uint64
next uint64
failPuts int
}
func newMem() *memStore { return &memStore{vals: map[string][]byte{}, revs: map[string]uint64{}} }
func (m *memStore) Get(k string) ([]byte, uint64, error) {
v, ok := m.vals[k]
if !ok {
return nil, 0, ErrNotKept
}
return v, m.revs[k], nil
}
func (m *memStore) Put(k string, v []byte, rev uint64) error {
if m.failPuts > 0 {
m.failPuts--
return errors.New("wrong last sequence")
}
if m.revs[k] != rev {
return errors.New("wrong last sequence")
}
m.next++
m.vals[k], m.revs[k] = v, m.next
return nil
}
func (m *memStore) Delete(k string) error { delete(m.vals, k); delete(m.revs, k); return nil }
func (m *memStore) Keys() ([]string, error) {
var ks []string
for k := range m.vals {
ks = append(ks, k)
}
sort.Strings(ks)
return ks, nil
}
func (m *memStore) read() StateRead {
return StateRead{Keys: m.Keys, Get: func(k string) (json.RawMessage, error) { return m.vals[k], nil }}
}
const runner = "mesh.seat.mesh-pipeline-runner.event."
func pat(h int) time.Time { return time.Date(2026, 11, 16, h, 0, 0, 0, time.UTC) }
func started(p, module string, h int) string {
return fmt.Sprintf(`{"v":1,"pipeline":%q,"module":%q,"branch":"main","commit":"c1","at":%q,"steps":[{"step":"build","artifact":%q,"status":"required"},{"step":"unit","status":"required"}]}`,
p, module, pat(h).Format(time.RFC3339), module+":program")
}
func TestPipelinesAreKeptFromTheRunnersEventsAndReadWithTheirProgress(t *testing.T) {
ps, gs := newMem(), newMem()
apply := func(subject, body string, h int) {
t.Helper()
if err := ApplyPipelineEvent(subject, []byte(body), ps, gs, pat(h)); err != nil {
t.Fatalf("%s: %v", subject, err)
}
}
apply(runner+"pipeline-started.adwaita.main.c1", started("main.969.adwaita", "adwaita", 1), 1)
apply(runner+"pipeline-started.avahi.main.c1", started("main.970.avahi", "avahi", 2), 2)
running := `{"v":1,"pipeline":"main.969.adwaita","module":"adwaita","branch":"main","commit":"c1","result":{"step":"build","artifact":"adwaita:program","status":"required","outcome":"running","ask":"ask/1","seat":"node-build-go"}}`
apply(runner+"step-started.adwaita.main.c1", running, 3)
if err := ApplyProgress("mesh.seat.node-build-go.event.step-progress.ace", []byte(`{"asker":"mesh-pipeline-runner","ask":"ask/1","phase":"build","at":"2026-11-16T03:01:00Z"}`), gs); err != nil {
t.Fatal(err)
}
// An older progress of the same ask changes nothing: the latest is kept.
if err := ApplyProgress("mesh.seat.node-build-go.event.step-progress.g14", []byte(`{"asker":"x","ask":"ask/1","phase":"fetch","at":"2026-11-16T03:00:00Z"}`), gs); err != nil {
t.Fatal(err)
}
r := Reader{PipelinesState: ps.read(), ProgressState: gs.read()}
got, err := r.Pipelines(PipelinesQuery{})
if err != nil || len(got.Pipelines) != 2 || got.Pipelines[0].Module != "avahi" || got.Pipelines[1].Module != "adwaita" {
t.Fatalf("newest first: %+v, %v", got, err)
}
ad := got.Pipelines[1]
if ad.Outcome != "running" || len(ad.Steps) != 2 || ad.Steps[0].Progress == nil || ad.Steps[0].Progress.Phase != "build" || ad.Steps[0].Progress.Node != "ace" ||
!ad.Steps[0].Progress.At.Equal(time.Date(2026, 11, 16, 3, 1, 0, 0, time.UTC)) || ad.Steps[1].Progress != nil || ad.Steps[1].Status != "required" {
t.Fatalf("the running step carries the latest progress: %+v", ad.Steps)
}
// The step's end closes its progress; the pipeline's end says the outcome and every step whole.
apply(runner+"step-ended.adwaita.main.c1", `{"v":1,"pipeline":"main.969.adwaita","module":"adwaita","branch":"main","commit":"c1","result":{"step":"build","artifact":"adwaita:program","status":"required","outcome":"passed","ask":"ask/1"}}`, 4)
if len(gs.vals) != 0 {
t.Errorf("the progress of an ended step stays: %v", gs.vals)
}
apply(runner+"pipeline-ended.adwaita.main.c1", `{"v":1,"pipeline":"main.969.adwaita","module":"adwaita","branch":"main","commit":"c1","outcome":"failed","why":"unit failed","ended":"2026-11-16T05:00:00Z",
"steps":[{"step":"build","artifact":"adwaita:program","status":"required","outcome":"passed"},{"step":"unit","status":"required","outcome":"failed","why":"TestX"}]}`, 5)
// A step event heard after the end changes nothing.
apply(runner+"step-started.adwaita.main.c1", running, 6)
for _, tc := range []struct {
name string
q PipelinesQuery
want []string
}{
{"all", PipelinesQuery{}, []string{"avahi", "adwaita"}},
{"outcome", PipelinesQuery{Outcome: "failed"}, []string{"adwaita"}},
{"module", PipelinesQuery{Module: "avahi"}, []string{"avahi"}},
{"branch", PipelinesQuery{Branch: "other"}, nil},
{"since heard", PipelinesQuery{Since: pat(6)}, []string{"adwaita"}},
{"limit", PipelinesQuery{Limit: 1}, []string{"avahi"}},
} {
a, err := r.Pipelines(tc.q)
var mods []string
for _, p := range a.Pipelines {
mods = append(mods, p.Module)
}
if err != nil || fmt.Sprint(mods) != fmt.Sprint(tc.want) {
t.Errorf("%s: %v, %v (want %v)", tc.name, mods, err, tc.want)
}
}
a, _ := r.Pipelines(PipelinesQuery{Limit: 1})
if !a.Cut || a.Matching != 2 {
t.Errorf("a cut says so: %+v", a)
}
a, _ = r.Pipelines(PipelinesQuery{Outcome: "failed"})
if w := a.Pipelines[0]; w.Why != "unit failed" || w.Ended == nil || w.Steps[1].Outcome != "failed" || w.Steps[0].Progress != nil || w.Steps[0].Outcome != "passed" {
t.Errorf("the ended pipeline is whole: %+v", w)
}
}
func TestAStepEventBeforeItsPipelinesStartCreatesThePipelineAndTheStartFillsIt(t *testing.T) {
ps, gs := newMem(), newMem()
if err := ApplyPipelineEvent(runner+"step-ended.m.main.c1", []byte(`{"v":1,"pipeline":"main.1.m","module":"m","branch":"main","commit":"c1","result":{"step":"build","artifact":"m:program","status":"required","outcome":"passed"}}`), ps, gs, pat(1)); err != nil {
t.Fatal(err)
}
if err := ApplyPipelineEvent(runner+"pipeline-started.m.main.c1", []byte(started("main.1.m", "m", 0)), ps, gs, pat(2)); err != nil {
t.Fatal(err)
}
a, _ := Reader{PipelinesState: ps.read()}.Pipelines(PipelinesQuery{})
if p := a.Pipelines[0]; len(p.Steps) != 2 || p.Steps[0].Outcome != "passed" || p.Steps[1].Step != "unit" || p.Started == nil || p.Outcome != "running" {
t.Fatalf("%+v", p)
}
}
func TestPipelineWritesRetryAMovedKeyAndRefuseWhatDoesNotReadAndPruneToTheCap(t *testing.T) {
ps, gs := newMem(), newMem()
ps.failPuts = pipelineWrites - 1
if err := ApplyPipelineEvent(runner+"pipeline-started.m.main.c1", []byte(started("main.1.m", "m", 0)), ps, gs, pat(1)); err != nil || len(ps.vals) != 1 {
t.Fatalf("a moved key is tried again: %v", err)
}
ps.failPuts = pipelineWrites
if err := ApplyPipelineEvent(runner+"pipeline-started.m.main.c1", []byte(started("main.1.m", "m", 0)), ps, gs, pat(1)); err == nil {
t.Error("a key that keeps moving is left for the bus to give again")
}
for _, bad := range [][2]string{{runner + "pipeline-started.m.main.c1", `{`}, {runner + "pipeline-started.m.main.c1", `{"module":"m"}`}, {runner + "wait-held.m.main.c1", `{}`}, {"mesh.mod.x.y", `{}`}} {
if err := ApplyPipelineEvent(bad[0], []byte(bad[1]), ps, gs, pat(1)); err == nil {
t.Errorf("%s %s was kept", bad[0], bad[1])
}
}
if err := ApplyProgress("mesh.seat.node-build-go.event.step-progress.ace", []byte(`{"ask":"","phase":"build"}`), gs); err == nil {
t.Error("a progress of no ask was kept")
}
// Over its cap by more than the slack, the longest unheard go, the newest stay.
ps = newMem()
for i := 0; i < MaxPipelines+pruneSlack+1; i++ {
p := fmt.Sprintf("main.%d.m", i)
if err := ApplyPipelineEvent(runner+"pipeline-started.m.main.c1", []byte(started(p, "m", 0)), ps, gs, pat(0).Add(time.Duration(i)*time.Second)); err != nil {
t.Fatal(err)
}
}
if len(ps.vals) != MaxPipelines {
t.Fatalf("%d pipelines kept, the cap is %d", len(ps.vals), MaxPipelines)
}
if _, _, err := ps.Get("main.0.m"); err == nil {
t.Error("the oldest heard stays")
}
if _, _, err := ps.Get(fmt.Sprintf("main.%d.m", MaxPipelines+pruneSlack)); err != nil {
t.Error("the newest heard went")
}
}
func TestPipelinesQueryReadsItsArguments(t *testing.T) {
q, err := ParsePipelinesQuery(map[string]any{"branch": "main", "since": "2026-11-16T01:00:00Z", "limit": float64(3)})
if err != nil || q.Branch != "main" || q.Limit != 3 || !q.Since.Equal(pat(1)) {
t.Fatalf("%+v %v", q, err)
}
for _, bad := range []map[string]any{{"since": "yesterday"}, {"limit": "3"}, {"limit": float64(-1)}, {"limit": 1.5}} {
if _, err := ParsePipelinesQuery(bad); err == nil {
t.Errorf("%v was read", bad)
}
}
if _, err := (Reader{}).Pipelines(PipelinesQuery{}); err == nil {
t.Error("a state that cannot be read here reads as none")
}
}