UNPKG

mazzaroth-it

Version:

Tool that helps with integration testing of mazzaroth contracts.

198 lines (170 loc) 6.2 kB
'use strict'; var _mazzarothJs = require('mazzaroth-js'); var _util = require('util'); var _util2 = _interopRequireDefault(_util); var _commander = require('commander'); var _commander2 = _interopRequireDefault(_commander); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _child_process = require('child_process'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert); var _jsSha = require('js-sha3'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // import path from 'path' require('app-module-path').addPath(_path2.default.resolve(`${__dirname}/../node_modules`)); const runCmd = _util2.default.promisify(_child_process.exec); const defaultChannel = '0'.repeat(64); const defaultSender = '0'.repeat(64); const defaultOwner = '3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29'; const defaultAddr = 'http://localhost:8081'; const defaultConfig = 'configs'; function getAbi(abi) { if (abi['type'] === 'config') { return abi['value']; } if (abi['type'] === 'file') { return JSON.parse(_fs2.default.readFileSync(abi['value'])); } return []; } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } const testCmd = _commander2.default.command('test'); const testDesc = ` Runs some integrations tests based on a test config. Examples: mazzaroth-it test test_config.json `; testCmd.description(testDesc).option('-c --config <s>', `Web address of the host node default: ${defaultConfig}`).option('-n --skip_docker', `Whether to skip docker startup before running the tests.`); testCmd.action(async function (options) { const configPath = options.config || defaultConfig; console.log(configPath); if (_fs2.default.lstatSync(configPath).isDirectory()) { _fs2.default.readdir(configPath, async function (err, items) { if (err) { console.log(err); return; } for (let i = 0; i < items.length; i++) { // ignore . files if (items[i][0] === '.') { continue; } const fullPath = _path2.default.join(configPath, items[i]); console.log('----------------------------------'); console.log(`Running test config ${fullPath}`); console.log('----------------------------------'); const config = JSON.parse(_fs2.default.readFileSync(fullPath)); await runTest(config, options.skip_docker); } }); } else { const config = JSON.parse(_fs2.default.readFileSync(configPath)); await runTest(config, options.skip_docker); } }); async function runTest(config, skipDocker) { const channel = defaultChannel || config['channel-id']; const host = config['node-addr'] || defaultAddr; const testSets = config['test-sets']; const warmupMs = config['warmup-ms'] || 1000; const deployMs = config['deploy-ms'] || 3000; const transactionMs = config['transaction-ms'] || 3000; const maxBlockExpirations = 5; const abi = getAbi(config['abi']); let xdrTypes = {}; if (config['xdr-types']) { xdrTypes = require(_path2.default.resolve(config['xdr-types'])); } const wasmFile = _fs2.default.readFileSync(config['contract']); const action = { channelID: config.channel_id || defaultChannel, nonce: '1', blockExpirationNumber: '5', category: { type: 2, data: { type: 1, data: { contractBytes: wasmFile.toString('base64'), abi: abi, contractHash: _jsSha.sha3_256.create().update(wasmFile.buffer).hex(), version: '0.1' } } } }; for (const setName in testSets) { let testOutput = ''; let killed = false; let containerID = ''; if (!skipDocker) { // docker run -p 8081:8081 kochavalabs/mazzaroth start standalone const result = await runCmd('docker run -d -p 8081:8081 kochavalabs/mazzaroth start standalone'); containerID = result.stdout; console.log(`Container started: ${containerID}`); } let functionName = ''; try { const configAction = { channelID: config.channel_id || defaultChannel, nonce: '0', blockExpirationNumber: '5', category: { type: 2, data: { type: 2, data: { owner: defaultOwner, admins: [] } } } }; await sleep(warmupMs); const owner = config['owner'] || defaultSender; const client = new _mazzarothJs.NodeClient(host, owner); const testSet = config['test-sets'][setName]; await client.transactionForReceipt(configAction, null, deployMs); await client.transactionForReceipt(action, null, deployMs); testOutput += `Running test: ${setName} \n`; for (const testIndex in testSet) { const test = testSet[testIndex]; const sender = test['sender'] || defaultSender; const client = new _mazzarothJs.NodeClient(host, sender); const contractClient = new _mazzarothJs.ContractClient(abi, client, xdrTypes, channel, null, transactionMs); functionName = test['function_name']; const result = await contractClient[functionName](maxBlockExpirations, ...test['args'].map(x => { if (typeof x === 'object' && x !== null) { return JSON.stringify(x); } return x; })); _assert2.default.deepStrictEqual(result, test['result'], `${setName} ${functionName} ${JSON.stringify(result)}`); testOutput += ` Pass: ${functionName} \n`; } } catch (e) { testOutput += ` Fail: ${functionName} \n\n`; console.log(testOutput); console.log(e); if (!skipDocker) { await runCmd(`docker kill ${containerID}`); } killed = true; } if (!killed) { console.log(testOutput); if (!skipDocker) { await runCmd(`docker kill ${containerID}`); } } } } _commander2.default.on('command:*', function (command) { _commander2.default.help(); }); _commander2.default.parse(process.argv);