Files
muSRFlow/tests/runtime-validation.test.js
T

396 lines
22 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const childProcess = require('child_process');
const crypto = require('crypto');
// These checks exercise Linux packaging scripts, including the WSL payload.
// Non-Linux hosts still run the platform-independent release contract tests.
if (process.platform !== 'linux') {
console.log('Linux runtime validation tests skipped on ' + process.platform);
process.exit(0);
}
const root = path.resolve(__dirname, '..');
const validator = path.join(root, 'packaging/runtime/validate-runtime.sh');
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-runtime-validation-'));
const tools = ['musrfit', 'musrt0', 'musrview', 'msr2data', 'dump_header', 'root-config'];
function createRuntime(name) {
const prefix = path.join(temporary, name);
fs.mkdirSync(path.join(prefix, 'bin'), { recursive: true });
fs.mkdirSync(path.join(prefix, 'lib'));
const library = Buffer.alloc(64);
Buffer.from([127, 69, 76, 70, 2, 1, 1]).copy(library);
library.writeUInt16LE(3, 16);
library.writeUInt16LE(process.arch === 'arm64' ? 183 : 62, 18);
fs.writeFileSync(path.join(prefix, 'lib', 'libCore.so'), library);
fs.writeFileSync(path.join(prefix, 'lib', 'libPMusr.so'), library);
fs.writeFileSync(path.join(prefix, 'musrfit_startup.xml'), '<musrfit/>\n');
for (const tool of tools) {
fs.writeFileSync(path.join(prefix, 'bin', tool), `#!/bin/sh
set -eu
prefix="$(cd "$(dirname "$0")/.." && pwd -P)"
tool="$(basename "$0")"
[ "$#" -eq 1 ]
[ "$1" = --version ]
[ "$ROOTSYS" = "$prefix" ]
[ "$MUSRFITPATH" = "$prefix" ]
[ "$LD_LIBRARY_PATH" = "$prefix/lib" ]
[ "$PATH" = "$prefix/bin:/usr/bin:/bin" ]
[ "$PWD" != "$prefix" ]
if [ "$tool" = "$TEST_FAIL_TOOL" ]; then
echo "error while loading shared libraries: libMissing.so" >&2
exit 127
fi
if [ "$tool" = "$TEST_HANG_TOOL" ]; then
exec /bin/sleep 30
fi
printf '%s --version\\n' "$tool" >> "$TEST_RUNTIME_LOG"
if [ "$tool" = root-config ]; then
echo "$TEST_ROOT_VERSION"
else
version="$TEST_VERSION"
if [ "$tool" = "$TEST_STALE_TOOL" ]; then
version=1.11.1
fi
echo "$tool version: $version, git-branch: root6, git-rev: test - $TEST_REVISION (Release), ROOT version: $TEST_ROOT_VERSION"
fi
`, { mode: 0o755 });
}
const recorded = runScript(validator, ['--record-manifest', prefix]);
assert.strictEqual(recorded.status, 0, recorded.stderr);
return prefix;
}
function runScript(script, args, overrides = {}) {
const result = childProcess.spawnSync('bash', [script].concat(args), {
cwd: root,
env: Object.assign({}, process.env, {
ROOTSYS: '/unrelated/root', MUSRFITPATH: '/unrelated/musrfit',
LD_LIBRARY_PATH: '/unrelated/libraries',
RUNTIME_CHECK_TIMEOUT: '2',
TEST_RUNTIME_LOG: path.join(temporary, 'tools.log'),
TEST_FAIL_TOOL: '', TEST_HANG_TOOL: '', TEST_STALE_TOOL: '',
TEST_VERSION: '1.12.1', TEST_ROOT_VERSION: '6.38.00', TEST_REVISION: '708f777b'
}, overrides),
encoding: 'utf8', timeout: 10000
});
assert.ifError(result.error);
return result;
}
try {
const complete = createRuntime('complete runtime with spaces');
fs.writeFileSync(path.join(temporary, 'tools.log'), '');
const result = runScript(validator, [complete]);
assert.strictEqual(result.status, 0, result.stderr);
assert(result.stdout.includes('Runtime validation passed.'));
assert.deepStrictEqual(fs.readFileSync(path.join(temporary, 'tools.log'), 'utf8').trim().split('\n'),
tools.map(function(tool) {
return tool + ' --version';
}));
const manifestPath = path.join(complete, 'runtime-manifest.json');
const savedManifest = fs.readFileSync(manifestPath, 'utf8');
const manifest = JSON.parse(savedManifest);
assert.strictEqual(manifest.schemaVersion, 1);
assert.strictEqual(manifest.platform, 'linux');
assert.strictEqual(manifest.arch, process.arch);
assert.strictEqual(manifest.root.version, '6.38.00');
assert.deepStrictEqual(manifest.musrfit, { version: '1.12.1', revision: '708f777b' });
assert.strictEqual(manifest.build.origin, 'existing');
assert.strictEqual(manifest.build.sourceRevision, null);
assert.strictEqual(manifest.build.options.root, null);
assert.strictEqual(manifest.build.options.musrfit, null);
assert(!manifest.runtimeId.includes(require('../package.json').version));
assert.strictEqual(Object.keys(manifest.files).length, 8);
// Ordinary packaging checks never silently create or repair metadata.
const unrecorded = createRuntime('unrecorded');
fs.unlinkSync(path.join(unrecorded, 'runtime-manifest.json'));
const absentManifest = runScript(validator, [unrecorded]);
assert.strictEqual(absentManifest.status, 1);
assert(absentManifest.stderr.includes('runtime manifest is missing'));
assert(!fs.existsSync(path.join(unrecorded, 'runtime-manifest.json')));
const adopted = runScript(validator, ['--record-manifest', unrecorded]);
assert.strictEqual(adopted.status, 0, adopted.stderr);
for (const replacement of ['{', '[]', JSON.stringify({ ...manifest, schemaVersion: 99 }),
JSON.stringify({ ...manifest, platform: 'darwin' }),
JSON.stringify({ ...manifest, arch: process.arch === 'x64' ? 'arm64' : 'x64' })]) {
fs.writeFileSync(manifestPath, replacement);
const invalid = runScript(validator, [complete]);
assert.strictEqual(invalid.status, 1);
assert(!invalid.stderr.includes('Traceback'));
assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), replacement);
}
fs.writeFileSync(manifestPath, savedManifest);
const wrongTarget = runScript(validator, [complete, '--arch', process.arch === 'x64' ? 'arm64' : 'x64']);
assert.strictEqual(wrongTarget.status, 1);
assert(wrongTarget.stderr.includes('does not match target'));
const stale = runScript(validator, [complete], { TEST_STALE_TOOL: 'msr2data' });
assert.strictEqual(stale.status, 1);
assert(stale.stderr.includes('different musrfit versions or revisions'));
const differentRoot = runScript(validator, [complete], { TEST_ROOT_VERSION: '6.36.00' });
assert.strictEqual(differentRoot.status, 1);
assert(differentRoot.stderr.includes('runtime manifest'));
const changed = createRuntime('changed executable');
fs.appendFileSync(path.join(changed, 'bin', 'musrview'), '# changed after recording\n');
const changedFile = runScript(validator, [changed]);
assert.strictEqual(changedFile.status, 1);
assert(changedFile.stderr.includes('runtime file does not match manifest: bin/musrview'));
const mixed = createRuntime('mixed architectures');
const foreignLibrary = fs.readFileSync(path.join(mixed, 'lib', 'libCore.so'));
foreignLibrary.writeUInt16LE(process.arch === 'arm64' ? 62 : 183, 18);
fs.writeFileSync(path.join(mixed, 'lib', 'libCore.so'), foreignLibrary);
assert(runScript(validator, [mixed]).stderr.includes('mixed native architectures'));
// Source builds record the exact checkout and real CMake settings. Refuse
// to attribute an existing binary to a different checkout or ROOT release.
const source = path.join(temporary, 'source');
const gitOptions = { encoding: 'utf8', env: Object.assign({}, process.env, {
GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1'
}) };
childProcess.execFileSync('git', ['init', '--quiet', source], gitOptions);
childProcess.execFileSync('git', ['-C', source, '-c', 'user.name=Runtime test',
'-c', 'user.email=runtime-test@example.invalid', 'commit', '--quiet', '--no-gpg-sign',
'--allow-empty', '-m', 'Runtime fixture'], gitOptions);
const revision = childProcess.execFileSync('git', ['-C', source, 'rev-parse', 'HEAD'], gitOptions).trim();
const rootCache = path.join(temporary, 'ROOT.CMakeCache.txt');
const musrfitCache = path.join(temporary, 'musrfit.CMakeCache.txt');
fs.writeFileSync(rootCache, 'gnuinstall:BOOL=OFF\nx11:BOOL=ON\nCMAKE_BUILD_TYPE:STRING=Release\n');
fs.writeFileSync(musrfitCache, 'qt_based_tools:BOOL=OFF\ntry_OpenMP:BOOL=ON\n');
const sourceArgs = ['--record-manifest', complete, '--source-repo', source, '--root-source', 'build',
'--root-version', '6.38.00', '--root-cache', rootCache, '--musrfit-cache', musrfitCache,
'--base-image', 'example/runtime-builder:test'];
const mismatchedSource = runScript(validator, sourceArgs);
assert.strictEqual(mismatchedSource.status, 1);
assert(mismatchedSource.stderr.includes('does not match the source checkout'));
assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), savedManifest);
const wrongRoot = runScript(validator, ['--record-manifest', complete, '--root-version', '9.99.00']);
assert.strictEqual(wrongRoot.status, 1);
assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), savedManifest);
const recordedSource = runScript(validator, sourceArgs, { TEST_REVISION: revision });
assert.strictEqual(recordedSource.status, 0, recordedSource.stderr);
const sourceManifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
assert.strictEqual(sourceManifest.build.sourceRevision, revision);
assert.strictEqual(sourceManifest.build.sourceDirty, false);
assert.strictEqual(sourceManifest.build.options.root.x11, 'ON');
assert.strictEqual(sourceManifest.build.options.musrfit.try_OpenMP, 'ON');
assert.strictEqual(sourceManifest.build.baseImage, 'example/runtime-builder:test');
assert.strictEqual(runScript(validator, [complete], { TEST_REVISION: revision }).status, 0);
fs.writeFileSync(path.join(source, 'local-change.cpp'), '// Local source change\n');
assert.strictEqual(runScript(validator, sourceArgs, { TEST_REVISION: revision }).status, 0);
assert.strictEqual(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).build.sourceDirty, true);
fs.writeFileSync(manifestPath, savedManifest);
// Download bundles retain the complete tree, not only the executable
// markers, and can be unpacked into an ordinary user-owned directory.
const archiveBuilder = path.join(root, 'packaging/runtime/build-runtime-archive.sh');
const downloadable = createRuntime('download runtime');
const downloadOutput = path.join(temporary, 'download output');
const downloadRoot = path.join(downloadOutput, 'runtime-downloads');
fs.mkdirSync(path.join(downloadable, 'fonts'));
fs.writeFileSync(path.join(downloadable, 'fonts', 'support file.txt'), 'ROOT support data\n');
fs.writeFileSync(path.join(downloadable, '.gitkeep'), 'placeholder');
fs.symlinkSync('libCore.so', path.join(downloadable, 'lib', 'libAlias.so'));
const downloadOptions = { RUNTIME_DIR: downloadable, OUT_DIR: downloadOutput, ARCH: process.arch };
const built = runScript(archiveBuilder, [], downloadOptions);
assert.strictEqual(built.status, 0, built.stderr);
assert.strictEqual(fs.readdirSync(downloadRoot).length, 1);
const bundleName = fs.readdirSync(downloadRoot)[0];
const bundle = path.join(downloadRoot, bundleName);
const description = JSON.parse(fs.readFileSync(path.join(bundle, 'runtime-download.json'), 'utf8'));
assert.strictEqual(description.schemaVersion, 1);
assert.strictEqual(description.kind, 'musrflow-runtime-download');
assert.strictEqual(description.artifact.rootDirectory, 'runtime');
assert.strictEqual(description.artifact.format, 'tar.gz');
const archive = path.join(bundle, description.artifact.filename);
const bytes = fs.readFileSync(archive);
assert.strictEqual(description.artifact.size, bytes.length);
assert.strictEqual(description.artifact.sha256, crypto.createHash('sha256').update(bytes).digest('hex'));
assert.strictEqual(fs.readFileSync(archive + '.sha256', 'utf8'),
description.artifact.sha256 + ' ' + description.artifact.filename + '\n');
const listing = childProcess.execFileSync('tar', ['-tzf', archive], { encoding: 'utf8' }).trim().split('\n');
assert(listing.every(function(name) {
return name.startsWith('runtime/');
}));
assert(listing.includes('runtime/fonts/support file.txt'));
assert(listing.includes('runtime/runtime-manifest.json'));
assert(!listing.some(function(name) {
return name.includes('.gitkeep');
}));
const unpacked = path.join(temporary, 'unpacked');
fs.mkdirSync(unpacked);
childProcess.execFileSync('tar', ['-xzf', archive, '-C', unpacked]);
assert.strictEqual(fs.readlinkSync(path.join(unpacked, 'runtime', 'lib', 'libAlias.so')), 'libCore.so');
assert.strictEqual(fs.statSync(path.join(unpacked, 'runtime', 'bin', 'musrfit')).mode & 0o777, 0o755);
assert.strictEqual(runScript(validator, [path.join(unpacked, 'runtime')]).status, 0);
const repeated = runScript(archiveBuilder, [], downloadOptions);
assert.strictEqual(repeated.status, 0, repeated.stderr);
assert(repeated.stdout.includes('Reusing runtime download:'));
assert.strictEqual(fs.readdirSync(downloadRoot).length, 1);
// Support-file changes also change the download checksum, even though the
// runtime's core-tool identity remains unchanged. Old bundles remain intact.
fs.appendFileSync(path.join(downloadable, 'fonts', 'support file.txt'), 'updated\n');
assert.strictEqual(runScript(archiveBuilder, [], downloadOptions).status, 0);
assert.strictEqual(fs.readdirSync(downloadRoot).length, 2);
assert.deepStrictEqual(fs.readFileSync(archive), bytes);
const otherName = fs.readdirSync(downloadRoot).find(function(name) {
return name !== bundleName;
});
const otherDescriptor = path.join(downloadRoot, otherName, 'runtime-download.json');
const updated = JSON.parse(fs.readFileSync(otherDescriptor, 'utf8'));
assert.strictEqual(updated.runtime.runtimeId, description.runtime.runtimeId);
assert.notStrictEqual(updated.artifact.sha256, description.artifact.sha256);
fs.writeFileSync(otherDescriptor, 'conflicting published metadata');
const conflict = runScript(archiveBuilder, [], downloadOptions);
assert.strictEqual(conflict.status, 1);
assert(conflict.stderr.includes('existing runtime download bundle has different contents'));
assert.strictEqual(fs.readFileSync(otherDescriptor, 'utf8'), 'conflicting published metadata');
const badLink = path.join(downloadable, 'lib', 'bad-link');
fs.symlinkSync('/missing/absolute/library.so', badLink);
const absoluteLink = runScript(archiveBuilder, [], downloadOptions);
assert.strictEqual(absoluteLink.status, 1);
assert(absoluteLink.stderr.includes('absolute symlink'));
fs.unlinkSync(badLink);
fs.writeFileSync(path.join(temporary, 'outside.dat'), 'external');
fs.symlinkSync('../../outside.dat', badLink);
assert(runScript(archiveBuilder, [], downloadOptions).stderr.includes('external, broken, or cyclic symlink'));
fs.unlinkSync(badLink);
const fifo = path.join(downloadable, 'pipe');
childProcess.execFileSync('mkfifo', [fifo]);
assert(runScript(archiveBuilder, [], downloadOptions).stderr.includes('special file'));
fs.unlinkSync(fifo);
const recursiveOutput = runScript(archiveBuilder, [], { RUNTIME_DIR: downloadable, OUT_DIR: downloadable });
assert.strictEqual(recursiveOutput.status, 1);
assert(recursiveOutput.stderr.includes('must not be inside the runtime tree'));
assert.strictEqual(runScript(archiveBuilder, [], { RUNTIME_DIR: changed, OUT_DIR: downloadOutput }).status, 1);
assert.strictEqual(fs.readdirSync(downloadRoot).length, 2, 'failed builds must leave no partial releases');
const missing = createRuntime('missing tools');
fs.unlinkSync(path.join(missing, 'bin', 'dump_header'));
fs.chmodSync(path.join(missing, 'bin', 'musrt0'), 0o644);
const absent = runScript(validator, [missing]);
assert.strictEqual(absent.status, 1);
assert(absent.stderr.includes('dump_header'));
assert(absent.stderr.includes('musrt0'));
const noRoot = createRuntime('missing ROOT');
fs.unlinkSync(path.join(noRoot, 'bin', 'root-config'));
assert.strictEqual(runScript(validator, [noRoot]).status, 1);
const noStartup = createRuntime('empty startup');
fs.writeFileSync(path.join(noStartup, 'musrfit_startup.xml'), '');
const emptyStartup = runScript(validator, [noStartup]);
assert.strictEqual(emptyStartup.status, 1);
assert(emptyStartup.stderr.includes('startup XML'));
const noLibraries = createRuntime('missing libraries');
fs.rmSync(path.join(noLibraries, 'lib'), { recursive: true });
assert.strictEqual(runScript(validator, [noLibraries]).status, 1);
const broken = runScript(validator, [complete], { TEST_FAIL_TOOL: 'musrview' });
assert.strictEqual(broken.status, 1);
assert(broken.stderr.includes('musrview --version failed (exit 127)'));
assert(broken.stderr.includes('libMissing.so'));
const hung = runScript(validator, [complete], {
TEST_HANG_TOOL: 'msr2data', RUNTIME_CHECK_TIMEOUT: '1'
});
assert.strictEqual(hung.status, 1);
assert(hung.stderr.includes('msr2data --version failed (exit 124)'));
assert.strictEqual(runScript(validator, [complete], { RUNTIME_CHECK_TIMEOUT: '0' }).status, 2);
assert.strictEqual(runScript(validator, []).status, 2);
assert.strictEqual(runScript(validator, [path.join(temporary, 'absent')]).status, 1);
// A rejected runtime must not invoke either packager or overwrite a
// previously published artifact, regardless of whether it is missing or broken.
const output = path.join(temporary, 'dist');
fs.mkdirSync(output);
const version = require('../package.json').version;
const packager = path.join(temporary, 'packager');
fs.writeFileSync(packager, '#!/bin/sh\necho invoked >> "$TEST_PACKAGER_LOG"\nexit 99\n', { mode: 0o755 });
for (const target of ['rpm', 'appimage']) {
const script = path.join(root, 'packaging/runtime/build-runtime-' + target + '.sh');
const artifact = path.join(output, 'musrflow-runtime-' + version + '-x86_64.'
+ (target === 'rpm' ? 'rpm' : 'AppImage'));
fs.writeFileSync(artifact, 'previous artifact');
for (const prefix of [missing, complete, changed]) {
const rejected = runScript(script, [], {
RUNTIME_DIR: prefix, OUT_DIR: output, ARCH: 'x86_64',
TEST_FAIL_TOOL: prefix === complete ? 'musrview' : '',
FPM_BIN: packager, MKSQUASHFS_BIN: packager, APPIMAGE_RUNTIME_BIN: packager,
TEST_PACKAGER_LOG: path.join(temporary, 'packager.log')
});
assert.strictEqual(rejected.status, 1, rejected.stderr);
assert.strictEqual(fs.readFileSync(artifact, 'utf8'), 'previous artifact');
assert(!fs.existsSync(path.join(temporary, 'packager.log')));
}
}
// electron-builder caches fpm in both versioned and legacy directories.
// Hide any host fpm so the fixture actually tests cache selection.
const fakeBin = path.join(temporary, 'fake-bin');
fs.mkdirSync(fakeBin);
// setup-node may install outside /usr/bin; retain Node in this isolated PATH.
fs.symlinkSync(process.execPath, path.join(fakeBin, 'node'));
fs.writeFileSync(path.join(fakeBin, 'fpm'), '#!/bin/sh\nexit 127\n', { mode: 0o755 });
const packageRuntime = createRuntime('package-runtime');
for (const layout of ['fpm@2/fpm-test/fpm', 'fpm/fpm-test/fpm']) {
const cache = fs.mkdtempSync(path.join(temporary, 'builder-cache-'));
const cachedFpm = path.join(cache, layout);
fs.mkdirSync(path.dirname(cachedFpm), { recursive: true });
fs.copyFileSync(packager, cachedFpm);
fs.chmodSync(cachedFpm, 0o755);
const selected = runScript(path.join(root, 'packaging/runtime/build-runtime-rpm.sh'), [], {
RUNTIME_DIR: packageRuntime, OUT_DIR: output, FPM_BIN: '',
ELECTRON_BUILDER_CACHE: cache, PATH: fakeBin + ':/usr/bin:/bin',
TEST_PACKAGER_LOG: path.join(cache, 'invoked.log')
});
assert.strictEqual(selected.status, 99, selected.stderr);
assert(fs.existsSync(path.join(cache, 'invoked.log')));
}
// The CI cleanup name must reach the engine without changing local builds.
const engineLog = path.join(temporary, 'engine.log');
const engine = path.join(fakeBin, 'engine');
fs.writeFileSync(engine, '#!/bin/sh\nprintf "%s\\n" "$@" >> "$TEST_ENGINE_LOG"\n', { mode: 0o755 });
for (const containerName of ['', 'runtime-ci-fixture']) {
fs.writeFileSync(engineLog, '');
const invoked = runScript(path.join(root, 'packaging/runtime/build.sh'), [], {
ENGINE: engine, MUSRFIT_REPO: source, CONTAINER_NAME: containerName,
TEST_ENGINE_LOG: engineLog, IMAGE: 'localhost/runtime-test:fixture'
});
assert.strictEqual(invoked.status, 0, invoked.stderr);
const argumentsList = fs.readFileSync(engineLog, 'utf8').trim().split('\n');
assert(argumentsList.includes('--rm'));
assert.strictEqual(argumentsList.includes('--name'), containerName !== '');
if (containerName) assert.strictEqual(argumentsList[argumentsList.indexOf('--name') + 1], containerName);
}
const build = fs.readFileSync(path.join(root, 'packaging/runtime/build-runtime.sh'), 'utf8');
assert(build.includes('-DROOT_DIR="$PREFIX/cmake"'));
assert(build.includes('export ROOTSYS="$PREFIX"'));
assert(build.includes('git config --global --add safe.directory "$MUSRFIT_REPO"'));
assert(build.includes('/validate-runtime.sh" --record-manifest "$PREFIX"'));
const wsl = fs.readFileSync(path.join(root, 'packaging/wsl2/build-wsl2.sh'), 'utf8');
assert(wsl.includes('validate-runtime.sh:/tmp/validate-runtime.sh:ro'));
assert(wsl.includes('runtime-manifest.py:/tmp/runtime-manifest.py:ro'));
assert(wsl.includes('bash /tmp/validate-runtime.sh "$PREFIX"'));
assert(!wsl.includes('WARNING: runtime self-check failed'));
const unsupportedAppImage = runScript(path.join(root, 'packaging/runtime/build-runtime-appimage.sh'), [], {
RUNTIME_DIR: complete, ARCH: 'arm64'
});
assert.strictEqual(unsupportedAppImage.status, 1);
assert(unsupportedAppImage.stderr.includes('launcher currently supports only x86_64'));
console.log('Runtime validation tests passed');
} finally {
fs.rmSync(temporary, { recursive: true, force: true });
}