-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_reconnect.py
286 lines (245 loc) · 10.3 KB
/
test_reconnect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Tests for reconnect."""
import logging
import os
import pathlib as pl
import time
import typing as tp
import allure
import pytest
import requests
from cardano_clusterlib import clusterlib
from cardano_node_tests.cluster_management import cluster_management
from cardano_node_tests.tests import common
from cardano_node_tests.utils import cluster_nodes
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import configuration
from cardano_node_tests.utils import helpers
from cardano_node_tests.utils.versions import VERSIONS
LOGGER = logging.getLogger(__name__)
TEST_RECONNECT = os.environ.get("TEST_RECONNECT") is not None
TEST_METRICS_RECONNECT = os.environ.get("TEST_METRICS_RECONNECT") is not None
@pytest.mark.skipif(
cluster_nodes.get_cluster_type().type != cluster_nodes.ClusterType.LOCAL,
reason="Runs only on local cluster",
)
@pytest.mark.skipif(
VERSIONS.cluster_era != VERSIONS.transaction_era,
reason="runs only with same cluster and Tx era",
)
class TestNodeReconnect:
"""Tests for nodes reconnect."""
@pytest.fixture
def payment_addrs(
self,
cluster_manager: cluster_management.ClusterManager,
cluster_singleton: clusterlib.ClusterLib,
) -> tp.List[clusterlib.AddressRecord]:
"""Create new payment addresses."""
cluster = cluster_singleton
num_addrs = 2
with cluster_manager.cache_fixture() as fixture_cache:
if fixture_cache.value:
return fixture_cache.value # type: ignore
addrs = clusterlib_utils.create_payment_addr_records(
*[
f"addr_rollback_ci{cluster_manager.cluster_instance_num}_{i}"
for i in range(num_addrs)
],
cluster_obj=cluster,
)
fixture_cache.value = addrs
# Fund source addresses
clusterlib_utils.fund_from_faucet(
*addrs,
cluster_obj=cluster,
faucet_data=cluster_manager.cache.addrs_data["user1"],
)
return addrs
def node_query_utxo(
self,
cluster_obj: clusterlib.ClusterLib,
node: str,
address: str = "",
tx_raw_output: tp.Optional[clusterlib.TxRawOutput] = None,
) -> tp.List[clusterlib.UTXOData]:
"""Query UTxO on given node."""
orig_socket = os.environ.get("CARDANO_NODE_SOCKET_PATH")
assert orig_socket
new_socket = pl.Path(orig_socket).parent / f"{node}.socket"
try:
os.environ["CARDANO_NODE_SOCKET_PATH"] = str(new_socket)
utxos = cluster_obj.g_query.get_utxo(address=address, tx_raw_output=tx_raw_output)
return utxos
finally:
os.environ["CARDANO_NODE_SOCKET_PATH"] = orig_socket
def node_get_tip(
self,
cluster_obj: clusterlib.ClusterLib,
node: str,
) -> tp.Dict[str, tp.Any]:
"""Query UTxO on given node."""
orig_socket = os.environ.get("CARDANO_NODE_SOCKET_PATH")
assert orig_socket
new_socket = pl.Path(orig_socket).parent / f"{node}.socket"
try:
os.environ["CARDANO_NODE_SOCKET_PATH"] = str(new_socket)
tip = cluster_obj.g_query.get_tip()
return tip
finally:
os.environ["CARDANO_NODE_SOCKET_PATH"] = orig_socket
def node_submit_tx(
self,
cluster_obj: clusterlib.ClusterLib,
node: str,
temp_template: str,
src_addr: clusterlib.AddressRecord,
dst_addr: clusterlib.AddressRecord,
) -> clusterlib.TxRawOutput:
"""Submit transaction on given node."""
orig_socket = os.environ.get("CARDANO_NODE_SOCKET_PATH")
assert orig_socket
new_socket = pl.Path(orig_socket).parent / f"{node}.socket"
curr_time = time.time()
destinations = [clusterlib.TxOut(address=dst_addr.address, amount=1_000_000)]
tx_files = clusterlib.TxFiles(signing_key_files=[src_addr.skey_file])
try:
os.environ["CARDANO_NODE_SOCKET_PATH"] = str(new_socket)
tx_raw_output = cluster_obj.g_transaction.send_tx(
src_address=src_addr.address,
tx_name=f"{temp_template}_{int(curr_time)}",
txouts=destinations,
tx_files=tx_files,
verify_tx=False,
)
return tx_raw_output
finally:
os.environ["CARDANO_NODE_SOCKET_PATH"] = orig_socket
def get_prometheus_metrics(self, port: int) -> requests.Response:
response = requests.get(f"http://localhost:{port}/metrics", timeout=10)
assert response, f"Request failed, status code {response.status_code}"
return response
def _node_synced(self, cluster_obj: clusterlib.ClusterLib, node: str) -> None:
sprogress = 0.0
old_sprogress = 0.0
for __ in range(5):
sprogress = float(self.node_get_tip(cluster_obj=cluster_obj, node=node)["syncProgress"])
if sprogress == 100:
break
if sprogress == old_sprogress:
msg = f"Cannot sync node2, sync progress: {sprogress}%"
raise AssertionError(msg)
old_sprogress = sprogress
time.sleep(2)
else:
msg = f"Cannot sync node2 in time, sync progress: {sprogress}%"
raise AssertionError(msg)
@allure.link(helpers.get_vcs_link())
@pytest.mark.skipif(not TEST_RECONNECT, reason="This is not a 'reconnect' testrun")
def test_reconnect(
self,
cluster_manager: cluster_management.ClusterManager,
cluster_singleton: clusterlib.ClusterLib,
payment_addrs: tp.List[clusterlib.AddressRecord],
):
"""Test that node reconnects after it was stopped.
* Stop the node2
* Submit Tx number 1 on node1
* Start the stopped node2
* Submit a Tx number 2 on node2
* Wait for 2 new blocks
* Check that node1 knows about Tx number 2, and/or node2 knows about Tx number 1
"""
cluster = cluster_singleton
temp_template = common.get_test_id(cluster)
node1 = "pool1"
node2 = "pool2"
def _assert(tx_outputs: tp.List[clusterlib.TxRawOutput]) -> None:
tx1_node2 = self.node_query_utxo(
cluster_obj=cluster, node=node2, tx_raw_output=tx_outputs[-2]
)
tx2_node1 = self.node_query_utxo(
cluster_obj=cluster, node=node1, tx_raw_output=tx_outputs[-1]
)
# If node1 knows about Tx number 2, and/or node2 knows about Tx number 1,
# the connection must have been established.
assert (
tx2_node1 or tx1_node2
), f"Connection failed?\ntx1_node2: {tx2_node1}\ntx2_node1: {tx2_node1}"
with cluster_manager.respin_on_failure():
for restart_no in range(1, 11):
LOGGER.info(f"Running restart number {restart_no}")
tx_outputs = []
# Stop the node2
cluster_nodes.stop_nodes([node2])
# Submit a Tx number 1 on the node1
tx_outputs.append(
self.node_submit_tx(
cluster_obj=cluster,
node=node1,
temp_template=f"{temp_template}_{restart_no}_node1",
src_addr=payment_addrs[0],
dst_addr=payment_addrs[0],
)
)
# Start the node2
cluster_nodes.start_nodes([node2])
time.sleep(5)
self._node_synced(cluster_obj=cluster, node=node2)
# Submit a Tx number 2 on the node2
tx_outputs.append(
self.node_submit_tx(
cluster_obj=cluster,
node=node2,
temp_template=f"{temp_template}_{restart_no}_node2",
src_addr=payment_addrs[1],
dst_addr=payment_addrs[1],
)
)
for check_no in range(1, 3):
cluster.wait_for_new_block(new_blocks=1)
try:
_assert(tx_outputs=tx_outputs)
except AssertionError:
if check_no > 1:
raise
LOGGER.info(f"AssertionError on check {check_no}")
@allure.link(helpers.get_vcs_link())
@pytest.mark.skipif(
not TEST_METRICS_RECONNECT, reason="This is not a 'metrics reconnect' testrun"
)
@pytest.mark.skipif(configuration.NUM_POOLS != 3, reason="`NUM_POOLS` must be 3")
@pytest.mark.skipif(not configuration.ENABLE_P2P, reason="Works only with P2P topology")
def test_metrics_reconnect(
self,
cluster_manager: cluster_management.ClusterManager,
cluster_singleton: clusterlib.ClusterLib,
):
"""Test using metrics that node reconnects after it was restarted."""
cluster = cluster_singleton
common.get_test_id(cluster)
node2 = "pool2"
prometheus_port = (
cluster_nodes.get_cluster_type()
.cluster_scripts.get_instance_ports(cluster_nodes.get_instance_num())
.prometheus_pool2
)
def _assert() -> None:
response = self.get_prometheus_metrics(prometheus_port)
metrics_pairs = [m.split() for m in response.text.strip().split("\n")]
metrics = {m[0]: m[1] for m in metrics_pairs}
assert int(metrics["cardano_node_metrics_inboundGovernor_hot"]) > 1
assert int(metrics["cardano_node_metrics_peerSelection_cold"]) == 0
with cluster_manager.respin_on_failure():
for restart_no in range(1, 200):
LOGGER.info(f"Running restart number {restart_no}")
# Restart node2
cluster_nodes.restart_nodes([node2])
self._node_synced(cluster_obj=cluster, node=node2)
for check_no in range(1, 11):
try:
_assert()
except AssertionError:
if check_no == 10:
raise
LOGGER.info(f"AssertionError on check {check_no}")
time.sleep(5)