From 722bad021ee6a04b8025c77a142f2287c5a3d1bd Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 25 Jul 2021 22:39:41 +0200 Subject: [PATCH] add cli tests --- apps/cic-eth/cic_eth/cli/rpc.py | 18 ++++-- .../cic_eth/data/config/dispatcher.ini | 2 + apps/cic-eth/cic_eth/data/config/eth.ini | 2 + apps/cic-eth/cic_eth/data/config/retry.ini | 3 + apps/cic-eth/tests/unit/cli/test_cli_args.py | 52 +++++++++++++++ .../cic-eth/tests/unit/cli/test_cli_celery.py | 17 +++++ apps/cic-eth/tests/unit/cli/test_cli_chain.py | 64 +++++++++++++++++++ 7 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 apps/cic-eth/cic_eth/data/config/dispatcher.ini create mode 100644 apps/cic-eth/cic_eth/data/config/eth.ini create mode 100644 apps/cic-eth/cic_eth/data/config/retry.ini create mode 100644 apps/cic-eth/tests/unit/cli/test_cli_args.py create mode 100644 apps/cic-eth/tests/unit/cli/test_cli_celery.py create mode 100644 apps/cic-eth/tests/unit/cli/test_cli_chain.py diff --git a/apps/cic-eth/cic_eth/cli/rpc.py b/apps/cic-eth/cic_eth/cli/rpc.py index cc073767..25e28f9b 100644 --- a/apps/cic-eth/cic_eth/cli/rpc.py +++ b/apps/cic-eth/cic_eth/cli/rpc.py @@ -24,20 +24,24 @@ class RPC: def get_default(self): - return RPCConnection.connect(self.chain_spec, 'default') + return self.get_by_label('default') + + + def get_by_label(self, label): + return RPCConnection.connect(self.chain_spec, label) @staticmethod - def from_config(config, use_signer=False): + def from_config(config, use_signer=False, default_label='default', signer_label='signer'): chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) - RPCConnection.register_location(config.get('RPC_HTTP_PROVIDER'), chain_spec, 'default') + RPCConnection.register_location(config.get('RPC_HTTP_PROVIDER'), chain_spec, default_label) if use_signer: - RPCConnection.register_constructor(ConnType.UNIX, EthUnixSignerConnection, 'signer') - RPCConnection.register_constructor(ConnType.HTTP, EthHTTPSignerConnection, 'signer') - RPCConnection.register_constructor(ConnType.HTTP_SSL, EthHTTPSignerConnection, 'signer') - RPCConnection.register_location(config.get('SIGNER_PROVIDER'), chain_spec, 'signer') + RPCConnection.register_constructor(ConnType.UNIX, EthUnixSignerConnection, signer_label) + RPCConnection.register_constructor(ConnType.HTTP, EthHTTPSignerConnection, signer_label) + RPCConnection.register_constructor(ConnType.HTTP_SSL, EthHTTPSignerConnection, signer_label) + RPCConnection.register_location(config.get('SIGNER_PROVIDER'), chain_spec, signer_label) rpc = RPC(chain_spec, config.get('RPC_HTTP_PROVIDER'), signer_provider=config.get('SIGNER_PROVIDER')) logg.info('set up rpc: {}'.format(rpc)) return rpc diff --git a/apps/cic-eth/cic_eth/data/config/dispatcher.ini b/apps/cic-eth/cic_eth/data/config/dispatcher.ini new file mode 100644 index 00000000..40e1b86e --- /dev/null +++ b/apps/cic-eth/cic_eth/data/config/dispatcher.ini @@ -0,0 +1,2 @@ +[dispatcher] +loop_interval = 1 diff --git a/apps/cic-eth/cic_eth/data/config/eth.ini b/apps/cic-eth/cic_eth/data/config/eth.ini new file mode 100644 index 00000000..fd58e918 --- /dev/null +++ b/apps/cic-eth/cic_eth/data/config/eth.ini @@ -0,0 +1,2 @@ +[eth] +gas_gifter_minimum_balance = 10000000000000000000000 diff --git a/apps/cic-eth/cic_eth/data/config/retry.ini b/apps/cic-eth/cic_eth/data/config/retry.ini new file mode 100644 index 00000000..4bec6b6b --- /dev/null +++ b/apps/cic-eth/cic_eth/data/config/retry.ini @@ -0,0 +1,3 @@ +[retry] +delay = +batch_size = diff --git a/apps/cic-eth/tests/unit/cli/test_cli_args.py b/apps/cic-eth/tests/unit/cli/test_cli_args.py new file mode 100644 index 00000000..d376aef2 --- /dev/null +++ b/apps/cic-eth/tests/unit/cli/test_cli_args.py @@ -0,0 +1,52 @@ +# standard imports +import os + +# external imports +import chainlib.cli + +# local imports +import cic_eth.cli + +script_dir = os.path.dirname(os.path.realpath(__file__)) +config_dir = os.path.join(script_dir, '..', '..', 'testdata', 'config') + + +def test_argumentparserto_config(): + + argparser = cic_eth.cli.ArgumentParser() + + local_flags = 0xffff + argparser.process_local_flags(local_flags) + argparser.add_argument('--foo', type=str) + args = argparser.parse_args([ + '--redis-host', 'foo', + '--redis-port', '123', + '--redis-db', '0', + '--redis-host-callback', 'bar', + '--redis-port-callback', '456', + '--redis-timeout', '10.0', + '-q', 'baz', + '--offset', '13', + '--no-history', + '-r','0xdeadbeef', + '-vv', + '--foo', 'bar', + ]) + + extra_args = { + 'foo': '_BARBARBAR', + } + config = cic_eth.cli.Config.from_args(args, chainlib.cli.argflag_std_base, local_flags, extra_args=extra_args, base_config_dir=config_dir) + + assert config.get('_BARBARBAR') == 'bar' + assert config.get('REDIS_HOST') == 'foo' + assert config.get('REDIS_PORT') == 123 + assert config.get('REDIS_DB') == 0 + assert config.get('_REDIS_HOST_CALLBACK') == 'bar' + assert config.get('_REDIS_PORT_CALLBACK') == 456 + assert config.get('REDIS_TIMEOUT') == 10.0 + assert config.get('CELERY_QUEUE') == 'baz' + assert config.get('SYNCER_NO_HISTORY') == True + assert config.get('SYNCER_OFFSET') == 13 + assert config.get('CIC_REGISTRY_ADDRESS') == '0xdeadbeef' + diff --git a/apps/cic-eth/tests/unit/cli/test_cli_celery.py b/apps/cic-eth/tests/unit/cli/test_cli_celery.py new file mode 100644 index 00000000..1727008b --- /dev/null +++ b/apps/cic-eth/tests/unit/cli/test_cli_celery.py @@ -0,0 +1,17 @@ +# standard imports +import tempfile + +# local imports +import cic_eth.cli + + +def test_cli_celery(): + cf = tempfile.mkdtemp() + + config = { + 'CELERY_RESULT_URL': 'filesystem://' + cf, + } + cic_eth.cli.CeleryApp.from_config(config) + + config['CELERY_BROKER_URL'] = 'filesystem://' + cf + cic_eth.cli.CeleryApp.from_config(config) diff --git a/apps/cic-eth/tests/unit/cli/test_cli_chain.py b/apps/cic-eth/tests/unit/cli/test_cli_chain.py new file mode 100644 index 00000000..8ebebc15 --- /dev/null +++ b/apps/cic-eth/tests/unit/cli/test_cli_chain.py @@ -0,0 +1,64 @@ +# external imports +import pytest +from chainlib.eth.gas import ( + Gas, + RPCGasOracle, + ) +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.block import ( + block_latest, + Block, + ) + +# local imports +import cic_eth.cli + + +@pytest.mark.xfail() +def test_cli_rpc( + eth_rpc, + eth_signer, + default_chain_spec, + ): + config = { + 'CHAIN_SPEC': str(default_chain_spec), + 'RPC_HTTP_PROVIDER': 'http://localhost:8545', + } + rpc = cic_eth.cli.RPC.from_config(config, default_label='foo') + conn = rpc.get_by_label('foo') + #o = block_latest() + #conn.do(o) + + +def test_cli_chain( + default_chain_spec, + eth_rpc, + eth_signer, + contract_roles, + agent_roles, + ): + ifc = cic_eth.cli.EthChainInterface() + + nonce_oracle = RPCNonceOracle(contract_roles['CONTRACT_DEPLOYER'], conn=eth_rpc) + gas_oracle = RPCGasOracle(conn=eth_rpc) + c = Gas(default_chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=eth_signer) + (tx_hash, o) = c.create(contract_roles['CONTRACT_DEPLOYER'], agent_roles['ALICE'], 1024) + r = eth_rpc.do(o) + + o = ifc.tx_receipt(r) + r = eth_rpc.do(o) + assert r['status'] == 1 + + o = ifc.block_by_number(1) + block_src = eth_rpc.do(o) + block = ifc.block_from_src(block_src) + assert block.number == 1 + + with pytest.raises(KeyError): + assert block_src['gasUsed'] == 21000 + assert block_src['gas_used'] == 21000 + + block_src = ifc.src_normalize(block_src) + assert block_src['gasUsed'] == 21000 + assert block_src['gas_used'] == 21000 +