Files
w3cjandClaude Sonnet 5.5 ee089d0c5d feat(cli): report a job that hit its time limit
GitHub marks a job that runs past its timeout-minutes as cancelled, and
gh run view --log-failed prints nothing for it, so ciwatch showed an
unexplained cancelled job. The only sign of the timeout is the check-run
annotation "The job has exceeded the maximum execution time of <limit>".
For each cancelled job in a completed run, ciwatch now fetches its
annotations through the usual retries. When one matches that message
exactly, the job is reported as timed out after the limit: on stderr, in
the job's block header and as timed_out_after in --json. ciwatch then
reads the full job log (gh run view --log --job) and shows the cancelled
step's last lines the way failed steps are shown.

This reads GitHub's annotation text. A job without the annotation is
reported exactly as before, so a reworded message can only miss a
timeout, never invent one. A failed annotations call warns and leaves
the job as it was. A failed log call keeps the timed-out verdict and
shows (log unavailable) and log_error. Ctrl-C in either call exits 4
with no table. Conclusions, exit codes and the rerun hint are unchanged.

The full log labels each line with its step, and the steps before the
cancelled one start in the same second, so the cancelled step's lines
are picked out by that label before steplog.Parse finds its window. If
no line carries the step's name, the whole log is parsed by the step's
times instead.

Runner gains Annotations and JobLog. docs/json.md, the README and
AGENTS.md name the new field and calls. The fixtures are the recorded
timeout and the recorded annotations of the concurrency-cancelled job.

Backlog:
- The jobResult and failedLogs comments in internal/cli/cli.go still
  speak of failed jobs and the failed-step log; they now also cover
  timed-out jobs and the full log. Off path: comments only, behaviour is
  correct.
- A timed-out job with no cancelled step, or whose cancelled step has no
  lines, says "(no failed step in the log)"; "(no cancelled step in the
  log)" would be more exact. Off path: wording only, the verdict and
  exit code are correct.
- The annotation fixtures are the raw API JSON, not the [{message}]
  shape --jq produces, so no test checks the jq expression. Off path:
  test tightness; the fixtures are unedited recordings and the decoder
  ignores the extra fields.
- If only some lines of a timed-out job's log carry the step's name and
  the rest are UNKNOWN STEP, stepLines drops the unlabelled ones. Off
  path: the same log-layout inference the design accepts for failed
  steps; the verdict and exit code don't depend on it.

Spec: acceptance cases
1. **AC-1:** Fixtures: the recorded timeout (run 45339081512, job 138258094336 "slow", its annotations and its full log). ciwatch:
   - writes `ciwatch: job 138258094336 (slow) of run 45339081512 (Timeout) timed out after 1m0s` to stderr.
   - In the table, the job's block starts `slow (Timeout, run 45339081512): timed out after 1m0s`. Under `Sleep past the limit:` it shows these 8 lines, in order, with colour codes removed: `##[group]Run echo "starting to sleep"`, `echo "starting to sleep"`, `sleep 150`, `echo "never printed"`, `shell: /usr/bin/bash -e {0}`, `##[endgroup]`, `starting to sleep`, `##[error]The operation was canceled.`. None of the "Print before" lines and none of the "Complete job" lines (`Cleaning up orphan processes`, …) appear.
   - In `--json`, the job has `"timed_out_after":"1m0s"` and `steps` holding that one step with `omitted: 0`.
   - Exit code 3.
2. **AC-2:** The concurrency cancel (`run-list-cancelled.json`, run 44707997310, cancelled job 136192284970). Its annotations have no timeout message. stdout, stderr, the newer-run line and exit code 3 are all byte-for-byte what they are today. The job gets no block and no `timed_out_after`, and the full log isn't fetched.
3. **AC-3:** The annotations call for a cancelled job fails, after the usual retries for network and temporary errors. ciwatch writes `ciwatch: can't fetch the annotations of job <id> (<name>) of run <id> (<workflow>): <err>` to stderr. That job is then reported as it is today, with no block and no `timed_out_after`, and the exit code is unchanged (3).
4. **AC-4:** The annotation says the job timed out, but the full-log call fails. The job is still reported as timed out on stderr and in the block header. The block shows `(log unavailable)`, `--json` has `log_error`, the usual `can't fetch the log` warning appears, and the exit code is unchanged.
5. **AC-5:** The exit code doesn't change. A run whose only problem is a timed-out job exits 3. In a mixed set (one failed run plus one timed-out run) the exit code is whatever `exitCode` gives today. The rerun hint still counts only failed runs.
6. **AC-6:** Ctrl-C during the annotations call or the full-log call: no partial table, exit 4, the same as during the failed-log fetch today.
7. **AC-7:** An annotation whose message only resembles the timeout message (for example `The operation was canceled.`, or the timeout text without the "of <limit>" ending) doesn't make the job timed out.

