package oltp
import (
"strings"
"testing"
)
// quarry#290: a rule may ask for the merge to be tested without
// blocking on the target. Strict premerge still wins where both match,
// and an unknown mode is the tip.
func TestMergeModeIsItsOwnRule(t *testing.T) {
db := testDB(t)
for _, r := range []struct{ branch, mode string }{
{"trunk", "merge"}, {"release-*", "premerge"}, {"stable", "bogus"},
} {
if err := db.SetCheckRule("r", r.branch, []string{"test"}, r.mode); err != nil {
t.Fatal(err)
}
}
if !db.MergeMode("r", "trunk") || db.PremergeMode("r", "trunk") {
t.Errorf("FAIL: trunk's merge rule reads merge=%v premerge=%v", db.MergeMode("r", "trunk"), db.PremergeMode("r", "trunk"))
}
if db.MergeMode("r", "release-1") || !db.PremergeMode("r", "release-1") {
t.Errorf("FAIL: a strict rule reads as merge mode")
}
if db.MergeMode("r", "stable") || db.PremergeMode("r", "stable") {
t.Errorf("FAIL: an unknown mode was kept instead of reading as the tip")
}
if err := db.SetCheckRule("r", "trunk*", []string{"test"}, "premerge"); err != nil {
t.Fatal(err)
}
if db.MergeMode("r", "trunk") {
t.Errorf("FAIL: merge mode held where a strict rule also matches")
}
t.Log("ok: merge is its own mode, strict wins, unknown is the tip")
}
// The newest pair for a tip is the one its checks are read from,
// whichever target it merged into, and another tip's pairs never
// answer for it.
func TestLatestPairForATip(t *testing.T) {
db := testDB(t)
tip, other := strings.Repeat("a", 64), strings.Repeat("a", 63)+"b"
t1, t2 := strings.Repeat("1", 64), strings.Repeat("2", 64)
if got, err := db.LatestPairFor("r", tip); err != nil || got != "" {
t.Fatalf("FAIL: no runs answered %q, %v", got, err)
}
add := func(id, pair string) {
t.Helper()
if err := db.AddCIRun(CIRun{RunID: id, Repo: "r", Checkin: pair, Pair: pair, Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
t.Fatal(err)
}
}
add("1", "pair:"+tip+":"+t1)
add("2", "pair:"+other+":"+t2)
if got, _ := db.LatestPairFor("r", tip); got != "pair:"+tip+":"+t1 {
t.Fatalf("FAIL: latest pair = %q, want the tip's own", got)
}
if _, err := db.w.Exec(`UPDATE ci_run SET started = '2000-01-01T00:00:00Z' WHERE run_id = '1'`); err != nil {
t.Fatal(err)
}
add("3", "pair:"+tip+":"+t2)
if got, _ := db.LatestPairFor("r", tip); got != "pair:"+tip+":"+t2 {
t.Fatalf("FAIL: latest pair = %q, want the newer target's", got)
}
t.Log("ok: the newest pair for the tip, never another tip's")
}