Files
wekan/tests/imageBuildOnlyModules.test.cjs
T
Lauri Ojansivu 3c34e65403 Docker image: build from the published .zip on the build machine, not under QEMU.
In v12.32 "Build and push multi-arch image" took 27 of the docker job's 29
minutes. For each of seven platforms, six of them emulated, the Dockerfile
installed a compiler toolchain, resolved and downloaded Node.js, reinstalled
programs/server with npm, re-ran the prune, bump, trim and prune passes, and
purged the toolchain again. That platform's .zip leg in release-all.yml had
already done all of it, with the same scripts.

Stage 1 (FROM --platform=$BUILDPLATFORM) now runs natively on the runner for
each target. It downloads that target's .zip, checks it against the
release's own .sha256sum, re-runs the telemetry check, reads bundle/node's
ELF header to confirm the target CPU, and lays out /build and
/usr/local/bin/node. The image's node is the .zip's own node: the file the
leg resolved and verified, rather than a second download that had to match
it. Stage 2 is the only part that runs as the target CPU. It adds the WeKan
user, applies an OS update, installs file, libatomic1 and docker-cli, and
copies stage 1's result. No npm and no compiler ever enter the image.

The docker job read the base image from the first FROM; it now reads the
last one, since the first is the build-machine stage.

Stage 1 was run outside Docker against the real v12.32 s390x zip: the
checksum, the telemetry check (47979 files) and the ELF check (machine 22)
passed, and the layout was right. The ELF check rejects the same node when
expected as ppc64le, and rejects a non-ELF file. No Docker build ran here.

tests/dockerImageFromBundle.test.cjs pins the two stages, the verification,
the per-architecture ELF numbers (shared with check-addon-arch.mjs), and
that no reinstall, Node.js download or toolchain comes back. It fails on
the previous Dockerfile. Nine tests pinned the old in-image steps; each now
pins the new behaviour, with the reason recorded beside it.

Thanks to xet7 !
2026-10-12 00:01:07 +03:00

112 lines
5.6 KiB
JavaScript

'use strict';
// What is BUILT WITH does not have to be SHIPPED.
// Run: node tests/imageBuildOnlyModules.test.cjs
//
// A container scan of ghcr.io/wekan/wekan:v10.91 reported 80 findings against
// "Node.js", three of them CRITICAL, and not one of them was in code WeKan runs:
//
// tar 6.2.1 x2 node-gyp's and cacache's own copies, in the bundle
// tar 7.5.11 npm's own copy, in /usr/local/lib/node_modules/npm
// sigstore, @sigstore/*, ip-address, brace-expansion npm's, same place
//
// Two build tools, shipped by accident. `npm` runs exactly once in the image -
// the `npm install` that populates programs/server - and the container starts
// bash + wekan-entrypoint.sh, which never calls it. node-gyp and
// @mapbox/node-pre-gyp are there to COMPILE native modules during that install,
// and the bundle compiles none: uWebSockets.js, bcrypt and argon2 all ship
// prebuilt .node files that node-gyp-build picks at require time.
//
// So both go, after the install that needs them:
// - releases/prune-build-only-modules.mjs walks what is reachable from
// programs/server/package.json without those two, and removes the rest - 83
// of 120 packages, verified by booting the pruned bundle.
// - the Dockerfile deletes npm and npx in its cleanup step.
//
// This suite pins that both still happen, in the image AND in every .zip.
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const repoRoot = path.resolve(__dirname, '..');
const read = f => fs.readFileSync(path.join(repoRoot, f), 'utf8');
const dockerfile = read('Dockerfile');
const workflow = read('.github/workflows/release-all.yml');
const installNode = read('releases/install-node-for-arch.sh');
const pruner = read('releases/prune-build-only-modules.mjs');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
console.log('imageBuildOnlyModules:');
test('the pruner exists and is driven by reachability, not by a list', () => {
// A hardcoded list of 83 package names would be wrong the first time Meteor
// changed a dev-bundle dependency, and wrong silently.
assert.ok(/BUILD_ONLY = \['node-gyp', '@mapbox\/node-pre-gyp'\]/.test(pruner),
'the two build-only roots are named');
assert.ok(/keep\.add\(name\)/.test(pruner) && /depsOf\(name\)/.test(pruner),
'and everything else is kept or dropped by walking dependencies from them');
});
test('it never touches the Meteor packages\' own npm dependencies', () => {
// programs/server/npm/node_modules/meteor/<package>/node_modules is loaded at
// run time. Only the top level of programs/server/node_modules is pruned.
assert.ok(/does NOT touch/.test(pruner) && /npm\/node_modules/.test(pruner),
'the script says so');
assert.ok(!/rmSync\([^)]*npm[/\\]node_modules/.test(pruner),
'and removes nothing under npm/node_modules');
});
test('the image installs nothing, so it has no build-only tree to prune', () => {
// The Docker image used to reinstall programs/server from the .zip and redo
// its post-processing, under QEMU for six of seven platforms. Since the image
// is built from the .zip as it was published (Dockerfile stage 1, checked
// against its .sha256sum), it has nothing to redo: the .zip's own leg in
// release-all.yml already ran these steps. See tests/dockerImageFromBundle.test.cjs.
const code = dockerfile.replace(/^\s*#.*$/gm, '');
assert.ok(!/npm install/.test(code), 'no npm install in the image');
assert.ok(!/node \/tmp\/prune-build-only-modules\.mjs/.test(code), 'and so nothing to prune');
});
test('the image ships no npm and no npx', () => {
// They are never put there: no npm tarball is unpacked and no npm is
// installed, so there is nothing to remove afterwards either.
const code = dockerfile.replace(/^\s*#.*$/gm, '');
assert.ok(!/bin\/npm|lib\/node_modules\/npm|npm install -g/.test(code),
'no npm or npx is installed into the image');
});
test('node itself stays - that is what runs WeKan (negative)', () => {
// The guard against over-correcting: this is about npm, not about node.
assert.ok(!/rm -rf [^\n]*\/usr\/local\/bin\/node\b/.test(dockerfile),
'the node binary is never removed');
assert.ok(/ln -s "\/usr\/local\/bin\/node" "\/out\/usr\/local\/bin\/nodejs"/.test(dockerfile),
'and is still linked as nodejs');
});
test('every leg that installs programs/server prunes afterwards', () => {
// A leg that installs and does not prune ships the tree again in its .zip.
// `bash "$SRC/releases/npm-retry.sh" npm install` and the unquoted container
// form; the bcrypt replacements use --ignore-scripts and are not these.
const installs = workflow.split('\n').flatMap(line =>
[...line.matchAll(/npm-retry\.sh"? npm install(?: --global npm@[^ &]+)?/g)]
.filter(match => !match[0].includes('--global npm@') && !line.includes('--ignore-scripts')));
assert.ok(installs.length >= 6, `expected the per-arch installs, found ${installs.length}`);
const prunes = (workflow.match(/prune-build-only-modules\.mjs/g) || []).length;
assert.ok(prunes >= installs.length - 1,
`${installs.length} bundle installs but only ${prunes} prunes`);
assert.ok(/prune-build-only-modules\.mjs/.test(installNode),
'and the extra-arch containers prune in install-node-for-arch.sh');
});
test('the container legs prune INSIDE the container, where node is', () => {
// /releases is mounted read-only and /bundle read-write; the runner's own node
// is not what those legs run.
assert.ok(/node \/releases\/prune-build-only-modules\.mjs \/bundle/.test(workflow),
'the arm64 leg prunes through the mounted path');
});
console.log(`\nimageBuildOnlyModules: ${passed} tests passed`);