Spec: non-goals
- Guessing a timeout from durations or from `timeout-minutes` in the workflow file.
- Jobs whose conclusion is `failure`, or the existing `timed_out` handling. That stays as it is.
- Changing exit codes, the run's or job's `conclusion` value, or the table's CONCLUSION column, which stays `cancelled`, as GitHub says.
- Runs that are still in progress, including with `--once`. Logs are only served once a run has completed.
- Changing the newer-run lookup for a cancelled run whose cancellation was really a timeout.
- Suggesting a rerun for a timed-out job.
- Paging past the first 100 annotations of a job.

Spec: inference
- **GitHub's annotation text.** A job counts as timed out only when one of its annotation messages matches `^The job has exceeded the maximum execution time of (\S+)$`. The limit is the captured text, shown as given. If GitHub rewords the message, ciwatch misses the timeout and reports the job as a plain cancelled job, which is today's behaviour. It can't invent a timeout from other text.
- **Where the cancelled step's lines are in the log.** This reuses `steplog.Parse`, which finds a step's lines by the step's start and end times in the jobs JSON. It starts at the `##[group]` line and ends at the last `##[error]` line, which is what keeps "Complete job" out. If GitHub changes the log layout, the block shows the wrong lines or none (`(no failed step in the log)`). The timed-out verdict and the exit code don't depend on this. Failed steps already rely on the same inference.

Spec: approved with
Approved under CJ's standing delegation (2026-11-24), including the inference: matching GitHub's annotation text can only miss a timeout, never invent one, and the step window reuses the approved steplog.Parse. Answers: (1) record job 136192284970's real annotations read-only with gh api repos/devvmhq/devvm/check-runs/136192284970/annotations into testdata/gh/ (they hold 'Canceling since a higher priority waiting request…' and 'The operation was canceled.', no timeout message), instead of a hand-written []; (2) the *-timed-out.json fixtures stay; (3) the newer-run hint stays unchanged; (4) don't copy job-log-failed.log.

Built in steps:
- test(cli): cover reporting a job that hit its time limit
- feat(cli): report a job that hit its time limit
- docs(cli): say that timed-out jobs get steps and tests too
- test(cli): compare the unchanged cancelled job byte for byte
- fix(cli): parse a timed-out job's whole log if no line names its step
- fix(docs): polish from review

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-23 23:50:33 -07:00

421 lines
18 KiB
Go

