141 lines
6.3 KiB
Python
Raw Normal View History

import inspect
from src.libs.custom_logger import get_custom_logger
from time import time
2023-11-01 14:02:29 +02:00
import pytest
2023-11-01 16:44:42 +02:00
import allure
from src.libs.common import to_base64, delay
from src.node.waku_message import WakuMessage
chore: RLN registration support (#16) * chore: parameters and volumes for RLN relay * chore: add startup test for RLN * fix: remove redundant rln_enabled from WakuNode * fix: - and _ magic in rln parameters * fix: key 'eth_testnet_key' -> 'eth_client_private_key' * fix: rln-register_only -> rln-register-only * fix: use extend instead of append for the volumes list * fix: use absolute path for the volumes names - mark volumes shared across containers * fix: remove :z attr from volumes * fix: remove filename from volume path * fix: remove request for ENR * test: plain RLN registration * fix: remove subscribe * fix: remove ENR related params * test: add run_container func to Docker Mananger * fix: remove run_container func from Docker Mananger - no need for exec * fix: pass exec commands instead of specialized docker exec func * fix: exclude RLN arguments from others * fix: separate RLN volumes by implementation * test: registration with nwaku * test: registration over existing credentials * test: add RLN Relay node startup * test: RLN credentials reuse for relay node startup * fix: clean up unnecessary commands * fix: clean up unnecessary commands for non RLN relay too * test: regression by sending one valid message * fix: add forgotten call to get enr_uri * fix: check RLN credentials set properly * fix: parenthesis in eval expression * fix: better check for RLN credentials * fix: update to new flags - gowaku - separate private key for go/nwaku * test: register RLN * fix: RLN credentials check * fix: remove enable rln-relay flag from registration * test: reorder commands to check go-waku registration * fix: restructure if statements for RLN registration * fix: different set of flag for RLN registration and operation * fix: forgotten "eth" in eth-contract-address * fix: remove redundant None from get_env_var call * fix: refactor rln credentials parsing from start function * fix: missed second return value * test: call to parse_rln_credentials * fix: remove redundant self in call to parse_rln_credentials * fix: remove rln related values if not valid * fix: refactored to accept multiple private keys - JSON source for RLN credentials - removed go-waku support for RLN * test: register RLN with 2 nwaku nodes * fix: missing open file, key errors * fix: return effective keystore_path * fix: cleanup unused env variables * fix: cleanup unused env variables from relay * fix: wait longer for credentials file to be written to disk * test: remove waiting for credentials file to be written to disk * fix: add select_private_key() * fix: merge parse_rln_credentials and parse_rln_registration_credentials * test: wait for filesystem cache * test: try with os.sync to flush cache * test: stop container to clear cache * fix: 15 sec wait + stop container to clear cache * test: RLN registration test with fixture and cred file check * test: added allure.step - RLN registration for single node - corresponding test to register all keys * fix: f-strings in the test * fix: sync naming for register_main_rln_relay_nodes * fix: add WARN message to log unset and expected RLN credentials * fix: pytest.skip added to exit tests when non nwaku image is used * fix: swap NODE2 for DEFAULT_NWAKU * fix: let rln_credential_store_ready to raise exception for retry * fix: let register_rln to raise exception too, when credential fine still not available * fix: remove container stop * fix: tune down retry timeouts * fix: remove unnecessary f-string * test: reduce unnecessary variables init * test: undo reduce unnecessary variables init * test: increase timeout for rln_credential_store_ready * test: refactor kwargs handling into sanitize_docker_flags * fix: created RLN registration check - changed rln_credential_store_ready to function * fix: delete unnecessary ports from register_rln init * fix: remove wait for registration entirely * test: RLN_CREDENTIALS env var example * fix: check_rln_registration to raise exception * fix: use f-string at check_rln_registration * fix: add gen_step_id function * fix: RLN_CREDENTIALS as loaded from .env * fix: RLN_CREDENTIALS example, skipping test if not set * test: RLN with actions * fix: tune up RLN timeouts for Github runners * test: filesystem write to / * fix: change RLN data to be stored at working directory * fix: catch exception instead of "if not" - print out container log * fix: wrap logs into debug msg * fix: print log file * fix: remove additional logging * fix: exit RLN cred parsing sooner when not used - delete proper keys from default_args * fix: Allure report * fix: Discord notifications * fix: remove f-string * fix: delete test workflow
2024-02-13 22:01:30 +08:00
from src.env_vars import (
NODE_1,
NODE_2,
ADDITIONAL_NODES,
NODEKEY,
)
from src.node.waku_node import WakuNode
2023-11-01 14:02:29 +02:00
from tenacity import retry, stop_after_delay, wait_fixed
from src.test_data import VALID_PUBSUB_TOPICS
2023-11-01 14:02:29 +02:00
logger = get_custom_logger(__name__)
2023-11-01 14:02:29 +02:00
class StepsRelay:
test_pubsub_topic = VALID_PUBSUB_TOPICS[1]
test_content_topic = "/test/1/waku-relay/proto"
test_payload = "Relay works!!"
@pytest.fixture(scope="function", autouse=True)
def relay_setup(self):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.main_nodes = []
self.optional_nodes = []
@pytest.fixture(scope="function")
def setup_main_relay_nodes(self, request):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.node1 = WakuNode(NODE_1, f"node1_{request.cls.test_id}")
self.node1.start(relay="true", nodekey=NODEKEY)
self.enr_uri = self.node1.get_enr_uri()
self.multiaddr_with_id = self.node1.get_multiaddr_with_id()
self.node2 = WakuNode(NODE_2, f"node2_{request.cls.test_id}")
self.node2.start(relay="true", discv5_bootstrap_node=self.enr_uri)
if self.node2.is_nwaku():
self.node2.add_peers([self.multiaddr_with_id])
self.main_nodes.extend([self.node1, self.node2])
@pytest.fixture(scope="function")
def setup_optional_relay_nodes(self, request):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
if ADDITIONAL_NODES:
nodes = [node.strip() for node in ADDITIONAL_NODES.split(",")]
else:
pytest.skip("ADDITIONAL_NODES is empty, cannot run test")
for index, node in enumerate(nodes):
node = WakuNode(node, f"node{index + 3}_{request.cls.test_id}")
node.start(relay="true", discv5_bootstrap_node=self.enr_uri)
if node.is_nwaku():
node.add_peers([self.multiaddr_with_id])
self.optional_nodes.append(node)
@pytest.fixture(scope="function")
def subscribe_main_relay_nodes(self):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.ensure_relay_subscriptions_on_nodes(self.main_nodes, [self.test_pubsub_topic])
2023-11-01 14:02:29 +02:00
@pytest.fixture(scope="function")
def subscribe_optional_relay_nodes(self):
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
self.ensure_relay_subscriptions_on_nodes(self.optional_nodes, [self.test_pubsub_topic])
@pytest.fixture(scope="function")
def relay_warm_up(self):
try:
self.wait_for_published_message_to_reach_relay_peer()
logger.info("WARM UP successful!!")
except Exception as ex:
raise TimeoutError(f"WARM UP FAILED WITH: {ex}")
# this method should be used only for the tests that use the relay_warm_up fixture
# otherwise use wait_for_published_message_to_reach_relay_peer
2023-11-01 16:44:42 +02:00
@allure.step
def check_published_message_reaches_relay_peer(self, message=None, pubsub_topic=None, message_propagation_delay=0.1, sender=None, peer_list=None):
if message is None:
message = self.create_message()
if pubsub_topic is None:
pubsub_topic = self.test_pubsub_topic
if not sender:
sender = self.node1
if not peer_list:
peer_list = self.main_nodes + self.optional_nodes
sender.send_relay_message(message, pubsub_topic)
delay(message_propagation_delay)
for index, peer in enumerate(peer_list):
logger.debug(f"Checking that peer NODE_{index + 1}:{peer.image} can find the published message")
get_messages_response = peer.get_relay_messages(pubsub_topic)
assert get_messages_response, f"Peer NODE_{index + 1}:{peer.image} couldn't find any messages"
assert len(get_messages_response) == 1, f"Expected 1 message but got {len(get_messages_response)}"
waku_message = WakuMessage(get_messages_response)
waku_message.assert_received_message(message)
@allure.step
def check_publish_without_relay_subscription(self, pubsub_topic):
try:
self.node1.send_relay_message(self.create_message(), pubsub_topic)
raise AssertionError("Publish with no subscription worked!!!")
except Exception as ex:
assert "Bad Request" in str(ex) or "Internal Server Error" in str(ex)
# we need much bigger timeout in CI because we run tests in parallel there and the machine itself is slower
@allure.step
def wait_for_published_message_to_reach_relay_peer(
self, timeout_duration=120, time_between_retries=1, pubsub_topic=None, sender=None, peer_list=None
):
@retry(stop=stop_after_delay(timeout_duration), wait=wait_fixed(time_between_retries), reraise=True)
2024-03-05 08:55:35 +02:00
def publish_and_check_relay_peer():
message = {"payload": to_base64(self.test_payload), "contentTopic": self.test_content_topic, "timestamp": int(time() * 1e9)}
self.check_published_message_reaches_relay_peer(message, pubsub_topic=pubsub_topic, sender=sender, peer_list=peer_list)
2024-03-05 08:55:35 +02:00
publish_and_check_relay_peer()
@allure.step
def ensure_relay_subscriptions_on_nodes(self, node_list, pubsub_topic_list):
for node in node_list:
node.set_relay_subscriptions(pubsub_topic_list)
@allure.step
def delete_relay_subscriptions_on_nodes(self, node_list, pubsub_topic_list):
for node in node_list:
node.delete_relay_subscriptions(pubsub_topic_list)
@allure.step
def create_message(self, **kwargs):
message = {"payload": to_base64(self.test_payload), "contentTopic": self.test_content_topic, "timestamp": int(time() * 1e9)}
message.update(kwargs)
return message
@allure.step
@retry(stop=stop_after_delay(120), wait=wait_fixed(1), reraise=True)
def subscribe_and_publish_with_retry(self, node_list, pubsub_topic_list):
self.ensure_relay_subscriptions_on_nodes(node_list, pubsub_topic_list)
self.check_published_message_reaches_relay_peer()