290 lines
13 KiB
Go
290 lines
13 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
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"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
type actLog []string
|
|
|
|
func (a *actLog) RecordHandAct(h views.HandAct) error {
|
|
*a = append(*a, h.Verb+": "+h.What+" / "+h.Why)
|
|
return nil
|
|
}
|
|
|
|
// verbsFixture is a running pipeline of app placed from a policy with a required lint, an optional unit, an optional review
|
|
// and a required approval, in a store, with the verbs over it.
|
|
func verbsFixture() (Verbs, memStore, *[]string, *stepAsks, *actLog, *[]string) {
|
|
reg, entries := placeFixture()
|
|
reg.Policy.Steps.Review = "optional"
|
|
h, store, said, _, _, _ := fixture()
|
|
p := seat.Pipeline{Pipeline: pkey, Branch: "main", Source: 5, Module: "app", Commit: "c1", Fingerprint: "src2:aa", Repository: "o/app",
|
|
Outcome: seat.PipelineOutcomeRunning}
|
|
p.Steps, _ = Place(p, reg, entries, time.Unix(1, 0), "the-runner")
|
|
store[pkey] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{p, 1}
|
|
asks, acts, dropped := &stepAsks{}, &actLog{}, &[]string{}
|
|
s := &Starter{Steps: asks, Name: "the-runner", Registration: func(string) (catalog.Registration, bool) { return reg, true },
|
|
Now: h.Now}
|
|
return Verbs{Hearer: h, Starter: s, View: func(string) (Half, bool) { return Half{Entries: entries}, true }, Acts: acts,
|
|
Drop: func(ask string) error { *dropped = append(*dropped, ask); return nil }}, store, said, asks, acts, dropped
|
|
}
|
|
|
|
func stepNamed(p seat.Pipeline, artifact, step string) seat.StepResult {
|
|
for _, s := range p.Steps {
|
|
if s.Artifact == artifact && s.Step == step {
|
|
return s
|
|
}
|
|
}
|
|
return seat.StepResult{}
|
|
}
|
|
|
|
// A code review ends the placed review step of the pipeline at its commit and fingerprint, whoever its author, recording the
|
|
// caller the bus proved in the step's result, its step-ended and the pipeline's show; a review no pipeline waits on is refused
|
|
// (design 47 G §70, §482).
|
|
func TestReviewEndsThePlacedReviewStepWithItsProvenCaller(t *testing.T) {
|
|
v, store, said, _, _, _ := verbsFixture()
|
|
in := seat.ReviewInput{Module: "app", Commit: "c1", Fingerprint: "src2:aa", Outcome: "passed", Said: "reads well"}
|
|
for name, bad := range map[string]seat.ReviewInput{
|
|
"another commit": {Module: "app", Commit: "c2", Fingerprint: "src2:aa", Outcome: "passed"},
|
|
"another fingerprint": {Module: "app", Commit: "c1", Fingerprint: "src2:bb", Outcome: "passed"},
|
|
"an outcome review lacks": {Module: "app", Commit: "c1", Fingerprint: "src2:aa", Outcome: "waiting"},
|
|
} {
|
|
if _, err := v.Review("agent.dev", bad); err == nil {
|
|
t.Errorf("%s: a review was taken", name)
|
|
}
|
|
}
|
|
a, err := v.Review("agent.dev", in)
|
|
if err != nil || a.Pipeline != pkey || a.Result.Outcome != "passed" || a.Result.By != "agent.dev" {
|
|
t.Fatalf("review = %+v, %v", a, err)
|
|
}
|
|
got := stepNamed(store[pkey].p, "", seat.StepResultStepReview)
|
|
if got.Outcome != "passed" || got.By != "agent.dev" || got.Why != "reads well" || got.Ended == nil {
|
|
t.Errorf("the review step = %+v", got)
|
|
}
|
|
if len(*said) != 1 || !strings.HasPrefix((*said)[0], "step-ended.") {
|
|
t.Errorf("said %v, want one step-ended", *said)
|
|
}
|
|
if _, err := v.Review("agent.dev", in); err == nil {
|
|
t.Error("a second review of an ended review step was taken")
|
|
}
|
|
}
|
|
|
|
// accept takes only a failed optional step, writes its hand act first, and refuses without one; rerun asks the step again
|
|
// under a new ask saying it is a rerun and its caller, reopens a failed pipeline and asks nothing of a refused one; cancel ends
|
|
// a running pipeline cancelled, each step cancelled and each ask's bound dropped (design 47 G §22, §30, §325, §394).
|
|
func TestAcceptRerunAndCancelWriteByCompareAndSetAndRecordTheirHandAct(t *testing.T) {
|
|
v, store, said, asks, acts, dropped := verbsFixture()
|
|
end := func(step, outcome string) {
|
|
p, rev, _ := store.Get(pkey)
|
|
for i := range p.Steps {
|
|
if p.Steps[i].Artifact == "app:program" && p.Steps[i].Step == step {
|
|
p.Steps[i].Outcome = outcome
|
|
}
|
|
}
|
|
store.Update(pkey, p, rev)
|
|
}
|
|
end("unit", seat.StepResultOutcomeFailed)
|
|
if _, err := v.Accept("operator", seat.AcceptInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "x"}); err == nil {
|
|
t.Error("a running required step was accepted")
|
|
}
|
|
if _, err := v.Accept("operator", seat.AcceptInput{Pipeline: pkey, Step: "unit", Artifact: "app:program"}); err == nil {
|
|
t.Error("an accept with no why was taken")
|
|
}
|
|
a, err := v.Accept("operator", seat.AcceptInput{Pipeline: pkey, Step: "unit", Artifact: "app:program", Why: "flaky"})
|
|
if err != nil || a.Result.Outcome != "accepted" || a.Result.Accepted == nil || a.Result.Accepted.By != "operator" || a.Result.Accepted.Why != "flaky" {
|
|
t.Fatalf("accept = %+v, %v", a, err)
|
|
}
|
|
if len(*acts) != 1 || !strings.Contains((*acts)[0], "mesh-pipeline-runner.accept") || !strings.Contains((*acts)[0], "flaky") {
|
|
t.Errorf("hand acts = %v", *acts)
|
|
}
|
|
// A rerun of the lint step: a failed pipeline runs again.
|
|
end("lint", seat.StepResultOutcomeFailed)
|
|
p, rev, _ := store.Get(pkey)
|
|
p.Outcome = seat.PipelineOutcomeFailed
|
|
store.Update(pkey, p, rev)
|
|
old := stepNamed(p, "app:program", "lint").Ask
|
|
r, err := v.Rerun("seat:mesh-healers", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"})
|
|
if err != nil || r.Result == nil || r.Result.Outcome != "running" || r.Result.Ask == old || !r.Result.Rerun || r.Result.By != "seat:mesh-healers" {
|
|
t.Fatalf("rerun = %+v, %v", r.Result, err)
|
|
}
|
|
if len(asks.asks) != 1 || asks.ids[0] != r.Result.Ask {
|
|
t.Fatalf("asked %v under %v, want one ask under %s", asks.asks, asks.ids, r.Result.Ask)
|
|
}
|
|
if b, ok := asks.asks[0].(nodebuildgo.StepAsk); !ok || b.ID != r.Result.Ask || b.Step == nil || !b.Step.Rerun || b.Step.By != "seat:mesh-healers" || b.Step.Step != "lint" {
|
|
t.Errorf("the ask = %+v", asks.asks[0])
|
|
}
|
|
if got, _, _ := store.Get(pkey); got.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Errorf("the failed pipeline is %s, want running again", got.Outcome)
|
|
}
|
|
if _, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "again"}); err == nil {
|
|
t.Error("a running step was asked again")
|
|
}
|
|
if _, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "approval", Why: "again"}); err == nil {
|
|
t.Error("an approval was asked again by rerun")
|
|
}
|
|
// A cancel ends the pipeline cancelled and drops the bounds of the asks still held.
|
|
c, err := v.Cancel("operator", seat.CancelInput{Pipeline: pkey, Why: "wrong branch"})
|
|
if err != nil || c.Pipeline.Outcome != "cancelled" || c.Pipeline.EndCount != 1 || len(*dropped) == 0 {
|
|
t.Fatalf("cancel = %+v, %v, dropped %v", c.Pipeline, err, *dropped)
|
|
}
|
|
if s := stepNamed(c.Pipeline, "app:program", "lint"); s.Outcome != "cancelled" || s.By != "operator" {
|
|
t.Errorf("the lint step = %+v", s)
|
|
}
|
|
if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: pkey, Why: "again"}); err == nil {
|
|
t.Error("a cancelled pipeline was cancelled again")
|
|
}
|
|
if _, err := v.Rerun("operator", seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "again"}); err == nil {
|
|
t.Error("a step of a cancelled pipeline was asked again")
|
|
}
|
|
last := (*said)[len(*said)-1]
|
|
if !strings.HasPrefix(last, "pipeline-ended.") {
|
|
t.Errorf("last said %q, want pipeline-ended", last)
|
|
}
|
|
if n := len(*acts); n != 3 {
|
|
t.Errorf("%d hand acts recorded, want accept, rerun, cancel (3): %v", n, *acts)
|
|
}
|
|
}
|
|
|
|
// A-222: a rerun of a run's step asks with the candidate's catalog half (its entries, here the directory), never the branch's,
|
|
// and is refused, nothing asked, when the candidate cannot be read.
|
|
func TestRerunOfARunAsksWithItsCandidatesHalf(t *testing.T) {
|
|
v, store, _, asks, _, _ := verbsFixture()
|
|
p, rev, _ := store.Get(pkey)
|
|
p.Run, p.Branch, p.Revision = "0a1b2c3d4e5f", "fix/1", 12
|
|
for i := range p.Steps {
|
|
if p.Steps[i].Step == "lint" && p.Steps[i].Artifact == "app:program" {
|
|
p.Steps[i].Outcome = seat.StepResultOutcomeFailed
|
|
}
|
|
}
|
|
store.Update(pkey, p, rev)
|
|
_, cand := placeFixture()
|
|
e := cand["app"]
|
|
e.Path = "candidate/app"
|
|
cand["app"] = e
|
|
var read struct {
|
|
group, branch string
|
|
revision int64
|
|
}
|
|
v.Candidate = func(group, branch string, revision int64) (Half, error) {
|
|
read.group, read.branch, read.revision = group, branch, revision
|
|
return Half{Revision: revision, Entries: cand}, nil
|
|
}
|
|
in := seat.RerunInput{Pipeline: pkey, Step: "lint", Artifact: "app:program", Why: "retry"}
|
|
if _, err := v.Rerun("operator", in); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, ok := asks.asks[0].(nodebuildgo.StepAsk); !ok || b.Step == nil || b.Step.Fetch.Dir != "candidate/app" {
|
|
t.Errorf("the ask = %+v, want the candidate's directory", asks.asks[0])
|
|
}
|
|
if read.group != "run-0a1b2c3d4e5f" || read.branch != "fix/1" || read.revision != 12 {
|
|
t.Errorf("the candidate read = %+v", read)
|
|
}
|
|
p, rev, _ = store.Get(pkey)
|
|
p.Steps[stepIndex(p, "lint", "app:program")].Outcome = seat.StepResultOutcomeFailed
|
|
store.Update(pkey, p, rev)
|
|
v.Candidate = func(string, string, int64) (Half, error) { return Half{}, errors.New("no answer") }
|
|
n := len(asks.asks)
|
|
if _, err := v.Rerun("operator", in); err == nil || len(asks.asks) != n {
|
|
t.Errorf("a rerun with no candidate read: %v, asked %d more", err, len(asks.asks)-n)
|
|
}
|
|
}
|
|
|
|
func stepIndex(p seat.Pipeline, step, artifact string) int {
|
|
for i, s := range p.Steps {
|
|
if s.Step == step && s.Artifact == artifact {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// A hand act the log does not take is a verb that does nothing (ADR 0565 clause 11).
|
|
func TestAVerbWhoseHandActIsNotRecordedDoesNothing(t *testing.T) {
|
|
v, store, _, _, _, _ := verbsFixture()
|
|
v.Acts = failingActs{}
|
|
if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: pkey, Why: "x"}); err == nil {
|
|
t.Fatal("cancel did something")
|
|
}
|
|
if p, _, _ := store.Get(pkey); p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Errorf("the pipeline is %s", p.Outcome)
|
|
}
|
|
}
|
|
|
|
type failingActs struct{}
|
|
|
|
func (failingActs) RecordHandAct(views.HandAct) error { return errors.New("no holder") }
|
|
|
|
// RN14 (§306, §394): start starts by hand the pipeline of a watched branch's head, recorded in the hand-act log, and a head
|
|
// already started is refused; ask-again asks a declined approval once more under an ask of its own, recorded, and an
|
|
// approval not declined is refused.
|
|
func TestStartAndAskAgain(t *testing.T) {
|
|
v, store, _, _, acts, _ := verbsFixture()
|
|
made := created{}
|
|
v.Starter.Pipelines, v.Starter.Say = made, func(string, any) error { return nil }
|
|
_, entries := placeFixture()
|
|
v.View = func(string) (Half, bool) { return Half{Source: 7, Revision: 3, Entries: entries}, true }
|
|
a, err := v.Start("operator", seat.StartInput{Branch: "main", Module: "app", Why: "no pipeline for the head"})
|
|
if err != nil || a.Pipeline != "main.7.app" || made["main.7.app"] == nil {
|
|
t.Fatalf("start = %+v, %v; written %v", a, err, len(made))
|
|
}
|
|
if _, err := v.Start("operator", seat.StartInput{Branch: "main", Module: "app", Why: "again"}); err == nil {
|
|
t.Error("a second start of the same head was not refused")
|
|
}
|
|
if _, err := v.Start("operator", seat.StartInput{Branch: "main", Module: "absent", Why: "x"}); err == nil {
|
|
t.Error("a start of a module the half does not hold was not refused")
|
|
}
|
|
if len(*acts) != 2 || !strings.HasPrefix((*acts)[0], "mesh-pipeline-runner.start: ") {
|
|
t.Errorf("hand acts %v, want the two starts recorded", *acts)
|
|
}
|
|
|
|
// ask-again: the approval of the fixture's pipeline is declined; asked again it is asked under a new ask.
|
|
h := v.Hearer
|
|
asker := h.Asker.(*askRecorder)
|
|
e := store[pkey]
|
|
first, err := ApprovalAsk(e.p, time.Unix(1, 0))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := range e.p.Steps {
|
|
s := &e.p.Steps[i]
|
|
switch s.Step {
|
|
case seat.StepResultStepApproval:
|
|
s.Outcome, s.Ask = seat.StepResultOutcomeDeclined, first.ID
|
|
default:
|
|
s.Outcome = seat.StepResultOutcomePassed
|
|
}
|
|
}
|
|
e.p.Outcome = seat.PipelineOutcomeFailed
|
|
store[pkey] = e
|
|
if _, err := v.AskAgain("operator", seat.AskAgainInput{Pipeline: pkey}); err == nil {
|
|
t.Error("an ask-again saying no why was taken")
|
|
}
|
|
got, err := v.AskAgain("operator", seat.AskAgainInput{Pipeline: pkey, Why: "asked by mistake"})
|
|
if err != nil || got.Result.Outcome != seat.StepResultOutcomeRunning || got.Result.Ask == "" || got.Result.Ask == first.ID || !got.Result.Rerun {
|
|
t.Fatalf("ask-again = %+v, %v", got, err)
|
|
}
|
|
if len(asker.asked) != 1 || asker.asked[0].ID != got.Result.Ask {
|
|
t.Errorf("asked %v, want one ask %s", asker.asked, got.Result.Ask)
|
|
}
|
|
if store[pkey].p.Outcome != seat.PipelineOutcomeRunning {
|
|
t.Errorf("the pipeline is %s after ask-again, want running", store[pkey].p.Outcome)
|
|
}
|
|
// The ask rebuilt from the step, as a warrant is held to it, is the ask asked.
|
|
if again, err := ApprovalAsk(store[pkey].p, *stepNamed(store[pkey].p, "", seat.StepResultStepApproval).Started); err != nil || again.ID != got.Result.Ask {
|
|
t.Errorf("the ask rebuilt = %s, %v; want %s", again.ID, err, got.Result.Ask)
|
|
}
|
|
if _, err := v.AskAgain("operator", seat.AskAgainInput{Pipeline: pkey, Why: "twice"}); err == nil {
|
|
t.Error("an approval running was asked again")
|
|
}
|
|
}
|