diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 8832da9..515efef 100755 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -1175,13 +1175,13 @@ def printHelp(): def finalise_daemon(d): - logging.info('Interrupting {}'.format(d.pid)) + logging.info('Interrupting {}'.format(d.handle.pid)) try: - d.send_signal(signal.CTRL_C_EVENT if os.name == 'nt' else signal.SIGINT) - d.wait(timeout=120) + d.handle.send_signal(signal.CTRL_C_EVENT if os.name == 'nt' else signal.SIGINT) + d.handle.wait(timeout=120) except Exception as e: logging.info(f'Error {e} for process {d.pid}') - for fp in (d.stdout, d.stderr, d.stdin): + for fp in [d.handle.stdout, d.handle.stderr, d.handle.stdin] + d.files: if fp: fp.close() @@ -1202,7 +1202,7 @@ def test_particl_encryption(data_dir, settings, chain, use_tor_proxy): if coin_settings['manage_daemon']: filename = coin_name + 'd' + ('.exe' if os.name == 'nt' else '') daemons.append(startDaemon(coin_settings['datadir'], coin_settings['bindir'], filename, daemon_args)) - swap_client.setDaemonPID(c, daemons[-1].pid) + swap_client.setDaemonPID(c, daemons[-1].handle.pid) swap_client.setCoinRunParams(c) swap_client.createCoinInterface(c) swap_client.waitForDaemonRPC(c, with_wallet=True) diff --git a/bin/basicswap_run.py b/bin/basicswap_run.py index 22ce1ca..c7be28e 100755 --- a/bin/basicswap_run.py +++ b/bin/basicswap_run.py @@ -18,6 +18,7 @@ import basicswap.config as cfg from basicswap import __version__ from basicswap.ui.util import getCoinName from basicswap.basicswap import BasicSwap +from basicswap.chainparams import chainparams from basicswap.http_server import HttpThread from basicswap.contrib.websocket_server import WebsocketServer @@ -28,18 +29,21 @@ if not len(logger.handlers): logger.addHandler(logging.StreamHandler(sys.stdout)) swap_client = None -# TODO: deduplicate -known_coins = [ - 'particl', - 'litecoin', - 'bitcoin', - 'namecoin', - 'monero', - 'pivx', - 'dash', - 'firo', - 'navcoin', -] + + +class Daemon: + __slots__ = ('handle', 'files') + + def __init__(self, handle, files): + self.handle = handle + self.files = files + + +def is_known_coin(coin_name: str) -> bool: + for k, v in chainparams: + if coin_name == v['name']: + return True + return False def signal_handler(sig, frame): @@ -49,7 +53,7 @@ def signal_handler(sig, frame): swap_client.stopRunning() -def startDaemon(node_dir, bin_dir, daemon_bin, opts=[]): +def startDaemon(node_dir, bin_dir, daemon_bin, opts=[], extra_config={}): daemon_bin = os.path.expanduser(os.path.join(bin_dir, daemon_bin)) datadir_path = os.path.expanduser(node_dir) @@ -71,9 +75,22 @@ def startDaemon(node_dir, bin_dir, daemon_bin, opts=[]): for line in config_to_add: fp.write(line + '\n') - args = [daemon_bin, '-datadir=' + datadir_path] + opts - logging.info('Starting node ' + daemon_bin + ' ' + '-datadir=' + node_dir) - return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=datadir_path) + args = [daemon_bin, ] + add_datadir: bool = extra_config.get('add_datadir', True) + if add_datadir: + args.append('-datadir=' + datadir_path) + args += opts + logging.info('Starting node ' + daemon_bin + ' ' + (('-datadir=' + node_dir) if add_datadir else '')) + + opened_files = [] + if extra_config.get('stdout_to_file', False): + stdout_dest = open(os.path.join(datadir_path, extra_config.get('stdout_filename', 'core_stdout.log')), 'w') + opened_files.append(stdout_dest) + else: + stdout_dest = subprocess.PIPE + + return Daemon(subprocess.Popen(args, stdin=subprocess.PIPE, stdout=stdout_dest, stderr=subprocess.PIPE, cwd=datadir_path), opened_files) +>>>>>>> 676701b (tests: Start dcrd) def startXmrDaemon(node_dir, bin_dir, daemon_bin, opts=[]): @@ -86,24 +103,27 @@ def startXmrDaemon(node_dir, bin_dir, daemon_bin, opts=[]): # return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) file_stdout = open(os.path.join(datadir_path, 'core_stdout.log'), 'w') file_stderr = open(os.path.join(datadir_path, 'core_stderr.log'), 'w') - return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=file_stdout, stderr=file_stderr, cwd=datadir_path) + return Daemon(subprocess.Popen(args, stdin=subprocess.PIPE, stdout=file_stdout, stderr=file_stderr, cwd=datadir_path), [file_stdout, file_stderr]) def startXmrWalletDaemon(node_dir, bin_dir, wallet_bin, opts=[]): daemon_bin = os.path.expanduser(os.path.join(bin_dir, wallet_bin)) + args = [daemon_bin, '--non-interactive'] + + needs_rewrite: bool = False + config_to_remove = ['daemon-address=', 'untrusted-daemon=', 'trusted-daemon=', 'proxy='] data_dir = os.path.expanduser(node_dir) config_path = os.path.join(data_dir, 'monero_wallet.conf') - args = [daemon_bin, '--non-interactive', '--config-file=' + config_path] + opts + if os.path.exists(config_path): + args += ['--config-file=' + config_path] + with open(config_path) as fp: + for line in fp: + if any(line.startswith(config_line) for config_line in config_to_remove): + logging.warning('Found old config in monero_wallet.conf: {}'.format(line.strip())) + needs_rewrite = True + args += opts - # Remove old config - needs_rewrite: bool = False - config_to_remove = ['daemon-address=', 'untrusted-daemon=', 'trusted-daemon=', 'proxy='] - with open(config_path) as fp: - for line in fp: - if any(line.startswith(config_line) for config_line in config_to_remove): - logging.warning('Found old config in monero_wallet.conf: {}'.format(line.strip())) - needs_rewrite = True if needs_rewrite: logging.info('Rewriting monero_wallet.conf') shutil.copyfile(config_path, config_path + '.last') @@ -117,7 +137,7 @@ def startXmrWalletDaemon(node_dir, bin_dir, wallet_bin, opts=[]): # TODO: return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=data_dir) wallet_stdout = open(os.path.join(data_dir, 'wallet_stdout.log'), 'w') wallet_stderr = open(os.path.join(data_dir, 'wallet_stderr.log'), 'w') - return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=wallet_stdout, stderr=wallet_stderr, cwd=data_dir) + return Daemon(subprocess.Popen(args, stdin=subprocess.PIPE, stdout=wallet_stdout, stderr=wallet_stderr, cwd=data_dir), [wallet_stdout, wallet_stderr]) def ws_new_client(client, server): @@ -186,7 +206,7 @@ def runClient(fp, data_dir, chain, start_only_coins): swap_client.log.info(f'Starting {display_name} daemon') filename = 'monerod' + ('.exe' if os.name == 'nt' else '') daemons.append(startXmrDaemon(v['datadir'], v['bindir'], filename)) - pid = daemons[-1].pid + pid = daemons[-1].handle.pid swap_client.log.info('Started {} {}'.format(filename, pid)) if v['manage_wallet_daemon'] is True: @@ -212,7 +232,7 @@ def runClient(fp, data_dir, chain, start_only_coins): opts.append('--trusted-daemon' if trusted_daemon else '--untrusted-daemon') filename = 'monero-wallet-rpc' + ('.exe' if os.name == 'nt' else '') daemons.append(startXmrWalletDaemon(v['datadir'], v['bindir'], filename, opts)) - pid = daemons[-1].pid + pid = daemons[-1].handle.pid swap_client.log.info('Started {} {}'.format(filename, pid)) continue @@ -221,7 +241,7 @@ def runClient(fp, data_dir, chain, start_only_coins): filename = c + 'd' + ('.exe' if os.name == 'nt' else '') daemons.append(startDaemon(v['datadir'], v['bindir'], filename)) - pid = daemons[-1].pid + pid = daemons[-1].handle.pid pids.append((c, pid)) swap_client.setDaemonPID(c, pid) swap_client.log.info('Started {} {}'.format(filename, pid)) @@ -281,18 +301,18 @@ def runClient(fp, data_dir, chain, start_only_coins): closed_pids = [] for d in daemons: - swap_client.log.info('Interrupting {}'.format(d.pid)) + swap_client.log.info('Interrupting {}'.format(d.handle.pid)) try: - d.send_signal(signal.CTRL_C_EVENT if os.name == 'nt' else signal.SIGINT) + d.handle.send_signal(signal.CTRL_C_EVENT if os.name == 'nt' else signal.SIGINT) except Exception as e: - swap_client.log.info('Interrupting %d, error %s', d.pid, str(e)) + swap_client.log.info('Interrupting %d, error %s', d.handle.pid, str(e)) for d in daemons: try: - d.wait(timeout=120) - for fp in (d.stdout, d.stderr, d.stdin): + d.handle.wait(timeout=120) + for fp in [d.handle.stdout, d.handle.stderr, d.handle.stdin] + d.files: if fp: fp.close() - closed_pids.append(d.pid) + closed_pids.append(d.handle.pid) except Exception as ex: swap_client.log.error('Error: {}'.format(ex)) @@ -359,7 +379,7 @@ def main(): continue if name == 'startonlycoin': for coin in [s.lower() for s in s[1].split(',')]: - if coin not in known_coins: + if is_known_coin(coin) is False: raise ValueError(f'Unknown coin: {coin}') start_only_coins.add(coin) continue diff --git a/tests/basicswap/common.py b/tests/basicswap/common.py index 8da408b..4c67bb4 100644 --- a/tests/basicswap/common.py +++ b/tests/basicswap/common.py @@ -108,19 +108,19 @@ def checkForks(ro): def stopDaemons(daemons): for d in daemons: - logging.info('Interrupting %d', d.pid) + logging.info('Interrupting %d', d.handle.pid) try: - d.send_signal(signal.SIGINT) + d.handle.send_signal(signal.SIGINT) except Exception as e: - logging.info('Interrupting %d, error %s', d.pid, str(e)) + logging.info('Interrupting %d, error %s', d.handle.pid, str(e)) for d in daemons: try: - d.wait(timeout=20) - for fp in (d.stdout, d.stderr, d.stdin): + d.handle.wait(timeout=20) + for fp in [d.handle.stdout, d.handle.stderr, d.handle.stdin] + d.files: if fp: fp.close() except Exception as e: - logging.info('Closing %d, error %s', d.pid, str(e)) + logging.info('Closing %d, error %s', d.handle.pid, str(e)) def wait_for_bid(delay_event, swap_client, bid_id, state=None, sent: bool = False, wait_for: int = 20) -> None: diff --git a/tests/basicswap/extended/test_dash.py b/tests/basicswap/extended/test_dash.py index 2931f1f..97a7dc5 100644 --- a/tests/basicswap/extended/test_dash.py +++ b/tests/basicswap/extended/test_dash.py @@ -298,7 +298,7 @@ class Test(unittest.TestCase): except Exception: callrpc_cli(cfg.BITCOIN_BINDIR, btc_data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet') cls.daemons.append(startDaemon(btc_data_dir, cfg.BITCOIN_BINDIR, cfg.BITCOIND)) - logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].handle.pid) dash_data_dir = os.path.join(cfg.TEST_DATADIRS, str(DASH_NODE)) ''' @@ -309,7 +309,7 @@ class Test(unittest.TestCase): callrpc_cli(DASH_BINDIR, dash_data_dir, 'regtest', '-wallet=wallet.dat create', 'dash-wallet') ''' cls.daemons.append(startDaemon(dash_data_dir, DASH_BINDIR, DASHD)) - logging.info('Started %s %d', DASHD, cls.daemons[-1].pid) + logging.info('Started %s %d', DASHD, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): data_dir = os.path.join(cfg.TEST_DATADIRS, str(i)) @@ -319,7 +319,7 @@ class Test(unittest.TestCase): except Exception: callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'particl-wallet') cls.daemons.append(startDaemon(data_dir, cfg.PARTICL_BINDIR, cfg.PARTICLD)) - logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): rpc = make_part_cli_rpc_func(i) @@ -342,9 +342,9 @@ class Test(unittest.TestCase): fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') cls.swap_clients.append(BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i))) swap_client = cls.swap_clients[-1] - swap_client.setDaemonPID(Coins.BTC, cls.daemons[0].pid) - swap_client.setDaemonPID(Coins.DASH, cls.daemons[1].pid) - swap_client.setDaemonPID(Coins.PART, cls.daemons[2 + i].pid) + swap_client.setDaemonPID(Coins.BTC, cls.daemons[0].handle.pid) + swap_client.setDaemonPID(Coins.DASH, cls.daemons[1].handle.pid) + swap_client.setDaemonPID(Coins.PART, cls.daemons[2 + i].handle.pid) waitForRPC(dashRpc, expect_wallet=False) if len(dashRpc('listwallets')) < 1: @@ -417,6 +417,10 @@ class Test(unittest.TestCase): stopDaemons(cls.daemons) + cls.http_threads.clear() + cls.swap_clients.clear() + cls.daemons.clear() + super(Test, cls).tearDownClass() def test_02_part_dash(self): diff --git a/tests/basicswap/extended/test_dcr.py b/tests/basicswap/extended/test_dcr.py index 4be916c..b8cdefe 100644 --- a/tests/basicswap/extended/test_dcr.py +++ b/tests/basicswap/extended/test_dcr.py @@ -6,27 +6,69 @@ # file LICENSE or http://www.opensource.org/licenses/mit-license.php. import logging +import os import unittest +import basicswap.config as cfg + from basicswap.basicswap import ( Coins, ) -from tests.basicswap.util import ( - REQUIRED_SETTINGS, +from basicswap.rpc import ( + waitForRPC, ) from tests.basicswap.common import ( stopDaemons, + make_rpc_func, ) +from tests.basicswap.util import ( + REQUIRED_SETTINGS, +) + from tests.basicswap.test_xmr import BaseTest from basicswap.interface.dcr import DCRInterface +from bin.basicswap_run import startDaemon logger = logging.getLogger() +DCR_BINDIR = os.path.expanduser(os.getenv('DCR_BINDIR', os.path.join(cfg.DEFAULT_TEST_BINDIR, 'decred'))) +DCRD = os.getenv('DCRD', 'dcrd' + cfg.bin_suffix) +DCR_WALLET = os.getenv('DCR_WALLET', 'dcrwallet' + cfg.bin_suffix) +DCR_CLI = os.getenv('DCR_CLI', 'dcrctl' + cfg.bin_suffix) + +DCR_BASE_PORT = 44932 +DCR_BASE_RPC_PORT = 45932 + + +def prepareDCDDataDir(datadir, node_id, conf_file, dir_prefix, base_p2p_port, base_rpc_port, num_nodes=3): + node_dir = os.path.join(datadir, dir_prefix + str(node_id)) + if not os.path.exists(node_dir): + os.makedirs(node_dir) + cfg_file_path = os.path.join(node_dir, conf_file) + if os.path.exists(cfg_file_path): + return + with open(cfg_file_path, 'w+') as fp: + config = [ + 'regnet=1\n', # or simnet? + 'debuglevel=debug\n', + f'listen=127.0.0.1:{base_p2p_port}\n', + f'rpclisten=127.0.0.1:{base_rpc_port}\n', + f'rpcuser=test{node_id}\n', + f'rpcpass=test_pass{node_id}\n',] + + for i in range(0, num_nodes): + if node_id == i: + continue + config.append('addpeer=127.0.0.1:{}\n'.format(base_p2p_port + i)) + + for line in config: + fp.write(line) + class Test(BaseTest): __test__ = True test_coin_from = Coins.DCR - decred_daemons = [] + dcr_daemons = [] start_ltc_nodes = False start_xmr_nodes = False @@ -39,12 +81,42 @@ class Test(BaseTest): logging.info('Finalising Decred Test') super(Test, cls).tearDownClass() - stopDaemons(cls.decred_daemons) + stopDaemons(cls.dcr_daemons) + cls.dcr_daemons.clear() @classmethod def coins_loop(cls): super(Test, cls).coins_loop() + @classmethod + def prepareExtraDataDir(cls, i): + extra_opts = [] + if not cls.restore_instance: + data_dir = prepareDCDDataDir(cfg.TEST_DATADIRS, i, 'dcrd.conf', 'dcr_', base_p2p_port=DCR_BASE_PORT, base_rpc_port=DCR_BASE_RPC_PORT) + + appdata = os.path.join(cfg.TEST_DATADIRS, 'dcr_' + str(i)) + datadir = os.path.join(appdata, 'data') + extra_opts.append(f'--appdata="{appdata}"') + cls.dcr_daemons.append(startDaemon(appdata, DCR_BINDIR, DCRD, opts=extra_opts, extra_config={'add_datadir': False, 'stdout_to_file': True, 'stdout_filename': 'dcrd_stdout.log'})) + logging.info('Started %s %d', DCRD, cls.dcr_daemons[-1].handle.pid) + + waitForRPC(make_rpc_func(i, base_rpc_port=DCR_BASE_RPC_PORT), max_tries=12) + + @classmethod + def addCoinSettings(cls, settings, datadir, node_id): + settings['chainclients']['decred'] = { + 'connection_type': 'rpc', + 'manage_daemon': False, + 'rpcport': DCR_BASE_RPC_PORT + node_id, + 'rpcuser': 'test' + str(node_id), + 'rpcpassword': 'test_pass' + str(node_id), + 'datadir': os.path.join(datadir, 'dcr_' + str(node_id)), + 'bindir': DCR_BINDIR, + 'use_csv': True, + 'use_segwit': True, + 'blocks_confirmed': 1, + } + def test_001_decred(self): logging.info('---------- Test {}'.format(self.test_coin_from.name)) diff --git a/tests/basicswap/extended/test_firo.py b/tests/basicswap/extended/test_firo.py index 1439133..d13bab0 100644 --- a/tests/basicswap/extended/test_firo.py +++ b/tests/basicswap/extended/test_firo.py @@ -137,13 +137,13 @@ class Test(BaseTest): callrpc_cli(FIRO_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'firo-wallet') cls.firo_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'firo_' + str(i)), FIRO_BINDIR, FIROD, opts=extra_opts)) - logging.info('Started %s %d', FIROD, cls.firo_daemons[-1].pid) + logging.info('Started %s %d', FIROD, cls.firo_daemons[-1].handle.pid) waitForRPC(make_rpc_func(i, base_rpc_port=FIRO_BASE_RPC_PORT)) @classmethod def addPIDInfo(cls, sc, i): - sc.setDaemonPID(Coins.FIRO, cls.firo_daemons[i].pid) + sc.setDaemonPID(Coins.FIRO, cls.firo_daemons[i].handle.pid) @classmethod def prepareExtraCoins(cls): @@ -180,6 +180,7 @@ class Test(BaseTest): super(Test, cls).tearDownClass() stopDaemons(cls.firo_daemons) + cls.firo_daemons.clear() @classmethod def addCoinSettings(cls, settings, datadir, node_id): diff --git a/tests/basicswap/extended/test_nav.py b/tests/basicswap/extended/test_nav.py index 1da6a2b..196771f 100644 --- a/tests/basicswap/extended/test_nav.py +++ b/tests/basicswap/extended/test_nav.py @@ -159,13 +159,13 @@ class Test(TestFunctions): data_dir = prepareDataDir(cfg.TEST_DATADIRS, i, 'navcoin.conf', 'nav_', base_p2p_port=NAV_BASE_PORT, base_rpc_port=NAV_BASE_RPC_PORT) cls.nav_daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, 'nav_' + str(i)), NAV_BINDIR, NAVD, opts=extra_opts)) - logging.info('Started %s %d', NAVD, cls.nav_daemons[-1].pid) + logging.info('Started %s %d', NAVD, cls.nav_daemons[-1].handle.pid) waitForRPC(make_rpc_func(i, base_rpc_port=NAV_BASE_RPC_PORT), max_tries=12) @classmethod def addPIDInfo(cls, sc, i): - sc.setDaemonPID(Coins.NAV, cls.nav_daemons[i].pid) + sc.setDaemonPID(Coins.NAV, cls.nav_daemons[i].handle.pid) @classmethod def sync_blocks(cls, wait_for: int = 20, num_nodes: int = 3) -> None: @@ -217,6 +217,7 @@ class Test(TestFunctions): super(Test, cls).tearDownClass() stopDaemons(cls.nav_daemons) + cls.nav_daemons.clear() @classmethod def addCoinSettings(cls, settings, datadir, node_id): diff --git a/tests/basicswap/extended/test_network.py b/tests/basicswap/extended/test_network.py index 05eaebf..71b014e 100644 --- a/tests/basicswap/extended/test_network.py +++ b/tests/basicswap/extended/test_network.py @@ -206,7 +206,7 @@ class Test(unittest.TestCase): callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat -legacy create', 'particl-wallet') cls.part_daemons.append(startDaemon(os.path.join(TEST_DIR, 'part_' + str(i)), cfg.PARTICL_BINDIR, cfg.PARTICLD)) - logging.info('Started %s %d', cfg.PARTICLD, cls.part_daemons[-1].pid) + logging.info('Started %s %d', cfg.PARTICLD, cls.handle.part_daemons[-1].handle.pid) for i in range(NUM_NODES): # Load mnemonics after all nodes have started to avoid staking getting stuck in TryToSync @@ -230,7 +230,7 @@ class Test(unittest.TestCase): callrpc_cli(cfg.BITCOIN_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat -legacy create', 'bitcoin-wallet') cls.btc_daemons.append(startDaemon(os.path.join(TEST_DIR, 'btc_' + str(i)), cfg.BITCOIN_BINDIR, cfg.BITCOIND)) - logging.info('Started %s %d', cfg.BITCOIND, cls.part_daemons[-1].pid) + logging.info('Started %s %d', cfg.BITCOIND, cls.handle.part_daemons[-1].handle.pid) waitForRPC(make_rpc_func(i, base_rpc_port=BTC_BASE_RPC_PORT)) @@ -248,8 +248,8 @@ class Test(unittest.TestCase): settings = json.load(fs) fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') sc = BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i)) - sc.setDaemonPID(Coins.BTC, cls.btc_daemons[i].pid) - sc.setDaemonPID(Coins.PART, cls.part_daemons[i].pid) + sc.setDaemonPID(Coins.BTC, cls.btc_daemons[i].handle.pid) + sc.setDaemonPID(Coins.PART, cls.part_daemons[i].handle.pid) sc.start() cls.swap_clients.append(sc) @@ -274,7 +274,7 @@ class Test(unittest.TestCase): cls.coins_update_thread.start() except Exception: traceback.print_exc() - Test.tearDownClass() + cls.tearDownClass() raise ValueError('setUpClass() failed.') @classmethod @@ -303,6 +303,11 @@ class Test(unittest.TestCase): stopDaemons(cls.part_daemons) stopDaemons(cls.btc_daemons) + cls.part_daemons.clear() + cls.btc_daemons.clear() + cls.http_threads.clear() + cls.swap_clients.clear() + super(Test, cls).tearDownClass() def wait_for_num_nodes(self, port, expect_nodes, wait_for=20): diff --git a/tests/basicswap/extended/test_nmc.py b/tests/basicswap/extended/test_nmc.py index 2076ad1..70d146b 100644 --- a/tests/basicswap/extended/test_nmc.py +++ b/tests/basicswap/extended/test_nmc.py @@ -276,16 +276,16 @@ class Test(unittest.TestCase): if os.path.exists(os.path.join(cfg.BITCOIN_BINDIR, 'bitcoin-wallet')): callrpc_cli(cfg.BITCOIN_BINDIR, btc_data_dir, 'regtest', '-wallet=wallet.dat -legacy create', 'bitcoin-wallet') cls.daemons.append(startDaemon(btc_data_dir, cfg.BITCOIN_BINDIR, cfg.BITCOIND)) - logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].handle.pid) cls.daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, str(NMC_NODE)), cfg.NAMECOIN_BINDIR, cfg.NAMECOIND)) - logging.info('Started %s %d', cfg.NAMECOIND, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.NAMECOIND, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): data_dir = os.path.join(cfg.TEST_DATADIRS, str(i)) if os.path.exists(os.path.join(cfg.PARTICL_BINDIR, 'particl-wallet')): callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat -legacy create', 'particl-wallet') cls.daemons.append(startDaemon(data_dir, cfg.PARTICL_BINDIR, cfg.PARTICLD)) - logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): rpc = make_part_cli_rpc_func(i) @@ -307,9 +307,9 @@ class Test(unittest.TestCase): settings = json.load(fs) fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') cls.swap_clients.append(BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i))) - cls.swap_clients[-1].setDaemonPID(Coins.BTC, cls.daemons[0].pid) - cls.swap_clients[-1].setDaemonPID(Coins.NMC, cls.daemons[1].pid) - cls.swap_clients[-1].setDaemonPID(Coins.PART, cls.daemons[2 + i].pid) + cls.swap_clients[-1].setDaemonPID(Coins.BTC, cls.daemons[0].handle.pid) + cls.swap_clients[-1].setDaemonPID(Coins.NMC, cls.daemons[1].handle.pid) + cls.swap_clients[-1].setDaemonPID(Coins.PART, cls.daemons[2 + i].handle.pid) cls.swap_clients[-1].start() t = HttpThread(cls.swap_clients[i].fp, TEST_HTTP_HOST, TEST_HTTP_PORT + i, False, cls.swap_clients[i]) @@ -376,6 +376,9 @@ class Test(unittest.TestCase): c.fp.close() stopDaemons(cls.daemons) + cls.http_threads.clear() + cls.swap_clients.clear() + cls.daemons.clear() super(Test, cls).tearDownClass() diff --git a/tests/basicswap/extended/test_pivx.py b/tests/basicswap/extended/test_pivx.py index 98eaf73..31706fe 100644 --- a/tests/basicswap/extended/test_pivx.py +++ b/tests/basicswap/extended/test_pivx.py @@ -310,9 +310,9 @@ class Test(unittest.TestCase): except Exception: callrpc_cli(cfg.BITCOIN_BINDIR, btc_data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet') cls.daemons.append(startDaemon(btc_data_dir, cfg.BITCOIN_BINDIR, cfg.BITCOIND)) - logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.BITCOIND, cls.daemons[-1].handle.pid) cls.daemons.append(startDaemon(os.path.join(cfg.TEST_DATADIRS, str(PIVX_NODE)), PIVX_BINDIR, PIVXD)) - logging.info('Started %s %d', PIVXD, cls.daemons[-1].pid) + logging.info('Started %s %d', PIVXD, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): data_dir = os.path.join(cfg.TEST_DATADIRS, str(i)) @@ -322,7 +322,7 @@ class Test(unittest.TestCase): except Exception: callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'particl-wallet') cls.daemons.append(startDaemon(data_dir, cfg.PARTICL_BINDIR, cfg.PARTICLD)) - logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].pid) + logging.info('Started %s %d', cfg.PARTICLD, cls.daemons[-1].handle.pid) for i in range(NUM_NODES): rpc = make_part_cli_rpc_func(i) @@ -344,9 +344,9 @@ class Test(unittest.TestCase): settings = json.load(fs) fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') cls.swap_clients.append(BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i))) - cls.swap_clients[-1].setDaemonPID(Coins.BTC, cls.daemons[0].pid) - cls.swap_clients[-1].setDaemonPID(Coins.PIVX, cls.daemons[1].pid) - cls.swap_clients[-1].setDaemonPID(Coins.PART, cls.daemons[2 + i].pid) + cls.swap_clients[-1].setDaemonPID(Coins.BTC, cls.daemons[0].handle.pid) + cls.swap_clients[-1].setDaemonPID(Coins.PIVX, cls.daemons[1].handle.pid) + cls.swap_clients[-1].setDaemonPID(Coins.PART, cls.daemons[2 + i].handle.pid) cls.swap_clients[-1].start() t = HttpThread(cls.swap_clients[i].fp, TEST_HTTP_HOST, TEST_HTTP_PORT + i, False, cls.swap_clients[i]) @@ -410,6 +410,9 @@ class Test(unittest.TestCase): c.fp.close() stopDaemons(cls.daemons) + cls.http_threads.clear() + cls.swap_clients.clear() + cls.daemons.clear() super(Test, cls).tearDownClass() diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index f94c01d..9ad41d7 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -15,7 +15,6 @@ import logging import unittest import traceback import threading -import subprocess import basicswap.config as cfg from basicswap.db import ( @@ -92,7 +91,7 @@ from tests.basicswap.common import ( from basicswap.db_util import ( remove_expired_data, ) -from bin.basicswap_run import startDaemon, startXmrDaemon +from bin.basicswap_run import startDaemon, startXmrDaemon, startXmrWalletDaemon logger = logging.getLogger() @@ -140,29 +139,6 @@ def prepareXmrDataDir(datadir, node_id, conf_file): fp.write('add-exclusive-node=127.0.0.1:{}\n'.format(XMR_BASE_P2P_PORT + i)) -def startXmrWalletRPC(node_dir, bin_dir, wallet_bin, node_id, opts=[]): - daemon_bin = os.path.expanduser(os.path.join(bin_dir, wallet_bin)) - - data_dir = os.path.expanduser(node_dir) - args = [daemon_bin] - args += ['--non-interactive'] - args += ['--daemon-address=127.0.0.1:{}'.format(XMR_BASE_RPC_PORT + node_id)] - args += ['--no-dns'] - args += ['--rpc-bind-port={}'.format(XMR_BASE_WALLET_RPC_PORT + node_id)] - args += ['--wallet-dir={}'.format(os.path.join(data_dir, 'wallets'))] - args += ['--log-file={}'.format(os.path.join(data_dir, 'wallet.log'))] - args += ['--rpc-login=test{0}:test_pass{0}'.format(node_id)] - args += ['--shared-ringdb-dir={}'.format(os.path.join(data_dir, 'shared-ringdb'))] - args += ['--allow-mismatched-daemon-version'] - - args += opts - logging.info('Starting daemon {} --wallet-dir={}'.format(daemon_bin, node_dir)) - - wallet_stdout = open(os.path.join(data_dir, 'wallet_stdout.log'), 'w') - wallet_stderr = open(os.path.join(data_dir, 'wallet_stderr.log'), 'w') - return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=wallet_stdout, stderr=wallet_stderr, cwd=data_dir) - - def prepare_swapclient_dir(datadir, node_id, network_key, network_pubkey, with_coins=set(), cls=None): basicswap_dir = os.path.join(datadir, 'basicswap_' + str(node_id)) if not os.path.exists(basicswap_dir): @@ -392,7 +368,7 @@ class BaseTest(unittest.TestCase): callrpc_cli(cfg.PARTICL_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'particl-wallet') cls.part_daemons.append(startDaemon(os.path.join(TEST_DIR, 'part_' + str(i)), cfg.PARTICL_BINDIR, cfg.PARTICLD)) - logging.info('Started %s %d', cfg.PARTICLD, cls.part_daemons[-1].pid) + logging.info('Started %s %d', cfg.PARTICLD, cls.part_daemons[-1].handle.pid) if not cls.restore_instance: for i in range(NUM_NODES): @@ -422,7 +398,7 @@ class BaseTest(unittest.TestCase): callrpc_cli(cfg.BITCOIN_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'bitcoin-wallet') cls.btc_daemons.append(startDaemon(os.path.join(TEST_DIR, 'btc_' + str(i)), cfg.BITCOIN_BINDIR, cfg.BITCOIND)) - logging.info('Started %s %d', cfg.BITCOIND, cls.part_daemons[-1].pid) + logging.info('Started %s %d', cfg.BITCOIND, cls.part_daemons[-1].handle.pid) waitForRPC(make_rpc_func(i, base_rpc_port=BTC_BASE_RPC_PORT)) @@ -434,7 +410,7 @@ class BaseTest(unittest.TestCase): callrpc_cli(cfg.LITECOIN_BINDIR, data_dir, 'regtest', '-wallet=wallet.dat create', 'litecoin-wallet') cls.ltc_daemons.append(startDaemon(os.path.join(TEST_DIR, 'ltc_' + str(i)), cfg.LITECOIN_BINDIR, cfg.LITECOIND)) - logging.info('Started %s %d', cfg.LITECOIND, cls.part_daemons[-1].pid) + logging.info('Started %s %d', cfg.LITECOIND, cls.part_daemons[-1].handle.pid) waitForRPC(make_rpc_func(i, base_rpc_port=LTC_BASE_RPC_PORT)) @@ -443,11 +419,22 @@ class BaseTest(unittest.TestCase): if not cls.restore_instance: prepareXmrDataDir(TEST_DIR, i, 'monerod.conf') - cls.xmr_daemons.append(startXmrDaemon(os.path.join(TEST_DIR, 'xmr_' + str(i)), cfg.XMR_BINDIR, cfg.XMRD)) - logging.info('Started %s %d', cfg.XMRD, cls.xmr_daemons[-1].pid) + node_dir = os.path.join(TEST_DIR, 'xmr_' + str(i)) + cls.xmr_daemons.append(startXmrDaemon(node_dir, cfg.XMR_BINDIR, cfg.XMRD)) + logging.info('Started %s %d', cfg.XMRD, cls.xmr_daemons[-1].handle.pid) waitForXMRNode(i) - cls.xmr_daemons.append(startXmrWalletRPC(os.path.join(TEST_DIR, 'xmr_' + str(i)), cfg.XMR_BINDIR, cfg.XMR_WALLET_RPC, i)) + opts = [ + '--daemon-address=127.0.0.1:{}'.format(XMR_BASE_RPC_PORT + i), + '--no-dns', + '--rpc-bind-port={}'.format(XMR_BASE_WALLET_RPC_PORT + i), + '--wallet-dir={}'.format(os.path.join(node_dir, 'wallets')), + '--log-file={}'.format(os.path.join(node_dir, 'wallet.log')), + '--rpc-login=test{0}:test_pass{0}'.format(i), + '--shared-ringdb-dir={}'.format(os.path.join(node_dir, 'shared-ringdb')), + '--allow-mismatched-daemon-version', + ] + cls.xmr_daemons.append(startXmrWalletDaemon(node_dir, cfg.XMR_BINDIR, cfg.XMR_WALLET_RPC, opts=opts)) for i in range(NUM_XMR_NODES): cls.xmr_wallet_auth.append(('test{0}'.format(i), 'test_pass{0}'.format(i))) @@ -487,11 +474,11 @@ class BaseTest(unittest.TestCase): cls.network_pubkey = settings['network_pubkey'] fp = open(os.path.join(basicswap_dir, 'basicswap.log'), 'w') sc = BasicSwap(fp, basicswap_dir, settings, 'regtest', log_name='BasicSwap{}'.format(i)) - sc.setDaemonPID(Coins.BTC, cls.btc_daemons[i].pid) - sc.setDaemonPID(Coins.PART, cls.part_daemons[i].pid) + sc.setDaemonPID(Coins.BTC, cls.btc_daemons[i].handle.pid) + sc.setDaemonPID(Coins.PART, cls.part_daemons[i].handle.pid) if cls.start_ltc_nodes: - sc.setDaemonPID(Coins.LTC, cls.ltc_daemons[i].pid) + sc.setDaemonPID(Coins.LTC, cls.ltc_daemons[i].handle.pid) cls.addPIDInfo(sc, i) sc.start() @@ -593,7 +580,7 @@ class BaseTest(unittest.TestCase): except Exception: traceback.print_exc() - Test.tearDownClass() + cls.tearDownClass() raise ValueError('setUpClass() failed.') @classmethod