Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/runcandidates_test.go
T
jschoubben c2950e332f A-194: indexer_runs reads the state runs and the run candidates, indexer_run_drop drops one by hand
The drop is a hand act recorded through mesh-hand-acts before it is asked; the indexer, the state's owner, drops the
run's candidate and its key.

Builds: design 47 G §64; A-194

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:57:11 +01:00

189 lines
8.7 KiB
Go

package index
import (
"context"
"encoding/json"
"errors"
"slices"
"testing"
"time"
"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"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// Design 47 G §64, §190, A-160, A-174: a run on request's candidate is built from the runner's run-started, each member taken
// in from its registration's repository at its commit (read through the seat git), and dropped once every member's pipeline of
// the run has ended; what the indexer knows of the run is kept in its state runs, so a restart between two ends loses nothing.
func TestARunsCandidateIsBuiltOnItsStartAndDroppedWhenEveryMembersPipelineEnded(t *testing.T) {
const c1, c2 = "1111111111111111111111111111111111111111", "2222222222222222222222222222222222222222"
bed := func(t *testing.T) (*Indexer, *memStore, *memStore, *int) {
t.Helper()
ix, _, _ := manifestsIndexer(t)
cands, runs := newMem(), newMem()
ix.Store, ix.Runs, ix.Revisions = cands, runs, newMemObjects(0)
ix.Registry = &Registry{Registrations: MemoryStore()}
for _, r := range []meshcatalog.Registration{
{Module: "avahi", Origin: catalogueRepo, Path: "modules/avahi", Branches: []string{"main"}},
{Module: "netcheck", Origin: "novox/mesh-networking", Path: "modules/netcheck", Branches: []string{"main", "beta"}},
} {
data, _ := json.Marshal(r)
if err := ix.Registry.Registrations.Create(context.Background(), RegistrationKey(r.Module), data); err != nil {
t.Fatal(err)
}
}
reads := 0
ix.Read = func(_ context.Context, repository, commit, dir string) (RepoRead, error) {
reads++
module := map[string]string{"modules/avahi": "avahi", "modules/netcheck": "netcheck"}[dir]
return RepoRead{Root: "r", Trees: map[string]string{dir: "tree-" + commit[:4]}, Manifests: map[string]string{dir: catalogueManifest(t, module)},
Locks: map[string]map[string]string{dir: {"go.sum": avahiGoSum}}}, nil
}
return ix, cands, runs, &reads
}
ctx := context.Background()
started := meshpipelinerunner.RunStarted{V: 1, Run: "7", By: "lead@shanks", Members: []meshpipelinerunner.Member{
{Module: "avahi", Commit: c1, Branch: "feature/1966-x"}, {Module: "netcheck", Commit: c2, Branch: "beta"}}}
ended := func(module string) meshpipelinerunner.PipelineEnded {
return meshpipelinerunner.PipelineEnded{V: 1, Pipeline: "p", Module: module, Branch: "b", Commit: "c", Outcome: "passed", Run: "7"}
}
key, _ := meshcatalogclient.CandidatesKey("run-7")
t.Run("built on its start, one pull per member, and dropped with the run after a restart", func(t *testing.T) {
ix, cands, runs, reads := bed(t)
if err := ix.StartRun(ctx, started); err != nil {
t.Fatal(err)
}
raw, _, found, _ := cands.Get(ctx, key)
c, err := DecodeCandidate(raw)
if !found || err != nil || c.Group != "run-7" || len(c.Pulls) != 2 || *reads != 2 {
t.Fatalf("candidate %+v (%v, found %v, %d reads), want run-7 with two pulls read through the seat git", c, err, found, *reads)
}
for _, p := range c.Pulls {
m := p.Modules[0]
if p.Number != 0 || len(p.Modules) != 1 || p.Fps[m].Manifest == "" || (p.Fps[m].Fp2 == "" && p.Fps[m].None2 == "") ||
(m == "avahi") != (p.Repository == catalogueRepo && p.Head == c1) {
t.Errorf("pull %+v, want number 0, its registration's repository at its commit, a manifest and a fingerprint or why none", p)
}
}
if err := ix.EndRunPipeline(ctx, ended("avahi")); err != nil {
t.Fatal(err)
}
// A restart: a fresh indexer over the same states.
again := &Indexer{Store: cands, Runs: runs, Revisions: ix.Revisions, Registry: ix.Registry, Read: ix.Read}
if _, _, found, _ := cands.Get(ctx, key); !found {
t.Fatal("the candidate went before its last member's pipeline ended")
}
if err := again.EndRunPipeline(ctx, ended("netcheck")); err != nil {
t.Fatal(err)
}
if _, _, found, _ := cands.Get(ctx, key); found {
t.Error("the candidate stays after every member's pipeline ended")
}
if k, _ := runs.Keys(ctx); len(k) != 0 {
t.Errorf("runs holds %v after the run's end, want none", k)
}
})
t.Run("ends heard before the start drop it at the start", func(t *testing.T) {
ix, cands, runs, _ := bed(t)
for _, m := range []string{"avahi", "netcheck"} {
if err := ix.EndRunPipeline(ctx, ended(m)); err != nil {
t.Fatal(err)
}
}
if err := ix.StartRun(ctx, started); err != nil {
t.Fatal(err)
}
k1, _ := cands.Keys(ctx)
k2, _ := runs.Keys(ctx)
if len(k1)+len(k2) != 0 {
t.Errorf("candidates %v, runs %v, want none", k1, k2)
}
})
t.Run("a member with no registration is never taken in; a pipeline of no run is none of this", func(t *testing.T) {
ix, cands, _, _ := bed(t)
lost := started
lost.Members = []meshpipelinerunner.Member{{Module: "gone", Commit: c1}}
if err := ix.StartRun(ctx, lost); !errors.Is(err, ErrUnreadable) {
t.Errorf("a member with no registration: %v, want unreadable", err)
}
if err := ix.EndRunPipeline(ctx, meshpipelinerunner.PipelineEnded{Module: "avahi"}); err != nil {
t.Error(err)
}
if k, _ := cands.Keys(ctx); len(k) != 0 {
t.Errorf("candidates %v, want none", k)
}
})
t.Run("a member's take-in starts from its own branch only when its registration watches it", func(t *testing.T) {
reg := meshcatalog.Registration{Branches: []string{"main", "beta"}}
for branch, want := range map[string]string{"beta": "beta", "main": TrunkBranch, "feature/1966-x": TrunkBranch, "": TrunkBranch} {
if got := RunBase(reg, branch); got != want {
t.Errorf("RunBase(%q) = %q, want %q", branch, got, want)
}
}
})
t.Run("run-<id> is a run's group beside try-<digits>, never a pull request's", func(t *testing.T) {
for g, want := range map[string]bool{"run-7": true, "run-a1-b": true, "try-3": true, "run-": false, "run-a.b": false, "feat/x": false} {
if meshcatalogclient.IsTryGroup(g) != want {
t.Errorf("IsTryGroup(%q) = %v, want %v", g, !want, want)
}
}
})
}
// Design 47 G §64, A-194: the state runs and the run candidates are read by indexer_runs and a run is dropped by hand by
// indexer_run_drop, its hand act written first.
func TestTheRunsAreReadAndDroppedByHand(t *testing.T) {
ctx := context.Background()
cands, runs := newMem(), newMem()
ix := &Indexer{Store: cands, Runs: runs, Now: func() time.Time { return time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) }}
put := func(s *memStore, key string, v any) {
t.Helper()
data, _ := json.Marshal(v)
if err := s.Create(ctx, key, data); err != nil {
t.Fatal(err)
}
}
const id = "0a1b2c3d4e5f"
put(runs, "run-"+id, Run{V: 1, Run: id, Members: []meshpipelinerunner.Member{{Module: "avahi", Commit: "c1"}, {Module: "netcheck", Commit: "c2"}}, Ended: []string{"avahi"}})
for _, g := range []string{"run-" + id, "run-ffffffffffff"} {
key, _ := meshcatalogclient.CandidatesKey(g)
put(cands, key, map[string]string{"group": g})
}
read, err := ReadRuns(ctx, runs, cands)
if err != nil || len(read.Runs) != 1 || read.Runs[0].Run != id || !read.Runs[0].Candidate || !slices.Equal(read.Runs[0].Waiting, []string{"netcheck"}) || !slices.Equal(read.Orphans, []string{"run-ffffffffffff"}) {
t.Fatalf("runs = %+v, %v, want the run waiting for netcheck with its candidate and one orphan candidate", read, err)
}
var recorded []string
var asked []string
tools := RunsTools(runs, cands, func(key string, value []byte) (TakeInAsk, error) {
asked = append(asked, key)
a, err := DecodeTakeIn(value)
if err != nil {
return a, err
}
a.Result = &TakenIn{}
*a.Result = ix.DropRunByHand(ctx, a.RunDrop, a.By, a.Why)
return a, nil
}, ix.now, func(what, why string) error { recorded = append(recorded, what); return nil })
if len(tools) != 2 || tools[0].Name != RunsTool || tools[1].Name != RunDropTool {
t.Fatalf("tools = %v", tools)
}
if _, err := tools[1].Run(map[string]any{"run": id}); err == nil || len(recorded) != 0 {
t.Fatalf("a drop with no why was done: %v, %v", err, recorded)
}
if _, err := tools[1].Run(map[string]any{"run": id, "why": "its pipelines never end"}); err != nil || len(recorded) != 1 || len(asked) != 1 {
t.Fatalf("drop: %v, recorded %v, asked %v: want the hand act recorded, then the ask", err, recorded, asked)
}
read, _ = ReadRuns(ctx, runs, cands)
if len(read.Runs) != 0 || len(read.Orphans) != 1 {
t.Errorf("after the drop: %+v, want the run and its candidate gone and the orphan left", read)
}
if got := ix.DropRunByHand(ctx, "ffffffffffff", "lead", "an orphan"); !got.Stored {
t.Errorf("an orphan candidate is not dropped: %+v", got)
}
if got := ix.DropRunByHand(ctx, id, "lead", "again"); got.Stored {
t.Errorf("a run already dropped was dropped again: %+v", got)
}
}