|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { join } from 'node:path' |
1 | 3 | import { describe, expect, it } from 'vitest' |
| 4 | +import { REPO_ROOT } from './lib/repo-root.mjs' |
2 | 5 | import { |
3 | 6 | isRunsOnJob, |
4 | 7 | MATRIX_RUNNERS, |
@@ -42,6 +45,50 @@ function isExpressionRunner(job) { |
42 | 45 | const expressionRunnerRefs = () => |
43 | 46 | refsWhere(({ job }) => isExpressionRunner(job)) |
44 | 47 |
|
| 48 | +// A new RunsOn machine can still be running apt from boot. apt-get fails at |
| 49 | +// once on the held lock unless told to wait for it. |
| 50 | +const APT_COMMAND = |
| 51 | + /\bapt(?:-get)?\s.*\b(?:update|install|upgrade|dist-upgrade|remove|purge|autoremove)\b/ |
| 52 | + |
| 53 | +export function aptFinding(run) { |
| 54 | + for (const line of String(run).split('\n')) { |
| 55 | + if (/^\s*#/.test(line)) continue |
| 56 | + if (APT_COMMAND.test(line) && !line.includes('DPkg::Lock::Timeout=')) |
| 57 | + return `runs apt without waiting for the lock: "${line.trim()}". Add -o DPkg::Lock::Timeout=300.` |
| 58 | + } |
| 59 | + return null |
| 60 | +} |
| 61 | + |
| 62 | +/** A step, or the steps of the local composite action it uses. */ |
| 63 | +function stepsOf(step, seen = new Set()) { |
| 64 | + const uses = typeof step?.uses === 'string' ? step.uses.trim() : '' |
| 65 | + if (!uses.startsWith('./')) return [step] |
| 66 | + const dir = uses.replace(/^\.\//, '').replace(/\/$/, '') |
| 67 | + if (seen.has(dir)) return [] |
| 68 | + seen.add(dir) |
| 69 | + for (const file of ['action.yml', 'action.yaml']) { |
| 70 | + const rel = `${dir}/${file}` |
| 71 | + if (existsSync(join(REPO_ROOT, rel))) |
| 72 | + return (readWorkflow(rel)?.runs?.steps ?? []).flatMap((inner) => |
| 73 | + stepsOf(inner, seen), |
| 74 | + ) |
| 75 | + } |
| 76 | + return [] |
| 77 | +} |
| 78 | + |
| 79 | +export function aptFindingsInRunsOnJobs() { |
| 80 | + return jobs |
| 81 | + .filter(({ job, ref }) => isRunsOnJob(job, ref)) |
| 82 | + .flatMap(({ ref, job }) => |
| 83 | + (job.steps ?? []).flatMap((step, i) => |
| 84 | + stepsOf(step) |
| 85 | + .map((inner) => aptFinding(inner?.run ?? '')) |
| 86 | + .filter(Boolean) |
| 87 | + .map((finding) => `${ref} step ${i + 1}: ${finding}`), |
| 88 | + ), |
| 89 | + ) |
| 90 | +} |
| 91 | + |
45 | 92 | describe('RunsOn runner labels', () => { |
46 | 93 | it('builds the labels ADR 0004 names', () => { |
47 | 94 | expect(runsOnLabel(16)).toBe( |
@@ -99,3 +146,19 @@ describe('RunsOn runner labels', () => { |
99 | 146 | ).toBe(true) |
100 | 147 | }) |
101 | 148 | }) |
| 149 | + |
| 150 | +describe('apt on a new RunsOn machine', () => { |
| 151 | + it('recognises an apt command that does not wait for the lock', () => { |
| 152 | + expect(aptFinding('sudo apt-get update')).toMatch(/DPkg::Lock::Timeout/) |
| 153 | + expect(aptFinding('sudo apt install -y jq')).toMatch(/DPkg::Lock::Timeout/) |
| 154 | + expect( |
| 155 | + aptFinding('sudo apt-get -o DPkg::Lock::Timeout=300 install -y doxygen'), |
| 156 | + ).toBeNull() |
| 157 | + expect(aptFinding('# sudo apt-get update')).toBeNull() |
| 158 | + expect(aptFinding('apt-cache policy doxygen')).toBeNull() |
| 159 | + }) |
| 160 | + |
| 161 | + it('waits for the apt lock in every RunsOn job', () => { |
| 162 | + expect(aptFindingsInRunsOnJobs()).toEqual([]) |
| 163 | + }) |
| 164 | +}) |
0 commit comments