run_test.go
| 1 | package ci |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | |
| 10 | "hearthforge/internal/config" |
| 11 | "hearthforge/internal/db" |
| 12 | ) |
| 13 | |
| 14 | // newRunnerDB gives a Runner backed by a fresh database holding one repo. |
| 15 | func newRunnerDB(t *testing.T) (*Runner, *db.DB) { |
| 16 | t.Helper() |
| 17 | dir := t.TempDir() |
| 18 | d, err := db.Open(filepath.Join(dir, "hearthforge.db")) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | t.Cleanup(func() { d.Close() }) |
| 23 | for _, q := range []string{ |
| 24 | `INSERT INTO repositories (id, name, is_private, created_at) VALUES (1, 'demo', 0, 'now')`, |
| 25 | `INSERT INTO users (id, username, created_at) VALUES (1, 'admin', 'now')`, |
| 26 | } { |
| 27 | if _, err := d.ExecContext(context.Background(), q); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | return New(&config.Config{DataDir: dir, CIMaxConcurrent: 1, CIMaxHistory: 50}, d), d |
| 32 | } |
| 33 | |
| 34 | func insertRun(t *testing.T, d *db.DB, status string) int64 { |
| 35 | t.Helper() |
| 36 | res, err := d.ExecContext(context.Background(), |
| 37 | `INSERT INTO ci_runs (repo_id, trigger_source, commit_sha, status) VALUES (1, 'manual', 'abc', ?)`, status) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | id, _ := res.LastInsertId() |
| 42 | return id |
| 43 | } |
| 44 | |
| 45 | func TestRetryRunRejectsUnfinishedRun(t *testing.T) { |
| 46 | r, d := newRunnerDB(t) |
| 47 | for _, status := range []string{"pending", "running", "queued"} { |
| 48 | id := insertRun(t, d, status) |
| 49 | if err := r.RetryRun(context.Background(), id, 1); !errors.Is(err, ErrRunNotFinished) { |
| 50 | t.Fatalf("status %s: got %v", status, err) |
| 51 | } |
| 52 | var got string |
| 53 | if err := d.QueryRowContext(context.Background(), |
| 54 | `SELECT status FROM ci_runs WHERE id = ?`, id).Scan(&got); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if got != status { |
| 58 | t.Fatalf("status %s was changed to %s", status, got) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestRetryRunMissingRun(t *testing.T) { |
| 64 | r, _ := newRunnerDB(t) |
| 65 | if err := r.RetryRun(context.Background(), 999, 1); err == nil { |
| 66 | t.Fatal("expected an error") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // The terminal status list in RetryRun must match the statuses runs finish |
| 71 | // with, otherwise no run could ever be retried. |
| 72 | func TestRetryRunAcceptsFinishedStatuses(t *testing.T) { |
| 73 | r, d := newRunnerDB(t) |
| 74 | // A full concurrency slot keeps the retry in the queue, so the run does |
| 75 | // not start and this test never talks to a container engine. |
| 76 | r.running[-1] = &task{} |
| 77 | for _, status := range []string{"success", "failure", "warning", "cancelled", "skipped"} { |
| 78 | id := insertRun(t, d, status) |
| 79 | if err := r.RetryRun(context.Background(), id, 1); err != nil { |
| 80 | t.Fatalf("status %s: %v", status, err) |
| 81 | } |
| 82 | var got string |
| 83 | if err := d.QueryRowContext(context.Background(), |
| 84 | `SELECT status FROM ci_runs WHERE id = ?`, id).Scan(&got); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if got != "queued" { |
| 88 | t.Fatalf("status %s became %s, want queued", status, got) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // finishSlot must not release a slot another execution owns: a retry that |
| 94 | // restarted the same run holds its own task. |
| 95 | func TestFinishSlotKeepsForeignTask(t *testing.T) { |
| 96 | r, _ := newRunnerDB(t) |
| 97 | mine, theirs := &task{}, &task{} |
| 98 | r.running[7] = theirs |
| 99 | r.finishSlot(7, mine) |
| 100 | if r.running[7] != theirs { |
| 101 | t.Fatal("finishSlot released a slot it does not own") |
| 102 | } |
| 103 | r.finishSlot(7, theirs) |
| 104 | if _, ok := r.running[7]; ok { |
| 105 | t.Fatal("finishSlot did not release its own slot") |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestMaskLog(t *testing.T) { |
| 110 | secrets := []string{"abc", "abcdef"} |
| 111 | if got := maskLog("x abcdef y", secrets, false); got != "x [MASKED] y" { |
| 112 | t.Errorf("longest first: got %q", got) |
| 113 | } |
| 114 | if got := maskLog("x ab", secrets, false); got != "x ab" { |
| 115 | t.Errorf("complete log: got %q", got) |
| 116 | } |
| 117 | if got := maskLog("x abcdefg ab", secrets, true); got != "x [MASKED]g " { |
| 118 | t.Errorf("partial log: got %q", got) |
| 119 | } |
| 120 | if got := maskLog("x ab"+logTruncatedNotice, secrets, false); got != "x "+logTruncatedNotice { |
| 121 | t.Errorf("truncated log: got %q", got) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestRegistryIsLowercase(t *testing.T) { |
| 126 | env, _ := buildEnvVars(1, "MyRepo", "", "git.example.com", runRow{}, &Config{}, nil, nil) |
| 127 | if !slices.Contains(env, "CI_REGISTRY=git.example.com/myrepo") { |
| 128 | t.Fatalf("env = %v", env) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // A retried old run finishes last, so it must survive the prune. |
| 133 | func TestPruneHistoryKeepsRecentlyFinished(t *testing.T) { |
| 134 | r, d := newRunnerDB(t) |
| 135 | r.cfg.CIMaxHistory = 2 |
| 136 | var ids []int64 |
| 137 | for i, finished := range []string{"2026-01-04", "2026-01-02", "2026-01-03"} { |
| 138 | ids = append(ids, insertRun(t, d, "success")) |
| 139 | if _, err := d.ExecContext(context.Background(), |
| 140 | `UPDATE ci_runs SET finished_at = ? WHERE id = ?`, finished, ids[i]); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | } |
| 144 | r.pruneHistory(context.Background(), 1) |
| 145 | var left []int64 |
| 146 | rows, err := d.QueryContext(context.Background(), `SELECT id FROM ci_runs ORDER BY id`) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | defer rows.Close() |
| 151 | for rows.Next() { |
| 152 | var id int64 |
| 153 | rows.Scan(&id) |
| 154 | left = append(left, id) |
| 155 | } |
| 156 | if !slices.Equal(left, []int64{ids[0], ids[2]}) { |
| 157 | t.Fatalf("kept %v, want %v", left, []int64{ids[0], ids[2]}) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestRetryRunRejectsRunStillCleaningUp(t *testing.T) { |
| 162 | r, d := newRunnerDB(t) |
| 163 | id := insertRun(t, d, "cancelled") |
| 164 | r.running[id] = &task{} |
| 165 | if err := r.RetryRun(context.Background(), id, 1); !errors.Is(err, ErrRunNotFinished) { |
| 166 | t.Fatalf("got %v", err) |
| 167 | } |
| 168 | } |
| 169 |