|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { |
| 4 | + chmodSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | +import { dirname, join } from 'node:path'; |
| 12 | +import { describe, it } from 'node:test'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | +import { parse } from 'yaml'; |
| 15 | + |
| 16 | +const workflowPath = join( |
| 17 | + dirname(fileURLToPath(import.meta.url)), |
| 18 | + '..', |
| 19 | + 'workflows', |
| 20 | + 'ci.yml', |
| 21 | +); |
| 22 | +const testSteps = parse(readFileSync(workflowPath, 'utf8')).jobs.test.steps; |
| 23 | + |
| 24 | +function step(name) { |
| 25 | + const value = testSteps.find((candidate) => candidate.name === name); |
| 26 | + assert.ok(value, `missing ${name} step`); |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +describe('ci.yml disk-pressure evidence', () => { |
| 31 | + it('starts sampling before npm ci and preserves those samples for upload', () => { |
| 32 | + const install = step('Install dependencies').run; |
| 33 | + const npmCi = install.indexOf('npm ci'); |
| 34 | + |
| 35 | + assert.match( |
| 36 | + install, |
| 37 | + /DISK_SAMPLES="\$\{RUNNER_TEMP\}\/disk-pressure-samples\.log"/, |
| 38 | + ); |
| 39 | + assert.ok(npmCi > install.indexOf('DFSAMPLE ')); |
| 40 | + assert.match(install, /\( while sleep 10; do sample_disk; done \) &/); |
| 41 | + assert.ok(npmCi > install.indexOf('( while sleep 10')); |
| 42 | + assert.match(install, /trap .*SAMPLER_PID.* EXIT/); |
| 43 | + |
| 44 | + const tests = step('Run tests and generate reports').run; |
| 45 | + assert.match( |
| 46 | + tests, |
| 47 | + /DISK_SAMPLES="\$\{RUNNER_TEMP\}\/disk-pressure-samples\.log"\nif \[ ! -s "\$DISK_SAMPLES" \]; then\n {2}echo "DISKCONTEXT .*" > "\$DISK_SAMPLES" 2>\/dev\/null \|\| true\nfi/, |
| 48 | + ); |
| 49 | + assert.ok(tests.indexOf('export TMPDIR=') > tests.indexOf('DISK_SAMPLES=')); |
| 50 | + |
| 51 | + const sampleFormat = (script) => { |
| 52 | + const match = script.match( |
| 53 | + /sample="DFSAMPLE .*\/proc\/meminfo 2>\/dev\/null(?: \|\| true)?\)\]"/, |
| 54 | + ); |
| 55 | + assert.ok(match); |
| 56 | + return match[0] |
| 57 | + .replaceAll('${RUNNER_TEMP:-/tmp}', '${TMPDIR}') |
| 58 | + .replace( |
| 59 | + ' /proc/meminfo 2>/dev/null || true)]', |
| 60 | + ' /proc/meminfo 2>/dev/null)]', |
| 61 | + ); |
| 62 | + }; |
| 63 | + const headerLine = (script) => |
| 64 | + script |
| 65 | + .split('\n') |
| 66 | + .find((line) => line.trimStart().startsWith('echo "DISKCONTEXT ')) |
| 67 | + ?.trim(); |
| 68 | + assert.equal(headerLine(install), headerLine(tests)); |
| 69 | + assert.equal(sampleFormat(install), sampleFormat(tests)); |
| 70 | + |
| 71 | + const upload = step('Upload disk-pressure samples'); |
| 72 | + assert.equal(upload.if, '${{ failure() }}'); |
| 73 | + assert.equal(upload.with['if-no-files-found'], 'ignore'); |
| 74 | + assert.equal( |
| 75 | + upload.with.path, |
| 76 | + '${{ runner.temp }}/disk-pressure-samples.log', |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('keeps install failure status while writing the pre-install sample', () => { |
| 81 | + const root = mkdtempSync(join(tmpdir(), 'ci-disk-pressure-')); |
| 82 | + const npm = join(root, 'npm'); |
| 83 | + writeFileSync(npm, '#!/usr/bin/env bash\nexit 42\n'); |
| 84 | + chmodSync(npm, 0o755); |
| 85 | + |
| 86 | + try { |
| 87 | + const result = spawnSync( |
| 88 | + 'bash', |
| 89 | + ['-e', '-o', 'pipefail', '-c', step('Install dependencies').run], |
| 90 | + { |
| 91 | + encoding: 'utf8', |
| 92 | + timeout: 30_000, |
| 93 | + env: { |
| 94 | + ...process.env, |
| 95 | + PATH: `${root}:${process.env.PATH}`, |
| 96 | + RUNNER_TEMP: root, |
| 97 | + }, |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + assert.equal(result.error, undefined); |
| 102 | + assert.equal( |
| 103 | + result.status, |
| 104 | + 42, |
| 105 | + `signal: ${result.signal}\nerror: ${result.error}\nstdout: ${result.stdout}\nstderr: ${result.stderr}`, |
| 106 | + ); |
| 107 | + const samples = readFileSync( |
| 108 | + join(root, 'disk-pressure-samples.log'), |
| 109 | + 'utf8', |
| 110 | + ); |
| 111 | + assert.match(samples, /^DISKCONTEXT /m); |
| 112 | + assert.match(samples, /^DFSAMPLE /m); |
| 113 | + } finally { |
| 114 | + rmSync(root, { recursive: true, force: true }); |
| 115 | + } |
| 116 | + }); |
| 117 | +}); |
0 commit comments