From d89a58242f8f73013f2e60a5f65da4bc9584558c Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sun, 13 Apr 2025 13:00:08 +0200 Subject: [PATCH] tests: Fix test_btc_xmr on windows. --- basicswap/rpc.py | 39 ++---------------- tests/basicswap/common.py | 50 +++++++++++++++++++++--- tests/basicswap/common_xmr.py | 6 +-- tests/basicswap/extended/test_dash.py | 4 +- tests/basicswap/extended/test_firo.py | 4 +- tests/basicswap/extended/test_network.py | 16 ++++---- tests/basicswap/extended/test_pivx.py | 4 +- tests/basicswap/extended/test_wow.py | 3 +- tests/basicswap/test_bch_xmr.py | 14 ++++--- tests/basicswap/test_reload.py | 4 +- tests/basicswap/test_xmr.py | 48 ++++++++++++++++------- 11 files changed, 107 insertions(+), 85 deletions(-) diff --git a/basicswap/rpc.py b/basicswap/rpc.py index 43b4b46..49eff25 100644 --- a/basicswap/rpc.py +++ b/basicswap/rpc.py @@ -1,15 +1,13 @@ # -*- coding: utf-8 -*- # Copyright (c) 2020-2024 tecnovert +# Copyright (c) 2025 The Basicswap developers # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. -import os import json -import shlex -import urllib import traceback -import subprocess +import urllib from xmlrpc.client import ( Fault, Transport, @@ -104,7 +102,7 @@ def callrpc(rpc_port, auth, method, params=[], wallet=None, host="127.0.0.1"): r = json.loads(v.decode("utf-8")) except Exception as ex: traceback.print_exc() - raise ValueError("RPC server error " + str(ex) + ", method: " + method) + raise ValueError(f"RPC server error: {ex}, method: {method}") if "error" in r and r["error"] is not None: raise ValueError("RPC error " + str(r["error"])) @@ -120,36 +118,7 @@ def openrpc(rpc_port, auth, wallet=None, host="127.0.0.1"): return Jsonrpc(url) except Exception as ex: traceback.print_exc() - raise ValueError("RPC error " + str(ex)) - - -def callrpc_cli(bindir, datadir, chain, cmd, cli_bin="particl-cli", wallet=None): - cli_bin = os.path.join(bindir, cli_bin) - - args = [ - cli_bin, - ] - if chain != "mainnet": - args.append("-" + chain) - args.append("-datadir=" + datadir) - if wallet is not None: - args.append("-rpcwallet=" + wallet) - args += shlex.split(cmd) - - p = subprocess.Popen( - args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - out = p.communicate() - - if len(out[1]) > 0: - raise ValueError("RPC error " + str(out[1])) - - r = out[0].decode("utf-8").strip() - try: - r = json.loads(r) - except Exception: - pass - return r + raise ValueError(f"RPC error: {ex}") def make_rpc_func(port, auth, wallet=None, host="127.0.0.1"): diff --git a/tests/basicswap/common.py b/tests/basicswap/common.py index 1fe2483..5bf2710 100644 --- a/tests/basicswap/common.py +++ b/tests/basicswap/common.py @@ -6,10 +6,12 @@ # Distributed under the MIT software license, see the accompanying # file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php. -import os import json -import signal import logging +import os +import shlex +import signal +import subprocess from urllib.request import urlopen from .util import read_json_api @@ -133,11 +135,12 @@ def checkForks(ro): def stopDaemons(daemons): for d in daemons: - logging.info("Interrupting %d", d.handle.pid) + logging.info(f"Interrupting {d.handle.pid}") + signal_type = signal.SIGTERM if os.name == "nt" else signal.SIGINT try: - d.handle.send_signal(signal.SIGINT) + d.handle.send_signal(signal_type) except Exception as e: - logging.info("Interrupting %d, error %s", d.handle.pid, str(e)) + logging.info(f"Interrupting {d.handle.pid}, error: {e}") for d in daemons: try: d.handle.wait(timeout=20) @@ -145,7 +148,7 @@ def stopDaemons(daemons): if fp: fp.close() except Exception as e: - logging.info("Closing %d, error %s", d.handle.pid, str(e)) + logging.info(f"Closing {d.handle.pid}, error: {e}") def wait_for_bid( @@ -494,3 +497,38 @@ def compare_bid_states_unordered(states, expect_states, ignore_states=[]) -> boo logging.info("Have states: {}".format(json.dumps(states, indent=4))) raise e return True + + +def callrpc_cli( + bindir, + datadir, + chain, + cmd, + cli_bin="particl-cli" + (".exe" if os.name == "nt" else ""), + wallet=None, +): + cli_bin = os.path.join(bindir, cli_bin) + args = [ + cli_bin, + ] + if chain != "mainnet": + args.append("-" + chain) + args.append("-datadir=" + datadir) + if wallet is not None: + args.append("-rpcwallet=" + wallet) + args += shlex.split(cmd) + + p = subprocess.Popen( + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + out = p.communicate() + + if len(out[1]) > 0: + raise ValueError(f"RPC error: {out[1]}") + + r = out[0].decode("utf-8").strip() + try: + r = json.loads(r) + except Exception: + pass + return r diff --git a/tests/basicswap/common_xmr.py b/tests/basicswap/common_xmr.py index 583901d..3c847a1 100644 --- a/tests/basicswap/common_xmr.py +++ b/tests/basicswap/common_xmr.py @@ -557,12 +557,12 @@ def prepare_nodes( ): bins_path = os.path.join(TEST_PATH, "bin") for i in range(num_nodes): - logging.info("Preparing node: %d.", i) - client_path = os.path.join(TEST_PATH, "client{}".format(i)) + logging.info(f"Preparing node: {i}.") + client_path = os.path.join(TEST_PATH, f"client{i}") try: shutil.rmtree(client_path) except Exception as ex: - logging.warning("setUpClass %s", str(ex)) + logging.warning(f"setUpClass {ex}") run_prepare( i, diff --git a/tests/basicswap/extended/test_dash.py b/tests/basicswap/extended/test_dash.py index cb90523..1ba2f58 100644 --- a/tests/basicswap/extended/test_dash.py +++ b/tests/basicswap/extended/test_dash.py @@ -40,9 +40,6 @@ from basicswap.basicswap_util import ( from basicswap.util.address import ( toWIF, ) -from basicswap.rpc import ( - callrpc_cli, -) from basicswap.contrib.key import ( ECKey, ) @@ -53,6 +50,7 @@ from tests.basicswap.util import ( read_json_api, ) from tests.basicswap.common import ( + callrpc_cli, checkForks, stopDaemons, wait_for_offer, diff --git a/tests/basicswap/extended/test_firo.py b/tests/basicswap/extended/test_firo.py index b402d5a..236d5e2 100644 --- a/tests/basicswap/extended/test_firo.py +++ b/tests/basicswap/extended/test_firo.py @@ -25,13 +25,11 @@ from basicswap.util import ( make_int, format_amount, ) -from basicswap.rpc import ( - callrpc_cli, -) from tests.basicswap.util import ( read_json_api, ) from tests.basicswap.common import ( + callrpc_cli, stopDaemons, wait_for_bid, make_rpc_func, diff --git a/tests/basicswap/extended/test_network.py b/tests/basicswap/extended/test_network.py index 15df05d..d1a6334 100644 --- a/tests/basicswap/extended/test_network.py +++ b/tests/basicswap/extended/test_network.py @@ -32,7 +32,6 @@ from basicswap.util.address import ( ) from basicswap.rpc import ( callrpc, - callrpc_cli, ) from basicswap.contrib.key import ( ECKey, @@ -44,19 +43,20 @@ from tests.basicswap.util import ( read_json_api, ) from tests.basicswap.common import ( - prepareDataDir, - make_rpc_func, - checkForks, - stopDaemons, - delay_for, - TEST_HTTP_HOST, - TEST_HTTP_PORT, BASE_P2P_PORT, BASE_RPC_PORT, BASE_ZMQ_PORT, BTC_BASE_PORT, BTC_BASE_RPC_PORT, + callrpc_cli, + checkForks, + delay_for, + make_rpc_func, PREFIX_SECRET_KEY_REGTEST, + prepareDataDir, + stopDaemons, + TEST_HTTP_HOST, + TEST_HTTP_PORT, waitForRPC, ) diff --git a/tests/basicswap/extended/test_pivx.py b/tests/basicswap/extended/test_pivx.py index d71e454..e12b918 100644 --- a/tests/basicswap/extended/test_pivx.py +++ b/tests/basicswap/extended/test_pivx.py @@ -40,9 +40,6 @@ from basicswap.basicswap_util import ( from basicswap.util.address import ( toWIF, ) -from basicswap.rpc import ( - callrpc_cli, -) from basicswap.contrib.key import ( ECKey, ) @@ -53,6 +50,7 @@ from tests.basicswap.util import ( read_json_api, ) from tests.basicswap.common import ( + callrpc_cli, checkForks, stopDaemons, wait_for_bid, diff --git a/tests/basicswap/extended/test_wow.py b/tests/basicswap/extended/test_wow.py index 8a022e0..ca5729a 100644 --- a/tests/basicswap/extended/test_wow.py +++ b/tests/basicswap/extended/test_wow.py @@ -174,7 +174,8 @@ class Test(BaseTest): "create_wallet", {"filename": "testwallet", "language": "English"}, ) - cls.callwownodewallet(cls, i, "open_wallet", {"filename": "testwallet"}) + else: + cls.callwownodewallet(cls, i, "open_wallet", {"filename": "testwallet"}) @classmethod def addPIDInfo(cls, sc, i): diff --git a/tests/basicswap/test_bch_xmr.py b/tests/basicswap/test_bch_xmr.py index be76fe5..4ebfa84 100644 --- a/tests/basicswap/test_bch_xmr.py +++ b/tests/basicswap/test_bch_xmr.py @@ -20,6 +20,7 @@ from basicswap.bin.run import startDaemon from basicswap.util.crypto import sha256 from tests.basicswap.test_btc_xmr import BasicSwapTest from tests.basicswap.common import ( + callrpc_cli, make_rpc_func, prepareDataDir, stopDaemons, @@ -37,9 +38,6 @@ from basicswap.contrib.test_framework.script import ( OP_CHECKSEQUENCEVERIFY, ) from basicswap.interface.bch import BCHInterface -from basicswap.rpc import ( - callrpc_cli, -) from basicswap.util import ensure from .test_xmr import test_delay_event, callnoderpc @@ -134,14 +132,20 @@ class TestBCH(BasicSwapTest): if not line.startswith("findpeers"): fp.write(line) - if os.path.exists(os.path.join(BITCOINCASH_BINDIR, "bitcoin-wallet")): + bch_wallet_bin = "bitcoin-wallet" + (".exe" if os.name == "nt" else "") + if os.path.exists( + os.path.join( + BITCOINCASH_BINDIR, + bch_wallet_bin, + ) + ): try: callrpc_cli( BITCOINCASH_BINDIR, data_dir, "regtest", "-wallet=wallet.dat create", - "bitcoin-wallet", + bch_wallet_bin, ) except Exception as e: logging.warning("bch: bitcoin-wallet create failed") diff --git a/tests/basicswap/test_reload.py b/tests/basicswap/test_reload.py index 899cfa1..b3b47cc 100644 --- a/tests/basicswap/test_reload.py +++ b/tests/basicswap/test_reload.py @@ -24,9 +24,6 @@ import threading import multiprocessing from unittest.mock import patch -from basicswap.rpc import ( - callrpc_cli, -) from tests.basicswap.util import ( read_json_api, post_json_api, @@ -38,6 +35,7 @@ from tests.basicswap.common import ( waitForNumSwapping, ) from tests.basicswap.common_xmr import ( + callrpc_cli, prepare_nodes, ) import basicswap.bin.run as runSystem diff --git a/tests/basicswap/test_xmr.py b/tests/basicswap/test_xmr.py index bd55bd7..4404f82 100644 --- a/tests/basicswap/test_xmr.py +++ b/tests/basicswap/test_xmr.py @@ -39,7 +39,6 @@ from basicswap.util.address import ( ) from basicswap.rpc import ( callrpc, - callrpc_cli, ) from basicswap.rpc_xmr import ( callrpc_xmr, @@ -61,6 +60,7 @@ from tests.basicswap.util import ( read_json_api, ) from tests.basicswap.common import ( + callrpc_cli, prepareDataDir, make_rpc_func, checkForks, @@ -376,7 +376,7 @@ class BaseTest(unittest.TestCase): if os.path.isdir(TEST_DIR): if RESET_TEST: - logging.info("Removing " + TEST_DIR) + logging.info("Removing test dir " + TEST_DIR) for name in os.listdir(TEST_DIR): if name == "pivx-params": continue @@ -388,6 +388,8 @@ class BaseTest(unittest.TestCase): else: logging.info("Restoring instance from " + TEST_DIR) cls.restore_instance = True + else: + logging.info("Creating test dir " + TEST_DIR) if not os.path.exists(TEST_DIR): os.makedirs(TEST_DIR) @@ -399,19 +401,25 @@ class BaseTest(unittest.TestCase): try: logging.info("Preparing coin nodes.") + part_wallet_bin = "particl-wallet" + (".exe" if os.name == "nt" else "") for i in range(NUM_NODES): if not cls.restore_instance: data_dir = prepareDataDir(TEST_DIR, i, "particl.conf", "part_") - if os.path.exists( - os.path.join(cfg.PARTICL_BINDIR, "particl-wallet") + if not os.path.exists( + os.path.join( + cfg.PARTICL_BINDIR, + part_wallet_bin, + ) ): + logging.warning(f"{part_wallet_bin} not found.") + else: try: callrpc_cli( cfg.PARTICL_BINDIR, data_dir, "regtest", "-wallet=wallet.dat -legacy create", - "particl-wallet", + part_wallet_bin, ) except Exception as e: logging.warning( @@ -422,7 +430,7 @@ class BaseTest(unittest.TestCase): data_dir, "regtest", "-wallet=wallet.dat create", - "particl-wallet", + part_wallet_bin, ) cls.part_daemons.append( @@ -471,6 +479,7 @@ class BaseTest(unittest.TestCase): ) rpc("reservebalance", [False]) + btc_wallet_bin = "bitcoin-wallet" + (".exe" if os.name == "nt" else "") for i in range(NUM_BTC_NODES): if not cls.restore_instance: data_dir = prepareDataDir( @@ -481,9 +490,14 @@ class BaseTest(unittest.TestCase): base_p2p_port=BTC_BASE_PORT, base_rpc_port=BTC_BASE_RPC_PORT, ) - if os.path.exists( - os.path.join(cfg.BITCOIN_BINDIR, "bitcoin-wallet") + if not os.path.exists( + os.path.join( + cfg.BITCOIN_BINDIR, + btc_wallet_bin, + ) ): + logging.warning(f"{btc_wallet_bin} not found.") + else: if BTC_USE_DESCRIPTORS: # How to set blank and disable_private_keys with wallet util? pass @@ -494,7 +508,7 @@ class BaseTest(unittest.TestCase): data_dir, "regtest", "-wallet=wallet.dat -legacy create", - "bitcoin-wallet", + btc_wallet_bin, ) except Exception as e: logging.warning( @@ -505,7 +519,7 @@ class BaseTest(unittest.TestCase): data_dir, "regtest", "-wallet=wallet.dat create", - "bitcoin-wallet", + btc_wallet_bin, ) cls.btc_daemons.append( @@ -536,6 +550,7 @@ class BaseTest(unittest.TestCase): ) if cls.start_ltc_nodes: + ltc_wallet_bin = "litecoin-wallet" + (".exe" if os.name == "nt" else "") for i in range(NUM_LTC_NODES): if not cls.restore_instance: data_dir = prepareDataDir( @@ -547,14 +562,16 @@ class BaseTest(unittest.TestCase): base_rpc_port=LTC_BASE_RPC_PORT, ) if os.path.exists( - os.path.join(cfg.LITECOIN_BINDIR, "litecoin-wallet") + os.path.join(cfg.LITECOIN_BINDIR, ltc_wallet_bin) ): + logging.warning(f"{ltc_wallet_bin} not found.") + else: callrpc_cli( cfg.LITECOIN_BINDIR, data_dir, "regtest", "-wallet=wallet.dat create", - "litecoin-wallet", + ltc_wallet_bin, ) cls.ltc_daemons.append( @@ -620,9 +637,10 @@ class BaseTest(unittest.TestCase): "create_wallet", {"filename": "testwallet", "language": "English"}, ) - cls.callxmrnodewallet( - cls, i, "open_wallet", {"filename": "testwallet"} - ) + else: + cls.callxmrnodewallet( + cls, i, "open_wallet", {"filename": "testwallet"} + ) for i in range(NUM_NODES): # Hook for descendant classes