package api
import (
"strconv"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// trunkMoves adds a check-in to trunk on file, so every rung stands
// behind it.
func trunkMoves(t *testing.T, dir, file, body string, sync func() (int, error)) {
t.Helper()
zrun(t, dir, "update", "trunk")
zwrite(t, dir, file, body)
zrun(t, dir, "add", file)
zrun(t, dir, "commit", "-m", "trunk moves", "--user", "u")
if _, err := sync(); err != nil {
t.Fatal(err)
}
}
// landing.update says what it did: the merge it wrote and that the
// branch changed, then that a second call has nothing to do.
func TestUpdateAnswersTheMergeItWroteAndThenThatThereIsNothingToDo(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
before := ztip(t, dir, "low")
trunkMoves(t, dir, "t.txt", "t\n", sync)
got, err := r.Invoke(as("dan", "i"), "landing.update", landingSayIn{Repo: "test", ID: ids["low"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingUpdateOut)
now := ztip(t, dir, "low")
if now == before || !out.Changed || out.Hash != now || out.State != "open" || out.Say == "" {
t.Errorf("FAIL: the update answered %+v with the branch at %s (was %s)", out, now, before)
}
got, err = r.Invoke(as("dan", "i"), "landing.update", landingSayIn{Repo: "test", ID: ids["low"]})
if err != nil {
t.Fatalf("FAIL: a second update: %v", err)
}
again := got.(landingUpdateOut)
if again.Changed || again.Hash != "" || again.Say == "" || ztip(t, dir, "low") != now {
t.Errorf("FAIL: the second update answered %+v", again)
}
if !t.Failed() {
t.Log("ok")
}
}
// A press on a stack whose base stands behind trunk updates the base
// itself, runs the checks again and arms the stack; it does not stop
// and ask a person to press update.
func TestAStackWhoseBaseIsBehindIsUpdatedByThePress(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
d, _ := prefixDeps(t, dir, ix, sync, ci)
var runs []string
d.CIRunPair = func(_, branch, _, _ string) error { runs = append(runs, branch); return nil }
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingLandOut)
if ztip(t, dir, "low") == before {
t.Errorf("FAIL: the base was not updated")
}
if len(out.Landed) != 0 || len(out.Armed) != 1 || out.Armed[0] != ids["high"] {
t.Errorf("FAIL: the press answered %+v, want nothing landed and the top armed", out)
}
if !strings.Contains(out.Reason, "updated") {
t.Errorf("FAIL: the reason does not say the base was updated: %q", out.Reason)
}
if len(runs) == 0 {
t.Errorf("FAIL: no checks were run again after the update")
}
if !t.Failed() {
t.Log("ok")
}
}
// A conflict stops the walk, writes nothing, and the reason says so (the
// walk's preflight reads it before any update is tried).
func TestAStackWhoseBaseCannotBeUpdatedSaysWhy(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
trunkMoves(t, dir, "low.txt", "trunk wrote this\n", sync)
before := ztip(t, dir, "low")
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingLandOut)
if ztip(t, dir, "low") != before {
t.Errorf("FAIL: a conflicting update wrote to the base")
}
if out.Stopped != ids["low"] || !strings.Contains(out.Reason, "conflict") || len(out.Armed) != 0 {
t.Errorf("FAIL: the press answered %+v, want the base stopped on a conflict, nothing armed", out)
}
if !t.Failed() {
t.Log("ok")
}
}
// The armed sweep does the same for a rung that is not green.
func TestTheSweepUpdatesAnArmedBaseThatIsBehindAndNotGreen(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
store := &waitStub{waiting: map[string]string{}, cleared: map[string]bool{}}
for _, b := range []string{"low", "mid", "high"} {
store.rows = append(store.rows, armRow(t, dir, ids, b))
}
if landed := d.AutoAcceptSweep(store); len(landed) != 0 {
t.Errorf("FAIL: the sweep landed %v", landed)
}
if ztip(t, dir, "low") == before {
t.Errorf("FAIL: the sweep did not update the base")
}
if w := store.waiting[ids["low"]]; !strings.Contains(w, "updated") {
t.Errorf("FAIL: the base's autoWaiting does not say it was updated: %q", w)
}
// A second sweep reads the forge's own merge through the arm-lapse
// check: the arms were given the tips from before it.
d.AutoAcceptSweep(store)
if len(store.lapsed) != 0 {
t.Errorf("FAIL: the forge's own update lapsed an arm on the next sweep: %v", store.lapsed)
}
if !t.Failed() {
t.Log("ok")
}
}
func armRow(t *testing.T, dir string, ids map[string]string, branch string) oltp.AutoAccept {
t.Helper()
return oltp.AutoAccept{Repo: "test", Landing: ids[branch], Account: "dan", Tip: ztip(t, dir, branch)}
}
// denyJoin is a protection that admits nobody onto the target.
type denyJoin struct{ fakeProtect }
func (denyJoin) MayJoin(string, string, string) bool { return false }
// A press that is refused for protection writes nothing onto the rungs.
func TestARefusedPressOnAProtectedTargetWritesNoUpdate(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
d.Protect = denyJoin{}
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
reruns := 0
d.CIRunPair = func(_, _, _, _ string) error { reruns++; return nil }
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingLandOut)
if ztip(t, dir, "low") != before || reruns != 0 {
t.Errorf("FAIL: a refused press wrote a check-in or started runs (%d)", reruns)
}
if len(out.Landed) != 0 || out.Stopped == "" {
t.Errorf("FAIL: the press answered %+v, want a refusal", out)
}
if !t.Failed() {
t.Log("ok")
}
}
// A rung whose checks are still going is not updated; once they have
// finished it is, once.
func TestARungWithChecksStillRunningIsUpdatedOnlyWhenTheyFinish(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
d, _ := prefixDeps(t, dir, ix, sync, ci)
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
pair := "pair:" + before + ":" + ztip(t, dir, "trunk")
ci.runs[pair] = []CIRun{{Checkin: pair, Workflow: "ci", Pipeline: "ci", Status: "running"}}
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
press := func() {
t.Helper()
if _, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]}); err != nil {
t.Fatal(err)
}
}
press()
if ztip(t, dir, "low") != before {
t.Errorf("FAIL: a rung whose checks were running was updated")
}
ci.runs[pair][0].Status = "success"
press()
once := ztip(t, dir, "low")
if once == before {
t.Errorf("FAIL: the rung was not updated after its checks finished")
}
press()
if ztip(t, dir, "low") != once {
t.Errorf("FAIL: a rung no longer behind was updated again")
}
if !t.Failed() {
t.Log("ok")
}
}
// A busy trunk cannot keep a rung updating for ever: past updateCap in
// an hour the walk waits, and says why.
func TestABusyTrunkStopsTheUpdatesAtTheCap(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
store := &waitStub{waiting: map[string]string{}, cleared: map[string]bool{}}
for _, b := range []string{"low", "mid", "high"} {
store.rows = append(store.rows, armRow(t, dir, ids, b))
}
for i := 0; i < updateCap; i++ {
trunkMoves(t, dir, "t"+strconv.Itoa(i)+".txt", "t\n", sync)
before := ztip(t, dir, "low")
d.AutoAcceptSweep(store)
if ztip(t, dir, "low") == before {
t.Fatalf("FAIL: update %d was not made", i+1)
}
}
trunkMoves(t, dir, "tn.txt", "t\n", sync)
before := ztip(t, dir, "low")
d.AutoAcceptSweep(store)
if ztip(t, dir, "low") != before {
t.Errorf("FAIL: the rung was updated past the cap")
}
if w := store.waiting[ids["low"]]; !strings.Contains(w, "trunk keeps moving") || !strings.Contains(w, strconv.Itoa(updateCap)) {
t.Errorf("FAIL: autoWaiting reads %q", w)
}
if !t.Failed() {
t.Log("ok")
}
}
// A middle rung that conflicts with the rung below at update time stops
// the walk, with the conflict in autoWaiting, and writes nothing. The
// walk's preflight reads only the base, so this reaches the update.
func TestAMiddleRungThatConflictsAtUpdateTimeStopsAndWritesNothing(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
zrun(t, dir, "update", "low")
zwrite(t, dir, "mid.txt", "low wrote this first\n")
zrun(t, dir, "add", "mid.txt")
zrun(t, dir, "commit", "-m", "low takes mid's file", "--user", "u")
if _, err := sync(); err != nil {
t.Fatal(err)
}
before := ztip(t, dir, "mid")
store := &waitStub{waiting: map[string]string{}, cleared: map[string]bool{}}
for _, b := range []string{"mid", "high"} {
store.rows = append(store.rows, armRow(t, dir, ids, b))
}
d.AutoAcceptSweep(store)
if ztip(t, dir, "mid") != before {
t.Errorf("FAIL: a conflicting update wrote a check-in")
}
if w := store.waiting[ids["mid"]]; !strings.Contains(w, "merge cleanly") {
t.Errorf("FAIL: mid's autoWaiting does not state the conflict: %q", w)
}
if !t.Failed() {
t.Log("ok")
}
}