diff options
author | Dimitry <winsvega@mail.ru> | 2017-01-28 00:31:19 +0800 |
---|---|---|
committer | Dimitry <winsvega@mail.ru> | 2017-01-28 00:31:19 +0800 |
commit | ea7e06b2970a01cd11b3a062425f5c5cafc19ee7 (patch) | |
tree | 82f0dad0a0c0c37591e8c8d542710b8d909f086d | |
parent | bc8ad7422d32a940bab194173083a684adcc2706 (diff) | |
download | dexon-tests-ea7e06b2970a01cd11b3a062425f5c5cafc19ee7.tar.gz dexon-tests-ea7e06b2970a01cd11b3a062425f5c5cafc19ee7.tar.zst dexon-tests-ea7e06b2970a01cd11b3a062425f5c5cafc19ee7.zip |
rpc tests on js
-rw-r--r-- | RPCTests/main.js | 151 | ||||
-rw-r--r-- | RPCTests/modules/ethconsole.js | 97 | ||||
m--------- | RPCTests/modules/ethereum-console | 0 | ||||
-rw-r--r-- | RPCTests/modules/startnode.js | 27 | ||||
-rw-r--r-- | RPCTests/modules/testutils.js | 69 | ||||
-rw-r--r-- | RPCTests/modules/utils.js | 35 | ||||
-rw-r--r-- | RPCTests/scripts/addPeer.js | 1 | ||||
-rw-r--r-- | RPCTests/scripts/getLastBlock.js | 1 | ||||
-rw-r--r-- | RPCTests/scripts/getNewAccount.js | 3 | ||||
-rw-r--r-- | RPCTests/scripts/getNodeInfo.js | 1 | ||||
-rw-r--r-- | RPCTests/scripts/sleep.js | 4 | ||||
-rw-r--r-- | RPCTests/scripts/test00_node02_AddPeer.js | 26 | ||||
-rw-r--r-- | RPCTests/scripts/test01_node01_getPeerCount.js | 11 | ||||
-rw-r--r-- | RPCTests/scripts/test02_node01_mineBlocks.js | 31 | ||||
-rw-r--r-- | RPCTests/scripts/test03_node02_getBlockHash.js | 13 | ||||
-rw-r--r-- | RPCTests/scripts/test04_node02_mineBlocks.js | 31 | ||||
-rw-r--r-- | RPCTests/scripts/test05_node01_getBlockHash.js | 13 | ||||
-rw-r--r-- | RPCTests/scripts/testNewAccount.js | 12 |
18 files changed, 435 insertions, 91 deletions
diff --git a/RPCTests/main.js b/RPCTests/main.js index 0cba752eb..ffb5cfa62 100644 --- a/RPCTests/main.js +++ b/RPCTests/main.js @@ -1,66 +1,119 @@ -var exec = require('child_process').exec; +//requires installed node v6 +//requires ETHEREUM_TEST_PATH env variable set (for full path to the ipc sockets) +//requires ethereum eth path +var async = require("./modules/async"); var utils = require('./modules/utils.js'); -var startNode = require('./modules/startnode.js'); +var testutils = require('./modules/testutils.js'); +var ethconsole = require('./modules/ethconsole.js'); var ethpath = '/home/wins/Ethereum/cpp-ethereum/build/eth/eth'; var testdir = process.env.ETHEREUM_TEST_PATH + "/RPCTests/dynamic"; -var async = require("./modules/async"); - -function cb(err, data) -{ -console.log(data); -} +var dynamic = {}; -function main(goto, args) +function cb(){} +function main() { +testutils.readTestsInFolder("./scripts"); async.series([ -function(cb) { utils.readFile('./scripts/genesis.json', cb); }, -function(cb) { cb(); console.log("as2"); }, -function(cb) { cb(); console.log("as3"); } -], function() { console.log("as4") }) - -/*utils.sleep(2000).then(() => { - console.log('Result: '); -});*/ - -/*switch(goto) -{ -case 1: utils.mkdir('./dynamic', main, 2); break; -case 2: utils.readFile('./scripts/genesis.json', main, 3); break; -case 3: console.log(args); break; -}*/ - - -//utils.writeFile('./dynamic/genesis.json', data, (err) => { if (err) throw err;}); -//startNode(ethpath, testdir + "/ethnode1", testdir + "/genesis.json", 30305); +function(cb) { + utils.setDebug(false); + ethconsole.startNode(ethpath, testdir + "/ethnode1", testdir + "/genesis.json", 30305, cb); +}, +function(cb) { + prepareDynamicVars(cb); +}, +function(cb) { + ethconsole.stopNode(testdir + "/ethnode1", cb); +}, +function(cb) { + ethconsole.startNode(ethpath, testdir + "/ethnode1", testdir + "/genesis.json", 30305, cb); + dynamic["node1_port"] = "30305"; +}, +function(cb) { + ethconsole.startNode(ethpath, testdir + "/ethnode2", testdir + "/genesis.json", 30306, cb); + dynamic["node2_port"] = "30306"; +}, +function(cb) { + runAllTests(cb); +}, +function(cb) { + ethconsole.stopNode(testdir + "/ethnode1", cb); + ethconsole.stopNode(testdir + "/ethnode2", cb); } - -/*var callback1 = function (err, data) {}; -fs.readFile('./scripts/genesis.json', 'utf8', (err, data) => { callback1 (err,data) }); -startNode = require('./modules/startnode.js'); +], function() { + utils.rmdir(testdir); } +) +}//main -callback1 = function (err, data) +function prepareDynamicVars(finished) { - if (err) throw err; - data = data.replace("[ADDRESS]", "0x112233445566778899"); - mkdir('./dynamic'); - fs.writeFile('./dynamic/genesis.json', data, (err) => { if (err) throw err;}); - - - exec('pwd', function callback(error, stdout, stderr){ - dir = stdout; - startNode(ethpath, 'privatechain', testdir + "/ethnode1", 30305); - }); - - //rmdir('./dynamic'); -}*/ - - -main(1); + async.series([ + function(cb) { + ethconsole.runScriptOnNode(testdir + "/ethnode1", "./scripts/testNewAccount.js", {}, cb); + }, + function(cb) { + dynamic["account"] = ethconsole.getLastResponse(); + utils.mkdir(testdir); + testutils.generateCustomGenesis(testdir + '/genesis.json', "./scripts/genesis.json", dynamic["account"], cb); + }, + function(cb) { + ethconsole.runScriptOnNode(testdir + "/ethnode1", "./scripts/getNodeInfo.js", {}, cb); + }, + function(cb) { + dynamic["node1_ID"] = ethconsole.getLastResponse().id; + cb(); + } + ], function() { finished(); }) +} +function runAllTests(finished) +{ + var currentTest = -1; + var updateDynamic = function(){}; + + function nextTest() + { + currentTest++; + if (currentTest == testutils.getTestCount()) + finished(); + else + { + var testObject = testutils.getTestNumber(currentTest); + var nodepath; + if (testObject.node == '01') + nodepath = testdir + "/ethnode1"; + if (testObject.node == '02') + nodepath = testdir + "/ethnode2"; + + ethconsole.runScriptOnNode(nodepath, testObject.file, dynamic, updateDynamic); + } + } + + updateDynamic = function updateDynamic() + { + async.series([ + function(cb) { + ethconsole.runScriptOnNode(testdir + "/ethnode1", "./scripts/getLastBlock.js", {}, cb); + }, + function(cb) { + dynamic["node1_lastblock"] = ethconsole.getLastResponse(); + cb(); + }, + function(cb) { + ethconsole.runScriptOnNode(testdir + "/ethnode2", "./scripts/getLastBlock.js", {}, cb); + }, + function(cb) { + dynamic["node2_lastblock"] = ethconsole.getLastResponse(); + cb(); + } + ], function() { nextTest(); }); + } + nextTest(); +} +main(); diff --git a/RPCTests/modules/ethconsole.js b/RPCTests/modules/ethconsole.js new file mode 100644 index 000000000..d3ee707bb --- /dev/null +++ b/RPCTests/modules/ethconsole.js @@ -0,0 +1,97 @@ +const fs = require('fs'); +var lastResponse; +var nodes = {}; + +module.exports = { + +startNode: function startNode (nodeExec, dataDir, genesisPath, listeningPort, finished) +{ + var utils = require('./utils.js'); + var spawn = require('child_process').spawn + var options = [ + '--private', 'privatechain', + '-d', dataDir, + '--config', genesisPath, + '--ipcpath', dataDir + '/geth.ipc', + '--ipc', + '--listen', listeningPort, + '--test' + ] + utils.cLog('starting node') + utils.cLog(nodeExec + ' ' + options.join(' ')) + var node = spawn(nodeExec, options) + node.stdout.on('data', (data) => { + utils.cLog(`stdout: ${data}`) + }) + node.stderr.on('data', (data) => { + utils.cLog(`stderr: ${data}`) + }) + node.on('close', (code) => { + utils.cLog(`child process exited with code ${code}`) + }) + + nodes[dataDir] = node; + utils.sleep(6000).then(() => { + utils.cLog("Node Started"); + finished(); + }); +}, + +stopNode: function stopNode(dataDir, finished) +{ + nodes[dataDir].kill(); + var utils = require('./utils.js'); + utils.sleep(1000).then(() => { + finished(); + }); +}, + + +runScriptOnNode: function runScriptOnNode(dataDir, jsScript, args, finished) +{ + var utils = require('./utils.js'); + var ipcPath = dataDir + '/geth.ipc'; + + var Web3 = require('./ethereum-console/node_modules/web3'); + var web3admin = require('./ethereum-console/web3Admin.js'); + var net = require('net'); + + utils.cLog("Connecting to node at " + ipcPath); + var web3 = new Web3(new Web3.providers.IpcProvider(ipcPath, net)); + web3admin.extend(web3); + global.web3 = web3; + + var onScriptCallback = function (err, data) + { + utils.cLog(data); + lastResponse = data; + finished(); + } + global.callback = onScriptCallback; + global.args = args; + + var vm = require('vm'); + utils.cLog("Executing " + jsScript + " ..."); + fs.readFile(jsScript, 'utf8', function (err, data) + { + if (err) + { + utils.cLog(err); + finished(); + } + else + { + var script = new vm.Script(data); + - script.runInThisContext(); + } + }); +}, + + +getLastResponse: function getLastResponse() +{ + return lastResponse; +} + +}//exports + diff --git a/RPCTests/modules/ethereum-console b/RPCTests/modules/ethereum-console new file mode 160000 +Subproject 4dbc626aab094b10d859c78059285138774da70 diff --git a/RPCTests/modules/startnode.js b/RPCTests/modules/startnode.js deleted file mode 100644 index f0877b065..000000000 --- a/RPCTests/modules/startnode.js +++ /dev/null @@ -1,27 +0,0 @@ -function startNode (nodeExec, dataDir, genesisPath, listeningPort) -{ - var spawn = require('child_process').spawn - var options = [ - '--private', 'privatechain', - '-d', dataDir, - '--config', genesisPath, - '--ipcpath', dataDir + '/geth.ipc', - '--ipc', - '--listen', listeningPort, - '--test' - ] - console.log('starting node') - console.log(nodeExec + ' ' + options.join(' ')) - var node = spawn(nodeExec, options) - node.stdout.on('data', (data) => { - console.log(`stdout: ${data}`) - }) - node.stderr.on('data', (data) => { - console.log(`stderr: ${data}`) - }) - node.on('close', (code) => { - console.log(`child process exited with code ${code}`) - }) -} - -module.exports = startNode diff --git a/RPCTests/modules/testutils.js b/RPCTests/modules/testutils.js new file mode 100644 index 000000000..7128c6972 --- /dev/null +++ b/RPCTests/modules/testutils.js @@ -0,0 +1,69 @@ +const fs = require('fs'); +var utils = require('./utils.js'); +var tests = {}; +var testCount = 0; + +module.exports = { + +generateCustomGenesis: function generateCustomGenesis(path, originalPath, accountName, finished) +{ + var onFileRead = function (err, data) {}; + fs.readFile(originalPath, 'utf8', (err, data) => { onFileRead (err,data) }); + + onFileRead = function (err, data) + { + if (err) + throw err; + + data = data.replace("[ADDRESS]", accountName); + fs.writeFile(path, data, (err) => { + if (err) + throw err; + finished(); + }); + } +}, + +readTestsInFolder: function readTestsInFolder(path) +{ + var res = utils.listFiles(path); + res.forEach(function(file) { + var testn = file.indexOf("test"); + var slashn = file.indexOf("_"); + if (testn != -1 && slashn != -1) + { + testNumber = parseInt(file.substring(testn + 4, slashn)); + var noden = file.indexOf("node"); + var slashn = file.indexOf("_", slashn+1); + var tmpFile = file.indexOf("~"); + if (noden != -1 && slashn != -1 && tmpFile == -1) + { + if (tests[testNumber]) + console.log("Error: dublicate test found " + file); + else + { + var testObject = {}; + testObject.file = file; + testObject.node = file.substring(noden + 4, slashn); + tests[testNumber] = testObject; + testCount++; + } + } + } + }); +}, + +getTestCount: function getTestCount() +{ + return testCount; +}, + +getTestNumber: function getTestNumber(n) +{ + if(n < testCount); + return tests[n]; +} + + +}//modules + diff --git a/RPCTests/modules/utils.js b/RPCTests/modules/utils.js index 763c9894e..24e9bd811 100644 --- a/RPCTests/modules/utils.js +++ b/RPCTests/modules/utils.js @@ -1,7 +1,5 @@ const fs = require('fs'); -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); - } +var debug = true; module.exports = { @@ -9,14 +7,20 @@ module.exports = { return new Promise(resolve => setTimeout(resolve, ms)); }, + setDebug: function setDebug(value) { debug = value; }, + + getDebug: function getDebug() { return debug; }, - mkdir: function mkdir(path, callback, arg) { + cLog: function cLog(value) { if (debug) console.log(value); }, + + testLog: function testLog(value) { console.log(value); }, + + mkdir: function mkdir(path) { try { fs.mkdirSync(path); } catch(e) { if ( e.code != 'EEXIST' ) throw e; } - callback(arg); }, rmdir: function rmdir(path) { @@ -33,12 +37,27 @@ module.exports = { } }, - readFile: function readFile(path, callback) { - fs.readFile(path, 'utf8', (err, data) => { callback (err, data) }); + readFile: function readFile(path, callback, cb) { + fs.readFile(path, 'utf8', (err, data) => { callback (err, data, cb) }); }, writeFile: function writeFile(path, data) { fs.writeFile(path, data, (err) => { if (err) throw err;}); + }, + + listFiles: function listFiles(dir) { + + var results = []; + fs.readdirSync(dir).forEach(function(file) { + file = dir+'/'+file; + var stat = fs.statSync(file); + + if (stat && stat.isDirectory()) { + results = results.concat(listFiles(file)) + } else results.push(file); + }); + + return results; } -} +} //exports diff --git a/RPCTests/scripts/addPeer.js b/RPCTests/scripts/addPeer.js new file mode 100644 index 000000000..3da731748 --- /dev/null +++ b/RPCTests/scripts/addPeer.js @@ -0,0 +1 @@ +web3.admin.addPeer("enode://" + args[0], function(err, res){ callback(err, res); }) diff --git a/RPCTests/scripts/getLastBlock.js b/RPCTests/scripts/getLastBlock.js new file mode 100644 index 000000000..5ea2efafa --- /dev/null +++ b/RPCTests/scripts/getLastBlock.js @@ -0,0 +1 @@ +web3.eth.getBlock("latest", function(err, res){ callback(err, res); }) diff --git a/RPCTests/scripts/getNewAccount.js b/RPCTests/scripts/getNewAccount.js deleted file mode 100644 index eceacc23f..000000000 --- a/RPCTests/scripts/getNewAccount.js +++ /dev/null @@ -1,3 +0,0 @@ -sleep = require('./modules/sleep.js'); -var result; -web3.personal.newAccount("123", function(err,res){result = res; console.log(res);}) diff --git a/RPCTests/scripts/getNodeInfo.js b/RPCTests/scripts/getNodeInfo.js new file mode 100644 index 000000000..7452d9ca3 --- /dev/null +++ b/RPCTests/scripts/getNodeInfo.js @@ -0,0 +1 @@ +web3.admin.getNodeInfo(function(err,res){ callback(err, res); }) diff --git a/RPCTests/scripts/sleep.js b/RPCTests/scripts/sleep.js deleted file mode 100644 index 8308c93d3..000000000 --- a/RPCTests/scripts/sleep.js +++ /dev/null @@ -1,4 +0,0 @@ -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} -module.exports = sleep diff --git a/RPCTests/scripts/test00_node02_AddPeer.js b/RPCTests/scripts/test00_node02_AddPeer.js new file mode 100644 index 000000000..233648dc3 --- /dev/null +++ b/RPCTests/scripts/test00_node02_AddPeer.js @@ -0,0 +1,26 @@ +process.stdout.write("TEST_addPeerOnNode2 "); +var onResult = {}; +web3.admin.addPeer("enode://" + args["node1_ID"] + "@127.0.0.1:" + args["node1_port"], function(err, res){ onResult(err, res); }) + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +var onGetPeerCount = {}; +onResult = function (err,res) +{ + //wait for peer being added + sleep(1000).then(() => { + web3.net.getPeerCount(function(err, res){ onGetPeerCount(err, res); }) + }); +} + +onGetPeerCount = function (err, res) +{ + if (res == 1) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} + diff --git a/RPCTests/scripts/test01_node01_getPeerCount.js b/RPCTests/scripts/test01_node01_getPeerCount.js new file mode 100644 index 000000000..155846dbf --- /dev/null +++ b/RPCTests/scripts/test01_node01_getPeerCount.js @@ -0,0 +1,11 @@ +process.stdout.write("TEST_getPeerCountOnNode1 "); +var onResult = {}; +web3.net.getPeerCount(function(err, res){ onResult(err, res); }) +onResult = function (err,res) +{ + if (res == 1) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} diff --git a/RPCTests/scripts/test02_node01_mineBlocks.js b/RPCTests/scripts/test02_node01_mineBlocks.js new file mode 100644 index 000000000..14bc0bb63 --- /dev/null +++ b/RPCTests/scripts/test02_node01_mineBlocks.js @@ -0,0 +1,31 @@ +process.stdout.write("TEST_mineBlockOnNode1 "); + +var latestBlock; +web3.eth.getBlockNumber(function(err, res){ onGetBlockNumber1(err, res); }) +onGetBlockNumber1 = function (err, res) +{ + latestBlock = res; + web3.test.mineBlocks(1, function(err, res){ onResult(err, res); }) +} + + +onResult = function (err,res) +{ + function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + //wait for block being mined + sleep(1000).then(() => { + web3.eth.getBlockNumber(function(err, res){ onGetBlockNumber(err, res); }) + }); +} + +onGetBlockNumber = function (err, res) +{ + if (res == latestBlock + 1) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} diff --git a/RPCTests/scripts/test03_node02_getBlockHash.js b/RPCTests/scripts/test03_node02_getBlockHash.js new file mode 100644 index 000000000..fc09f8423 --- /dev/null +++ b/RPCTests/scripts/test03_node02_getBlockHash.js @@ -0,0 +1,13 @@ +process.stdout.write("TEST_getBlockHashOnNode2 "); + +var onResult = {}; +web3.eth.getBlock("latest", function(err, res){ onResult(err, res); }) + +onResult = function (err,res) +{ + if (res.hash == args["node1_lastblock"].hash) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} diff --git a/RPCTests/scripts/test04_node02_mineBlocks.js b/RPCTests/scripts/test04_node02_mineBlocks.js new file mode 100644 index 000000000..70936cc41 --- /dev/null +++ b/RPCTests/scripts/test04_node02_mineBlocks.js @@ -0,0 +1,31 @@ +process.stdout.write("TEST_mineBlockOnNode2 "); + +var latestBlock; +web3.eth.getBlockNumber(function(err, res){ onGetBlockNumber1(err, res); }) +onGetBlockNumber1 = function (err, res) +{ + latestBlock = res; + web3.test.mineBlocks(1, function(err, res){ onResult(err, res); }) +} + + +onResult = function (err,res) +{ + function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + //wait for block being mined and propagated + sleep(1000).then(() => { + web3.eth.getBlockNumber(function(err, res){ onGetBlockNumber(err, res); }) + }); +} + +onGetBlockNumber = function (err, res) +{ + if (res == latestBlock + 1) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} diff --git a/RPCTests/scripts/test05_node01_getBlockHash.js b/RPCTests/scripts/test05_node01_getBlockHash.js new file mode 100644 index 000000000..d3a3b0bbf --- /dev/null +++ b/RPCTests/scripts/test05_node01_getBlockHash.js @@ -0,0 +1,13 @@ +process.stdout.write("TEST_getBlockHashOnNode1 "); + +var onResult = {}; +web3.eth.getBlock("latest", function(err, res){ onResult(err, res); }) + +onResult = function (err,res) +{ + if (res.hash == args["node2_lastblock"].hash) + console.log("OK"); + else + console.log("FAILED "); + callback(err, res); +} diff --git a/RPCTests/scripts/testNewAccount.js b/RPCTests/scripts/testNewAccount.js new file mode 100644 index 000000000..ae3224384 --- /dev/null +++ b/RPCTests/scripts/testNewAccount.js @@ -0,0 +1,12 @@ +process.stdout.write("TEST_newAccount "); +var onResult = {}; +web3.personal.newAccount("123", function(err,res){ onResult(err,res); }) + +onResult = function (err,res) +{ + if (res.length == 42) + console.log("OK"); + else + console.log("FAILED"); + callback(err, res); +} |