package cli_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/devvmhq/ciwatch/internal/cli"
)
// The recorded job of devvmhq/ciwatch that hit its timeout-minutes of 1m0s:
// the run, the job, the file of its full log, and the line ciwatch writes.
const (
timeoutRun = "45339081512"
timeoutJob = "138258094336"
timeoutLog = "job-45339081512-138258094336.log"
timeoutList = "run-list-timeout.json"
timeoutStderr = "ciwatch: job " + timeoutJob + " (slow) of run " + timeoutRun + " (Timeout) timed out after 1m0s"
timeoutHeader = "slow (Timeout, run " + timeoutRun + "): timed out after 1m0s"
annotationFile = "annotations-138258094336.json"
)
// timeoutOpts is the fake gh for the recorded timeout: its annotations and its
// full log.
func timeoutOpts() fakeOpts {
return fakeOpts{
annotations: map[string]string{timeoutJob: annotationFile},
jobLog: map[string]string{timeoutJob: timeoutLog},
}
}
// timeoutTable is what the table shows after the rows for the recorded
// timeout: the cancelled step's lines, colour codes removed.
const timeoutTable = `WORKFLOW STATUS CONCLUSION
Timeout completed cancelled
slow completed cancelled
` + timeoutHeader + `
Sleep past the limit:
##[group]Run echo "starting to sleep"
echo "starting to sleep"
sleep 150
echo "never printed"
shell: /usr/bin/bash -e {0}
##[endgroup]
starting to sleep
##[error]The operation was canceled.
`
// TestTimeoutReportsTheJob covers AC-1 and AC-5: the recorded timeout is
// reported on stderr and in the table, from the full log of the job, and the
// exit code stays 3.
func TestTimeoutReportsTheJob(t *testing.T) {
app, calls := fakeWith(t, timeoutList, nil, nil, timeoutOpts())
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
if !strings.Contains(stderr.String(), timeoutStderr+"\n") {
t.Errorf("stderr = %q, want the line %q", stderr.String(), timeoutStderr)
}
if got := stdout.String(); got != timeoutTable {
t.Errorf("stdout = %q, want %q", got, timeoutTable)
}
for _, unwanted := range []string{"before the slow step", "Complete job", "Cleaning up orphan processes"} {
if strings.Contains(stdout.String(), unwanted) {
t.Errorf("stdout = %q, want no %q", stdout.String(), unwanted)
}
}
if want := []string{timeoutJob}; !slices.Equal(calls.annotationIDs, want) {
t.Errorf("annotations fetched for jobs %v, want exactly %v", calls.annotationIDs, want)
}
if want := []string{timeoutJob}; !slices.Equal(calls.jobLogIDs, want) {
t.Errorf("full logs fetched for jobs %v, want exactly %v", calls.jobLogIDs, want)
}
if calls.logs != 0 {
t.Errorf("--log-failed calls = %d, want none: it prints nothing for a cancelled job", calls.logs)
}
if want := []string{"run", "view", timeoutRun, "--attempt", "1", "--log", "--job", timeoutJob}; len(calls.jobLogArgs) != 1 || !slices.Equal(calls.jobLogArgs[0], want) {
t.Errorf("full log call = %q, want %q", calls.jobLogArgs, want)
}
}
// TestTimeoutFindsTheStepWithoutStepLabels covers AC-1: the cancelled step is
// found by its times when no line carries its name, as when gh labels every
// line UNKNOWN STEP: the block is still shown and ends at the error line.
func TestTimeoutFindsTheStepWithoutStepLabels(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("..", "..", "testdata", "logs", timeoutLog))
if err != nil {
t.Fatal(err)
}
var relabelled []byte
for _, l := range bytes.SplitAfter(raw, []byte("\n")) {
if parts := bytes.SplitN(l, []byte("\t"), 3); len(parts) == 3 {
l = bytes.Join([][]byte{parts[0], []byte("UNKNOWN STEP"), parts[2]}, []byte("\t"))
}
relabelled = append(relabelled, l...)
}
opts := timeoutOpts()
opts.jobLogBytes = map[string][]byte{timeoutJob: relabelled}
app, _ := fakeWith(t, timeoutList, nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
got := stdout.String()
if strings.Contains(got, "(no failed step in the log)") || !strings.Contains(got, timeoutHeader+"\n Sleep past the limit:\n") {
t.Errorf("stdout = %q, want the cancelled step's block", got)
}
if !strings.HasSuffix(got, " starting to sleep\n ##[error]The operation was canceled.\n") || strings.Contains(got, "Cleaning up orphan") {
t.Errorf("stdout = %q, want the lines to end at the error line", got)
}
}
// TestTimeoutJSONMatchesGolden covers AC-1: the job in --json has
// timed_out_after and its one step, and the output is the golden file.
func TestTimeoutJSONMatchesGolden(t *testing.T) {
app, _ := fakeWith(t, timeoutList, nil, nil, timeoutOpts())
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"--json", "HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
want, err := os.ReadFile(filepath.Join("..", "..", "testdata", "golden", "run-list-timeout.json"))
if err != nil {
t.Fatal(err)
}
if stdout.String() != string(want) {
t.Errorf("output differs from the golden file:\n got: %s\nwant: %s", stdout.String(), want)
}
app, _ = fakeWith(t, timeoutList, nil, nil, timeoutOpts())
jobs, _ := jsonJobs(t, app, cli.ExitCancelled)
if got := string(jobs[timeoutJob]["timed_out_after"]); got != `"1m0s"` {
t.Errorf("timed_out_after = %s, want \"1m0s\"", got)
}
steps := jsonSteps(t, jobs[timeoutJob])
if len(steps) != 1 || steps[0].Name != "Sleep past the limit" || steps[0].Omitted != 0 || len(steps[0].Lines) != 8 {
t.Errorf("steps = %+v, want the one cancelled step with 8 lines and none omitted", steps)
}
if got := string(jobs[timeoutJob]["conclusion"]); got != `"cancelled"` {
t.Errorf("conclusion = %s, want \"cancelled\", as GitHub says", got)
}
}
// TestCancelledWithoutTimeoutAnnotationIsUnchanged covers AC-2: the
// concurrency cancel, whose recorded annotations have no timeout message, is
// reported as before: no block, no timed_out_after, no full log.
func TestCancelledWithoutTimeoutAnnotationIsUnchanged(t *testing.T) {
const cancelledJob = "136192284970"
opts := fakeOpts{annotations: map[string]string{cancelledJob: "annotations-136192284970.json"}}
app, calls := fakeWith(t, "run-list-cancelled.json", nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
if !slices.Contains(calls.annotationIDs, cancelledJob) {
t.Errorf("annotations fetched for jobs %v, want the cancelled job %s among them", calls.annotationIDs, cancelledJob)
}
if len(calls.jobLogIDs) != 0 || calls.logs != 0 {
t.Errorf("full log calls = %v, --log-failed calls = %d, want none", calls.jobLogIDs, calls.logs)
}
if strings.Contains(stdout.String(), "timed out") || strings.Contains(stderr.String(), "timed out") {
t.Errorf("stdout = %q, stderr = %q, want no timeout", stdout.String(), stderr.String())
}
if strings.Contains(stdout.String(), "\n\n") {
t.Errorf("stdout = %q, want the table without a block", stdout.String())
}
app, _ = fakeWith(t, "run-list-cancelled.json", nil, nil, opts)
jobs, _ := jsonJobs(t, app, cli.ExitCancelled)
for id, job := range jobs {
if _, ok := job["timed_out_after"]; ok {
t.Errorf("job %s has timed_out_after %s, want none", id, job["timed_out_after"])
}
}
// Byte for byte as today: with no annotations at all (what ciwatch saw
// before it asked for them), stdout, stderr and the JSON are the same as
// with the recorded annotations, newer-run line included.
base := func(args ...string) (string, string) {
app, _ := fakeWith(t, "run-list-cancelled.json", nil, nil, fakeOpts{})
var out, errOut bytes.Buffer
if code := app.Run(t.Context(), args, &out, &errOut); code != cli.ExitCancelled {
t.Fatalf("baseline exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, errOut.String())
}
return out.String(), errOut.String()
}
withAnn := func(args ...string) (string, string) {
app, _ := fakeWith(t, "run-list-cancelled.json", nil, nil, opts)
var out, errOut bytes.Buffer
if code := app.Run(t.Context(), args, &out, &errOut); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, errOut.String())
}
return out.String(), errOut.String()
}
for _, args := range [][]string{{"HEAD"}, {"--json", "HEAD"}} {
wantOut, wantErr := base(args...)
gotOut, gotErr := withAnn(args...)
if !strings.Contains(wantOut, "cancelled") {
t.Fatalf("baseline stdout = %q, want the cancelled run", wantOut)
}
if gotOut != wantOut {
t.Errorf("%v: stdout = %q, want the unchanged %q", args, gotOut, wantOut)
}
if gotErr != wantErr {
t.Errorf("%v: stderr = %q, want the unchanged %q", args, gotErr, wantErr)
}
}
}
// TestAnnotationsFailureReportsTheJobAsToday covers AC-3: when the annotations
// call fails, for a reason retries can't cure or after the retries run out,
// ciwatch warns and reports the job as a plain cancelled job.
func TestAnnotationsFailureReportsTheJobAsToday(t *testing.T) {
tests := []struct {
name string
opts func(t *testing.T) fakeOpts
wantCalls int
wantInErr string
}{
{"other error", func(t *testing.T) fakeOpts {
o := timeoutOpts()
o.annotationsErr = map[string]error{timeoutJob: stderrErr(t, "stderr-404.txt")}
return o
}, 1, "HTTP 404"},
{"network error until the retries run out", func(*testing.T) fakeOpts {
o := timeoutOpts()
o.annotationsNet = map[string]int{timeoutJob: 1000}
return o
}, 9, "gave up after 8 retries"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app, calls := fakeWith(t, timeoutList, nil, nil, tt.opts(t))
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
warning := "ciwatch: can't fetch the annotations of job " + timeoutJob + " (slow) of run " + timeoutRun + " (Timeout): "
if !strings.Contains(stderr.String(), warning) || !strings.Contains(stderr.String(), tt.wantInErr) {
t.Errorf("stderr = %q, want %q followed by the error (%q)", stderr.String(), warning, tt.wantInErr)
}
if strings.Contains(stderr.String(), "timed out after") {
t.Errorf("stderr = %q, want no timeout", stderr.String())
}
if want := "WORKFLOW STATUS CONCLUSION\nTimeout completed cancelled\n slow completed cancelled\n"; stdout.String() != want {
t.Errorf("stdout = %q, want the table of a plain cancelled job: %q", stdout.String(), want)
}
if len(calls.annotationIDs) != tt.wantCalls {
t.Errorf("annotations calls = %d, want %d", len(calls.annotationIDs), tt.wantCalls)
}
if len(calls.jobLogIDs) != 0 {
t.Errorf("full logs fetched for jobs %v, want none", calls.jobLogIDs)
}
app, _ = fakeWith(t, timeoutList, nil, nil, tt.opts(t))
jobs, _ := jsonJobs(t, app, cli.ExitCancelled)
if _, ok := jobs[timeoutJob]["timed_out_after"]; ok {
t.Errorf("job has timed_out_after %s, want none", jobs[timeoutJob]["timed_out_after"])
}
})
}
}
// TestTimeoutLogFailureIsStillReported covers AC-4: with the annotation saying
// timeout and the full log failing, the job is reported as timed out with
// (log unavailable), a warning, log_error, and the same exit code.
func TestTimeoutLogFailureIsStillReported(t *testing.T) {
opts := timeoutOpts()
opts.jobLogErr = map[string]error{timeoutJob: stderrErr(t, "stderr-404.txt")}
app, _ := fakeWith(t, timeoutList, nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
if !strings.Contains(stderr.String(), timeoutStderr+"\n") {
t.Errorf("stderr = %q, want the line %q", stderr.String(), timeoutStderr)
}
if !strings.Contains(stderr.String(), "ciwatch: can't fetch the log of job "+timeoutJob+" (slow) of run "+timeoutRun+" (Timeout)") {
t.Errorf("stderr = %q, want the warning that it can't fetch the log", stderr.String())
}
if want := timeoutHeader + "\n (log unavailable)\n"; !strings.Contains(stdout.String(), want) {
t.Errorf("stdout = %q, want it to contain %q", stdout.String(), want)
}
app, _ = fakeWith(t, timeoutList, nil, nil, opts)
jobs, _ := jsonJobs(t, app, cli.ExitCancelled)
job := jobs[timeoutJob]
if got := string(job["timed_out_after"]); got != `"1m0s"` {
t.Errorf("timed_out_after = %s, want \"1m0s\"", got)
}
if got := string(job["log_error"]); got == "" || got == `""` {
t.Errorf("log_error = %q, want the error", got)
}
}
// TestTimeoutExitCodes covers AC-5: a run whose only problem is a timed-out
// job exits 3; a failed run next to it exits as it does today, and the rerun
// hint counts only the failed run.
func TestTimeoutExitCodes(t *testing.T) {
timedOut := attemptRun(timeoutRun, "Timeout", "completed", "cancelled", 1)
t.Run("timeout only", func(t *testing.T) {
opts := timeoutOpts()
opts.listRaw = []string{list(timedOut)}
app, _ := fakeWith(t, "", nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Errorf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
if !strings.Contains(stderr.String(), timeoutStderr) {
t.Errorf("stderr = %q, want the timeout reported", stderr.String())
}
if strings.Contains(stderr.String(), "to rerun") {
t.Errorf("stderr = %q, want no rerun hint", stderr.String())
}
})
t.Run("failed run and timed-out run", func(t *testing.T) {
opts := timeoutOpts()
opts.listRaw = []string{list(failed(failedRun, "CI", 1), timedOut)}
opts.viewFixture = map[string]string{failedRun: "run-view-44707997310-test-failure.json"}
app, _ := fakeWith(t, "", nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitFailed {
t.Errorf("exit code = %d, want %d (stderr %q)", code, cli.ExitFailed, stderr.String())
}
if !strings.Contains(stderr.String(), timeoutStderr) {
t.Errorf("stderr = %q, want the timeout reported", stderr.String())
}
if !strings.Contains(stderr.String(), "ciwatch: 1 run failed; to rerun its failed jobs: ciwatch --rerun-failed "+sha) {
t.Errorf("stderr = %q, want the rerun hint to count 1 run", stderr.String())
}
})
}
// TestTimeoutCancelledByCtrlC covers AC-6: Ctrl-C during the annotations call
// or the full-log call prints no table and exits 4.
func TestTimeoutCancelledByCtrlC(t *testing.T) {
tests := []struct {
name string
set func(opts *fakeOpts, cancel context.CancelFunc)
saw func(c *counts) bool
}{
{
"annotations call", func(o *fakeOpts, cancel context.CancelFunc) { o.onAnnotations = func(string) { cancel() } },
func(c *counts) bool { return len(c.annotationIDs) > 0 },
},
{
"full-log call", func(o *fakeOpts, cancel context.CancelFunc) { o.onJobLog = func(string) { cancel() } },
func(c *counts) bool { return len(c.jobLogIDs) > 0 },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := timeoutOpts()
tt.set(&opts, cancel)
app, calls := fakeWith(t, timeoutList, nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(ctx, []string{"HEAD"}, &stdout, &stderr); code != cli.ExitUnchecked {
t.Errorf("exit code = %d, want %d (stderr %q)", code, cli.ExitUnchecked, stderr.String())
}
if !tt.saw(calls) {
t.Errorf("the %s was never made", tt.name)
}
if stdout.Len() != 0 {
t.Errorf("stdout = %q, want nothing", stdout.String())
}
if !strings.Contains(stderr.String(), "context canceled") {
t.Errorf("stderr = %q, want the cancellation", stderr.String())
}
})
}
}
// TestTimeoutNeedsTheExactMessage covers AC-7: annotations that only resemble
// the timeout message don't make a job timed out. The messages are the
// recorded ones, cut or changed by hand.
func TestTimeoutNeedsTheExactMessage(t *testing.T) {
for _, msg := range []string{
"The operation was canceled.",
"The job has exceeded the maximum execution time",
"The job has exceeded the maximum execution time of",
"The job has exceeded the maximum execution time of ",
"The job has exceeded the maximum execution time of 1m0s and then some",
"Error: The job has exceeded the maximum execution time of 1m0s",
"the job has exceeded the maximum execution time of 1m0s",
} {
t.Run(msg, func(t *testing.T) {
raw, err := json.Marshal([]map[string]string{{"message": msg}})
if err != nil {
t.Fatal(err)
}
opts := timeoutOpts()
opts.annotations = map[string]string{timeoutJob: string(raw)}
app, calls := fakeWith(t, timeoutList, nil, nil, opts)
var stdout, stderr bytes.Buffer
if code := app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr); code != cli.ExitCancelled {
t.Fatalf("exit code = %d, want %d (stderr %q)", code, cli.ExitCancelled, stderr.String())
}
if want := []string{timeoutJob}; !slices.Equal(calls.annotationIDs, want) {
t.Errorf("annotations fetched for jobs %v, want %v: the cancelled job is checked", calls.annotationIDs, want)
}
if strings.Contains(stdout.String(), "timed out") || strings.Contains(stderr.String(), "timed out") || len(calls.jobLogIDs) != 0 {
t.Errorf("stdout = %q, stderr = %q, full logs for %v, want no timeout", stdout.String(), stderr.String(), calls.jobLogIDs)
}
})
}
// The limit is whatever follows "of ", as given.
opts := timeoutOpts()
opts.annotations = map[string]string{timeoutJob: fmt.Sprintf(`[{"message":"The operation was canceled."},{"message":%q}]`, "The job has exceeded the maximum execution time of 6h0m0s")}
app, _ := fakeWith(t, timeoutList, nil, nil, opts)
var stdout, stderr bytes.Buffer
app.Run(t.Context(), []string{"HEAD"}, &stdout, &stderr)
if !strings.Contains(stderr.String(), "(Timeout) timed out after 6h0m0s\n") {
t.Errorf("stderr = %q, want the limit 6h0m0s as given", stderr.String())
}
}