diff --git a/.github/dataset-workflows/onboard-openneuro.yml b/.github/dataset-workflows/onboard-openneuro.yml index 6a7536a20..1c549b807 100644 --- a/.github/dataset-workflows/onboard-openneuro.yml +++ b/.github/dataset-workflows/onboard-openneuro.yml @@ -153,6 +153,10 @@ jobs: set -uo pipefail log="/tmp/prepare-${DATASET_ID}.log" on_id="on${DATASET_ID#ds}" + # Initialised because this step runs under `set -u` and the branch below can + # leave it unset; the copy and finalize reporters already do this. Without it + # the `[ -z "$msg" ]` fallback test itself died with `msg: unbound variable`. + msg="" # The upstream case keeps its own deliberate wording: the marker is the # classifier's signal for `upstream_inaccessible` and the sentence tells @@ -176,7 +180,15 @@ jobs: # multibyte character inside [...] matches its individual BYTES and can # fire on unrelated UTF-8. A line number plus a `sed` window has no such # ambiguity. - line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1)" + # `|| true` is load-bearing, not defensive noise. Actions runs a `run:` + # step under `bash -e {0}`, and this step adds `pipefail`, so when the log + # holds no marker `grep` exits 1, pipefail promotes it to the pipeline's + # status, and `-e` kills the step AT THIS ASSIGNMENT. The fallback below + # was then unreachable for exactly the failures it exists for: an OOM, a + # runner eviction, a cancellation, or a failure so early the log was never + # written. Nothing was posted at all, and the roll-up this whole change + # replaced is what landed. + line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1 || true)" if [ -n "$line" ]; then msg="$(sed -n "${line},$((line + 5))p" "$log" \ | sed -e 's/\x1b\[[0-9;]*m//g' -e 's/✖//g' \ @@ -281,7 +293,9 @@ jobs: log="/tmp/copy-${DATASET_ID}-${{ matrix.shard }}.log" on_id="on${DATASET_ID#ds}" msg="" - line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1)" + # `|| true`: see the prepare reporter. Under `bash -e` plus pipefail, a log + # with no marker makes grep exit 1 and kills the step before the fallback. + line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1 || true)" if [ -n "$line" ]; then msg="$(sed -n "${line},$((line + 5))p" "$log" \ | sed -e 's/\x1b\[[0-9;]*m//g' -e 's/✖//g' \ @@ -367,7 +381,9 @@ jobs: log="/tmp/finalize-${DATASET_ID}.log" on_id="on${DATASET_ID#ds}" msg="" - line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1)" + # `|| true`: see the prepare reporter. Under `bash -e` plus pipefail, a log + # with no marker makes grep exit 1 and kills the step before the fallback. + line="$(grep -nF '✖' "$log" 2>/dev/null | tail -n 1 | cut -d: -f1 || true)" if [ -n "$line" ]; then msg="$(sed -n "${line},$((line + 5))p" "$log" \ | sed -e 's/\x1b\[[0-9;]*m//g' -e 's/✖//g' \ diff --git a/backend/test/import-capture-workflow.test.ts b/backend/test/import-capture-workflow.test.ts index 13ca224bf..bf614c13c 100644 --- a/backend/test/import-capture-workflow.test.ts +++ b/backend/test/import-capture-workflow.test.ts @@ -22,7 +22,8 @@ */ import { describe, expect, test } from "bun:test"; -import { readFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; import { join } from "node:path"; import { classifyImportFailure } from "../src/services/import-failure-cause"; @@ -167,3 +168,114 @@ describe("the deployable copy keeps the properties capture depends on", () => { expect(workflow()).toContain('grep -qF "[openneuro-upstream-inaccessible]"'); }); }); + +/** + * The reporters RUN here, rather than being asserted as text. + * + * Every test above reads the file, and that is what let issue #1364 ship: the + * fallback assertion was `expect(workflow()).toContain(fallback)`, which passed + * over dead code. Actions runs a `run:` step under `bash -e {0}` and these steps + * add `pipefail`, so a log with no `✖` made `grep` exit 1, pipefail promoted it to + * the pipeline's status, and `-e` killed the step at the assignment -- before the + * fallback that the test was busy confirming the existence of. Nothing was posted, + * and the stage roll-up this epic replaced is what landed, for exactly the failure + * modes the fallback exists for: an OOM, an evicted runner, a cancellation, or a + * failure early enough that the log was never written. + * + * The script is extracted from the file and executed up to the point where `msg` is + * final; the `jq` payload and the `curl` POST are cut off, so nothing here needs jq + * and nothing can reach `api.nemar.org`. `bash -e` is passed explicitly because + * that is what Actions does and it is the whole point. + */ +describe("the reporters survive a log with no failure marker", () => { + /** The `run:` body of one reporter step, dedented, cut where `msg` is final. */ + function reporterScript(stage: "prepare" | "copy" | "finalize"): string { + const src = workflow(); + const start = src.indexOf(`- name: Report ${stage} failure`); + expect(start).toBeGreaterThan(-1); + const runAt = src.indexOf("run: |", start); + expect(runAt).toBeGreaterThan(-1); + const body = src.slice(src.indexOf("\n", runAt) + 1); + const lines: string[] = []; + for (const line of body.split("\n")) { + // The step's body is indented under `run: |`; the first line at or below the + // step's own indentation ends it. + if (line.trim() !== "" && !line.startsWith(" ")) break; + lines.push(line.replace(/^ {10}/, "")); + if (line.includes('payload="$(jq -nc')) break; + } + const cut = lines.findIndex((l) => l.includes('payload="$(jq -nc')); + const kept = (cut === -1 ? lines : lines.slice(0, cut)).join("\n"); + // `${{ matrix.shard }}` is an Actions expression, not shell: the copy reporter + // interpolates it into its log path. Substituted with a literal so the script + // runs, exactly as Actions would have substituted it before bash saw it. + return `${kept.replace(/\$\{\{ matrix\.shard \}\}/g, "0")}\nprintf '%s\\n' "$msg"\n`; + } + + async function runReporter( + stage: "prepare" | "copy" | "finalize", + logContents: string | null, + ): Promise<{ code: number; out: string }> { + const dir = mkdtempSync(join(tmpdir(), "reporter-")); + const datasetId = "ds000001"; + if (logContents !== null) { + const name = stage === "copy" ? `copy-${datasetId}-0.log` : `${stage}-${datasetId}.log`; + writeFileSync(join(dir, name), logContents); + } + const script = join(dir, "reporter.sh"); + // The reporters hardcode /tmp; point them at the scratch directory instead so a + // parallel test (or a real /tmp file) cannot feed them. + writeFileSync(script, reporterScript(stage).replaceAll('"/tmp/', `"${dir}/`)); + const proc = Bun.spawn(["bash", "-e", script], { + env: { ...process.env, DATASET_ID: datasetId, RUN_URL: "https://example/run/1" }, + stdout: "pipe", + stderr: "pipe", + }); + const [out, err] = await Promise.all([ + new Response(proc.stdout).text(), + new Response(proc.stderr).text(), + ]); + return { code: await proc.exited, out: out + err }; + } + + for (const stage of ["prepare", "copy", "finalize"] as const) { + test(`${stage}: a log with no marker yields the named fallback`, async () => { + const { code, out } = await runReporter(stage, "cloning...\nsome output\nno marker here\n"); + expect(code).toBe(0); + expect(out).toContain(`${stage} failed with no CLI failure line captured`); + }); + + test(`${stage}: a MISSING log yields the named fallback`, async () => { + // The OOM and evicted-runner case: the step that would have written the log + // never got far enough to create it. + const { code, out } = await runReporter(stage, null); + expect(code).toBe(0); + expect(out).toContain(`${stage} failed with no CLI failure line captured`); + }); + + test(`${stage}: a real CLI failure line still wins over the fallback`, async () => { + // The fix must not have turned every report into the fallback. + const { code, out } = await runReporter( + stage, + `some output\n✖ Failed to push: remote: Invalid username or token.\nmore\n`, + ); + expect(code).toBe(0); + expect(out).toContain("Failed to push"); + expect(out).not.toContain("no CLI failure line captured"); + // ANSI stripped, marker removed, collapsed to one line. + expect(out).not.toContain("["); + expect(out).not.toContain("✖"); + }); + } + + test("prepare still prefers the upstream-inaccessible marker when present", async () => { + // Its own branch, and the one case whose wording the classifier keys on. + const { code, out } = await runReporter( + "prepare", + "[openneuro-upstream-inaccessible] some detail\n", + ); + expect(code).toBe(0); + expect(out).toContain("[openneuro-upstream-inaccessible]"); + expect(out).toContain("not anonymously readable"); + }); +});