|
14 | 14 | // limitations under the License. |
15 | 15 |
|
16 | 16 | import assert from 'node:assert/strict'; |
17 | | -import { mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs'; |
| 17 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 18 | +import { createServer } from 'node:http'; |
18 | 19 | import { createRequire } from 'node:module'; |
19 | 20 | import { dirname, resolve } from 'node:path'; |
20 | 21 | import { fileURLToPath } from 'node:url'; |
21 | 22 | import vm from 'node:vm'; |
22 | 23 | import test from 'node:test'; |
23 | | -import { execFileSync } from 'node:child_process'; |
| 24 | +import { execFile, execFileSync } from 'node:child_process'; |
| 25 | +import { promisify } from 'node:util'; |
24 | 26 |
|
25 | 27 | const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); |
26 | 28 | const bundle = resolve(root, 'actions/paths-filter/dist/index.js'); |
@@ -204,3 +206,81 @@ test('aggregate jobs still report failures but do not keep cancelled PR runs ali |
204 | 206 | assert.ok(!condition.includes('always()'), workflow); |
205 | 207 | } |
206 | 208 | }); |
| 209 | + |
| 210 | +test('ingress validates the Maven wrapper before its first Maven invocation', () => { |
| 211 | + const steps = workflows.get('integrated-test-k8s-ingress.yml').jobs.build.steps; |
| 212 | + const preflight = steps.findIndex(step => step.name === 'Ensure Maven wrapper JAR'); |
| 213 | + const firstMaven = steps.findIndex(step => /\.\/mvnw\b/.test(step.run ?? '')); |
| 214 | + assert.ok(preflight >= 0 && firstMaven > preflight); |
| 215 | + assert.equal(steps[preflight].shell, 'bash'); |
| 216 | +}); |
| 217 | + |
| 218 | +test('ingress Maven wrapper preflight handles cached, downloaded and failed artifacts', { timeout: 45000 }, async t => { |
| 219 | + const preflight = workflows.get('integrated-test-k8s-ingress.yml').jobs.build.steps |
| 220 | + .find(step => step.name === 'Ensure Maven wrapper JAR'); |
| 221 | + assert.ok(preflight, 'The ingress job must validate the wrapper before running Maven'); |
| 222 | + const temporary = mkdtempSync(resolve(root, '.github/scripts/.wrapper-test-')); |
| 223 | + t.after(() => rmSync(temporary, { recursive: true, force: true })); |
| 224 | + |
| 225 | + // Only the archive/launcher-entry contract is under test; no Maven download is needed. |
| 226 | + const classes = resolve(temporary, 'classes'); |
| 227 | + const launcher = 'org/apache/maven/wrapper/MavenWrapperMain.class'; |
| 228 | + mkdirSync(dirname(resolve(classes, launcher)), { recursive: true }); |
| 229 | + writeFileSync(resolve(classes, launcher), 'launcher entry fixture'); |
| 230 | + const validJar = resolve(temporary, 'valid.jar'); |
| 231 | + const otherJar = resolve(temporary, 'other.jar'); |
| 232 | + execFileSync('jar', ['cf', validJar, '-C', classes, '.']); |
| 233 | + writeFileSync(resolve(classes, 'Other.class'), 'unrelated entry fixture'); |
| 234 | + execFileSync('jar', ['cf', otherJar, '-C', classes, 'Other.class']); |
| 235 | + const valid = readFileSync(validJar); |
| 236 | + const corrupt = Buffer.from('incomplete cached download'); |
| 237 | + const fixtures = [ |
| 238 | + { name: 'valid cache avoids downloading', initial: valid, requests: 0, succeeds: true }, |
| 239 | + { name: 'missing wrapper is downloaded', requests: 1, succeeds: true }, |
| 240 | + { name: 'corrupt cache is replaced', initial: corrupt, requests: 1, succeeds: true }, |
| 241 | + { name: 'two transient 503s are retried', failures: 2, requests: 3, succeeds: true }, |
| 242 | + { name: 'invalid download preserves the old file', initial: corrupt, payload: Buffer.from('not a jar'), requests: 1 }, |
| 243 | + { name: 'archive without launcher is rejected', payload: readFileSync(otherJar), requests: 1 }, |
| 244 | + { name: 'persistent 503s stop after bounded retries', failures: 6, initial: corrupt, requests: 6 }, |
| 245 | + { name: 'missing wrapper URL fails without a request', missingUrl: true, requests: 0 }, |
| 246 | + ]; |
| 247 | + const run = promisify(execFile); |
| 248 | + for (const [index, fixture] of fixtures.entries()) { |
| 249 | + await t.test(fixture.name, async subtest => { |
| 250 | + const working = resolve(temporary, `case-${index}`); |
| 251 | + const wrapper = resolve(working, '.mvn/wrapper'); |
| 252 | + const target = resolve(wrapper, 'maven-wrapper.jar'); |
| 253 | + mkdirSync(wrapper, { recursive: true }); |
| 254 | + if (fixture.initial) writeFileSync(target, fixture.initial); |
| 255 | + let requests = 0; |
| 256 | + const server = createServer((_request, response) => { |
| 257 | + requests += 1; |
| 258 | + response.writeHead(requests <= (fixture.failures ?? 0) ? 503 : 200); |
| 259 | + response.end(requests <= (fixture.failures ?? 0) ? 'unavailable' : fixture.payload ?? valid); |
| 260 | + }); |
| 261 | + await new Promise((resolve, reject) => { |
| 262 | + server.once('error', reject); |
| 263 | + server.listen(0, '127.0.0.1', resolve); |
| 264 | + }); |
| 265 | + subtest.after(() => new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve()))); |
| 266 | + writeFileSync(resolve(wrapper, 'maven-wrapper.properties'), fixture.missingUrl |
| 267 | + ? 'distributionUrl=unused\n' |
| 268 | + : `wrapperUrl=http://127.0.0.1:${server.address().port}/wrapper.jar\n`); |
| 269 | + const execution = run('bash', ['--noprofile', '--norc', '-e', '-o', 'pipefail', '-c', preflight.run], { |
| 270 | + cwd: working, |
| 271 | + env: { ...process.env, NO_PROXY: '127.0.0.1', no_proxy: '127.0.0.1' }, |
| 272 | + timeout: 20000, |
| 273 | + }); |
| 274 | + if (fixture.succeeds) { |
| 275 | + await execution; |
| 276 | + assert.deepEqual(readFileSync(target), valid); |
| 277 | + } else { |
| 278 | + await assert.rejects(execution, error => Number.isInteger(error.code) && error.code !== 0); |
| 279 | + if (fixture.initial) assert.deepEqual(readFileSync(target), fixture.initial); |
| 280 | + else assert.equal(existsSync(target), false); |
| 281 | + } |
| 282 | + assert.equal(requests, fixture.requests); |
| 283 | + assert.ok(readdirSync(wrapper).every(name => !name.startsWith('maven-wrapper.jar.')), 'Temporary downloads must be removed'); |
| 284 | + }); |
| 285 | + } |
| 286 | +}); |
0 commit comments