|
| 1 | +"""Tests for basic SMASH operations.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from http import HTTPStatus |
| 6 | + |
| 7 | +import pytest |
| 8 | +import requests |
| 9 | +from cardano_clusterlib import clusterlib |
| 10 | + |
| 11 | +from cardano_node_tests.utils import configuration |
| 12 | +from cardano_node_tests.utils import dbsync_queries |
| 13 | +from cardano_node_tests.utils import dbsync_utils |
| 14 | +from cardano_node_tests.utils import helpers |
| 15 | +from cardano_node_tests.utils import logfiles |
| 16 | +from cardano_node_tests.utils import smash_utils |
| 17 | + |
| 18 | +LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def check_smash_availability() -> None: |
| 23 | + """Fixture to check SMASH availability before each test.""" |
| 24 | + if not configuration.HAS_SMASH: |
| 25 | + pytest.skip("Skipping test because SMASH service is not available.") |
| 26 | + |
| 27 | + |
| 28 | +def check_request_error( |
| 29 | + err: requests.exceptions.RequestException, |
| 30 | + expected_status: HTTPStatus, |
| 31 | + expected_code: str | None, |
| 32 | + expected_description: str, |
| 33 | +) -> None: |
| 34 | + """Assert expected HTTP errors in requests, handling both JSON and text responses.""" |
| 35 | + response = err.response |
| 36 | + assert response.status_code == expected_status |
| 37 | + |
| 38 | + try: |
| 39 | + error_data = response.json() |
| 40 | + actual_code = error_data.get("code") |
| 41 | + actual_description = error_data.get("description") |
| 42 | + except ValueError: |
| 43 | + # If not JSON, treat the entire response as text |
| 44 | + actual_code = None |
| 45 | + actual_description = response.text.strip() |
| 46 | + |
| 47 | + if expected_code: |
| 48 | + assert actual_code == expected_code |
| 49 | + |
| 50 | + assert actual_description == expected_description |
| 51 | + |
| 52 | + |
| 53 | +class TestBasicSmash: |
| 54 | + """Basic tests for SMASH service.""" |
| 55 | + |
| 56 | + @pytest.fixture() |
| 57 | + def locked_pool( |
| 58 | + self, |
| 59 | + cluster_lock_pool: tuple[clusterlib.ClusterLib, str], |
| 60 | + ) -> dbsync_queries.PoolDataDBRow: |
| 61 | + """Get id of locked pool from cluster_lock_pool fixture.""" |
| 62 | + cluster_obj, pool_name = cluster_lock_pool |
| 63 | + pools_ids = cluster_obj.g_query.get_stake_pools() |
| 64 | + locked_pool_number = pool_name.replace("node-pool", "") |
| 65 | + pattern = re.compile(r"pool" + re.escape(locked_pool_number) + r"(\D|$)") |
| 66 | + pools = [next(dbsync_queries.query_pool_data(p)) for p in pools_ids] |
| 67 | + locked_pool = next((p for p in pools if pattern.search(p.metadata_url)), None) |
| 68 | + locked_pool_data = dbsync_utils.get_pool_data(locked_pool.view) |
| 69 | + if locked_pool_data is None: |
| 70 | + err_msg = f"Locked pool data not found for pool_id: {locked_pool.view}" |
| 71 | + raise ValueError(err_msg) |
| 72 | + return locked_pool_data |
| 73 | + |
| 74 | + @pytest.fixture(scope="session") |
| 75 | + def smash( |
| 76 | + self, |
| 77 | + ) -> None | smash_utils.SmashClient: |
| 78 | + """Create SMASH client.""" |
| 79 | + smash = smash_utils.get_client() |
| 80 | + return smash |
| 81 | + |
| 82 | + def test_fetch_pool_metadata( |
| 83 | + self, locked_pool: dbsync_queries.PoolDataDBRow, smash: smash_utils.SmashClient |
| 84 | + ): |
| 85 | + pool_id = locked_pool.view |
| 86 | + |
| 87 | + # Offchain metadata is inserted into database few minutes after start of a cluster |
| 88 | + def _query_func(): |
| 89 | + pool_metadata = next(iter(dbsync_queries.query_off_chain_pool_data(pool_id)), None) |
| 90 | + assert pool_metadata is not None, dbsync_utils.NO_RESPONSE_STR |
| 91 | + return pool_metadata |
| 92 | + |
| 93 | + metadata_dbsync = dbsync_utils.retry_query(query_func=_query_func, timeout=360) |
| 94 | + |
| 95 | + expected_metadata = smash_utils.PoolMetadata( |
| 96 | + name=metadata_dbsync.json["name"], |
| 97 | + description=metadata_dbsync.json["description"], |
| 98 | + ticker=metadata_dbsync.ticker_name, |
| 99 | + homepage=metadata_dbsync.json["homepage"], |
| 100 | + ) |
| 101 | + actual_metadata = smash.get_pool_metadata(pool_id, metadata_dbsync.hash.hex()) |
| 102 | + assert expected_metadata == actual_metadata |
| 103 | + |
| 104 | + def test_delist_pool( |
| 105 | + self, |
| 106 | + locked_pool: dbsync_queries.PoolDataDBRow, |
| 107 | + smash: smash_utils.SmashClient, |
| 108 | + request: pytest.FixtureRequest, |
| 109 | + worker_id: str, |
| 110 | + ): |
| 111 | + # Define and register function that ensures pool is re-enlisted after test completion |
| 112 | + def pool_cleanup(): |
| 113 | + smash.enlist_pool(locked_pool.hash) |
| 114 | + |
| 115 | + request.addfinalizer(pool_cleanup) |
| 116 | + |
| 117 | + # Delist the pool |
| 118 | + expected_delisted_pool = smash_utils.PoolData(pool_id=locked_pool.hash.hex()) |
| 119 | + actual_delisted_pool = smash.delist_pool(locked_pool.hash) |
| 120 | + assert expected_delisted_pool == actual_delisted_pool |
| 121 | + |
| 122 | + # Check if fetching metadata for a delisted pool returns an error |
| 123 | + try: |
| 124 | + smash.get_pool_metadata(locked_pool.hash, locked_pool.metadata_hash) |
| 125 | + except requests.exceptions.RequestException as err: |
| 126 | + check_request_error( |
| 127 | + err, HTTPStatus.FORBIDDEN, None, f"Pool {locked_pool.hash} is delisted" |
| 128 | + ) |
| 129 | + |
| 130 | + # Ignore expected errors in logs that would fail test in teardown phase |
| 131 | + err_msg = "Delisted pool already exists!" |
| 132 | + expected_err_regexes = [err_msg] |
| 133 | + logfiles.add_ignore_rule( |
| 134 | + files_glob="smash.stdout", |
| 135 | + regex="|".join(expected_err_regexes), |
| 136 | + ignore_file_id=worker_id, |
| 137 | + ) |
| 138 | + # Ensure re-delisting an already delisted pool returns an error |
| 139 | + try: |
| 140 | + smash.delist_pool(locked_pool.hash) |
| 141 | + except requests.exceptions.RequestException as err: |
| 142 | + check_request_error(err, HTTPStatus.BAD_REQUEST, "DbInsertError", err_msg) |
| 143 | + |
| 144 | + def test_enlist_pool( |
| 145 | + self, |
| 146 | + locked_pool: dbsync_queries.PoolDataDBRow, |
| 147 | + smash: smash_utils.SmashClient, |
| 148 | + ): |
| 149 | + # Ensure enlisting an already enlisted pool returns an error |
| 150 | + try: |
| 151 | + smash.enlist_pool(locked_pool.hash) |
| 152 | + except requests.exceptions.RequestException as err: |
| 153 | + check_request_error( |
| 154 | + err, |
| 155 | + HTTPStatus.NOT_FOUND, |
| 156 | + "RecordDoesNotExist", |
| 157 | + "The requested record does not exist.", |
| 158 | + ) |
| 159 | + |
| 160 | + # Delist the pool |
| 161 | + smash.delist_pool(locked_pool.hash) |
| 162 | + try: |
| 163 | + smash.get_pool_metadata(locked_pool.hash, locked_pool.metadata_hash) |
| 164 | + except requests.exceptions.RequestException as err: |
| 165 | + check_request_error( |
| 166 | + err, HTTPStatus.FORBIDDEN, None, f"Pool {locked_pool.hash} is delisted" |
| 167 | + ) |
| 168 | + |
| 169 | + # Enlist the pool |
| 170 | + actual_res_enlist = smash.enlist_pool(locked_pool.hash) |
| 171 | + expected_res_enlist = smash_utils.PoolData(pool_id=locked_pool.hash) |
| 172 | + assert expected_res_enlist == actual_res_enlist |
| 173 | + |
| 174 | + def test_reserve_ticker( |
| 175 | + self, |
| 176 | + locked_pool: dbsync_queries.PoolDataDBRow, |
| 177 | + smash: smash_utils.SmashClient, |
| 178 | + request: pytest.FixtureRequest, |
| 179 | + ): |
| 180 | + pool_id = locked_pool.hash.hex() |
| 181 | + # Register cleanup function that removes ticker from database after test completion |
| 182 | + request.addfinalizer(dbsync_queries.delete_reserved_pool_tickers) |
| 183 | + |
| 184 | + # Reserve ticker |
| 185 | + ticker = helpers.get_rand_str(length=3) |
| 186 | + actual_response = smash.reserve_ticker(ticker_name=ticker, pool_hash=pool_id) |
| 187 | + expected_response = smash_utils.PoolTicker(name=f"{ticker}") |
| 188 | + assert expected_response == actual_response |
| 189 | + |
| 190 | + # Reserve already taken ticker |
| 191 | + try: |
| 192 | + smash.reserve_ticker(ticker_name=ticker, pool_hash=pool_id) |
| 193 | + except requests.exceptions.RequestException as err: |
| 194 | + check_request_error( |
| 195 | + err, |
| 196 | + HTTPStatus.BAD_REQUEST, |
| 197 | + "TickerAlreadyReserved", |
| 198 | + f'Ticker name "{ticker}" is already reserved', |
| 199 | + ) |
0 commit comments