From 3fc7e574c9f06a7020b551aa6e2ab4d0107a7db2 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Thu, 21 Nov 2024 12:44:32 +0100 Subject: [PATCH] feat: make testnet cleanup work also outside of framework Make the testnet cleanup script work also when the testnet was not started through framework testnet scripts, e.g. directly in bootstrap dir. --- cardano_node_tests/testnet_cleanup.py | 32 ++++++++++++++++++--- cardano_node_tests/utils/testnet_cleanup.py | 29 +++++++++++++++++-- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/cardano_node_tests/testnet_cleanup.py b/cardano_node_tests/testnet_cleanup.py index 32084be18..86cc6c188 100755 --- a/cardano_node_tests/testnet_cleanup.py +++ b/cardano_node_tests/testnet_cleanup.py @@ -9,9 +9,11 @@ import argparse import logging import os +import pathlib as pl import sys -from cardano_node_tests.utils import cluster_nodes +from cardano_clusterlib import clusterlib + from cardano_node_tests.utils import helpers from cardano_node_tests.utils import testnet_cleanup @@ -28,6 +30,19 @@ def get_args() -> argparse.Namespace: type=helpers.check_dir_arg, help="Path to a directory with testing artifacts", ) + parser.add_argument( + "-f", + "--address", + required=True, + help="Faucet address", + ) + parser.add_argument( + "-s", + "--skey-file", + required=True, + type=helpers.check_file_arg, + help="Path to faucet skey file", + ) return parser.parse_args() @@ -38,15 +53,24 @@ def main() -> int: ) args = get_args() - if not os.environ.get("CARDANO_NODE_SOCKET_PATH"): + socket_env = os.environ.get("CARDANO_NODE_SOCKET_PATH") + if not socket_env: LOGGER.error("The `CARDANO_NODE_SOCKET_PATH` environment variable is not set.") return 1 if not os.environ.get("BOOTSTRAP_DIR"): LOGGER.error("The `BOOTSTRAP_DIR` environment variable is not set.") return 1 - cluster_obj = cluster_nodes.get_cluster_type().get_cluster_obj() - testnet_cleanup.cleanup(cluster_obj=cluster_obj, location=args.artifacts_base_dir) + state_dir = pl.Path(socket_env).parent + cluster_obj = clusterlib.ClusterLib( + state_dir=state_dir, + ) + testnet_cleanup.cleanup( + cluster_obj=cluster_obj, + location=args.artifacts_base_dir, + faucet_address=args.address, + faucet_skey_file=args.skey_file, + ) return 0 diff --git a/cardano_node_tests/utils/testnet_cleanup.py b/cardano_node_tests/utils/testnet_cleanup.py index d0e72bbe5..b0c584f00 100644 --- a/cardano_node_tests/utils/testnet_cleanup.py +++ b/cardano_node_tests/utils/testnet_cleanup.py @@ -172,14 +172,37 @@ def group_files(file_paths: tp.Generator[pl.Path, None, None]) -> tp.List[tp.Lis return path_groups +def _get_faucet_payment_rec( + address: str = "", + skey_file: clusterlib.FileType = "", +) -> clusterlib.AddressRecord: + if address or skey_file: + if not (address and skey_file): + err = "Both 'address' and 'skey_file' need to be set." + raise ValueError(err) + + faucet_payment = clusterlib.AddressRecord( + address=address, + vkey_file=pl.Path("/nonexistent"), # We don't need this for faucet + skey_file=pl.Path(skey_file), + ) + else: + # Try to infer the faucet address and keys from cluster env + cluster_env = cluster_nodes.get_cluster_env() + faucet_addr_file = cluster_env.state_dir / "shelley" / "faucet.addr" + faucet_payment = create_addr_record(faucet_addr_file) + + return faucet_payment + + def cleanup( cluster_obj: clusterlib.ClusterLib, location: clusterlib.FileType, + faucet_address: str = "", + faucet_skey_file: clusterlib.FileType = "", ) -> None: """Cleanup a testnet with the help of testing artifacts.""" - cluster_env = cluster_nodes.get_cluster_env() - faucet_addr_file = cluster_env.state_dir / "shelley" / "faucet.addr" - faucet_payment = create_addr_record(faucet_addr_file) + faucet_payment = _get_faucet_payment_rec(address=faucet_address, skey_file=faucet_skey_file) files_found = group_files(find_files(location)) stake_deposit_amt = cluster_obj.g_query.get_address_deposit()