89 lines
5.5 KiB
JavaScript
89 lines
5.5 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 crypto = require('crypto');
|
|
const { selectRuntime, publishRuntime, downloadRuntime } = require('../scripts/ci-runtime-registry');
|
|
|
|
async function main() {
|
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-registry-runtime-'));
|
|
const env = { GITEA_SERVER_URL: 'https://gitea.example.test', GITEA_REPOSITORY: 'owner/muSRFlow',
|
|
PACKAGES_TOKEN: 'token', RUNTIME_TOKEN: 'token' };
|
|
const contents = Buffer.from('archive fixture');
|
|
const sha = crypto.createHash('sha256').update(contents).digest('hex');
|
|
const descriptor = { schemaVersion: 1, kind: 'musrflow-runtime-download',
|
|
runtime: { platform: 'linux', arch: 'x64', runtimeId: 'test-runtime', root: { version: '6.38.00' },
|
|
build: { sourceRevision: 'a'.repeat(40), sourceDirty: false } },
|
|
artifact: { filename: 'runtime-fixture.tar.gz', sha256: sha, size: contents.length, format: 'tar.gz', rootDirectory: 'runtime' } };
|
|
const stored = new Map();
|
|
let requests = 0;
|
|
async function registry(url, options) {
|
|
requests++;
|
|
assert.strictEqual(options.headers.Authorization, 'token token');
|
|
assert.strictEqual(options.redirect, 'error');
|
|
if (options.method === 'POST') return new Response(null, { status: 201 });
|
|
if (options.method === 'PUT') {
|
|
const chunks = [];
|
|
for await (const chunk of options.body) chunks.push(chunk);
|
|
if (stored.has(url)) return new Response(null, { status: 409 });
|
|
stored.set(url, Buffer.concat(chunks));
|
|
return new Response(null, { status: 201 });
|
|
}
|
|
assert(stored.has(url), 'Unknown registry URL: ' + url);
|
|
return new Response(stored.get(url));
|
|
}
|
|
try {
|
|
const bundle = path.join(directory, 'bundle');
|
|
fs.mkdirSync(bundle);
|
|
fs.writeFileSync(path.join(bundle, 'runtime-download.json'), JSON.stringify(descriptor));
|
|
fs.writeFileSync(path.join(bundle, descriptor.artifact.filename), contents);
|
|
fs.writeFileSync(path.join(bundle, descriptor.artifact.filename + '.sha256'), `${sha} ${descriptor.artifact.filename}\n`);
|
|
const pin = await publishRuntime(bundle, env, registry);
|
|
assert.strictEqual(stored.size, 3);
|
|
assert(pin.runtimes['linux-x64'].descriptorUrl.includes('linux-x64-' + sha));
|
|
assert.deepStrictEqual(await publishRuntime(bundle, env, registry), pin);
|
|
const config = path.join(directory, 'runtime-source.json');
|
|
fs.writeFileSync(config, JSON.stringify(pin));
|
|
assert.strictEqual(selectRuntime(config, { LEGACY_RUNTIME_RUN_ID: '123' }).source, 'registry');
|
|
assert.strictEqual(selectRuntime(config, { RUNTIME_RUN_OVERRIDE: '123' }).runId, '123');
|
|
assert.throws(() => selectRuntime(config, { RUNTIME_RUN_OVERRIDE: '../bad' }), /numeric/);
|
|
const output = path.join(directory, 'download');
|
|
await downloadRuntime(config, output, env, registry);
|
|
assert(fs.readFileSync(path.join(output, descriptor.artifact.filename)).equals(contents));
|
|
await assert.rejects(downloadRuntime(config, output, env, registry), /already exists/);
|
|
const failureOutput = path.join(directory, 'failure');
|
|
const goodDescriptor = stored.get(pin.runtimes['linux-x64'].descriptorUrl);
|
|
stored.set(pin.runtimes['linux-x64'].descriptorUrl, Buffer.from('{}'));
|
|
await assert.rejects(downloadRuntime(config, failureOutput, env, registry), /checksum/);
|
|
assert(!fs.existsSync(failureOutput));
|
|
stored.set(pin.runtimes['linux-x64'].descriptorUrl, goodDescriptor);
|
|
const archiveUrl = new URL(descriptor.artifact.filename, pin.runtimes['linux-x64'].descriptorUrl).href;
|
|
stored.set(archiveUrl, Buffer.from('corrupt'));
|
|
await assert.rejects(downloadRuntime(config, failureOutput, env, registry), /checksum or size/);
|
|
stored.set(archiveUrl, Buffer.alloc(contents.length + 1));
|
|
await assert.rejects(downloadRuntime(config, failureOutput, env, registry), /exceeds expected size/);
|
|
assert(!fs.existsSync(failureOutput));
|
|
await assert.rejects(downloadRuntime(config, failureOutput, env, async () => new Response(null, { status: 401 })), /HTTP 401/);
|
|
const altered = structuredClone(pin);
|
|
altered.runtimes['linux-x64'].descriptorUrl = 'https://other.example.test/runtime-download.json';
|
|
fs.writeFileSync(config, JSON.stringify(altered));
|
|
const before = requests;
|
|
await assert.rejects(downloadRuntime(config, failureOutput, env, registry), /this Gitea owner/);
|
|
assert.strictEqual(requests, before, 'never send tokens to a different origin');
|
|
fs.writeFileSync(config, JSON.stringify({ schemaVersion: 1, runtimes: { 'linux-x64': null } }));
|
|
assert.throws(() => selectRuntime(config, {}), /No approved runtime pin/);
|
|
assert.strictEqual(selectRuntime(config, { LEGACY_RUNTIME_RUN_ID: '123' }).runId, '123');
|
|
fs.writeFileSync(path.join(bundle, descriptor.artifact.filename), Buffer.from('changed'));
|
|
await assert.rejects(publishRuntime(bundle, env, registry), /size mismatch/);
|
|
assert.strictEqual(requests, before);
|
|
console.log('Durable runtime registry and pin tests passed');
|
|
} finally {
|
|
fs.rmSync(directory, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
main().catch(error => { console.error(error); process.exitCode = 1; });
|