Skip to content
quarry Artifact [bc51ef6e04]
Login

Artifact [bc51ef6e04]

Artifact bc51ef6e0480da65556f7349505b9c3e200eb8463ef3e5f7c4aadd9553dd7cfc:


package oltp

import "testing"

// #302: a run launched again for the same check-in and pipeline is a
// re-run of the one before it. The relation is read off the recorded
// runs, both ways, and never crosses a pipeline, a check-in or a pair.
func TestCIRunsCarryTheirRerunChain(t *testing.T) {
	db := testDB(t)
	add := func(id, checkin, pair, pipeline string) {
		t.Helper()
		if err := db.AddCIRun(CIRun{RunID: id, Repo: "r", Checkin: checkin, Pair: pair,
			Workflow: "w", Pipeline: pipeline, Status: "running"}); err != nil {
			t.Fatal(err)
		}
	}
	add("a1", "c1", "", "ci/a.json")
	add("b1", "c1", "", "ci/b.json")
	add("a2", "c1", "", "ci/a.json")
	add("x1", "c2", "", "ci/a.json")
	add("a3", "c1", "", "ci/a.json")
	add("p1", "pair:c1:t", "pair:c1:t", "ci/a.json")

	want := map[string][2]string{ // run -> {rerun of, rerun as}
		"a1": {"", "a2"}, "a2": {"a1", "a3"}, "a3": {"a2", ""},
		"b1": {"", ""}, "x1": {"", ""}, "p1": {"", ""},
	}
	for id, w := range want {
		r, ok, err := db.CIRunByID("r", id)
		if err != nil || !ok {
			t.Fatalf("FAIL: run %s: %v %v", id, ok, err)
		}
		if r.RerunOf != w[0] || r.RerunAs != w[1] {
			t.Errorf("FAIL: run %s rerun of %q as %q, want of %q as %q", id, r.RerunOf, r.RerunAs, w[0], w[1])
		}
	}
	rows, err := db.CIRuns("r", 10)
	if err != nil {
		t.Fatal(err)
	}
	for _, r := range rows {
		if r.RunID == "a2" && (r.RerunOf != "a1" || r.RerunAs != "a3") {
			t.Errorf("FAIL: a listed run lost its chain: %+v", r)
		}
	}
}

// #499: a rerun asked for by name records the run it repeats, and that
// record wins over the order the runs were launched in. The original is
// left as it was.
func TestCIRunRerunOfIsPersistedAndLeavesTheOriginal(t *testing.T) {
	db := testDB(t)
	add := func(id, of string) {
		t.Helper()
		if err := db.AddCIRun(CIRun{RunID: id, Repo: "r", Checkin: "c1",
			Workflow: "w", Pipeline: "ci/a.json", Status: "completed", RerunOf: of}); err != nil {
			t.Fatal(err)
		}
	}
	add("a1", "")
	add("a2", "")
	before, _, _ := db.CIRunByID("r", "a1")
	add("a3", "a1")
	a3, _, _ := db.CIRunByID("r", "a3")
	a1, _, _ := db.CIRunByID("r", "a1")
	a2, _, _ := db.CIRunByID("r", "a2")
	if a3.RerunOf != "a1" {
		t.Errorf("FAIL: a3 rerun of %q, want a1", a3.RerunOf)
	}
	if a1.RerunAs != "a3" {
		t.Errorf("FAIL: a1 rerun as %q, want a3", a1.RerunAs)
	}
	if a2.RerunAs != "" {
		t.Errorf("FAIL: a2 rerun as %q, want none: its successor is a rerun of another", a2.RerunAs)
	}
	a1.RerunAs, before.RerunAs = "", ""
	if a1 != before {
		t.Errorf("FAIL: the original changed: %+v then %+v", before, a1)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}