From 01d8ec8a2ab567b5aa9e3dafa31a972197327197 Mon Sep 17 00:00:00 2001 From: Allen Han Date: Mon, 8 Jun 2020 09:21:55 -0500 Subject: [PATCH 1/6] init coding --- tests/CMakeLists.txt | 5 +++ tests/Node.py | 8 ++++ tests/trace_plugin_test.py | 86 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100755 tests/trace_plugin_test.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2ab120e21e5..5cde9311309 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -53,6 +53,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/nodeos_high_transaction_test.py ${CMA configure_file(${CMAKE_CURRENT_SOURCE_DIR}/light_validation_sync_test.py ${CMAKE_CURRENT_BINARY_DIR}/light_validation_sync_test.py COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/eosio_blocklog_prune_test.py ${CMAKE_CURRENT_BINARY_DIR}/eosio_blocklog_prune_test.py COPYONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cli_test.py ${CMAKE_CURRENT_BINARY_DIR}/cli_test.py COPYONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/trace_plugin_test.py ${CMAKE_CURRENT_BINARY_DIR}/trace_plugin_test.py COPYONLY) #To run plugin_test with all log from blockchain displayed, put --verbose after --, i.e. plugin_test -- --verbose add_test(NAME plugin_test COMMAND plugin_test --report_level=detailed --color_output) @@ -162,6 +163,10 @@ set_property(TEST nodeos_repeat_transaction_lr_test PROPERTY LABELS long_running add_test(NAME cli_test COMMAND tests/cli_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +add_test(NAME trace_plugin_test COMMAND tests/trace_plugin_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +set_tests_properties(trace_plugin_test PROPERTIES TIMEOUT 40) +set_property(TEST trace_plugin_test PROPERTY LABELS nonparallelizable_tests) + add_subdirectory(se_tests) if(ENABLE_COVERAGE_TESTING) diff --git a/tests/Node.py b/tests/Node.py index 1a1ee82772f..98d3781e9d8 100644 --- a/tests/Node.py +++ b/tests/Node.py @@ -1488,6 +1488,14 @@ def createSnapshot(self): param = { } return self.processCurlCmd("producer", "create_snapshot", json.dumps(param)) + # kill all exsiting nodeos in case lingering from previous test + @staticmethod + def killAllNodeos(): + # kill the eos server + cmd="pkill -9 %s" % (Utils.EosServerName) + ret_code = subprocess.call(cmd.split(), stdout=Utils.FNull) + Utils.Print("cmd: %s, ret:%d" % (cmd, ret_code)) + @staticmethod def findStderrFiles(path): files=[] diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py new file mode 100755 index 00000000000..cd94fef4bf5 --- /dev/null +++ b/tests/trace_plugin_test.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +import json +import os +import shutil +import time +import unittest + +from testUtils import Utils +from testUtils import Account +from Cluster import Cluster +from TestHelper import TestHelper +from Node import Node +from WalletMgr import WalletMgr + +class TraceApiPluginTest(unittest.TestCase): + sleep_s = 2 + base_node_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT) + base_wallet_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT) + keosd = WalletMgr(True, TestHelper.DEFAULT_PORT, TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT, TestHelper.LOCAL_HOST) + node_id = 1 + nodeos = Node(TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT, node_id, None, None, keosd) + data_dir = Utils.getNodeDataDir(node_id) + test_wallet_name="testwallet" + acct_list = None + test_wallet=None + + # make a fresh data dir + def createDataDir(self): + if os.path.exists(self.data_dir): + shutil.rmtree(self.data_dir) + os.makedirs(self.data_dir) + + # kill nodeos and keosd and clean up dir + def cleanEnv(self) : + self.keosd.killall(True) + WalletMgr.cleanup() + Node.killAllNodeos() + if os.path.exists(self.data_dir): + shutil.rmtree(self.data_dir) + time.sleep(self.sleep_s) + + # start keosd and nodeos + def startEnv(self) : + self.createDataDir(self) + self.keosd.launch() + nodeos_plugins = (" --plugin %s --plugin %s --plugin %s --plugin %s --plugin %s ") % ( "eosio::trace_api_plugin", + "eosio::producer_plugin", + "eosio::producer_api_plugin", + "eosio::chain_api_plugin", + "eosio::http_plugin") + nodeos_flags = (" --data-dir=%s --trace-dir=%s --trace-no-abis --filter-on=%s --access-control-allow-origin=%s " + "--contracts-console --http-validate-host=%s --verbose-http-errors ") % (self.data_dir, self.data_dir, "\"*\"", "\'*\'", "false") + start_nodeos_cmd = ("%s -e -p eosio %s %s ") % (Utils.EosServerPath, nodeos_plugins, nodeos_flags) + self.nodeos.launchCmd(start_nodeos_cmd, self.node_id) + time.sleep(self.sleep_s) + + def add_accounts(self) : + ''' add user account ''' + for acct in self.acct_list: + self.nodeos.createInitializeAccount(acct, creatorAccount) + + def add_transactions(self) : + ''' add transactions ''' + + def test_TraceApi(self) : + ''' 1. add accounts, + 2. push a few transactions, + 3. verify via get_block + ''' + + @classmethod + def setUpClass(self): + self.cleanEnv(self) + self.startEnv(self) + self.test_wallet=self.keosd.create(self.test_wallet_name, self.acct_list) + self.acct_list = Cluster.createAccountKeys(3) + self.acct_list[0].name = "alice" + self.acct_list[1].name = "bob" + self.acct_list[2].name = "charlie" + + @classmethod + def tearDownClass(self): + self.cleanEnv(self) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file From 44e6f2dc8be66608558a12e223fb837afd637d88 Mon Sep 17 00:00:00 2001 From: Allen Han Date: Tue, 9 Jun 2020 19:26:34 -0500 Subject: [PATCH 2/6] Use Cluster instead --- tests/CMakeLists.txt | 2 +- tests/Cluster.py | 7 ++- tests/trace_plugin_test.py | 107 +++++++++++++++++++------------------ 3 files changed, 62 insertions(+), 54 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4bce215ee0c..7503c53bc93 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -169,7 +169,7 @@ set_tests_properties(plugin_http_api_test PROPERTIES TIMEOUT 40) set_property(TEST plugin_http_api_test PROPERTY LABELS nonparallelizable_tests) add_test(NAME trace_plugin_test COMMAND tests/trace_plugin_test.py WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) -set_tests_properties(trace_plugin_test PROPERTIES TIMEOUT 40) +set_tests_properties(trace_plugin_test PROPERTIES TIMEOUT 100) set_property(TEST trace_plugin_test PROPERTY LABELS nonparallelizable_tests) add_subdirectory(se_tests) diff --git a/tests/Cluster.py b/tests/Cluster.py index df631658431..d9624ba486f 100644 --- a/tests/Cluster.py +++ b/tests/Cluster.py @@ -166,7 +166,7 @@ def setAlternateVersionLabels(self, file): # pylint: disable=too-many-statements def launch(self, pnodes=1, unstartedNodes=0, totalNodes=1, prodCount=1, topo="mesh", delay=1, onlyBios=False, dontBootstrap=False, totalProducers=None, sharedProducers=0, extraNodeosArgs=" --http-max-response-time-ms 990000 ", useBiosBootFile=True, specificExtraNodeosArgs=None, onlySetProds=False, - pfSetupPolicy=PFSetupPolicy.FULL, alternateVersionLabelsFile=None, associatedNodeLabels=None, loadSystemContract=True): + pfSetupPolicy=PFSetupPolicy.FULL, alternateVersionLabelsFile=None, associatedNodeLabels=None, loadSystemContract=True, enableTrace=False): """Launch cluster. pnodes: producer nodes count unstartedNodes: non-producer nodes that are configured into the launch, but not started. Should be included in totalNodes. @@ -252,6 +252,11 @@ def launch(self, pnodes=1, unstartedNodes=0, totalNodes=1, prodCount=1, topo="me if PFSetupPolicy.hasPreactivateFeature(pfSetupPolicy): nodeosArgs += " --plugin eosio::producer_api_plugin" + if enableTrace : + nodeosArgs += " --plugin eosio::trace_api_plugin" + nodeosArgs += " --trace-no-abis" + nodeosArgs += " --trace-dir=." + if nodeosArgs: cmdArr.append("--nodeos") cmdArr.append(nodeosArgs) diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py index cd94fef4bf5..4b397e8f3d9 100755 --- a/tests/trace_plugin_test.py +++ b/tests/trace_plugin_test.py @@ -11,76 +11,79 @@ from TestHelper import TestHelper from Node import Node from WalletMgr import WalletMgr +from core_symbol import CORE_SYMBOL class TraceApiPluginTest(unittest.TestCase): - sleep_s = 2 - base_node_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT) - base_wallet_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT) - keosd = WalletMgr(True, TestHelper.DEFAULT_PORT, TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT, TestHelper.LOCAL_HOST) - node_id = 1 - nodeos = Node(TestHelper.LOCAL_HOST, TestHelper.DEFAULT_PORT, node_id, None, None, keosd) - data_dir = Utils.getNodeDataDir(node_id) - test_wallet_name="testwallet" - acct_list = None - test_wallet=None - - # make a fresh data dir - def createDataDir(self): - if os.path.exists(self.data_dir): - shutil.rmtree(self.data_dir) - os.makedirs(self.data_dir) + sleep_s = 1 + cluster=Cluster(walletd=True, defproduceraPrvtKey=None) + walletMgr=WalletMgr(True, TestHelper.DEFAULT_PORT, TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT, TestHelper.LOCAL_HOST) + accounts = [] + account_names = ["alice", "bob", "charlie"] # kill nodeos and keosd and clean up dir def cleanEnv(self) : - self.keosd.killall(True) - WalletMgr.cleanup() - Node.killAllNodeos() - if os.path.exists(self.data_dir): - shutil.rmtree(self.data_dir) - time.sleep(self.sleep_s) + self.cluster.killall(allInstances=True) + self.cluster.cleanup() + self.walletMgr.killall(allInstances=True) + self.walletMgr.cleanup() # start keosd and nodeos def startEnv(self) : - self.createDataDir(self) - self.keosd.launch() - nodeos_plugins = (" --plugin %s --plugin %s --plugin %s --plugin %s --plugin %s ") % ( "eosio::trace_api_plugin", - "eosio::producer_plugin", - "eosio::producer_api_plugin", - "eosio::chain_api_plugin", - "eosio::http_plugin") - nodeos_flags = (" --data-dir=%s --trace-dir=%s --trace-no-abis --filter-on=%s --access-control-allow-origin=%s " - "--contracts-console --http-validate-host=%s --verbose-http-errors ") % (self.data_dir, self.data_dir, "\"*\"", "\'*\'", "false") - start_nodeos_cmd = ("%s -e -p eosio %s %s ") % (Utils.EosServerPath, nodeos_plugins, nodeos_flags) - self.nodeos.launchCmd(start_nodeos_cmd, self.node_id) + self.walletMgr.launch() +# self.cluster.setWalletMgr(self.walletMgr) + self.cluster.launch(totalNodes=1, enableTrace=True) + testWalletName="testwallet" + testWallet=self.walletMgr.create(testWalletName, [self.cluster.eosioAccount, self.cluster.defproduceraAccount]) + self.cluster.validateAccounts(None) + self.accounts=Cluster.createAccountKeys(len(self.account_names)) + node = self.cluster.getNode(0) + for idx in range(len(self.account_names)): + self.accounts[idx].name = self.account_names[idx] + self.walletMgr.importKey(self.accounts[idx], testWallet) + for account in self.accounts: + node.createInitializeAccount(account, self.cluster.eosioAccount, buyRAM=1000000, stakedDeposit=5000000, waitForTransBlock=True, exitOnError=True) time.sleep(self.sleep_s) - def add_accounts(self) : - ''' add user account ''' - for acct in self.acct_list: - self.nodeos.createInitializeAccount(acct, creatorAccount) + def test_TraceApi(self) : + node = self.cluster.getNode(0) + for account in self.accounts: + self.assertIsNotNone(node.verifyAccount(account)) - def add_transactions(self) : - ''' add transactions ''' + expectedAmount = Node.currencyIntToStr(5000000, CORE_SYMBOL) + account_balances = [] + for account in self.accounts: + amount = node.getAccountEosBalanceStr(account.name) + self.assertEqual(amount, expectedAmount) + account_balances.append(amount) - def test_TraceApi(self) : - ''' 1. add accounts, - 2. push a few transactions, - 3. verify via get_block - ''' + xferAmount = Node.currencyIntToStr(123456, CORE_SYMBOL) + trans = node.transferFunds(self.accounts[0], self.accounts[1], xferAmount, "test transfer a->b") + transId = Node.getTransId(trans) + blockNum = Node.getTransBlockNum(trans) + + self.assertEqual(node.getAccountEosBalanceStr(self.accounts[0].name), Utils.deduceAmount(expectedAmount, xferAmount)) + self.assertEqual(node.getAccountEosBalanceStr(self.accounts[1].name), Utils.addAmount(expectedAmount, xferAmount)) + time.sleep(self.sleep_s) + base_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, node.port) + cmd_str = base_cmd_str + "trace_api/get_block -X POST -d " + ("'{\"block_num\":%s}'") % blockNum + ret_json = Utils.runCmdReturnJson(cmd_str) + self.assertIn("transactions", ret_json) + + isTrxInBlock = False + for trx in ret_json["transactions"]: + self.assertIn("id", trx) + if (trx["id"] == transId) : + isTrxInBlock = True + self.assertTrue(isTrxInBlock) @classmethod def setUpClass(self): self.cleanEnv(self) self.startEnv(self) - self.test_wallet=self.keosd.create(self.test_wallet_name, self.acct_list) - self.acct_list = Cluster.createAccountKeys(3) - self.acct_list[0].name = "alice" - self.acct_list[1].name = "bob" - self.acct_list[2].name = "charlie" - @classmethod - def tearDownClass(self): - self.cleanEnv(self) + #@classmethod + #def tearDownClass(self): + # self.cleanEnv(self) if __name__ == "__main__": unittest.main() \ No newline at end of file From 7c92513497fd02b3dbcd0253bd05d83412ce0f57 Mon Sep 17 00:00:00 2001 From: Allen Han Date: Wed, 10 Jun 2020 11:20:03 -0500 Subject: [PATCH 3/6] Fix the env clean up issue --- tests/trace_plugin_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py index 4b397e8f3d9..d55523a075e 100755 --- a/tests/trace_plugin_test.py +++ b/tests/trace_plugin_test.py @@ -16,9 +16,10 @@ class TraceApiPluginTest(unittest.TestCase): sleep_s = 1 cluster=Cluster(walletd=True, defproduceraPrvtKey=None) - walletMgr=WalletMgr(True, TestHelper.DEFAULT_PORT, TestHelper.LOCAL_HOST, TestHelper.DEFAULT_WALLET_PORT, TestHelper.LOCAL_HOST) + walletMgr=WalletMgr(True) accounts = [] account_names = ["alice", "bob", "charlie"] + cluster.setWalletMgr(walletMgr) # kill nodeos and keosd and clean up dir def cleanEnv(self) : @@ -29,9 +30,8 @@ def cleanEnv(self) : # start keosd and nodeos def startEnv(self) : - self.walletMgr.launch() -# self.cluster.setWalletMgr(self.walletMgr) self.cluster.launch(totalNodes=1, enableTrace=True) + self.walletMgr.launch() testWalletName="testwallet" testWallet=self.walletMgr.create(testWalletName, [self.cluster.eosioAccount, self.cluster.defproduceraAccount]) self.cluster.validateAccounts(None) @@ -81,9 +81,9 @@ def setUpClass(self): self.cleanEnv(self) self.startEnv(self) - #@classmethod - #def tearDownClass(self): - # self.cleanEnv(self) + @classmethod + def tearDownClass(self): + self.cleanEnv(self) if __name__ == "__main__": unittest.main() \ No newline at end of file From c7940b6a9abac9ad95a2b159afc574795615d12f Mon Sep 17 00:00:00 2001 From: Allen Han Date: Wed, 10 Jun 2020 11:28:15 -0500 Subject: [PATCH 4/6] remove unnecessary import --- tests/trace_plugin_test.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py index d55523a075e..0351dac4435 100755 --- a/tests/trace_plugin_test.py +++ b/tests/trace_plugin_test.py @@ -1,12 +1,8 @@ #!/usr/bin/env python3 -import json -import os -import shutil import time import unittest from testUtils import Utils -from testUtils import Account from Cluster import Cluster from TestHelper import TestHelper from Node import Node From 1c9f37ed330b03b6bd25d31fd4c6bc7d28781284 Mon Sep 17 00:00:00 2001 From: Allen Han Date: Tue, 16 Jun 2020 14:33:47 -0500 Subject: [PATCH 5/6] Fix for comments from Brian --- tests/Cluster.py | 7 +------ tests/trace_plugin_test.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/Cluster.py b/tests/Cluster.py index 70b0f2b2817..760204468f0 100644 --- a/tests/Cluster.py +++ b/tests/Cluster.py @@ -166,7 +166,7 @@ def setAlternateVersionLabels(self, file): # pylint: disable=too-many-statements def launch(self, pnodes=1, unstartedNodes=0, totalNodes=1, prodCount=1, topo="mesh", delay=1, onlyBios=False, dontBootstrap=False, totalProducers=None, sharedProducers=0, extraNodeosArgs=" --http-max-response-time-ms 990000 ", useBiosBootFile=True, specificExtraNodeosArgs=None, onlySetProds=False, - pfSetupPolicy=PFSetupPolicy.FULL, alternateVersionLabelsFile=None, associatedNodeLabels=None, loadSystemContract=True, enableTrace=False): + pfSetupPolicy=PFSetupPolicy.FULL, alternateVersionLabelsFile=None, associatedNodeLabels=None, loadSystemContract=True): """Launch cluster. pnodes: producer nodes count unstartedNodes: non-producer nodes that are configured into the launch, but not started. Should be included in totalNodes. @@ -252,11 +252,6 @@ def launch(self, pnodes=1, unstartedNodes=0, totalNodes=1, prodCount=1, topo="me if PFSetupPolicy.hasPreactivateFeature(pfSetupPolicy): nodeosArgs += " --plugin eosio::producer_api_plugin" - if enableTrace : - nodeosArgs += " --plugin eosio::trace_api_plugin" - nodeosArgs += " --trace-no-abis" - nodeosArgs += " --trace-dir=." - if nodeosArgs: cmdArr.append("--nodeos") cmdArr.append(nodeosArgs) diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py index 0351dac4435..69273f9844d 100755 --- a/tests/trace_plugin_test.py +++ b/tests/trace_plugin_test.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import json import time import unittest @@ -14,7 +15,6 @@ class TraceApiPluginTest(unittest.TestCase): cluster=Cluster(walletd=True, defproduceraPrvtKey=None) walletMgr=WalletMgr(True) accounts = [] - account_names = ["alice", "bob", "charlie"] cluster.setWalletMgr(walletMgr) # kill nodeos and keosd and clean up dir @@ -26,20 +26,28 @@ def cleanEnv(self) : # start keosd and nodeos def startEnv(self) : - self.cluster.launch(totalNodes=1, enableTrace=True) + account_names = ["alice", "bob", "charlie"] + traceNodeosArgs = " --plugin eosio::trace_api_plugin --trace-no-abis --trace-dir=." + self.cluster.launch(totalNodes=1, extraNodeosArgs=traceNodeosArgs) self.walletMgr.launch() testWalletName="testwallet" testWallet=self.walletMgr.create(testWalletName, [self.cluster.eosioAccount, self.cluster.defproduceraAccount]) self.cluster.validateAccounts(None) - self.accounts=Cluster.createAccountKeys(len(self.account_names)) + self.accounts=Cluster.createAccountKeys(len(account_names)) node = self.cluster.getNode(0) - for idx in range(len(self.account_names)): - self.accounts[idx].name = self.account_names[idx] + for idx in range(len(account_names)): + self.accounts[idx].name = account_names[idx] self.walletMgr.importKey(self.accounts[idx], testWallet) for account in self.accounts: node.createInitializeAccount(account, self.cluster.eosioAccount, buyRAM=1000000, stakedDeposit=5000000, waitForTransBlock=True, exitOnError=True) time.sleep(self.sleep_s) + def get_block(self, params: str, node: Node) -> json: + time.sleep(self.sleep_s) + base_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, node.port) + cmd_str = base_cmd_str + "trace_api/get_block -X POST -d " + ("'{\"block_num\":%s}'") % params + return Utils.runCmdReturnJson(cmd_str) + def test_TraceApi(self) : node = self.cluster.getNode(0) for account in self.accounts: @@ -59,10 +67,8 @@ def test_TraceApi(self) : self.assertEqual(node.getAccountEosBalanceStr(self.accounts[0].name), Utils.deduceAmount(expectedAmount, xferAmount)) self.assertEqual(node.getAccountEosBalanceStr(self.accounts[1].name), Utils.addAmount(expectedAmount, xferAmount)) - time.sleep(self.sleep_s) - base_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, node.port) - cmd_str = base_cmd_str + "trace_api/get_block -X POST -d " + ("'{\"block_num\":%s}'") % blockNum - ret_json = Utils.runCmdReturnJson(cmd_str) + ret_json = self.get_block(blockNum, node) + print(ret_json) self.assertIn("transactions", ret_json) isTrxInBlock = False From 8c1d54d6ff777b53bf7c2fd4d46a357bea70d86a Mon Sep 17 00:00:00 2001 From: Allen Han Date: Tue, 16 Jun 2020 16:13:54 -0500 Subject: [PATCH 6/6] Added trans verify via node, and not cleaning when exit --- tests/trace_plugin_test.py | 41 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/tests/trace_plugin_test.py b/tests/trace_plugin_test.py index 69273f9844d..9b0e7fd2318 100755 --- a/tests/trace_plugin_test.py +++ b/tests/trace_plugin_test.py @@ -18,11 +18,13 @@ class TraceApiPluginTest(unittest.TestCase): cluster.setWalletMgr(walletMgr) # kill nodeos and keosd and clean up dir - def cleanEnv(self) : + def cleanEnv(self, shouldCleanup: bool) : self.cluster.killall(allInstances=True) - self.cluster.cleanup() + if shouldCleanup: + self.cluster.cleanup() self.walletMgr.killall(allInstances=True) - self.walletMgr.cleanup() + if shouldCleanup: + self.walletMgr.cleanup() # start keosd and nodeos def startEnv(self) : @@ -43,7 +45,6 @@ def startEnv(self) : time.sleep(self.sleep_s) def get_block(self, params: str, node: Node) -> json: - time.sleep(self.sleep_s) base_cmd_str = ("curl http://%s:%s/v1/") % (TestHelper.LOCAL_HOST, node.port) cmd_str = base_cmd_str + "trace_api/get_block -X POST -d " + ("'{\"block_num\":%s}'") % params return Utils.runCmdReturnJson(cmd_str) @@ -67,25 +68,39 @@ def test_TraceApi(self) : self.assertEqual(node.getAccountEosBalanceStr(self.accounts[0].name), Utils.deduceAmount(expectedAmount, xferAmount)) self.assertEqual(node.getAccountEosBalanceStr(self.accounts[1].name), Utils.addAmount(expectedAmount, xferAmount)) - ret_json = self.get_block(blockNum, node) - print(ret_json) - self.assertIn("transactions", ret_json) + time.sleep(self.sleep_s) + + # verify trans via node api before calling trace_api RPC + blockFromNode = node.getBlock(blockNum) + self.assertIn("transactions", blockFromNode) + isTrxInBlockFromNode = False + for trx in blockFromNode["transactions"]: + self.assertIn("trx", trx) + self.assertIn("id", trx["trx"]) + if (trx["trx"]["id"] == transId) : + isTrxInBlockFromNode = True + break + self.assertTrue(isTrxInBlockFromNode) - isTrxInBlock = False - for trx in ret_json["transactions"]: + # verify trans via trace_api by calling get_block RPC + blockFromTraceApi = self.get_block(blockNum, node) + self.assertIn("transactions", blockFromTraceApi) + isTrxInBlockFromTraceApi = False + for trx in blockFromTraceApi["transactions"]: self.assertIn("id", trx) if (trx["id"] == transId) : - isTrxInBlock = True - self.assertTrue(isTrxInBlock) + isTrxInBlockFromTraceApi = True + break + self.assertTrue(isTrxInBlockFromTraceApi) @classmethod def setUpClass(self): - self.cleanEnv(self) + self.cleanEnv(self, shouldCleanup=True) self.startEnv(self) @classmethod def tearDownClass(self): - self.cleanEnv(self) + self.cleanEnv(self, shouldCleanup=False) # not cleanup to save log in case for further investigation if __name__ == "__main__": unittest.main() \ No newline at end of file