|
| 1 | +package harness |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// goroutineDump is a representative Go runtime panic + goroutine dump as it |
| 9 | +// appears interleaved in act's stdout/stderr when the cascade CLI (or act |
| 10 | +// itself) crashes mid-run. The dump corrupts act's --json stream, so no |
| 11 | +// "Job failed" event is parseable and the run would otherwise be misclassified |
| 12 | +// as a transient flake and retried away. |
| 13 | +const goroutineDump = `time="2026-06-13T00:00:00Z" level=info msg="⭐ Run Main cascade orchestrate" |
| 14 | +panic: runtime error: invalid memory address or nil pointer dereference |
| 15 | +[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10a3f20] |
| 16 | +
|
| 17 | +goroutine 1 [running]: |
| 18 | +github.com/stablekernel/cascade/internal/orchestrate.(*Planner).Plan(0x0) |
| 19 | + /cascade/internal/orchestrate/planner.go:142 +0x1a0 |
| 20 | +main.main() |
| 21 | + /cascade/main.go:33 +0x9c |
| 22 | +` |
| 23 | + |
| 24 | +// TestDetectCrashSignature_GoPanicAndGoroutineDump verifies a real Go-runtime |
| 25 | +// crash signature in raw act output is detected so it can be treated as a |
| 26 | +// definitive failure rather than a transient flake. |
| 27 | +func TestDetectCrashSignature_GoPanicAndGoroutineDump(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + logs string |
| 33 | + wantCrash bool |
| 34 | + wantReasony string // substring expected in the reason when wantCrash |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "panic plus goroutine dump", |
| 38 | + logs: goroutineDump, |
| 39 | + wantCrash: true, |
| 40 | + wantReasony: "panic", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "fatal error at line start", |
| 44 | + logs: "some log\nfatal error: concurrent map writes\n\ngoroutine 7 [running]:\n", |
| 45 | + wantCrash: true, |
| 46 | + wantReasony: "fatal error", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "goroutine dump with runtime frame", |
| 50 | + logs: "goroutine 42 [running]:\nruntime.gopanic(0x1, 0x2)\n\t/usr/local/go/src/runtime/panic.go:884\n", |
| 51 | + wantCrash: true, |
| 52 | + wantReasony: "goroutine", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "SIGSEGV signal line", |
| 56 | + logs: "unexpected\n[signal SIGSEGV: segmentation violation code=0x1 addr=0x0]\n", |
| 57 | + wantCrash: true, |
| 58 | + wantReasony: "SIGSEGV", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "benign log mentioning the word panic in prose is not a crash", |
| 62 | + logs: `{"jobID":"deploy","msg":"Do not panic: rollback is automatic","level":"info"}`, |
| 63 | + wantCrash: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "benign log mentioning goroutine count in prose is not a crash", |
| 67 | + logs: `{"jobID":"deploy","msg":"started 8 goroutines for the worker pool","level":"info"}`, |
| 68 | + wantCrash: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "ordinary successful output has no crash signature", |
| 72 | + logs: `{"jobID":"build","jobResult":"success","msg":"🏁 Job succeeded","level":"info"}`, |
| 73 | + wantCrash: false, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + tt := tt |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + crashed, reason := detectCrashSignature(tt.logs) |
| 82 | + if crashed != tt.wantCrash { |
| 83 | + t.Fatalf("detectCrashSignature crashed = %v, want %v (reason=%q)", crashed, tt.wantCrash, reason) |
| 84 | + } |
| 85 | + if tt.wantCrash { |
| 86 | + if reason == "" { |
| 87 | + t.Fatalf("expected a non-empty crash reason when crashed") |
| 88 | + } |
| 89 | + if tt.wantReasony != "" && !strings.Contains(strings.ToLower(reason), strings.ToLower(tt.wantReasony)) { |
| 90 | + t.Fatalf("crash reason %q does not mention %q", reason, tt.wantReasony) |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// TestParseActOutput_SetsCrashFields verifies the parser surfaces a runtime |
| 98 | +// crash on the result so the classifier downstream can act on it. |
| 99 | +func TestParseActOutput_SetsCrashFields(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + |
| 102 | + result, err := ParseActOutput(goroutineDump) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("ParseActOutput returned error: %v", err) |
| 105 | + } |
| 106 | + if !result.Crashed { |
| 107 | + t.Fatalf("expected Crashed=true for a panic+goroutine dump") |
| 108 | + } |
| 109 | + if result.CrashReason == "" { |
| 110 | + t.Fatalf("expected a non-empty CrashReason") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// TestNormalizeWorkflowResult_CrashIsNotTransient is the core safety guarantee: |
| 115 | +// a non-zero act exit carrying a Go-runtime crash signature must be classified |
| 116 | +// as a REAL (non-transient) failure so the scenario runner does not retry it as |
| 117 | +// a flake and the stack trace surfaces. |
| 118 | +func TestNormalizeWorkflowResult_CrashIsNotTransient(t *testing.T) { |
| 119 | + t.Parallel() |
| 120 | + |
| 121 | + result := &ExtendedWorkflowResult{ |
| 122 | + Conclusion: "success", |
| 123 | + Jobs: map[string]*JobResultExtended{}, |
| 124 | + Logs: goroutineDump, |
| 125 | + Crashed: true, |
| 126 | + CrashReason: "panic: runtime error: invalid memory address or nil pointer " + |
| 127 | + "dereference", |
| 128 | + } |
| 129 | + normalizeWorkflowResult(result, ".github/workflows/orchestrate.yaml", 2) |
| 130 | + |
| 131 | + if result.Conclusion != "failure" { |
| 132 | + t.Fatalf("Conclusion = %q, want failure", result.Conclusion) |
| 133 | + } |
| 134 | + if result.ExecError { |
| 135 | + t.Fatalf("a crash must NOT be tagged as a transient ExecError") |
| 136 | + } |
| 137 | + if !strings.Contains(strings.ToLower(result.Error), "crash") { |
| 138 | + t.Fatalf("error %q should describe the crash", result.Error) |
| 139 | + } |
| 140 | + |
| 141 | + // And it must not be retried by the classifier. |
| 142 | + failErr := workflowFailureError("orchestrate", result) |
| 143 | + if IsTransientWorkflowError(failErr) { |
| 144 | + t.Fatalf("a crash failure must not be classified transient: %v", failErr) |
| 145 | + } |
| 146 | +} |
0 commit comments