Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/failed_test.go
T
jschoubben 267bd0a9cb RN11: the runner says a failed head as the finding pipeline-failed
A required step that failed or could not run writes the finding of its commit, owned by the commit's author as the forge
records it (read through the seat git's pull); one with no known author has no owner and says why, written at the next step
end of that commit; a newer revision passing the steps clears it. The runner's finding writer is the held writer.

Builds: design 47 G §33, §540, A-191
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:53:20 +01:00

167 lines
6.2 KiB
Go

package pipeline
import (
"context"
"errors"
"strconv"
"strings"
"testing"
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
)
// heldFindings is a finding writer in memory, by key.
type heldFindings struct {
held map[string]condseat.Finding
cleared []string
}
func newHeld() *heldFindings { return &heldFindings{held: map[string]condseat.Finding{}} }
func (h *heldFindings) Write(_ context.Context, f condseat.Finding) error {
h.held[f.Scope+"."+f.ID+"."+f.Kind] = f
return nil
}
func (h *heldFindings) Clear(_ context.Context, scope, id, kind, _ string) error {
delete(h.held, scope+"."+id+"."+kind)
h.cleared = append(h.cleared, id)
return nil
}
func (h *heldFindings) Held() []condseat.Finding {
var out []condseat.Finding
for _, f := range h.held {
out = append(out, f)
}
return out
}
// A required step that failed or could not run is the finding pipeline-failed of its commit, owned by the commit's author as
// the forge records it; one whose author is not known has no owner and says why, and the next step end of that commit writes it
// (design 47 G §33, §540, A-191); an optional step that failed says nothing.
func TestAFailedRequiredStepIsTheFindingOfItsCommit(t *testing.T) {
for _, c := range []struct {
name, step, outcome string
author string
authorErr error
wantFinding bool
wantOwner string
}{
{"required failed", "lint", "failed", "alice", nil, true, "forge:alice"},
{"required could not run", "lint", "could not run", "alice", nil, true, "forge:alice"},
{"no pull request merged it", "lint", "failed", "", errors.New("not found"), true, ""},
{"optional failed", "unit", "failed", "alice", nil, false, ""},
{"required passed", "lint", "passed", "alice", nil, false, ""},
} {
t.Run(c.name, func(t *testing.T) {
h, store, _, _, _, _ := fixture()
p := store[pkey]
p.p.Repository = "novox/app"
store[pkey] = p
f := newHeld()
h.Failures = &Failures{Findings: f, Observer: "mesh-delivery-pipeline", Now: h.Now,
Author: func(repository, commit string) (string, error) {
if repository != "novox/app" || commit != "c1" {
t.Errorf("the author of %s in %s was asked", commit, repository)
}
return c.author, c.authorErr
}}
ask := map[string]string{"lint": "a1", "unit": "a2"}[c.step]
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended(ask, c.step, c.outcome)); err != nil {
t.Fatal(err)
}
got, ok := f.held["merge.c1.pipeline-failed"]
if ok != c.wantFinding {
t.Fatalf("finding written: %v, want %v (%v)", ok, c.wantFinding, f.held)
}
if !ok {
return
}
if got.Owner != c.wantOwner || got.Observer != "mesh-delivery-pipeline" || got.Subject.ID != "c1" {
t.Fatalf("finding %+v, want owner %q", got, c.wantOwner)
}
for _, name := range []string{"app", "main", "c1", "lint"} {
if !strings.Contains(got.Explanation, name) {
t.Errorf("the explanation does not name %q: %s", name, got.Explanation)
}
}
if c.wantOwner == "" && !strings.Contains(got.Explanation, "no owner") {
t.Errorf("an ownerless finding does not say why: %s", got.Explanation)
}
})
}
}
// The owner of a failed head whose author could not be read is written at the next step end of that commit (A-191).
func TestTheOwnerOfAFailedHeadIsWrittenAtTheNextStepEnd(t *testing.T) {
h, store, _, _, _, _ := fixture()
f := newHeld()
known := false
h.Failures = &Failures{Findings: f, Now: h.Now, Author: func(string, string) (string, error) {
if !known {
return "", errors.New("the forge did not answer")
}
return "bob", nil
}}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "failed")); err != nil {
t.Fatal(err)
}
if f.held["merge.c1.pipeline-failed"].Owner != "" {
t.Fatalf("an owner before the author is known: %+v", f.held)
}
known = true
p := store[pkey]
p.p.Steps[1].Status = seat.StepResultStatusRequired
store[pkey] = p
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a2", "unit", "failed")); err != nil {
t.Fatal(err)
}
if got := f.held["merge.c1.pipeline-failed"].Owner; got != "forge:bob" {
t.Fatalf("owner %q, want forge:bob", got)
}
}
// A newer revision of the branch passing the step that failed clears the finding of the older commit; one that does not pass
// it, and a newer head failing on its own, leave it, and the newer failing head is a finding of its own (§33, §540).
func TestANewerPassingRevisionClearsTheFinding(t *testing.T) {
h, store, _, _, _, _ := fixture()
f := newHeld()
h.Failures = &Failures{Findings: f, Now: h.Now, Author: func(string, string) (string, error) { return "alice", nil }}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "failed")); err != nil {
t.Fatal(err)
}
newer := func(source int64, commit, ask string) string {
key := "main." + strconv.FormatInt(source, 10) + ".app"
store[key] = struct {
p seat.Pipeline
rev uint64
}{seat.Pipeline{V: 1, Pipeline: key, Branch: "main", Source: source, Module: "app", Commit: commit, Fingerprint: "src2:aa",
Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{{Artifact: "program", Step: seat.StepResultStepLint,
Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: ask, Seat: nodebuildgo.Seat}}}, 1}
return key
}
// A newer head that fails the step too is a finding of its own, and the older one stays.
key := newer(6, "c2", "b1")
ev := ended("b1", "lint", "failed")
ev.Pipeline, ev.Commit = key, "c2"
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ev); err != nil {
t.Fatal(err)
}
if _, ok := f.held["merge.c1.pipeline-failed"]; !ok || len(f.held) != 2 {
t.Fatalf("findings %v, want c1 and c2", f.held)
}
// A newest head passing it clears both older commits' findings.
key = newer(7, "c3", "b2")
ev = ended("b2", "lint", "passed")
ev.Pipeline, ev.Commit = key, "c3"
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ev); err != nil {
t.Fatal(err)
}
if len(f.held) != 0 {
t.Fatalf("findings %v stand after a newer revision passed the step", f.held)
}
}