Skip to content

Commit dec62d5

Browse files
committed
test(ci): require every RunsOn job to wait for the apt lock
A new RunsOn machine can still be running apt from boot, and apt-get fails at once on the held lock. The two apt-get steps on RunsOn already pass -o DPkg::Lock::Timeout=300. This test fails when a step in a RunsOn job, or in a local action it uses, runs apt without it.
1 parent 3ceda32 commit dec62d5

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

‎scripts/__tests__/workflow-runs-on.test.mjs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { existsSync } from 'node:fs'
2+
import { join } from 'node:path'
13
import { describe, expect, it } from 'vitest'
4+
import { REPO_ROOT } from './lib/repo-root.mjs'
25
import {
36
isRunsOnJob,
47
MATRIX_RUNNERS,
@@ -42,6 +45,50 @@ function isExpressionRunner(job) {
4245
const expressionRunnerRefs = () =>
4346
refsWhere(({ job }) => isExpressionRunner(job))
4447

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+
4592
describe('RunsOn runner labels', () => {
4693
it('builds the labels ADR 0004 names', () => {
4794
expect(runsOnLabel(16)).toBe(
@@ -99,3 +146,19 @@ describe('RunsOn runner labels', () => {
99146
).toBe(true)
100147
})
101148
})
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

Comments
 (0)