From 10dd608cde825453d9ca7ab19dc9ea2a66bca2f4 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 19 Dec 2024 16:02:57 +0800 Subject: [PATCH] test: node start --- src/env_vars.py | 24 ++++++-------------- src/node/node_vars.py | 15 +++++++++++++ src/node/nomos_node.py | 38 ++++++++++++++++++++++++++++++-- tests/data_integrity/__init__.py | 0 tests/e2e/__init__.py | 0 tests/e2e/test_2node_alive.py | 14 ++++++++++++ 6 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 src/node/node_vars.py create mode 100644 tests/data_integrity/__init__.py create mode 100644 tests/e2e/__init__.py create mode 100644 tests/e2e/test_2node_alive.py diff --git a/src/env_vars.py b/src/env_vars.py index 6f1488b..78c051d 100644 --- a/src/env_vars.py +++ b/src/env_vars.py @@ -15,25 +15,15 @@ def get_env_var(var_name, default=None): # Configuration constants. Need to be upercase to appear in reports -DEFAULT_NOMOS = { - "image": "nomos:latest", - "volumes": "./testnet:/etc/nomos,./tests/kzgrs/kzgrs_test_params:/kzgrs_test_params:z", - "ports": "3000/udp,18080/tcp", - "entrypoint": "/etc/nomos/scripts/run_nomos_node.sh", -} -NOMOS_EXECUTOR = { - "image": "nomos:latest", - "volumes": "./testnet:/etc/nomos,./tests/kzgrs/kzgrs_test_params:/kzgrs_test_params:z", - "ports": "3000/udp,18080/tcp", - "entrypoint": "/etc/nomos/scripts/run_nomos_executor.sh", -} +NOMOS = "nomos" +NOMOS_EXECUTOR = "nomos_executor" +CFGSYNC = "cfgsync" -CFGSYNC = {"image": "nomos:latest", "volumes": "./testnet:/etc/nomos", "ports": "", "entrypoint": "/etc/nomos/scripts/run_cfgsync.sh"} - -CFGSYNC = get_env_var("CFGSYNC", CFGSYNC) -NODE_1 = get_env_var("NODE_1", DEFAULT_NOMOS) +NODE_1 = get_env_var("NODE_1", NOMOS) NODE_2 = get_env_var("NODE_2", NOMOS_EXECUTOR) -ADDITIONAL_NODES = get_env_var("ADDITIONAL_NODES", f"{DEFAULT_NOMOS},{DEFAULT_NOMOS}") +NODE_3 = get_env_var("NODE_3", CFGSYNC) + +ADDITIONAL_NODES = get_env_var("ADDITIONAL_NODES", f"{NOMOS},{NOMOS}") # more nodes need to follow the NODE_X pattern DOCKER_LOG_DIR = get_env_var("DOCKER_LOG_DIR", "./log/docker") NETWORK_NAME = get_env_var("NETWORK_NAME", "nomos") diff --git a/src/node/node_vars.py b/src/node/node_vars.py new file mode 100644 index 0000000..3aad7f1 --- /dev/null +++ b/src/node/node_vars.py @@ -0,0 +1,15 @@ +nomos_nodes = { + "nomos": { + "image": "nomos:latest", + "volumes": ["./testnet:/etc/nomos", "./tests/kzgrs/kzgrs_test_params:/kzgrs_test_params:z"], + "ports": ["3000/udp", "18080/tcp"], + "entrypoint": "/etc/nomos/scripts/run_nomos_node.sh", + }, + "nomos_executor": { + "image": "nomos:latest", + "volumes": ["./testnet:/etc/nomos", "./tests/kzgrs/kzgrs_test_params:/kzgrs_test_params:z"], + "ports": ["3000/udp", "18080/tcp"], + "entrypoint": "/etc/nomos/scripts/run_nomos_executor.sh", + }, + "cfgsync": {"image": "nomos:latest", "volumes": ["./testnet:/etc/nomos"], "ports": "", "entrypoint": "/etc/nomos/scripts/run_cfgsync.sh"}, +} diff --git a/src/node/nomos_node.py b/src/node/nomos_node.py index 246106d..3311af0 100644 --- a/src/node/nomos_node.py +++ b/src/node/nomos_node.py @@ -1,10 +1,13 @@ +import json import os from src.libs.custom_logger import get_custom_logger from tenacity import retry, stop_after_delay, wait_fixed + from src.node.api_clients.rest import REST from src.node.docker_mananger import DockerManager from src.env_vars import DOCKER_LOG_DIR +from src.node.node_vars import nomos_nodes logger = get_custom_logger(__name__) @@ -19,8 +22,13 @@ def sanitize_docker_flags(input_flags): class NomosNode: - def __init__(self, docker_image, docker_log_prefix=""): - self._image_name = docker_image + def __init__(self, node_type, docker_log_prefix=""): + print(nomos_nodes) + self._image_name = nomos_nodes[node_type]["image"] + self._internal_ports = nomos_nodes[node_type]["ports"] + self._volumes = nomos_nodes[node_type]["volumes"] + self._entrypoint = nomos_nodes[node_type]["entrypoint"] + self._log_path = os.path.join(DOCKER_LOG_DIR, f"{docker_log_prefix}__{self._image_name.replace('/', '_')}.log") self._docker_manager = DockerManager(self._image_name) self._container = None @@ -29,3 +37,29 @@ class NomosNode: @retry(stop=stop_after_delay(60), wait=wait_fixed(0.1), reraise=True) def start(self, wait_for_node_sec=20, **kwargs): logger.debug("Starting Node...") + self._docker_manager.create_network() + self._ext_ip = self._docker_manager.generate_random_ext_ip() + self._ports = self._docker_manager.generate_ports(count=len(self._internal_ports)) + self._udp_port = self._ports[0] + self._tcp_port = self._ports[1] + self._api = REST(self._tcp_port) + + default_args = { + "listen-address": "0.0.0.0", + "log-level": "info", + "nat": f"extip:{self._ext_ip}", + } + + logger.debug(f"Using volumes {self._volumes}") + + self._container = self._docker_manager.start_container( + self._docker_manager.image, + ports=self._ports, + args=default_args, + log_path=self._log_path, + container_ip=self._ext_ip, + volumes=self._volumes, + remove_container=True, + ) + + logger.debug(f"Started container from image {self._image_name}") diff --git a/tests/data_integrity/__init__.py b/tests/data_integrity/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/e2e/__init__.py b/tests/e2e/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/e2e/test_2node_alive.py b/tests/e2e/test_2node_alive.py new file mode 100644 index 0000000..a4628f7 --- /dev/null +++ b/tests/e2e/test_2node_alive.py @@ -0,0 +1,14 @@ +from src.env_vars import CFGSYNC, NODE_1, NODE_2 +from src.node.nomos_node import NomosNode + + +class Test2NodeClAlive: + def test_cluster_start(self): + + self.node1 = NomosNode(CFGSYNC, f"node1_{1}") + self.node2 = NomosNode(NODE_1, f"node2_{2}") + self.node3 = NomosNode(NODE_2, f"node3_{3}") + + self.node1.start() + self.node2.start() + self.node3.start()