| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // fakeCI is the hub's hosted CI: whether it is on, then each status read |
| 14 | // in turn (the last one repeats); failed answers the logs=failed read. |
| 15 | type fakeCI struct { |
| 16 | on bool |
| 17 | onErr error |
| 18 | statuses []forge.CIStatus |
| 19 | failed *forge.CIStatus |
| 20 | calls []string |
| 21 | } |
| 22 | |
| 23 | func (f *fakeCI) CIHosted(_ context.Context, repo string) (bool, error) { |
| 24 | f.calls = append(f.calls, "hosted "+repo) |
| 25 | return f.on, f.onErr |
| 26 | } |
| 27 | |
| 28 | func (f *fakeCI) CIStatus(ctx context.Context, repo, branch, logs string) (forge.CIStatus, error) { |
| 29 | f.calls = append(f.calls, "status "+repo+" "+branch+" "+logs) |
| 30 | if ctx.Err() != nil { |
| 31 | return forge.CIStatus{}, ctx.Err() |
| 32 | } |
| 33 | if logs == forge.CILogsFailed && f.failed != nil { |
| 34 | return *f.failed, nil |
| 35 | } |
| 36 | if len(f.statuses) == 0 { |
| 37 | return forge.CIStatus{}, forge.ErrCIRead |
| 38 | } |
| 39 | st := f.statuses[0] |
| 40 | if len(f.statuses) > 1 { |
| 41 | f.statuses = f.statuses[1:] |
| 42 | } |
| 43 | return st, nil |
| 44 | } |
| 45 | |
| 46 | // hostedRig is a checks attempt on the hub rig's candidate, accepted in |
| 47 | // the runner's state as the instance dispatched it (with the offer). |
| 48 | func hostedRig(t *testing.T, ci *fakeCI) (*Engineer, stepRow, string) { |
| 49 | t.Helper() |
| 50 | e, f, r, cand := hubRig(t) |
| 51 | e.CI = ci |
| 52 | r.Specialist, r.StepID, r.HostedChecks = wire.SpecialistChecks, "c1", true |
| 53 | r.BaseSHA = gitOut(t, f.dir, "rev-parse", "main") |
| 54 | if err := e.O.State.AcceptDispatch(context.Background(), "d1", r.DispatchCommand); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | return e, heldStep(t, e), cand |
| 58 | } |
| 59 | |
| 60 | // heldStep is the checks attempt as the engine would run it next. |
| 61 | func heldStep(t *testing.T, e *Engineer) stepRow { |
| 62 | t.Helper() |
| 63 | if _, err := e.O.State.db.Exec("UPDATE steps SET not_before=''"); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | r, ok, err := e.O.State.nextStep(context.Background()) |
| 67 | if err != nil || !ok { |
| 68 | t.Fatalf("no attempt held: %v %v", ok, err) |
| 69 | } |
| 70 | return r |
| 71 | } |
| 72 | |
| 73 | func ciAt(sha, state string) forge.CIStatus { |
| 74 | return forge.CIStatus{NS: "oficina", Repo: "sample", Branch: "equipo/tk1-fix-typo", SHA: sha, State: state, UpdatedAt: 1794402264} |
| 75 | } |
| 76 | |
| 77 | // Offered hosted success and old resumed hosted phases cannot substitute for |
| 78 | // unavailable base-run evidence. |
| 79 | func TestHostedChecksPass(t *testing.T) { |
| 80 | for _, phase := range []string{"", phaseHosted} { |
| 81 | ci := &fakeCI{on: true} |
| 82 | e, r, cand := hostedRig(t, ci) |
| 83 | r.Phase = phase |
| 84 | ci.statuses = []forge.CIStatus{ciAt(cand, "success")} |
| 85 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 86 | if rep.Checks != wire.ChecksPassed || rep.Hosted == nil || rep.BaseChecksSHA != "" || len(ci.calls) == 0 { |
| 87 | t.Fatalf("candidate hosted gate supplied authority: %+v calls=%v", rep, ci.calls) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // TestHostedChecksFailure: a failure names its failing units first, from |
| 93 | // the one snapshot, with the failing logs only from a second read of the |
| 94 | // same run; if that read is of another run, the logs are left out. |
| 95 | func TestHostedChecksFailure(t *testing.T) { |
| 96 | for _, sameRun := range []bool{true, false} { |
| 97 | ci := &fakeCI{on: true} |
| 98 | e, r, cand := hostedRig(t, ci) |
| 99 | st := ciAt(cand, "failure") |
| 100 | st.Summary = `step "compile" failed` |
| 101 | st.Steps = []forge.CIStep{{Name: "compile", State: "failure"}, {Name: "check", State: "failure", Shards: []forge.CIShard{{Name: "shard 4 of 23", State: "success"}, {Name: "shard 5 of 23", State: "failure"}}}, {Name: "perf", State: "success"}} |
| 102 | full := st |
| 103 | full.Steps = []forge.CIStep{{Name: "compile", State: "failure", Log: "rastrillo: budget size: 1 problem(s)"}, {Name: "check", State: "failure", Shards: []forge.CIShard{{Name: "shard 5 of 23", State: "failure", Log: "--- FAIL: TestHeadless/2_project"}}}} |
| 104 | if !sameRun { |
| 105 | full.UpdatedAt++ |
| 106 | } |
| 107 | ci.statuses, ci.failed = []forge.CIStatus{st}, &full |
| 108 | rep, _ := e.hostedVerdict(context.Background(), r, judgeReport(r), st) |
| 109 | if rep.State != wire.StepDone || rep.Checks != wire.ChecksFailed || rep.Gate != wire.GateHosted || rep.ExitCode == nil || *rep.ExitCode == 0 { |
| 110 | t.Fatalf("report %+v", rep) |
| 111 | } |
| 112 | if rep.Hosted == nil || strings.Join(rep.Hosted.Failed, "|") != "compile|check shard 5 of 23" || rep.Hosted.State != "failure" { |
| 113 | t.Fatalf("binding %+v", rep.Hosted) |
| 114 | } |
| 115 | head := "Hosted CI on oficina/sample at " + cand[:7] + ": failed.\nFailed: compile; check shard 5 of 23\nstep \"compile\" failed\n" |
| 116 | if !strings.HasPrefix(rep.Output, head) { |
| 117 | t.Fatalf("output %q", rep.Output) |
| 118 | } |
| 119 | hasLogs := strings.Contains(rep.Output, "budget size: 1 problem") && strings.Contains(rep.Output, wire.CIUnitHeader("check shard 5 of 23")+"\n--- FAIL: TestHeadless/2_project") |
| 120 | if hasLogs != sameRun { |
| 121 | t.Fatalf("same run %v, logs %v: %q", sameRun, hasLogs, rep.Output) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // TestHostedFailedUnitsBounded: however many units fail, and however long |
| 127 | // their names, the binding stays within its bounds. |
| 128 | func TestHostedFailedUnitsBounded(t *testing.T) { |
| 129 | var steps []forge.CIStep |
| 130 | for i := 0; i < 40; i++ { |
| 131 | steps = append(steps, forge.CIStep{Name: strings.Repeat("é", 200) + string(rune('a'+i%26)), State: "failure"}) |
| 132 | } |
| 133 | names := failedUnits(steps) |
| 134 | if len(names) != wire.MaxHostedFailed { |
| 135 | t.Fatalf("%d names", len(names)) |
| 136 | } |
| 137 | for _, n := range names { |
| 138 | if len(n) > wire.MaxHostedName || !strings.HasPrefix(n, "é") || strings.ContainsRune(n, '�') { |
| 139 | t.Fatalf("name %q (%d bytes)", n, len(n)) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func TestHostedDiagnosticWaitsWithoutCertifying(t *testing.T) { |
| 145 | ci := &fakeCI{on: true} |
| 146 | e, r, cand := hostedRig(t, ci) |
| 147 | ci.statuses = []forge.CIStatus{ciAt(cand, "running")} |
| 148 | first := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 149 | if first.State != stepDeferred || first.BaseChecksSHA != "" { |
| 150 | t.Fatalf("pending diagnostic lost: %+v", first) |
| 151 | } |
| 152 | r.Phase = phaseHosted |
| 153 | ci.statuses = []forge.CIStatus{ciAt(cand, wire.HostedFailure)} |
| 154 | final := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 155 | if final.Checks != wire.ChecksFailed || final.BaseChecksSHA != "" || final.Hosted == nil { |
| 156 | t.Fatalf("hosted failure lost or certified: %+v", final) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // shardLog is a failing shard's long log: a test failure with its |
| 161 | // message early on, then noise, then its own last lines. |
| 162 | func shardLog(n, size int) string { |
| 163 | var b strings.Builder |
| 164 | fmt.Fprintf(&b, "=== RUN TestShard%d\n--- FAIL: TestShard%d (0.01s)\n shard_test.go:%d: shard %d broke\n", n, n, n, n) |
| 165 | for b.Len() < size { |
| 166 | fmt.Fprintf(&b, "noise line %d of shard %d, nothing to see here\n", b.Len(), n) |
| 167 | } |
| 168 | fmt.Fprintf(&b, "FAIL\tamadan.net/shard%d\t1.0s\n", n) |
| 169 | return b.String() |
| 170 | } |
| 171 | |
| 172 | // TestFailedLogsEveryShard (stopped-jobs audit, fix 4): with three |
| 173 | // failing shards of 40 KB each, every shard is named in the output and |
| 174 | // keeps its failing test and message, within the checks output bound. |
| 175 | // Before, only the tail of all of them joined was kept: the fix round saw |
| 176 | // the last shard only. |
| 177 | func TestFailedLogsEveryShard(t *testing.T) { |
| 178 | ci := &fakeCI{on: true} |
| 179 | e, r, cand := hostedRig(t, ci) |
| 180 | st := ciAt(cand, "failure") |
| 181 | full := st |
| 182 | check := forge.CIStep{Name: "check", State: "failure"} |
| 183 | for n := 1; n <= 3; n++ { |
| 184 | check.Shards = append(check.Shards, forge.CIShard{Name: fmt.Sprintf("shard %d of 23", n*5), State: "failure", Log: shardLog(n, 40<<10)}) |
| 185 | } |
| 186 | st.Steps = []forge.CIStep{{Name: "check", State: "failure", Shards: []forge.CIShard{{Name: "shard 5 of 23", State: "failure"}, {Name: "shard 10 of 23", State: "failure"}, {Name: "shard 15 of 23", State: "failure"}}}} |
| 187 | full.Steps = []forge.CIStep{check} |
| 188 | ci.statuses, ci.failed = []forge.CIStatus{st}, &full |
| 189 | rep, _ := e.hostedVerdict(context.Background(), r, judgeReport(r), st) |
| 190 | if len(rep.Output) > wire.MaxChecksOutput { |
| 191 | t.Fatalf("output %d bytes, over %d", len(rep.Output), wire.MaxChecksOutput) |
| 192 | } |
| 193 | for n := 1; n <= 3; n++ { |
| 194 | for _, want := range []string{wire.CIUnitHeader(fmt.Sprintf("check shard %d of 23", n*5)) + "\n", fmt.Sprintf("--- FAIL: TestShard%d (0.01s)\n shard_test.go:%d: shard %d broke\n", n, n, n), fmt.Sprintf("FAIL\tamadan.net/shard%d\t1.0s", n)} { |
| 195 | if !strings.Contains(rep.Output, want) { |
| 196 | t.Errorf("output lacks %q", want) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Each unit gets an equal share; what a short one leaves goes to the |
| 203 | // others, and a unit within its share is kept whole. |
| 204 | func TestCIUnitsShare(t *testing.T) { |
| 205 | short := wire.CIUnit{Name: "lint", Log: "--- FAIL: lint\n"} |
| 206 | long := wire.CIUnit{Name: "test", Log: shardLog(1, 20<<10)} |
| 207 | out := wire.JoinCIUnits([]wire.CIUnit{short, long}, 8<<10) |
| 208 | if len(out) > 8<<10 || len(out) < 7<<10 || !strings.Contains(out, wire.CIUnitHeader("lint")+"\n--- FAIL: lint\n") || !strings.Contains(out, "shard 1 broke") { |
| 209 | t.Fatalf("%d bytes:\n%s", len(out), out) |
| 210 | } |
| 211 | head, units := wire.SplitCIUnits("Hosted CI failed.\n" + out) |
| 212 | if head != "Hosted CI failed.\n" || len(units) != 2 || units[0].Name != "lint" || units[1].Name != "test" || !strings.Contains(units[1].Log, "shard 1 broke") { |
| 213 | t.Fatalf("split %q %+v", head, units) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // However many units fail, and however long their names, every unit is |
| 218 | // named before any log takes its share, within the bound. |
| 219 | func TestCIUnitsEveryNameFirst(t *testing.T) { |
| 220 | var units []wire.CIUnit |
| 221 | for i := 0; i < wire.MaxHostedFailed; i++ { |
| 222 | units = append(units, wire.CIUnit{Name: fmt.Sprintf("%02d ", i) + strings.Repeat("n", wire.MaxHostedName-3), Log: shardLog(i, 4<<10)}) |
| 223 | } |
| 224 | for _, limit := range []int{1500, 2600, 4500, 32 << 10} { |
| 225 | out := wire.JoinCIUnits(units, limit) |
| 226 | if len(out) > limit { |
| 227 | t.Fatalf("limit %d: %d bytes", limit, len(out)) |
| 228 | } |
| 229 | named := 0 |
| 230 | for _, u := range units { |
| 231 | if strings.Contains(out, wire.CIUnitHeader(u.Name)+"\n") { |
| 232 | named++ |
| 233 | } |
| 234 | } |
| 235 | if want := min(len(units), limit/(len(wire.CIUnitHeader(units[0].Name))+1)); named != want { |
| 236 | t.Fatalf("limit %d: %d units named, want %d", limit, named, want) |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // With more failing units than the binding names (wire.MaxHostedFailed), |
| 242 | // every unit's log is still its own part: the instance finds each by its |
| 243 | // header, not by the binding's capped list. |
| 244 | func TestFailedLogsBeyondTheNamedUnits(t *testing.T) { |
| 245 | ci := &fakeCI{on: true} |
| 246 | e, r, cand := hostedRig(t, ci) |
| 247 | st := ciAt(cand, "failure") |
| 248 | snap, logs := forge.CIStep{Name: "check", State: "failure"}, forge.CIStep{Name: "check", State: "failure"} |
| 249 | total := wire.MaxHostedFailed + 3 |
| 250 | for n := 1; n <= total; n++ { |
| 251 | name := fmt.Sprintf("shard %d of 23", n) |
| 252 | snap.Shards = append(snap.Shards, forge.CIShard{Name: name, State: "failure"}) |
| 253 | logs.Shards = append(logs.Shards, forge.CIShard{Name: name, State: "failure", Log: shardLog(n, 4<<10)}) |
| 254 | } |
| 255 | st.Steps = []forge.CIStep{snap} |
| 256 | full := st |
| 257 | full.Steps = []forge.CIStep{logs} |
| 258 | ci.statuses, ci.failed = []forge.CIStatus{st}, &full |
| 259 | rep, _ := e.hostedVerdict(context.Background(), r, judgeReport(r), st) |
| 260 | _, units := wire.SplitCIUnits(rep.Output) |
| 261 | if len(rep.Hosted.Failed) != wire.MaxHostedFailed || len(units) != total || len(rep.Output) > wire.MaxChecksOutput { |
| 262 | t.Fatalf("%d named, %d units found, %d bytes", len(rep.Hosted.Failed), len(units), len(rep.Output)) |
| 263 | } |
| 264 | for n, u := range units { |
| 265 | if u.Name != fmt.Sprintf("check shard %d of 23", n+1) || !strings.Contains(u.Log, fmt.Sprintf("--- FAIL: TestShard%d ", n+1)) || strings.Contains(u.Log, "=== CI unit") { |
| 266 | t.Fatalf("unit %d %q: %q", n, u.Name, u.Log) |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // A compiler's diagnostic early in a long log is kept when the log is |
| 272 | // cut, at the runner's share and at the fix round's. |
| 273 | func TestCIUnitsKeepDiagnostics(t *testing.T) { |
| 274 | log := "# amadan.net/oficina/equipo/internal/x\ninternal/x/x.go:12:3: undefined: bar\nbuild error: exit status 1\n" + shardLog(1, 30<<10) |
| 275 | for _, limit := range []int{1500, 8 << 10} { |
| 276 | out := wire.JoinCIUnits([]wire.CIUnit{{Name: "build", Log: log}}, limit) |
| 277 | if !strings.Contains(out, "internal/x/x.go:12:3: undefined: bar\n") || !strings.Contains(out, "build error: exit status 1\n") || len(out) > limit { |
| 278 | t.Fatalf("limit %d: %q", limit, out) |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // A local gate's full-sized output never pushes hosted CI's units out |
| 284 | // of the combined diagnostic: the whole stays within the bound the |
| 285 | // instance keeps, and every unit is in it. |
| 286 | func TestCombinedOutputKeepsHostedUnits(t *testing.T) { |
| 287 | local := strings.Repeat(strings.Repeat("l", 163)+"\n", 200) |
| 288 | var units []wire.CIUnit |
| 289 | for n := 1; n <= 3; n++ { |
| 290 | units = append(units, wire.CIUnit{Name: fmt.Sprintf("check shard %d of 23", n), Log: shardLog(n, 12<<10)}) |
| 291 | } |
| 292 | out := combinedOutput(local, "Hosted CI failed.\n"+wire.JoinCIUnits(units, wire.MaxChecksOutput)) |
| 293 | if len(out) > wire.MaxChecksOutput { |
| 294 | t.Fatalf("%d bytes", len(out)) |
| 295 | } |
| 296 | if _, got := wire.SplitCIUnits(out); len(got) != 3 || !strings.Contains(out, "--- FAIL: TestShard1 ") || !strings.HasSuffix(strings.TrimSpace(strings.Split(out, "\nHosted CI diagnostic:")[0]), "l") { |
| 297 | t.Fatalf("units %d:\n%s", len(got), out[:200]) |
| 298 | } |
| 299 | } |
| 300 | |