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