Skip to content

Commit eb400e7

Browse files
committed
Code review
- Added symbolic links for SMASH server binaries in `source_dbsync.sh` - Exported `PGPASSFILE` in `run-cardano-smash` script - it is needed in the end - Updated `test_pools.py` and `test_smash.py` to use keyword arguments - Added docstrings to test methods in `test_smash.py` - Added module level qualified imports - Fixed a fixture that cannot be session scoped
1 parent 9f1c8f3 commit eb400e7

File tree

5 files changed

+49
-35
lines changed

5 files changed

+49
-35
lines changed

.github/source_dbsync.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,17 @@ if [ -n "$DBSYNC_TAR_URL" ]; then
103103
rm -f "$DBSYNC_TAR_FILE"
104104
rm -f db-sync-node
105105
ln -s "${WORKDIR}/dbsync_download" db-sync-node || exit 1
106+
rm -f smash-server || rm -f smash-server/bin/cardano-smash-server
107+
mkdir -p smash-server/bin
108+
ln -s "${WORKDIR}/dbsync_download/bin/cardano-smash-server" smash-server/bin/cardano-smash-server || exit 1
106109
else
107-
# build db-sync
110+
# Build db-sync
108111
nix build --accept-flake-config .#cardano-db-sync -o db-sync-node \
109112
|| nix build --accept-flake-config .#cardano-db-sync:exe:cardano-db-sync -o db-sync-node \
110113
|| exit 1
114+
# The cardano-smash-server binary doesn't seem to be cached in nix binary cache, and we don't
115+
# want to pull the whole development environment. Therefore we'll not build it here, we'll depend only on
116+
# the binary from release archive.
111117
fi
112118

113119
[ -e db-sync-node/bin/cardano-db-sync ] || exit 1

cardano_node_tests/cluster_scripts/conway_fast/run-cardano-smash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SOCKET_PATH="$(readlink -m "$CARDANO_NODE_SOCKET_PATH")"
66
STATE_CLUSTER="${SOCKET_PATH%/*}"
77
STATE_CLUSTER_NAME="${STATE_CLUSTER##*/}"
88

9+
export PGPASSFILE="$STATE_CLUSTER/pgpass"
910
export SMASH_ADMIN="${SMASH_ADMIN:-admin}"
1011
export SMASH_PASSWORD="${SMASH_PASSWORD:-password}"
1112
export SMASH_ADMINS_FILE="$STATE_CLUSTER/admins.txt"

cardano_node_tests/tests/test_pools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,9 @@ def _query_func():
769769
)
770770

771771
dbsync_utils.retry_query(query_func=_query_func, timeout=360)
772-
smash_utils.check_smash_pool_errors(pool_creation_out.stake_pool_id, pool_metadata_hash)
772+
smash_utils.check_smash_pool_errors(
773+
pool_id=pool_creation_out.stake_pool_id, pool_metadata_hash=pool_metadata_hash
774+
)
773775

774776
@allure.link(helpers.get_vcs_link())
775777
@common.PARAM_USE_BUILD_CMD

cardano_node_tests/tests/test_smash.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ def locked_pool(
6565
assert locked_pool_data is not None, "Locked pool data not found!"
6666
return locked_pool_data
6767

68-
@pytest.fixture(scope="session")
68+
@pytest.fixture()
6969
def smash(
7070
self,
71-
) -> None | smash_utils.SmashClient:
71+
) -> smash_utils.SmashClient | None:
7272
"""Create SMASH client."""
7373
smash = smash_utils.get_client()
7474
if smash is None:
@@ -78,6 +78,7 @@ def smash(
7878
def test_fetch_pool_metadata(
7979
self, locked_pool: dbsync_types.PoolDataRecord, smash: smash_utils.SmashClient
8080
):
81+
"""Test fetching pool metadata from SMASH."""
8182
pool_id = locked_pool.hash
8283

8384
# Offchain metadata is inserted into database few minutes after start of a cluster
@@ -96,7 +97,9 @@ def _query_func():
9697
ticker=metadata_dbsync.ticker_name,
9798
homepage=metadata_dbsync.json["homepage"],
9899
)
99-
actual_metadata = smash.get_pool_metadata(pool_id, metadata_dbsync.hash.hex())
100+
actual_metadata = smash.get_pool_metadata(
101+
pool_id=pool_id, pool_meta_hash=metadata_dbsync.hash.hex()
102+
)
100103
assert expected_metadata == actual_metadata
101104

