Files
muSRFlow/tests/local-runtime.test.js
T
salman 882f594d5a Pass validated runtime artifacts between Gitea build workflows
Fetch an explicit musrfit revision, compile and validate the scientific runtime, and upload its descriptor, archive, and diagnostics. Require a selected runtime workflow run for Linux packaging, verify restoration through the existing offline installer, and upload a complete package set without implicit release publishing.

Fix clean-container ROOT discovery, allow scoped CI container cleanup, support both fpm cache layouts, and cover artifact restoration and build contracts with regression tests.
2026-10-29 16:42:30 +01:00

183 lines
11 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 { createHash } = require('crypto');
const tar = require('tar');
const { LocalRuntime, TOOLS } = require('../local-runtime');
const { restoreRuntime } = require('../scripts/ci-restore-runtime');
if (process.platform !== 'linux' || !['x64', 'arm64'].includes(process.arch)) {
console.log('Linux runtime installation tests skipped on ' + process.platform);
process.exit(0);
}
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-install-test-'));
const source = path.join(temporary, 'bundle');
const prefix = path.join(source, 'runtime');
const directory = path.join(temporary, 'personal runtimes');
const installer = new LocalRuntime({ directory: directory });
async function makeBundle(extraEntries = []) {
const archive = path.join(source, 'runtime.tar.gz');
await tar.c({ cwd: source, file: archive, gzip: true, portable: true }, ['runtime'].concat(extraEntries));
const bytes = fs.readFileSync(archive);
const descriptor = {
schemaVersion: 1, kind: 'musrflow-runtime-download',
runtime: JSON.parse(fs.readFileSync(path.join(prefix, 'runtime-manifest.json'), 'utf8')),
artifact: { filename: 'runtime.tar.gz', format: 'tar.gz', rootDirectory: 'runtime',
size: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') }
};
fs.writeFileSync(path.join(source, 'runtime-download.json'), JSON.stringify(descriptor));
return installer.inspectBundle(path.join(source, 'runtime-download.json'));
}
async function run() {
fs.mkdirSync(path.join(prefix, 'bin'), { recursive: true });
fs.mkdirSync(path.join(prefix, 'lib'));
const header = Buffer.alloc(64);
Buffer.from([127, 69, 76, 70, 2, 1, 1]).copy(header);
header.writeUInt16LE(process.arch === 'x64' ? 62 : 183, 18);
fs.writeFileSync(path.join(prefix, 'lib/libCore.so'), header);
fs.writeFileSync(path.join(prefix, 'lib/libPMusr.so'), header);
fs.symlinkSync('libCore.so', path.join(prefix, 'lib/libAlias.so'));
const manifest = { schemaVersion: 1, platform: 'linux', arch: process.arch, runtimeId: 'test-runtime',
root: { version: '6.38.00' }, musrfit: { version: '1.12.1', revision: '708f777b' }, files: {} };
for (const name of TOOLS.concat('root-config')) {
const output = name === 'root-config' ? '6.38.00' : name
+ ' version: 1.12.1, git-rev: test - 708f777b (Release), ROOT version: 6.38.00';
fs.writeFileSync(path.join(prefix, 'bin', name), '#!/bin/sh\necho "' + output + '"\n', { mode: 0o755 });
manifest.files['bin/' + name] = createHash('sha256').update(fs.readFileSync(path.join(prefix, 'bin', name))).digest('hex');
}
for (const name of ['lib/libCore.so', 'lib/libPMusr.so']) {
manifest.files[name] = createHash('sha256').update(header).digest('hex');
}
fs.writeFileSync(path.join(prefix, 'runtime-manifest.json'), JSON.stringify(manifest));
fs.writeFileSync(path.join(prefix, 'musrfit_startup.xml'), '<musrfit>unchanged</musrfit>');
const commands = {};
for (const name of TOOLS) commands[name] = path.join(prefix, 'bin', name);
const checked = await installer.checkTools({ commands: commands });
assert.strictEqual(checked.ok, true);
const viewPath = path.join(prefix, 'bin/musrview');
const originalView = fs.readFileSync(viewPath);
fs.writeFileSync(viewPath, '#!/bin/sh\nexec sleep 30\n');
const bounded = new LocalRuntime({ directory: directory, timeout: 100 });
const timedOut = await bounded.checkTools({ commands: commands });
assert.strictEqual(timedOut.ok, false);
assert(timedOut.tools.find(function(tool) { return tool.name === 'musrview'; }).error.includes('timed out'));
fs.writeFileSync(viewPath, originalView);
const missing = await installer.checkTools({ commands: Object.assign({}, commands, { musrview: '/absent/musrview' }) });
assert.strictEqual(missing.ok, false);
assert(missing.tools.find(function(tool) { return tool.name === 'musrview'; }).error);
// Host checks do not require a muSRFlow manifest or a root-config executable.
fs.renameSync(path.join(prefix, 'runtime-manifest.json'), path.join(prefix, 'saved-manifest.json'));
assert.strictEqual((await installer.checkTools({ commands: commands })).ok, true);
fs.renameSync(path.join(prefix, 'saved-manifest.json'), path.join(prefix, 'runtime-manifest.json'));
let plan = await makeBundle();
const descriptorPath = path.join(source, 'runtime-download.json');
const original = fs.readFileSync(descriptorPath, 'utf8');
for (const replacement of [
Object.assign({}, plan.descriptor, { runtime: Object.assign({}, manifest, { platform: 'darwin' }) }),
Object.assign({}, plan.descriptor, { runtime: Object.assign({}, manifest, { arch: process.arch === 'x64' ? 'arm64' : 'x64' }) }),
Object.assign({}, plan.descriptor, { artifact: Object.assign({}, plan.descriptor.artifact, { filename: '../runtime.tar.gz' }) }),
Object.assign({}, plan.descriptor, { artifact: Object.assign({}, plan.descriptor.artifact, { size: 1 }) })
]) {
fs.writeFileSync(descriptorPath, JSON.stringify(replacement));
await assert.rejects(installer.inspectBundle(descriptorPath));
}
fs.writeFileSync(descriptorPath, original);
// CI must restore exactly one selected bundle, never guess or reuse a
// developer's runtime. Exercise both artifact layouts and corrupt input.
const ciDirectory = path.join(temporary, 'ci-install');
const ciRoot = await restoreRuntime(source, ciDirectory);
assert.strictEqual(fs.readlinkSync(path.join(ciRoot, 'lib/libAlias.so')), 'libCore.so');
assert.strictEqual(fs.statSync(path.join(ciRoot, 'bin/musrfit')).mode & 0o111, 0o111);
const artifactDirectory = path.join(temporary, 'ci-artifact');
fs.mkdirSync(artifactDirectory);
await assert.rejects(restoreRuntime(artifactDirectory, ciDirectory), /exactly one/);
fs.cpSync(source, path.join(artifactDirectory, 'bundle'), { recursive: true });
assert.strictEqual(await restoreRuntime(artifactDirectory, ciDirectory), ciRoot);
fs.cpSync(source, path.join(artifactDirectory, 'second-bundle'), { recursive: true });
await assert.rejects(restoreRuntime(artifactDirectory, ciDirectory), /exactly one/);
const ciEnvFile = path.join(temporary, 'ci-env');
fs.writeFileSync(ciEnvFile, 'EXISTING=value\n');
const cliOptions = { encoding: 'utf8', env: Object.assign({}, process.env, { GITHUB_ENV: ciEnvFile }) };
const cliArgs = [path.join(__dirname, '../scripts/ci-restore-runtime.js'), source, ciDirectory];
const restoredCli = require('child_process').spawnSync(process.execPath, cliArgs, cliOptions);
assert.strictEqual(restoredCli.status, 0, restoredCli.stderr);
const exportedEnv = fs.readFileSync(ciEnvFile, 'utf8');
assert.strictEqual(exportedEnv, 'EXISTING=value\nRUNTIME_DIR=' + ciRoot + '\n');
const archiveBytes = fs.readFileSync(plan.archive);
const altered = Buffer.from(archiveBytes);
altered[altered.length - 1] ^= 1;
fs.writeFileSync(plan.archive, altered);
await assert.rejects(installer.install(plan, new AbortController().signal), /SHA-256/);
await assert.rejects(restoreRuntime(source, ciDirectory), /SHA-256/);
const rejectedCli = require('child_process').spawnSync(process.execPath, cliArgs, cliOptions);
assert.strictEqual(rejectedCli.status, 1);
assert(rejectedCli.stderr.includes('SHA-256'));
assert.strictEqual(fs.readFileSync(ciEnvFile, 'utf8'), exportedEnv);
assert.deepStrictEqual(fs.readdirSync(directory), []);
fs.writeFileSync(plan.archive, archiveBytes);
const statfs = fs.promises.statfs;
fs.promises.statfs = async function() { return { bavail: 0, bsize: 4096 }; };
try {
await assert.rejects(installer.install(plan, new AbortController().signal), /free disk space/);
assert.deepStrictEqual(fs.readdirSync(directory), []);
} finally {
fs.promises.statfs = statfs;
}
const phases = [];
const installed = await installer.install(plan, new AbortController().signal, function(state) {
phases.push(state.phase);
});
assert.strictEqual(installed.ok, true);
assert(installed.root.startsWith(directory + path.sep));
assert.strictEqual(fs.readlinkSync(path.join(installed.root, 'lib/libAlias.so')), 'libCore.so');
assert.strictEqual(fs.statSync(path.join(installed.root, 'bin/musrfit')).mode & 0o111, 0o111);
assert.strictEqual(fs.readFileSync(path.join(prefix, 'musrfit_startup.xml'), 'utf8'), '<musrfit>unchanged</musrfit>');
assert(phases.includes('Extracting runtime'));
const reused = await installer.install(plan, new AbortController().signal);
assert.strictEqual(reused.reused, true);
assert.strictEqual(reused.root, installed.root);
for (const phase of ['Verifying runtime archive', 'Extracting runtime']) {
const controller = new AbortController();
await assert.rejects(installer.install(plan, controller.signal, function(state) {
if (state.phase === phase) controller.abort();
}));
assert.strictEqual(fs.readdirSync(directory).some(function(name) { return name.startsWith('.install-'); }), false);
await installer.verifyInstalled(installed.root, manifest);
}
fs.appendFileSync(path.join(installed.root, 'bin/musrfit'), '# local damage\n');
const repaired = await installer.install(plan, new AbortController().signal);
assert.notStrictEqual(repaired.root, installed.root);
assert(fs.readFileSync(path.join(installed.root, 'bin/musrfit'), 'utf8').includes('local damage'));
await installer.verifyInstalled(repaired.root, manifest);
fs.symlinkSync('../../outside-file', path.join(prefix, 'lib', 'escape'));
plan = await makeBundle();
await assert.rejects(installer.install(plan, new AbortController().signal), /Unsafe runtime library link/);
assert.strictEqual(fs.existsSync(path.join(directory, 'outside-file')), false);
fs.unlinkSync(path.join(prefix, 'lib', 'escape'));
plan = await makeBundle(['runtime/bin/musrfit']);
await assert.rejects(installer.install(plan, new AbortController().signal), /duplicate/);
assert.strictEqual(fs.readdirSync(directory).some(function(name) { return name.startsWith('.install-'); }), false);
console.log('Local runtime detection, installation, cancellation, and repair tests passed');
}
run().catch(function(error) {
console.error(error);
process.exitCode = 1;
}).finally(function() {
fs.rmSync(temporary, { recursive: true, force: true });
});