2023-03-14 05:56:56 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const { exec, spawn } = require('child_process');
|
2021-07-12 09:22:20 +00:00
|
|
|
const yargs = require('yargs/yargs')
|
|
|
|
const { hideBin } = require('yargs/helpers')
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
const logger = {
|
|
|
|
warn: msg => console.error(`WARN: ${msg}`),
|
|
|
|
err: msg => console.error(`ERR: ${msg}`),
|
|
|
|
info: msg => console.log(`INFO: ${msg}`)
|
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
function panic(errMsg) {
|
|
|
|
logger.err(errMsg);
|
|
|
|
process.exit(-1);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
function checkTestEnv() {
|
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
const argv = yargs(hideBin(process.argv))
|
|
|
|
.usage('Usage: $0 [options] <tests>')
|
|
|
|
.example('$0 --network ethermint', 'run all tests using ethermint network')
|
2021-10-21 11:06:20 +00:00
|
|
|
.example('$0 --network ethermint --allowTests=test1,test2', 'run only test1 and test2 using ethermint network')
|
2021-07-12 09:22:20 +00:00
|
|
|
.help('h').alias('h', 'help')
|
|
|
|
.describe('network', 'set which network to use: ganache|ethermint')
|
2021-10-21 11:06:20 +00:00
|
|
|
.describe('batch', 'set the test batch in parallelized testing. Format: %d-%d')
|
|
|
|
.describe('allowTests', 'only run specified tests. Separated by comma.')
|
2022-10-13 05:53:17 +00:00
|
|
|
.boolean('verbose-log').describe('verbose-log', 'print laconicd output, default false')
|
2023-03-14 05:56:56 +00:00
|
|
|
.argv;
|
2021-07-12 09:22:20 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(path.join(__dirname, './node_modules'))) {
|
2023-03-14 05:56:56 +00:00
|
|
|
panic('node_modules not existed. Please run `yarn install` before running tests.');
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
const runConfig = {};
|
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
// Check test network
|
|
|
|
if (!argv.network) {
|
2023-03-14 05:56:56 +00:00
|
|
|
runConfig.network = 'ganache';
|
|
|
|
}
|
|
|
|
else {
|
2021-07-12 09:22:20 +00:00
|
|
|
if (argv.network !== 'ethermint' && argv.network !== 'ganache') {
|
2023-03-14 05:56:56 +00:00
|
|
|
panic('network is invalid. Must be ganache or ethermint');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
runConfig.network = argv.network;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 11:06:20 +00:00
|
|
|
if (argv.batch) {
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
const [toRunBatch, allBatches] = argv.batch.split('-').map(e => Number(e));
|
|
|
|
|
|
|
|
console.log([toRunBatch, allBatches]);
|
2021-10-21 11:06:20 +00:00
|
|
|
if (!toRunBatch || !allBatches) {
|
2023-03-14 05:56:56 +00:00
|
|
|
panic('bad batch input format');
|
2021-10-21 11:06:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
|
2021-10-21 11:06:20 +00:00
|
|
|
if (toRunBatch > allBatches) {
|
2023-03-14 05:56:56 +00:00
|
|
|
panic('test batch number is larger than batch counts');
|
2021-10-21 11:06:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
|
|
|
|
if (toRunBatch <= 0 || allBatches <=0 ) {
|
|
|
|
panic('test batch number or batch counts must be non-zero values');
|
2021-10-21 11:06:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
|
|
|
|
runConfig.batch = {};
|
|
|
|
runConfig.batch.this = toRunBatch;
|
|
|
|
runConfig.batch.all = allBatches;
|
2021-10-21 11:06:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
// only test
|
2023-03-14 05:56:56 +00:00
|
|
|
runConfig.onlyTest = !!argv['allowTests'] ? argv['allowTests'].split(',') : undefined;
|
|
|
|
runConfig.verboseLog = !!argv['verbose-log'];
|
|
|
|
|
|
|
|
logger.info(`Running on network: ${runConfig.network}`);
|
|
|
|
return runConfig;
|
2021-07-12 09:22:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
function loadTests(runConfig) {
|
|
|
|
let validTests = [];
|
2021-07-12 09:22:20 +00:00
|
|
|
fs.readdirSync(path.join(__dirname, 'suites')).forEach(dirname => {
|
2023-03-14 05:56:56 +00:00
|
|
|
const dirStat = fs.statSync(path.join(__dirname, 'suites', dirname));
|
2021-07-12 09:22:20 +00:00
|
|
|
if (!dirStat.isDirectory) {
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.warn(`${dirname} is not a directory. Skip this test suite.`);
|
|
|
|
return;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
const needFiles = ['package.json', 'test'];
|
2021-07-12 09:22:20 +00:00
|
|
|
for (const f of needFiles) {
|
|
|
|
if (!fs.existsSync(path.join(__dirname, 'suites', dirname, f))) {
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.warn(`${dirname} does not contains file/dir: ${f}. Skip this test suite.`);
|
|
|
|
return;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test package.json
|
|
|
|
try {
|
|
|
|
const testManifest = JSON.parse(fs.readFileSync(path.join(__dirname, 'suites', dirname, 'package.json'), 'utf-8'))
|
2023-03-14 05:56:56 +00:00
|
|
|
const needScripts = ['test-ganache', 'test-ethermint'];
|
2021-07-12 09:22:20 +00:00
|
|
|
for (const s of needScripts) {
|
2023-03-14 05:56:56 +00:00
|
|
|
if (Object.keys(testManifest['scripts']).indexOf(s) === -1) {
|
|
|
|
logger.warn(`${dirname} does not have test script: \`${s}\`. Skip this test suite.`);
|
|
|
|
return;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.warn(`${dirname} test package.json load failed. Skip this test suite.`);
|
|
|
|
logger.err(error);
|
|
|
|
return;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
validTests.push(dirname);
|
2021-07-12 09:22:20 +00:00
|
|
|
})
|
2021-10-21 11:06:20 +00:00
|
|
|
|
|
|
|
if (runConfig.onlyTest) {
|
2023-03-14 05:56:56 +00:00
|
|
|
validTests = validTests.filter(t => runConfig.onlyTest.indexOf(t) !== -1);
|
2021-10-21 11:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (runConfig.batch) {
|
2023-03-14 05:56:56 +00:00
|
|
|
const chunkSize = Math.ceil(validTests.length / runConfig.batch.all);
|
2021-10-21 11:06:20 +00:00
|
|
|
const toRunTests = validTests.slice(
|
|
|
|
(runConfig.batch.this - 1) * chunkSize,
|
|
|
|
runConfig.batch.this === runConfig.batch.all ? undefined : runConfig.batch.this * chunkSize
|
2023-03-14 05:56:56 +00:00
|
|
|
);
|
|
|
|
return toRunTests;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return validTests;
|
2021-10-21 11:06:20 +00:00
|
|
|
}
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
function performTestSuite({ testName, network }) {
|
|
|
|
const cmd = network === 'ganache' ? 'test-ganache' : 'test-ethermint';
|
2021-07-12 09:22:20 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const testProc = spawn('yarn', [cmd], {
|
|
|
|
cwd: path.join(__dirname, 'suites', testName)
|
2023-03-14 05:56:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
testProc.stdout.pipe(process.stdout);
|
|
|
|
testProc.stderr.pipe(process.stderr);
|
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
testProc.on('close', code => {
|
|
|
|
if (code === 0) {
|
2023-03-14 05:56:56 +00:00
|
|
|
console.log("end");
|
|
|
|
resolve();
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
else {
|
|
|
|
reject(new Error(`Test: ${testName} exited with error code ${code}`));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
async function performTests({ allTests, runConfig }) {
|
|
|
|
|
2021-07-12 09:22:20 +00:00
|
|
|
if (allTests.length === 0) {
|
2023-03-14 05:56:56 +00:00
|
|
|
panic('No tests are found or all invalid!');
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const currentTestName of allTests) {
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.info(`Start test: ${currentTestName}`);
|
|
|
|
await performTestSuite({ testName: currentTestName, network: runConfig.network });
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.info(`${allTests.length} test suites passed!`);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
function setupNetwork({ runConfig, timeout }) {
|
2021-07-12 09:22:20 +00:00
|
|
|
if (runConfig.network !== 'ethermint') {
|
|
|
|
// no need to start ganache. Truffle will start it
|
2023-03-14 05:56:56 +00:00
|
|
|
return;
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn the ethermint process
|
|
|
|
|
|
|
|
const spawnPromise = new Promise((resolve, reject) => {
|
2022-10-13 05:53:17 +00:00
|
|
|
const laconicdProc = spawn('./init-test-node.sh', {
|
2021-07-12 09:22:20 +00:00
|
|
|
cwd: __dirname,
|
2023-03-14 05:56:56 +00:00
|
|
|
stdio: ['ignore', runConfig.verboseLog ? 'pipe' : 'ignore', 'pipe'],
|
|
|
|
});
|
2021-07-12 09:22:20 +00:00
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.info(`Starting laconicd process... timeout: ${timeout}ms`);
|
2021-07-12 09:22:20 +00:00
|
|
|
if (runConfig.verboseLog) {
|
2023-03-14 05:56:56 +00:00
|
|
|
laconicdProc.stdout.pipe(process.stdout);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2022-10-13 05:53:17 +00:00
|
|
|
laconicdProc.stderr.on('data', d => {
|
2023-03-14 05:56:56 +00:00
|
|
|
const oLine = d.toString();
|
2021-07-12 09:22:20 +00:00
|
|
|
if (runConfig.verboseLog) {
|
2023-03-14 05:56:56 +00:00
|
|
|
process.stdout.write(oLine);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2021-10-08 13:38:42 +00:00
|
|
|
if (oLine.indexOf('Starting JSON-RPC server') !== -1) {
|
2023-03-14 05:56:56 +00:00
|
|
|
logger.info('laconicd started');
|
|
|
|
resolve(laconicdProc);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
});
|
|
|
|
});
|
2021-07-12 09:22:20 +00:00
|
|
|
|
|
|
|
const timeoutPromise = new Promise((resolve, reject) => {
|
2023-03-14 05:56:56 +00:00
|
|
|
setTimeout(() => reject(new Error('Start laconicd timeout!')), timeout);
|
|
|
|
});
|
|
|
|
return Promise.race([spawnPromise, timeoutPromise]);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
async function main() {
|
|
|
|
|
|
|
|
const runConfig = checkTestEnv();
|
|
|
|
const allTests = loadTests(runConfig);
|
2021-07-12 09:22:20 +00:00
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
console.log(`Running Tests: ${allTests.join()}`);
|
2021-10-21 11:06:20 +00:00
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
const proc = await setupNetwork({ runConfig, timeout: 50000 });
|
|
|
|
await performTests({ allTests, runConfig });
|
2021-07-12 09:22:20 +00:00
|
|
|
|
|
|
|
if (proc) {
|
2023-03-14 05:56:56 +00:00
|
|
|
proc.kill();
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
2023-03-14 05:56:56 +00:00
|
|
|
process.exit(0);
|
2021-07-12 09:22:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 13:38:42 +00:00
|
|
|
// Add handler to exit the program when UnhandledPromiseRejection
|
|
|
|
|
2021-10-21 11:06:20 +00:00
|
|
|
process.on('unhandledRejection', e => {
|
2023-03-14 05:56:56 +00:00
|
|
|
console.error(e);
|
|
|
|
process.exit(-1);
|
|
|
|
});
|
2021-10-08 13:38:42 +00:00
|
|
|
|
2023-03-14 05:56:56 +00:00
|
|
|
main();
|