331 lines
17 KiB
Go
331 lines
17 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
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"
|
|
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-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
func stepSeatOf(t *testing.T, name string) StepSeat {
|
|
t.Helper()
|
|
for _, bs := range StepSeats {
|
|
if bs.Seat == name {
|
|
return bs
|
|
}
|
|
}
|
|
t.Fatalf("no step seat %s", name)
|
|
return StepSeat{}
|
|
}
|
|
|
|
type stepAsks struct {
|
|
at []string
|
|
ids []string
|
|
asks []any
|
|
}
|
|
|
|
func (s *stepAsks) AskStep(accept, id string, body any) error {
|
|
s.at, s.ids, s.asks = append(s.at, accept), append(s.ids, id), append(s.asks, body)
|
|
return nil
|
|
}
|
|
|
|
func placeFixture() (catalog.Registration, map[string]catalog.CatalogEntry) {
|
|
reg := catalog.Registration{Module: "app", Branches: []string{"main"}, Policy: catalog.Policy{
|
|
Steps: catalog.StepStatuses{Lint: "required", Unit: "optional", Approval: "required"},
|
|
Artifacts: map[string]catalog.StepStatuses{"tools": {Lint: "bypass"}},
|
|
}}
|
|
return reg, map[string]catalog.CatalogEntry{
|
|
"app": {Module: "app", Path: "modules/app", Artifacts: []catalog.Artifact{{Ref: "app:program", Language: "go"}, {Ref: "app:tools", Language: "go"},
|
|
{Ref: "app:web", Language: "rust"}}, Inputs: &catalog.EntryInputs{Paths: map[string][]string{"app:program": {"cmd/app"}}}},
|
|
"mesh-build-go": {Module: "mesh-build-go", Acts: []catalog.ActingRule{{Seat: "node-build-go", Step: "build", On: "go"}}},
|
|
}
|
|
}
|
|
|
|
// The runner's asking half (design 47 G §22, §24, §323; A-91): a pipeline's steps are the ones the policy names, a bypassed
|
|
// step asked of no one, an artifact no build seat names ended could not run naming it, and each running step asked of its
|
|
// build seat's accepted kind step with the pipeline's commit and fingerprint; a step the policy leaves without a status is optional (§553).
|
|
func TestStartPlacesTheStepsThePolicyNamesAndAsksEachBuildSeat(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
store := created{}
|
|
asks := &stepAsks{}
|
|
s := &Starter{Pipelines: store, Say: func(string, any) error { return nil }, Steps: asks, Name: "the-runner",
|
|
Registration: func(m string) (catalog.Registration, bool) { return reg, m == "app" }}
|
|
if ok, err := s.Start(Begin{Branch: "main", Source: 4, Module: "app", Commit: "c1", Fp: "src2:aa", Repository: "o/app", Entries: entries}); !ok || err != nil {
|
|
t.Fatalf("start = %v, %v", ok, err)
|
|
}
|
|
var p seat.Pipeline
|
|
if err := json.Unmarshal(store["main.4.app"], &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
for _, st := range p.Steps {
|
|
got = append(got, st.Artifact+" "+st.Step+" "+st.Outcome)
|
|
}
|
|
// §553: integration, composition and review are named by no policy, so they are optional steps and run.
|
|
want := []string{" build running", "app:program lint running", "app:program unit running", "app:program integration running",
|
|
"app:tools lint bypassed", "app:tools unit running", "app:tools integration running",
|
|
"app:web lint could not run", "app:web unit could not run", "app:web integration could not run",
|
|
" composition running", " review waiting", " approval waiting"}
|
|
if strings.Join(got, "|") != strings.Join(want, "|") {
|
|
t.Errorf("steps = %q, want %q", got, want)
|
|
}
|
|
if !strings.Contains(p.Steps[7].Why, `"rust"`) {
|
|
t.Errorf("the step of an artifact no seat builds says %q, want it to name the language", p.Steps[7].Why)
|
|
}
|
|
if len(asks.asks) != 7 {
|
|
t.Fatalf("asked %d steps, want the build, the five running build steps and the composition", len(asks.asks))
|
|
}
|
|
// A-297: every step's ask id is the usual ask id form the holders accept, opened by the pipeline's start time.
|
|
for i, id := range asks.ids {
|
|
if err := buildstep.CheckAskID(id); err != nil || !strings.HasPrefix(id, strconv.FormatInt(p.Started.UnixMilli(), 36)) {
|
|
t.Errorf("ask %d's id %q is not the usual form opened by the pipeline's start: %v", i, id, err)
|
|
}
|
|
}
|
|
// A-99: the build is every pipeline's first step, required whatever the policy names, asked of mesh-builds for the
|
|
// module's own entry only.
|
|
b, ok := asks.asks[0].(builds.StepRun)
|
|
if !ok || asks.at[0] != builds.AcceptSubjectStep || p.Steps[0].Status != seat.StepResultStatusRequired || asks.ids[0] != p.Steps[0].Ask ||
|
|
len(b.Entries) != 1 || b.Entries[0] != (builds.BuildEntry{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}) || b.Pipeline != "main.4.app" {
|
|
t.Errorf("the build ask = %+v at %s", asks.asks[0], asks.at[0])
|
|
}
|
|
a := asks.asks[1].(nodebuildgo.StepAsk)
|
|
if asks.at[1] != "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner" || a.Kind != "step" || a.Asker != "the-runner" || a.ID != p.Steps[1].Ask || asks.ids[1] != a.ID ||
|
|
a.Step.Commit != "c1" || a.Step.Fingerprint != "src2:aa" || a.Step.Pipeline != "main.4.app" || a.Step.Artifact != "app:program" ||
|
|
a.Step.Step != "lint" || a.Step.Fetch.Repository != "o/app" || a.Step.Fetch.Dir != "modules/app" || a.Step.Revision != 4 ||
|
|
len(a.Step.Fetch.Paths) != 1 || a.Step.Fetch.Paths[0] != "cmd/app" {
|
|
t.Errorf("the ask = %+v at %s", a, asks.at[1])
|
|
}
|
|
// An artifact that names no paths still fetches its module alone: the directory with no paths (principle 10, A-447).
|
|
for _, x := range asks.asks[2:] {
|
|
if b, ok := x.(nodebuildgo.StepAsk); ok && b.Step != nil && b.Step.Artifact == "app:tools" && (b.Step.Fetch.Dir != "modules/app" || len(b.Step.Fetch.Paths) != 0) {
|
|
t.Errorf("the tools ask fetches %+v, want the module's directory and no paths", b.Step.Fetch)
|
|
}
|
|
}
|
|
// A second start of the revision asks nothing.
|
|
if ok, err := s.Start(Begin{Branch: "main", Source: 4, Module: "app", Commit: "c1", Entries: entries}); ok || err != nil || len(asks.asks) != 7 {
|
|
t.Errorf("second start = %v, %v, %d asks", ok, err, len(asks.asks))
|
|
}
|
|
}
|
|
|
|
// A pipeline whose other steps are all bypassed still has its build, required whatever the policy names, so its approval is
|
|
// not due at its start (A-99, §26).
|
|
func TestTheBuildHoldsTheApprovalWhateverThePolicyNames(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
reg.Policy = catalog.Policy{Steps: catalog.StepStatuses{Lint: "bypass", Unit: "bypass", Approval: "required"}}
|
|
store := created{}
|
|
s := &Starter{Pipelines: store, Say: func(string, any) error { return nil }, Steps: &stepAsks{},
|
|
Registration: func(string) (catalog.Registration, bool) { return reg, true }}
|
|
if _, err := s.Start(Begin{Branch: "main", Source: 4, Module: "app", Commit: "c1", Fp: "src2:aa", Entries: entries}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var p seat.Pipeline
|
|
_ = json.Unmarshal(store["main.4.app"], &p)
|
|
if p.Steps[0].Step != seat.StepResultStepBuild || p.Steps[0].Status != seat.StepResultStatusRequired || approvalDue(p) != -1 {
|
|
t.Errorf("steps = %+v; want the build first, required, and no approval due", p.Steps)
|
|
}
|
|
}
|
|
|
|
// The seat mesh-builds' end of the build step is taken as the others are, held to its one entry's module, commit and
|
|
// fingerprint; an end naming two entries is refused (A-97, A-99).
|
|
func TestTheBuildStepsEndIsTakenOnItsOneEntry(t *testing.T) {
|
|
bs := stepSeatOf(t, builds.Seat)
|
|
h, store, _, _, _, _ := fixture()
|
|
e := store[pkey]
|
|
e.p.Steps = append([]seat.StepResult{{Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning,
|
|
Ask: "b1", Seat: builds.Seat}}, e.p.Steps...)
|
|
store[pkey] = e
|
|
raw := `{"id":"b1","pipeline":"main.5.app","step":"build","node":"n1","outcome":"passed","entries":[{"module":"app","commit":"c1","fingerprint":"src2:aa"}]}`
|
|
ev, err := bs.Decode([]byte(raw))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := h.OnStepEnded(bs, bs.Prefix+"n1", ev)
|
|
if err != nil || p.Steps[0].Outcome != seat.StepResultOutcomePassed {
|
|
t.Fatalf("the build end: %v, step %+v", err, p.Steps[0])
|
|
}
|
|
if _, err := bs.Decode([]byte(`{"id":"b1","entries":[{"module":"a","fingerprint":"x"},{"module":"b","fingerprint":"y"}]}`)); err == nil {
|
|
t.Error("an end of two entries is refused")
|
|
}
|
|
}
|
|
|
|
// Composition is asked of the seat mesh-wants with the other steps, running at once, and its end is heard from that seat
|
|
// (design 47 G §22, §24): a pipeline whose policy names it places it running with its own ask, a bypassed one asks no one.
|
|
func TestCompositionIsAskedOfTheAssignerAndItsEndIsTaken(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
reg.Policy = catalog.Policy{Steps: catalog.StepStatuses{Lint: "bypass", Unit: "bypass", Composition: "required", Approval: "required"}}
|
|
p := seat.Pipeline{Pipeline: "main.4.app", Branch: "main", Module: "app", Commit: "c1", Fingerprint: "src2:aa", Revision: 7, Source: 4}
|
|
steps, asked := Place(p, reg, entries, time.Unix(1, 0), "the-runner")
|
|
var comp seat.StepResult
|
|
for _, s := range steps {
|
|
if s.Step == seat.StepResultStepComposition {
|
|
comp = s
|
|
}
|
|
}
|
|
if comp.Outcome != seat.StepResultOutcomeRunning || comp.Seat != wants.Seat || comp.Ask == "" {
|
|
t.Fatalf("composition = %+v, want running, asked of mesh-wants", comp)
|
|
}
|
|
var got *Asked
|
|
for i := range asked {
|
|
if asked[i].Seat == wants.Seat {
|
|
got = &asked[i]
|
|
}
|
|
}
|
|
r, ok := got.Ask.(wants.StepRun)
|
|
if got == nil || !ok || got.ID != comp.Ask || acceptStep[wants.Seat] != wants.AcceptSubjectStep || r.Step != "composition" ||
|
|
r.Commit != "c1" || r.Fingerprint != "src2:aa" || r.Pipeline != "main.4.app" || r.Revision != 7 || r.Source != 4 || r.Module != "app" {
|
|
t.Fatalf("the composition ask = %+v", got)
|
|
}
|
|
// Its end, from the seat's own subject, is taken like the others, a node's warning becoming the step's why.
|
|
bs := stepSeatOf(t, wants.Seat)
|
|
ev, err := bs.Decode([]byte(`{"id":"` + comp.Ask + `","pipeline":"main.5.app","step":"composition","module":"app","commit":"c1","fingerprint":"src2:aa","node":"n1","outcome":"passed","warnings":["n2 asleep"]}`))
|
|
if err != nil || ev.Said != "n2 asleep" || ev.Outcome != "passed" || ev.Module != "app" {
|
|
t.Fatalf("decoded end = %+v, %v", ev, err)
|
|
}
|
|
}
|
|
|
|
// A runner restarted after it wrote a pipeline and before its asks reached the queues asks them again when the branch's
|
|
// view holds its half: each step placed and still running, of a running pipeline of that branch, under the id it was
|
|
// placed under; an ended step, an ended pipeline and another branch's pipeline are asked of no one (design 47 G §88).
|
|
func TestResumeAsksTheStepsPlacedAndNotEnded(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
started := time.Unix(1, 0) // A-297: a step's ask id opens with its pipeline's start, so resuming later places it under the same id
|
|
placed := func(branch string, source int64) seat.Pipeline {
|
|
p := seat.Pipeline{Pipeline: Key(branch, source, "app"), Branch: branch, Source: source, Module: "app", Commit: "c1", Fingerprint: "src2:aa",
|
|
Repository: "o/app", Outcome: seat.PipelineOutcomeRunning, Started: &started}
|
|
p.Steps, _ = Place(p, reg, entries, started, "the-runner")
|
|
return p
|
|
}
|
|
main4, main5, beta6 := placed("main", 4), placed("main", 5), placed("beta", 6)
|
|
main5.Steps[1].Outcome = seat.StepResultOutcomePassed // the build of main.5 ended
|
|
ended := placed("main", 3)
|
|
ended.Outcome = seat.PipelineOutcomePassed
|
|
store := memStore{}
|
|
for _, p := range []seat.Pipeline{main4, main5, beta6, ended} {
|
|
store[p.Pipeline] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{p, 1}
|
|
}
|
|
asks := &stepAsks{}
|
|
s := &Starter{Steps: asks, Name: "the-runner", Registration: func(string) (catalog.Registration, bool) { return reg, true }}
|
|
n, err := s.Resume(store, "main", Half{Entries: entries})
|
|
// main.4 has the build, five running build steps and the composition of the fixture; main.5 one fewer, its build ended.
|
|
if err != nil || n != 13 || len(asks.asks) != 13 {
|
|
t.Fatalf("resume asked %d (%d put), %v; want 13", n, len(asks.asks), err)
|
|
}
|
|
for _, id := range asks.ids {
|
|
found := false
|
|
for _, p := range []seat.Pipeline{main4, main5} {
|
|
for _, st := range p.Steps {
|
|
found = found || (st.Ask == id && st.Outcome == seat.StepResultOutcomeRunning)
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("ask %s is of no running step of the branch's running pipelines", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A step ask carries the kept results of its artifact's package and the toolchain of its language (design 47 G §25, §416,
|
|
// A-104): the results whose package is the artifact's package or lies beneath it, read once for all the asks; a failed
|
|
// read asks the steps with none.
|
|
func TestAStepAskCarriesTheKeptResultsAndTheToolchain(t *testing.T) {
|
|
reg, entries := placeFixture()
|
|
e := entries["app"]
|
|
e.Artifacts[0].Package = &catalog.ArtifactPackage{Ecosystem: "go", Name: "git.example/o/app"}
|
|
entries["app"] = e
|
|
store, asks := created{}, &stepAsks{}
|
|
reads := 0
|
|
s := &Starter{Pipelines: store, Say: func(string, any) error { return nil }, Steps: asks, Name: "the-runner",
|
|
Registration: func(string) (catalog.Registration, bool) { return reg, true },
|
|
Kept: func(pkgs []string) (map[string][]nodebuildgo.KeptResult, error) {
|
|
reads++
|
|
if len(pkgs) != 1 || pkgs[0] != "git.example/o/app" {
|
|
t.Errorf("kept read for %v, want the one package of the entry", pkgs)
|
|
}
|
|
return map[string][]nodebuildgo.KeptResult{pkgs[0]: {{Digest: "sha256:aa", Level: "unit", Package: "git.example/o/app/sub"}}}, nil
|
|
},
|
|
Toolchain: func(l string) (*nodebuildgo.ToolchainUsed, error) {
|
|
return &nodebuildgo.ToolchainUsed{Language: l, Version: "1.26"}, nil
|
|
}}
|
|
if _, err := s.Start(Begin{Branch: "main", Source: 4, Module: "app", Commit: "c1", Fp: "src2:aa", Repository: "o/app", Entries: entries}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var program, tools *nodebuildgo.StepRun
|
|
for _, a := range asks.asks {
|
|
if ask, ok := a.(nodebuildgo.StepAsk); ok {
|
|
switch {
|
|
case ask.Step.Artifact == "app:program" && ask.Step.Step == "lint":
|
|
program = ask.Step
|
|
case ask.Step.Artifact == "app:tools" && ask.Step.Step == "unit":
|
|
tools = ask.Step
|
|
}
|
|
}
|
|
}
|
|
if reads != 1 || program == nil || len(program.Kept) != 1 || program.Kept[0].Digest != "sha256:aa" || program.Toolchain == nil || program.Toolchain.Version != "1.26" {
|
|
t.Errorf("the program's ask = %+v after %d reads", program, reads)
|
|
}
|
|
if tools == nil || len(tools.Kept) != 0 || tools.Toolchain == nil {
|
|
t.Errorf("an artifact with no package carries no kept result and its language's toolchain: %+v", tools)
|
|
}
|
|
for pkg, want := range map[string]bool{"a/b": true, "a/b/c": true, "a/bc": false, "a": false} {
|
|
if InPackage(pkg, "a/b") != want {
|
|
t.Errorf("InPackage(%s, a/b) = %v, want %v", pkg, !want, want)
|
|
}
|
|
}
|
|
if tc, err := ToolchainOf([]byte(`{"language":"go","version":"1.26.1","digests":[]}`)); err != nil || tc.Language != "go" || tc.Version != "1.26.1" {
|
|
t.Errorf("ToolchainOf = %+v, %v", tc, err)
|
|
}
|
|
}
|
|
|
|
// An artifact without a language is placed by its kind (design 47 G §324, §397, A-324, A-325): an image is asked of the build
|
|
// seat whose acting rule build is on kind:image, the form the seat definitions write; an archive's and an upstream
|
|
// artifact's lint, unit and integration are bypassed whatever the policy says, asked of no one; an artifact whose language no
|
|
// build seat serves stays could not run, naming it (§535).
|
|
func TestAnArtifactWithoutALanguageIsPlacedByItsKind(t *testing.T) {
|
|
reg := catalog.Registration{Module: "app", Policy: catalog.Policy{Steps: catalog.StepStatuses{Lint: "required", Unit: "required", Integration: "required"}}}
|
|
entries := map[string]catalog.CatalogEntry{
|
|
"app": {Module: "app", Artifacts: []catalog.Artifact{{Ref: "app:server", Kind: "image"}, {Ref: "app:files", Kind: "archive"},
|
|
{Ref: "app:mirror", Kind: "upstream"}, {Ref: "app:web", Language: "rust"}}},
|
|
"mesh-build-image": {Module: "mesh-build-image", Acts: []catalog.ActingRule{{Seat: "node-build-image", Step: "build", On: seatdef.OnKindImage}}},
|
|
}
|
|
steps, asks := Place(seat.Pipeline{Pipeline: "main.4.app", Module: "app"}, reg, entries, time.Unix(0, 0), "the-runner")
|
|
want := map[string]struct{ outcome, seat, status string }{
|
|
"app:server": {seat.StepResultOutcomeRunning, "node-build-image", "required"},
|
|
"app:files": {seat.StepResultOutcomeBypassed, "", seat.StepResultStatusBypass},
|
|
"app:mirror": {seat.StepResultOutcomeBypassed, "", seat.StepResultStatusBypass},
|
|
"app:web": {seat.StepResultOutcomeCouldNotRun, "", "required"},
|
|
}
|
|
n := 0
|
|
for _, s := range steps {
|
|
w, ok := want[s.Artifact]
|
|
if !ok {
|
|
continue
|
|
}
|
|
n++
|
|
if s.Outcome != w.outcome || s.Seat != w.seat || s.Status != w.status {
|
|
t.Errorf("%s %s is %s/%s at %q (%s), want %s/%s at %q", s.Artifact, s.Step, s.Status, s.Outcome, s.Seat, s.Why, w.status, w.outcome, w.seat)
|
|
}
|
|
}
|
|
if n != 12 {
|
|
t.Errorf("placed %d build steps of the four artifacts, want 12", n)
|
|
}
|
|
for _, a := range asks {
|
|
if a.Seat != builds.Seat && a.Seat != wants.Seat && a.Seat != "node-build-image" {
|
|
t.Errorf("asked %s, want only the build, the composition and the image's build seat", a.Seat)
|
|
}
|
|
}
|
|
if len(asks) != 5 {
|
|
t.Errorf("asked %d, want the build, the image's three steps and the composition", len(asks))
|
|
}
|
|
}
|