Files
wekan/tests/documentRuntimeDependencies.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

68 lines
4.3 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { transformSync } = require('@swc/core');
const root = path.resolve(__dirname, '..');
const temp = fs.mkdtempSync(path.join(root, '.tools/tmp/document-runtime-'));
const server = path.join(temp, 'programs/server');
const npm = path.join(server, 'npm/node_modules');
fs.mkdirSync(npm, { recursive: true });
const read = name => fs.readFileSync(path.join(root, name), 'utf8');
function load(name, extra = '') {
const module = { exports: {} };
const context = { module, exports: module.exports, Buffer, Uint8Array, console,
process: { cwd: () => server },
require(name) {
if (name === 'meteor/mongo') return { Mongo: { Collection: class {} } };
if (name === './imageGif') return {};
return require(name);
},
};
vm.runInNewContext(transformSync(read(name) + extra, { module: { type: 'commonjs' } }).code, context);
return module.exports;
}
(async () => {
try {
// Real archive decompression, with dependencies in the release layout.
fs.cpSync(path.join(root, 'node_modules/fflate'), path.join(npm, 'fflate'), { recursive: true });
fs.mkdirSync(path.join(npm, 'pdfjs-dist/legacy/build'), { recursive: true });
fs.writeFileSync(path.join(npm, 'pdfjs-dist/package.json'), '{}');
fs.writeFileSync(path.join(npm, 'pdfjs-dist/legacy/build/pdf.worker.mjs'), '');
fs.mkdirSync(path.join(npm, 'sharp'), { recursive: true });
fs.writeFileSync(path.join(npm, 'sharp/index.js'), 'module.exports = "release-native-module";');
const api = load('server/lib/documentGif.js', '\nexports.test = { officeSearchText, runtimeResolve };').test;
assert.ok(api.runtimeResolve('fflate').startsWith(npm + path.sep), 'must not accidentally use checkout dependencies');
assert.equal(api.runtimeResolve('pdfjs-dist/legacy/build/pdf.worker.mjs'), path.join(npm, 'pdfjs-dist/legacy/build/pdf.worker.mjs'));
assert.equal(api.runtimeResolve('pdfjs-dist/package.json'), path.join(npm, 'pdfjs-dist/package.json'));
assert.throws(() => api.runtimeResolve('wekan-nonexistent-dependency'), { code: 'MODULE_NOT_FOUND' });
const { zipSync, strToU8 } = require('fflate');
const cases = [
['xlsx', {
'xl/sharedStrings.xml': '<sst><si><t>Cloud spreadsheet</t></si></sst>',
'xl/worksheets/sheet1.xml': '<worksheet><c t="s"><v>0</v></c><c><f>PRIVATE_FORMULA</f><v>42</v></c></worksheet>',
}, 'Cloud spreadsheet 42'],
['docx', { 'word/document.xml': '<w:p>Cloud document &amp; text</w:p>' }, 'Cloud document & text'],
['pptx', { 'ppt/slides/slide1.xml': '<a:t>Cloud slides</a:t>' }, 'Cloud slides'],
];
for (const [extension, entries, expected] of cases) {
const archive = zipSync(Object.fromEntries(Object.entries(entries).map(([name, text]) => [name, strToU8(text)])));
assert.equal(await api.officeSearchText(archive, extension), expected);
}
await assert.rejects(api.officeSearchText(zipSync({ 'ignored.xml': strToU8('not a spreadsheet') }), 'xlsx'), /no readable parts/);
await assert.rejects(api.officeSearchText(zipSync({ 'word/document.xml': strToU8('a'.repeat(2 * 1024 * 1024 + 1)) }), 'docx'), /exceeds limit/);
const images = load('server/lib/imageGif.js', '\nexports.loadForTest = loadSharpAtRuntime;');
assert.equal(images.loadForTest(), 'release-native-module');
// Source development also resolves through the parent node_modules.
const sourceRequire = require('node:module').createRequire(path.join(root, 'npm/package.json'));
assert.equal(sourceRequire.resolve('fflate'), require.resolve('fflate'));
const docker = read('Dockerfile');
assert.match(docker, /apt-get install --assume-yes --no-install-recommends file(?:[ \t]+[^\n]*)?\n/);
// No removable build-dependency list any more; nothing may purge `file`.
assert.doesNotMatch(docker, /apt-get (?:remove|purge)[^\n]*\bfile\b/);
assert.doesNotMatch((docker.match(/ENV BUILD_DEPS="([^"]+)"/) || ['', ''])[1], /\bfile\b/);
console.log('Release dependencies: XLSX/DOCX/PPTX extraction, rejected archives, PDF assets, native-image lookup and Docker MIME utility pass.');
} finally { fs.rmSync(temp, { recursive: true, force: true }); }
})().catch(error => { console.error(error); process.exitCode = 1; });