Add nix runtime testing and pre-installing runtimes

This commit is contained in:
Thomas Hobson 2022-01-30 22:29:21 +13:00
parent 564da5a7eb
commit e022e34a37
No known key found for this signature in database
GPG key ID: 9F1FD9D87950DB6F
11 changed files with 306 additions and 176 deletions

39
api/src/bin/install.js Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env node
// Downloads all packages specified into the nix store
require('nocamel');
const config = require('../config');
const Logger = require('logplease');
const logger = Logger.create('install');
const cp = require('child_process');
logger.info('Setting loglevel to', config.log_level);
Logger.setLogLevel(config.log_level);
const runtimes_path = `${config.flake_path}#pistonRuntimeSets.${config.runtime_set}`;
logger.info(`Installing runtimes in ${runtimes_path}`);
logger.debug("Getting package listing");
const runtimes = JSON.parse(cp.execSync(`nix eval --json ${runtimes_path} --apply builtins.attrNames`));
logger.info(`Got runtimes: ${runtimes}`);
runtimes.forEach(runtime => {
logger.debug(`Loading metadata for ${runtime}`);
const runtime_metadata = JSON.parse(cp.execSync(`nix eval --json ${runtimes_path}.${runtime}.metadata`));
const namever = `${runtime}-${runtime_metadata.version}`;
logger.info(`Installing ${namever}`);
function install(key){
logger.debug(`Installing ${namever}-${key}`);
const command = `nix build ${runtimes_path}.${runtime}.metadata.${key} -o /piston/runtimes/${namever}-${key}`;
cp.execSync(command);
}
install("run");
if(runtime_metadata.compile) install("compile");
logger.info(`Installed ${namever}`);
});
logger.info("Done");

77
api/src/bin/pistond.js Executable file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env node
require('nocamel');
const Logger = require('logplease');
const express = require('express');
const expressWs = require('express-ws');
const globals = require('../globals');
const config = require('../config');
const cp = require('child_process');
const path = require('path');
const fs = require('fs/promises');
const fss = require('fs');
const body_parser = require('body-parser');
const runtime = require('../runtime');
const logger = Logger.create('pistond');
const app = express();
expressWs(app);
(async () => {
logger.info('Setting loglevel to', config.log_level);
Logger.setLogLevel(config.log_level);
logger.debug('Ensuring data directories exist');
Object.values(globals.data_directories).for_each(dir => {
let data_path = path.join(config.data_directory, dir);
logger.debug(`Ensuring ${data_path} exists`);
if (!fss.exists_sync(data_path)) {
logger.info(`${data_path} does not exist.. Creating..`);
try {
fss.mkdir_sync(data_path);
} catch (e) {
logger.error(`Failed to create ${data_path}: `, e.message);
}
}
});
logger.info('Loading packages');
const runtimes_data = cp.execSync(`nix eval --json ${config.flake_path}#pistonRuntimeSets.${config.runtime_set} --apply builtins.attrNames`).toString();
const runtimes = JSON.parse(runtimes_data);
runtimes.for_each(pkg => runtime.load_runtime(pkg));
logger.info('Starting API Server');
logger.debug('Constructing Express App');
logger.debug('Registering middleware');
app.use(body_parser.urlencoded({ extended: true }));
app.use(body_parser.json());
app.use((err, req, res, next) => {
return res.status(400).send({
stack: err.stack,
});
});
logger.debug('Registering Routes');
const api_v2 = require('../api/v2');
const api_v3 = require('../api/v3');
app.use('/api/v2', api_v2);
app.use('/api/v3', api_v3);
app.use((req, res, next) => {
return res.status(404).send({ message: 'Not Found' });
});
logger.debug('Calling app.listen');
const [address, port] = config.bind_address.split(':');
app.listen(port, address, () => {
logger.info('API server started on', config.bind_address);
});
})();

112
api/src/bin/test.js Executable file
View file

@ -0,0 +1,112 @@
#!/usr/bin/env node
// Test the specified package
require('nocamel');
const config = require('../config');
const Logger = require('logplease');
const logger = Logger.create('test');
const cp = require('child_process');
const runtime = require("../runtime");
const { Job } = require('../job');
(async function(){
logger.info('Setting loglevel to', config.log_level);
Logger.setLogLevel(config.log_level);
let runtimes_to_test;
let failed = false;
if(process.argv[2] === "--all"){
// load all
runtimes_to_test = JSON.parse(
cp.execSync(`nix eval ${config.flake_path}#pistonRuntimes --json --apply builtins.attrNames`)
);
}else{
runtimes_to_test = [process.argv[2]];
}
for (const runtime_name of runtimes_to_test) {
const runtime_path = `${config.flake_path}#pistonRuntimes.${runtime_name}`;
logger.info(`Testing runtime ${runtime_path}`);
logger.debug(`Loading runtime metadata`);
const metadata = JSON.parse(cp.execSync(`nix eval --json ${runtime_path}.metadata --json`));
logger.debug(`Loading runtime tests`);
const tests = JSON.parse(cp.execSync(`nix eval --json ${runtime_path}.tests --json`));
logger.debug(`Loading runtime`);
const testable_runtime = new runtime.Runtime({
...metadata,
flake_path: runtime_path
});
testable_runtime.ensure_built();
logger.info(`Running tests`);
for (const test of tests) {
const files = [];
for (const file_name of Object.keys(test.files)) {
const file_content = test.files[file_name];
const this_file = {
name: file_name,
content: file_content
};
if(file_name == test.main)
files.unshift(this_file);
else
files.push(this_file);
}
const job = new Job({
runtime: testable_runtime,
args: test.args || [],
stdin: test.stdin || "",
files,
timeouts: {
run: 3000,
compile: 10000
},
memory_limits: {
run: config.run_memory_limit,
compile: config.compile_memory_limit
}
});
await job.prime()
const result = await job.execute()
await job.cleanup()
if(result.run.stdout.trim() !== "OK"){
failed = true;
logger.error("Test Failed:")
console.log(job, result)
}else{
logger.info("Test Passed")
}
}
}
if(failed) {
logger.error("One or more tests failed")
process.exit(1);
}
else {
logger.info("All tests passed")
process.exit(0);
}
})()