mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:23:53 +08:00
fix: freeze release runtime tools and npm consumer artifacts
This commit is contained in:
1 parent
a70ec66fe3
commit
2f359c6bf0
63 files changed
+42562
-8738
No files matched your search
@@ -45,8 +45,27 @@ test('release images receive the same-run artifact and verify it before use', ()
|
||||
const workflow = yaml.load(
|
||||
fs.readFileSync('.github/workflows/build-docker-image.yml', 'utf8'),
|
||||
);
|
||||
assert.equal(workflow.jobs['build-static'].needs, 'validate');
|
||||
const upload = workflow.jobs['build-static'].steps.find(step => step.uses?.startsWith('actions/upload-artifact@'));
|
||||
assert.deepEqual(workflow.jobs['build-static'].needs, [
|
||||
'validate',
|
||||
'runtime-tools',
|
||||
]);
|
||||
const staticSteps = workflow.jobs['build-static'].steps;
|
||||
const toolsDownload = staticSteps.findIndex((step) =>
|
||||
step.uses?.startsWith('actions/download-artifact@'),
|
||||
);
|
||||
assert.notEqual(toolsDownload, -1);
|
||||
assert.equal(
|
||||
staticSteps[toolsDownload].with.name,
|
||||
'qinglong-runtime-tools-${{ github.sha }}',
|
||||
);
|
||||
assert.equal(staticSteps[toolsDownload].with.path, 'static/');
|
||||
const buildInfo = staticSteps.findIndex(
|
||||
(step) => step.run === 'pnpm build:info',
|
||||
);
|
||||
assert.ok(buildInfo > toolsDownload);
|
||||
const upload = workflow.jobs['build-static'].steps.find((step) =>
|
||||
step.uses?.startsWith('actions/upload-artifact@'),
|
||||
);
|
||||
assert.equal(upload.with['include-hidden-files'], true);
|
||||
for (const name of [
|
||||
'build-alpine',
|
||||
@@ -78,6 +97,65 @@ test('release images receive the same-run artifact and verify it before use', ()
|
||||
}
|
||||
});
|
||||
|
||||
test('npm publication uses the archive approved by every consumer without rebuilding it', () => {
|
||||
const yaml = require('js-yaml');
|
||||
const workflow = yaml.load(
|
||||
fs.readFileSync('.github/workflows/build-docker-image.yml', 'utf8'),
|
||||
);
|
||||
const gate = yaml.load(
|
||||
fs.readFileSync('.github/workflows/npm-package.yml', 'utf8'),
|
||||
);
|
||||
assert.deepEqual(gate.jobs.verified.needs, [
|
||||
'archive',
|
||||
'consumer',
|
||||
'source-fallback',
|
||||
]);
|
||||
assert.equal(gate.jobs.consumer.needs, 'archive');
|
||||
assert.deepEqual(gate.jobs.consumer.strategy.matrix.client, [
|
||||
{ npm: '10', mode: 'local' },
|
||||
{ npm: '12', mode: 'local' },
|
||||
{ npm: '12', mode: 'global' },
|
||||
]);
|
||||
assert.deepEqual(gate.jobs.consumer.strategy.matrix.sqlite, [
|
||||
'prebuilt',
|
||||
'source',
|
||||
]);
|
||||
const publish = workflow.jobs.publish;
|
||||
assert.ok(publish.needs.includes('npm-package'));
|
||||
assert.equal(
|
||||
publish.env.VERIFIED_TARBALL_SHA256,
|
||||
'${{ needs.npm-package.outputs.tarball-sha256 }}',
|
||||
);
|
||||
const approvedDownload = publish.steps.find((step) =>
|
||||
step.uses?.startsWith('actions/download-artifact@'),
|
||||
);
|
||||
assert.equal(
|
||||
approvedDownload.with.name,
|
||||
'${{ needs.npm-package.outputs.artifact-name }}',
|
||||
);
|
||||
const checks = publish.steps.findIndex((step) =>
|
||||
step.run?.includes(
|
||||
'assert.equal(fileHash(tarball), process.env.VERIFIED_TARBALL_SHA256)',
|
||||
),
|
||||
);
|
||||
const installer = publish.steps.findIndex((step) =>
|
||||
step.run?.includes('node scripts/install-runtime-tools.cjs'),
|
||||
);
|
||||
const publication = publish.steps.findIndex((step) =>
|
||||
step.run?.includes('publish "$QINGLONG_PUBLISH_TARBALL" --ignore-scripts'),
|
||||
);
|
||||
assert.ok(checks !== -1 && installer > checks && publication > installer);
|
||||
const operations = publish.steps.map((step) => step.run || '').join('\n');
|
||||
assert.doesNotMatch(
|
||||
operations,
|
||||
/build-npm-package|build:front|build:back|build:info/,
|
||||
);
|
||||
assert.doesNotMatch(
|
||||
operations,
|
||||
/\b(?:npm|pnpm)\s+(?:install|ci|rebuild|pack)\b/,
|
||||
);
|
||||
});
|
||||
|
||||
test('complete artifact manifests reject changed, missing, extra files and untracked build inputs', (t) => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-build-manifest-'));
|
||||
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
@@ -97,6 +175,8 @@ test('complete artifact manifests reject changed, missing, extra files and untra
|
||||
'dist/chunks/main.js',
|
||||
'dist/assets/main.css',
|
||||
'dist/.well-known/config',
|
||||
'runtime-tools.tgz',
|
||||
'runtime-tools-proof.json',
|
||||
];
|
||||
for (const file of artifacts) {
|
||||
fs.mkdirSync(path.dirname(path.join(dir, 'static', file)), {
|
||||
@@ -123,6 +203,8 @@ test('complete artifact manifests reject changed, missing, extra files and untra
|
||||
'dist/chunks/main.js',
|
||||
'dist/assets/main.css',
|
||||
'dist/.well-known/config',
|
||||
'runtime-tools.tgz',
|
||||
'runtime-tools-proof.json',
|
||||
]) {
|
||||
const file = path.join(dir, 'static', name);
|
||||
fs.writeFileSync(file, 'stale');
|
||||
|
||||
@@ -8,18 +8,21 @@ const yaml = require('js-yaml');
|
||||
const workflow = yaml.load(
|
||||
fs.readFileSync('.github/workflows/build-docker-image.yml', 'utf8'),
|
||||
);
|
||||
const publication = workflow.jobs.publish.steps.find(
|
||||
(step) => step.name === 'Publish npm package with OIDC',
|
||||
).run;
|
||||
const publicationStep = workflow.jobs.publish.steps.find(
|
||||
(step) => step.name === 'Publish the verified npm archive with OIDC',
|
||||
);
|
||||
assert.ok(publicationStep, 'Verified archive publication step must exist');
|
||||
const publication = publicationStep.run;
|
||||
|
||||
test('npm publication waits for every release image, including Python 3.10', () => {
|
||||
for (const name of [
|
||||
test('npm publication waits for the archive gate and every release image, including Python 3.10', () => {
|
||||
assert.deepEqual(workflow.jobs.publish.needs, [
|
||||
'build-alpine',
|
||||
'build-debian',
|
||||
'build-alpine310',
|
||||
'build-debian310',
|
||||
])
|
||||
assert.ok(workflow.jobs.publish.needs.includes(name), name);
|
||||
'npm-package',
|
||||
]);
|
||||
assert.equal(workflow.jobs.publish.permissions['id-token'], 'write');
|
||||
});
|
||||
|
||||
for (const [scenario, expectedStatus, published] of [
|
||||
@@ -28,30 +31,49 @@ for (const [scenario, expectedStatus, published] of [
|
||||
['unauthorized', 1, false],
|
||||
['network', 1, false],
|
||||
['unexpected-version', 1, false],
|
||||
['wrong-npm-version', 1, false],
|
||||
]) {
|
||||
test(`npm publication handles ${scenario} without publishing an unverified version`, (t) => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-npm-publication-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const bin = path.join(root, 'bin');
|
||||
fs.mkdirSync(bin);
|
||||
fs.symlinkSync(process.execPath, path.join(bin, 'node'));
|
||||
const npmCLI = path.join(
|
||||
root,
|
||||
'npm-publisher-tools/lib/node_modules/npm/bin/npm-cli.js',
|
||||
);
|
||||
fs.mkdirSync(path.dirname(npmCLI), { recursive: true });
|
||||
const tarball = path.join(root, 'verified qinglong-2.23.0.tgz');
|
||||
fs.writeFileSync(tarball, 'Fixture only: never install or publish this file.\n');
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'package.json'),
|
||||
JSON.stringify({ name: '@fixture/qinglong', version: '2.22.0' }),
|
||||
JSON.stringify({ name: '@fixture/qinglong', version: '2.23.0' }),
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(bin, 'npm'),
|
||||
`#!/usr/bin/env node
|
||||
npmCLI,
|
||||
`
|
||||
const fs = require('node:fs');
|
||||
if (process.argv[2] === 'publish') { fs.writeFileSync('published', 'yes'); process.exit(0); }
|
||||
if (process.argv[2] !== 'view') process.exit(99);
|
||||
const args = process.argv.slice(2);
|
||||
fs.appendFileSync('npm-calls.jsonl', JSON.stringify(args) + '\\n');
|
||||
if (args[0] === '--version') {
|
||||
console.log(process.env.QL_PUBLISH_SCENARIO === 'wrong-npm-version' ? '12.1.0' : '12.2.0');
|
||||
process.exit(0);
|
||||
}
|
||||
if (args[0] === 'publish') { fs.writeFileSync('published', JSON.stringify(args)); process.exit(0); }
|
||||
if (args[0] !== 'view') process.exit(99);
|
||||
switch (process.env.QL_PUBLISH_SCENARIO) {
|
||||
case 'existing': console.log(JSON.stringify('2.22.0')); break;
|
||||
case 'unexpected-version': console.log(JSON.stringify('2.21.0')); break;
|
||||
case 'existing': console.log(JSON.stringify('2.23.0')); break;
|
||||
case 'unexpected-version': console.log(JSON.stringify('2.22.0')); break;
|
||||
case 'missing': console.log(JSON.stringify({error:{code:'E404'}})); process.exit(1);
|
||||
case 'unauthorized': console.log(JSON.stringify({error:{code:'E401'}})); process.exit(1);
|
||||
case 'network': console.log('upstream unavailable'); process.exit(1);
|
||||
}
|
||||
`,
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(bin, 'npm'),
|
||||
'#!/bin/sh\nprintf wrong-client > wrong-path-npm\nexit 97\n',
|
||||
{ mode: 0o755 },
|
||||
);
|
||||
const result = spawnSync('bash', ['-c', publication], {
|
||||
@@ -62,9 +84,41 @@ case 'network': console.log('upstream unavailable'); process.exit(1);
|
||||
PATH: `${bin}${path.delimiter}${process.env.PATH}`,
|
||||
RUNNER_TEMP: root,
|
||||
QL_PUBLISH_SCENARIO: scenario,
|
||||
QINGLONG_PUBLISH_TARBALL: tarball,
|
||||
},
|
||||
});
|
||||
assert.equal(result.status, expectedStatus, result.stdout + result.stderr);
|
||||
assert.equal(fs.existsSync(path.join(root, 'published')), published);
|
||||
assert.equal(fs.existsSync(path.join(root, 'wrong-path-npm')), false);
|
||||
const calls = fs
|
||||
.readFileSync(path.join(root, 'npm-calls.jsonl'), 'utf8')
|
||||
.trim()
|
||||
.split('\n')
|
||||
.map((line) => JSON.parse(line));
|
||||
const expectedCalls = [['--version']];
|
||||
if (scenario !== 'wrong-npm-version')
|
||||
expectedCalls.push([
|
||||
'view',
|
||||
'@fixture/qinglong@2.23.0',
|
||||
'version',
|
||||
'--json',
|
||||
'--registry=https://registry.npmjs.org',
|
||||
]);
|
||||
if (published) {
|
||||
const publishArgs = [
|
||||
'publish',
|
||||
tarball,
|
||||
'--ignore-scripts',
|
||||
'--access',
|
||||
'public',
|
||||
'--registry=https://registry.npmjs.org',
|
||||
];
|
||||
expectedCalls.push(publishArgs);
|
||||
assert.deepEqual(
|
||||
JSON.parse(fs.readFileSync(path.join(root, 'published'))),
|
||||
publishArgs,
|
||||
);
|
||||
}
|
||||
assert.deepEqual(calls, expectedCalls);
|
||||
});
|
||||
}
|
||||
@@ -9,39 +9,72 @@ const { spawnSync } = require('node:child_process');
|
||||
const source = fs
|
||||
.readFileSync('shell/start.sh', 'utf8')
|
||||
.split('. ${QL_DIR}/shell/share.sh')[0];
|
||||
|
||||
function fixture(t, existing) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-env-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
fs.mkdirSync(path.join(root, 'shell'));
|
||||
fs.mkdirSync(path.join(root, 'scripts'));
|
||||
fs.writeFileSync(path.join(root, 'shell/test.sh'), '# fixture');
|
||||
fs.writeFileSync(path.join(root, '.env.example'), 'BACK_PORT=5700\n');
|
||||
const custom = 'BACK_PORT=5800\nJWT_SECRET=synthetic-test-only\n';
|
||||
if (existing)
|
||||
fs.writeFileSync(path.join(root, '.env'), custom, { mode: 0o600 });
|
||||
const trace = path.join(root, 'runtime-install.json');
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'scripts/install-runtime-tools.cjs'),
|
||||
`const fs = require('node:fs');
|
||||
fs.writeFileSync(process.env.QL_INSTALL_TRACE, JSON.stringify(process.argv.slice(2)));
|
||||
if (process.env.QL_INSTALL_FAILURE === '1') process.exit(19);
|
||||
`,
|
||||
);
|
||||
return { root, custom, trace };
|
||||
}
|
||||
|
||||
function startup(root, trace, command, fail = false) {
|
||||
return spawnSync(
|
||||
'/bin/bash',
|
||||
[
|
||||
'-c',
|
||||
`npm(){ [[ "$*" == 'prefix --global' ]] || return 2; printf '%s\\n' "$QL_DIR/global-prefix"; };
|
||||
node(){ command '${process.execPath}' "$@"; };
|
||||
pip3(){ :; }; apk(){ :; }; sudo(){ "$@"; }; python3(){ printf 3.11; };
|
||||
${source}`,
|
||||
'startup-test',
|
||||
command,
|
||||
],
|
||||
{
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
QL_DIR: root,
|
||||
QL_DATA_DIR: path.join(root, 'data'),
|
||||
QL_OS_TYPE: 'alpine',
|
||||
QL_INSTALL_TRACE: trace,
|
||||
QL_INSTALL_FAILURE: fail ? '1' : '0',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
for (const command of ['start', 'reload']) {
|
||||
for (const existing of [false, true]) {
|
||||
test(`${command} ${
|
||||
existing ? 'preserves existing' : 'initializes missing'
|
||||
} .env`, (t) => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-env-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
fs.mkdirSync(path.join(root, 'shell'));
|
||||
fs.writeFileSync(path.join(root, 'shell/test.sh'), '# fixture');
|
||||
fs.writeFileSync(path.join(root, '.env.example'), 'BACK_PORT=5700\n');
|
||||
const custom = 'BACK_PORT=5800\nJWT_SECRET=synthetic-test-only\n';
|
||||
if (existing)
|
||||
fs.writeFileSync(path.join(root, '.env'), custom, { mode: 0o600 });
|
||||
const result = spawnSync(
|
||||
'/bin/bash',
|
||||
[
|
||||
'-c',
|
||||
'npm(){ :; }; pip3(){ :; }; apk(){ :; }; sudo(){ "$@"; }; python3(){ printf 3.11; };\n' +
|
||||
source,
|
||||
'startup-test',
|
||||
command,
|
||||
],
|
||||
{
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
QL_DIR: root,
|
||||
QL_DATA_DIR: root + '/data',
|
||||
QL_OS_TYPE: 'alpine',
|
||||
},
|
||||
},
|
||||
);
|
||||
const { root, custom, trace } = fixture(t, existing);
|
||||
const result = startup(root, trace, command);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
if (command === 'start')
|
||||
assert.deepEqual(JSON.parse(fs.readFileSync(trace, 'utf8')), [
|
||||
'--archive',
|
||||
path.join(root, 'static/runtime-tools.tgz'),
|
||||
'--proof',
|
||||
path.join(root, 'static/runtime-tools-proof.json'),
|
||||
'--prefix',
|
||||
path.join(root, 'global-prefix'),
|
||||
]);
|
||||
else assert.equal(fs.existsSync(trace), false);
|
||||
assert.equal(
|
||||
fs.readFileSync(path.join(root, '.env'), 'utf8'),
|
||||
existing ? custom : 'BACK_PORT=5700\n',
|
||||
@@ -51,3 +84,18 @@ for (const command of ['start', 'reload']) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const existing of [false, true]) {
|
||||
test(`failed runtime installation stops before ${
|
||||
existing ? 'changing existing' : 'creating missing'
|
||||
} .env`, (t) => {
|
||||
const { root, custom, trace } = fixture(t, existing);
|
||||
const result = startup(root, trace, 'start', true);
|
||||
assert.equal(result.status, 19, result.stderr);
|
||||
assert.equal(fs.existsSync(trace), true);
|
||||
if (existing) {
|
||||
assert.equal(fs.readFileSync(path.join(root, '.env'), 'utf8'), custom);
|
||||
assert.equal(fs.statSync(path.join(root, '.env')).mode & 0o777, 0o600);
|
||||
} else assert.equal(fs.existsSync(path.join(root, '.env')), false);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user