Skip to content

Commit

Permalink
feat: make testnet cleanup work also outside of framework
Browse files Browse the repository at this point in the history
Make the testnet cleanup script work also when the testnet was not
started through framework testnet scripts, e.g. directly in bootstrap
dir.
  • Loading branch information
mkoura committed Nov 21, 2024
1 parent 1e80ff6 commit 3fc7e57
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
32 changes: 28 additions & 4 deletions cardano_node_tests/testnet_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()


Expand All @@ -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

Expand Down
29 changes: 26 additions & 3 deletions cardano_node_tests/utils/testnet_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 3fc7e57

Please sign in to comment.