102105
def test_delist_pool(
@@ -106,22 +109,23 @@ def test_delist_pool(
106109
request: pytest.FixtureRequest,
107110
worker_id: str,
108111
):
112+
"""Test delisting a pool from SMASH."""
109113
pool_id = locked_pool.hash
110114

111115
# Define and register function that ensures pool is re-enlisted after test completion
112116
def pool_cleanup():
113-
smash.enlist_pool(pool_id)
117+
smash.enlist_pool(pool_id=pool_id)
114118

115119
request.addfinalizer(pool_cleanup)
116120

117121
# Delist the pool
118122
expected_delisted_pool = smash_utils.PoolData(pool_id=pool_id)
119-
actual_delisted_pool = smash.delist_pool(pool_id)
123+
actual_delisted_pool = smash.delist_pool(pool_id=pool_id)
120124
assert expected_delisted_pool == actual_delisted_pool
121125

122126
# Check if fetching metadata for a delisted pool returns an error
123127
try:
124-
smash.get_pool_metadata(pool_id, locked_pool.metadata_hash)
128+
smash.get_pool_metadata(pool_id=pool_id, pool_meta_hash=locked_pool.metadata_hash)
125129
except requests.exceptions.RequestException as err:
126130
check_request_error(err, HTTPStatus.FORBIDDEN, None, f"Pool {pool_id} is delisted")
127131

@@ -144,10 +148,11 @@ def test_enlist_pool(
144148
locked_pool: dbsync_types.PoolDataRecord,
145149
smash: smash_utils.SmashClient,
146150
):
151+
"""Test enlisting a pool in SMASH."""
147152
pool_id = locked_pool.hash
148153
# Ensure enlisting an already enlisted pool returns an error
149154
try:
150-
smash.enlist_pool(pool_id)
155+
smash.enlist_pool(pool_id=pool_id)
151156
except requests.exceptions.RequestException as err:
152157
check_request_error(
153158
err,
@@ -157,14 +162,14 @@ def test_enlist_pool(
157162
)
158163

159164
# Delist the pool
160-
smash.delist_pool(pool_id)
165+
smash.delist_pool(pool_id=pool_id)
161166
try:
162-
smash.get_pool_metadata(pool_id, locked_pool.metadata_hash)
167+
smash.get_pool_metadata(pool_id=pool_id, pool_meta_hash=locked_pool.metadata_hash)
163168
except requests.exceptions.RequestException as err:
164169
check_request_error(err, HTTPStatus.FORBIDDEN, None, f"Pool {pool_id} is delisted")
165170

166171
# Enlist the pool
167-
actual_res_enlist = smash.enlist_pool(pool_id)
172+
actual_res_enlist = smash.enlist_pool(pool_id=pool_id)
168173
expected_res_enlist = smash_utils.PoolData(pool_id=pool_id)
169174
assert expected_res_enlist == actual_res_enlist
170175

@@ -174,6 +179,7 @@ def test_reserve_ticker(
174179
smash: smash_utils.SmashClient,
175180
request: pytest.FixtureRequest,
176181
):
182+
"""Test reserving a ticker for a pool in SMASH."""
177183
pool_id = random.choice(cluster.g_query.get_stake_pools())
178184

179185
# Register cleanup function that removes ticker from database after test completion

cardano_node_tests/utils/smash_utils.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
import dataclasses
2+
import datetime
13
import logging
24
import os
35
import typing as tp
4-
from dataclasses import dataclass
5-
from datetime import datetime
6-
from datetime import timedelta
7-
from zoneinfo import ZoneInfo
86

97
import requests
10-
from requests.auth import HTTPBasicAuth
118

129
from cardano_node_tests.utils import cluster_nodes
1310
from cardano_node_tests.utils import configuration
@@ -17,25 +14,25 @@
1714
LOGGER = logging.getLogger(__name__)
1815

1916

20-
@dataclass(frozen=True, order=True)
17+
@dataclasses.dataclass(frozen=True, order=True)
2118
class PoolMetadata:
2219
name: str
2320
description: str
2421
ticker: str
2522
homepage: str
2623

2724

28-
@dataclass(frozen=True, order=True)
25+
@dataclasses.dataclass(frozen=True, order=True)
2926
class PoolData:
3027
pool_id: str
3128

3229

33-
@dataclass(frozen=True, order=True)
30+
@dataclasses.dataclass(frozen=True, order=True)
3431
class PoolTicker:
3532
name: str
3633

3734

38-
@dataclass(frozen=True, order=True)
35+
@dataclasses.dataclass(frozen=True, order=True)
3936
class PoolError:
4037
cause: str
4138
pool_hash: str
@@ -58,11 +55,11 @@ def __init__(self, instance_num: int) -> None:
5855
self.base_url = f"http://localhost:{self.port}"
5956
self.auth = self._get_auth()
6057

61-
def _get_auth(self) -> None | HTTPBasicAuth:
58+
def _get_auth(self) -> requests.auth.HTTPBasicAuth | None:
6259
"""Get Basic Auth credentials if configured."""
6360
admin = os.environ.get("SMASH_ADMIN", "admin")
6461
password = os.environ.get("SMASH_PASSWORD", "password")
65-
return HTTPBasicAuth(admin, password) if admin and password else None
62+
return requests.auth.HTTPBasicAuth(admin, password) if admin and password else None
6663

6764
def get_pool_metadata(self, pool_id: str, pool_meta_hash: str) -> PoolMetadata:
6865
"""Fetch stake pool metadata from SMASH, returning a `PoolMetadata`."""
@@ -101,7 +98,7 @@ def reserve_ticker(self, ticker_name: str, pool_hash: str) -> PoolTicker:
10198
data = response.json()
10299
return PoolTicker(name=data["name"])
103100

104-
def get_pool_errors(self, pool_id: str, from_date: None | str = None) -> list[PoolError]:
101+
def get_pool_errors(self, pool_id: str, from_date: str | None = None) -> list[PoolError]:
105102
"""Fetch errors for a specific stake pool."""
106103
url = f"{self.base_url}/api/v1/errors/{pool_id}"
107104
params = {"fromDate": from_date} if from_date else None
@@ -136,7 +133,7 @@ class SmashManager:
136133

137134
@classmethod
138135
def get_smash_instance(cls) -> SmashClient:
139-
"""Return a singleton instance of `SmashClient` for the given cluster instance."""
136+
"""Get a singleton instance of `SmashClient` for the given cluster instance."""
140137
instance_num = cluster_nodes.get_instance_num()
141138
if instance_num not in cls.instances:
142139
cls.instances[instance_num] = SmashClient(instance_num)
@@ -151,21 +148,22 @@ def is_smash_running() -> bool:
151148
return cluster_nodes.services_status(service_names=["smash"])[0].status == "RUNNING"
152149

153150

154-
def get_client() -> None | SmashClient:
155-
"""Global access to the SMASH client singleton."""
151+
def get_client() -> SmashClient | None:
152+
"""Get and cache the SMASH client."""
156153
if not is_smash_running():
157154
return None
158155
return SmashManager.get_smash_instance()
159156

160157

161-
def check_smash_pool_errors(pool_id: str, pool_metadata_hash: str) -> None | list[PoolError]:
158+
def check_smash_pool_errors(pool_id: str, pool_metadata_hash: str) -> list[PoolError] | None:
159+
"""Check if pool errors are correctly reported in SMASH."""
162160
if not is_smash_running():
163161
return None
164162
smash = SmashManager.get_smash_instance()
165163

166164
# Test pool errors endpoint for a date set in the future
167-
utc_now = datetime.now(ZoneInfo("UTC"))
168-
future_date = (utc_now + timedelta(days=365 * 5)).strftime("%d.%m.%Y")
165+
utc_now = datetime.datetime.now(tz=datetime.timezone.utc)
166+
future_date = (utc_now + datetime.timedelta(days=365 * 5)).strftime("%d.%m.%Y")
169167
smash_pool_errors_future = smash.get_pool_errors(pool_id=pool_id, from_date=future_date)
170168
assert smash_pool_errors_future == []
171169

@@ -189,11 +187,11 @@ def check_smash_pool_errors(pool_id: str, pool_metadata_hash: str) -> None | lis
189187

190188
try:
191189
# Parse the time string and check if it has a valid format
192-
error_time_utc = datetime.strptime(smash_pool_error.time, "%d.%m.%Y. %H:%M:%S").replace(
193-
tzinfo=ZoneInfo("UTC")
194-
)
190+
error_time_utc = datetime.datetime.strptime(
191+
smash_pool_error.time, "%d.%m.%Y. %H:%M:%S"
192+
).replace(tzinfo=datetime.timezone.utc)
195193

196-
time_diff = timedelta(minutes=20)
194+
time_diff = datetime.timedelta(minutes=20)
197195
assert error_time_utc <= utc_now, "Error time must not be in the future."
198196
assert error_time_utc >= (utc_now - time_diff), (
199197
f"Error time must be within the last {time_diff}."
@@ -206,7 +204,8 @@ def check_smash_pool_errors(pool_id: str, pool_metadata_hash: str) -> None | lis
206204
return smash_pool_errors
207205

208206

209-
def check_smash_pool_retired(pool_id: str) -> None | list[PoolData]:
207+
def check_smash_pool_retired(pool_id: str) -> list[PoolData] | None:
208+
"""Check if pool is correctly reported as retired in SMASH."""
210209
if not is_smash_running():
211210
return None
212211
smash = SmashManager.get_smash_instance()

0 commit comments

Comments
 (0)