From b78e01d3ad78fc435f0724ba2e3305a2519eea40 Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Tue, 7 Nov 2023 13:08:14 +0100 Subject: [PATCH 1/4] feat: topos node init tests --- crates/topos/src/components/node/mod.rs | 6 +- crates/topos/src/components/node/services.rs | 2 +- crates/topos/src/config/base.rs | 4 +- crates/topos/tests/config.rs | 86 ++++++++++++++++++++ 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/crates/topos/src/components/node/mod.rs b/crates/topos/src/components/node/mod.rs index ee1ac1cf0..070a3bcc1 100644 --- a/crates/topos/src/components/node/mod.rs +++ b/crates/topos/src/components/node/mod.rs @@ -117,7 +117,7 @@ pub(crate) async fn handle_command( // Load genesis pointed by the local config let genesis = Genesis::new( home.join("subnet") - .join(config.base.subnet_id.clone()) + .join(config.base.subnet.clone()) .join("genesis.json"), ); @@ -129,7 +129,7 @@ pub(crate) async fn handle_command( info!( "🧢 New joiner: {} for the \"{}\" subnet as {:?}", - config.base.name, config.base.subnet_id, config.base.role + config.base.name, config.base.subnet, config.base.role ); let shutdown_token = CancellationToken::new(); @@ -179,7 +179,7 @@ pub(crate) async fn handle_command( } // TCE - if config.base.subnet_id == "topos" { + if config.base.subnet == "topos" { info!("Running topos TCE service...",); processes.push(services::spawn_tce_process( config.tce.clone().unwrap(), diff --git a/crates/topos/src/components/node/services.rs b/crates/topos/src/components/node/services.rs index 50c5c6f34..6526869b2 100644 --- a/crates/topos/src/components/node/services.rs +++ b/crates/topos/src/components/node/services.rs @@ -57,7 +57,7 @@ pub(crate) fn spawn_sequencer_process( shutdown: (CancellationToken, mpsc::Sender<()>), ) -> JoinHandle> { let config = SequencerConfiguration { - subnet_id: None, + subnet_id: config.subnet_id, public_key: keys.validator_pubkey(), subnet_jsonrpc_http: config.subnet_jsonrpc_http, subnet_jsonrpc_ws: config.subnet_jsonrpc_ws, diff --git a/crates/topos/src/config/base.rs b/crates/topos/src/config/base.rs index a8bf12182..f8092bfec 100644 --- a/crates/topos/src/config/base.rs +++ b/crates/topos/src/config/base.rs @@ -20,7 +20,7 @@ pub struct BaseConfig { pub role: NodeRole, #[serde(default = "default_subnet")] - pub subnet_id: String, + pub subnet: String, #[serde(default = "default_secrets_config")] pub secrets_config: Option, @@ -44,7 +44,7 @@ fn default_secrets_config() -> Option { impl BaseConfig { pub fn need_tce(&self) -> bool { - self.subnet_id == "topos" + self.subnet == "topos" } pub fn need_sequencer(&self) -> bool { diff --git a/crates/topos/tests/config.rs b/crates/topos/tests/config.rs index e0dd1acaa..ba47fb18a 100644 --- a/crates/topos/tests/config.rs +++ b/crates/topos/tests/config.rs @@ -123,3 +123,89 @@ async fn test_handle_command_init_with_custom_name() -> Result<(), Box Result<(), Box> { + // Test node init with env variables + let node_init_home_env = "/tmp/topos/test_command_init_name_precedence_env"; + let node_edge_path_env = polygon_edge_path(node_init_home_env).await; + let node_init_name_env = "TEST_NODE_ENV"; + let node_init_role_env = "full-node"; + let node_init_subnet_env = "topos-env"; + + let mut cmd = Command::cargo_bin("topos")?; + cmd.arg("node") + .env("TOPOS_POLYGON_EDGE_BIN_PATH", &node_edge_path_env) + .env("TOPOS_HOME", node_init_home_env) + .env("TOPOS_NODE_NAME", node_init_name_env) + .env("TOPOS_NODE_ROLE", node_init_role_env) + .env("TOPOS_NODE_SUBNET", node_init_subnet_env) + .arg("init"); + + let output = cmd.assert().success(); + let result: &str = std::str::from_utf8(&output.get_output().stdout)?; + + // Test node init with cli flags + assert!(result.contains("Created node config file")); + let home = PathBuf::from(node_init_home_env); + // Verification: check that the config file was created + let config_path = home + .join("node") + .join(node_init_name_env) + .join("config.toml"); + assert!(config_path.exists()); + // Check if config file params are according to env params + let config_contents = std::fs::read_to_string(&config_path).unwrap(); + assert!(config_contents.contains("name = \"TEST_NODE_ENV\"")); + assert!(config_contents.contains("role = \"fullnode\"")); + assert!(config_contents.contains("subnet = \"topos-env\"")); + std::fs::remove_dir_all(node_init_home_env)?; + + // Test node init with both cli and env flags + let node_init_home_cli = "/tmp/topos/test_command_init_name_precedence_cli"; + let node_edge_path_cli = polygon_edge_path(node_init_home_cli).await; + let node_init_name_cli = "TEST_NODE_CLI"; + let node_init_role_cli = "sequencer"; + let node_init_subnet_cli = "topos-cli"; + + let mut cmd = Command::cargo_bin("topos")?; + cmd.arg("node") + .env("TOPOS_POLYGON_EDGE_BIN_PATH", &node_edge_path_env) + .env("TOPOS_HOME", node_init_home_env) + .env("TOPOS_NODE_NAME", node_init_name_env) + .env("TOPOS_NODE_ROLE", node_init_role_env) + .env("TOPOS_NODE_SUBNET", node_init_subnet_env) + .arg("--edge-path") + .arg(node_edge_path_cli) + .arg("init") + .arg("--name") + .arg(node_init_name_cli) + .arg("--home") + .arg(node_init_home_cli) + .arg("--role") + .arg(node_init_role_cli) + .arg("--subnet") + .arg(node_init_subnet_cli); + + let output = cmd.assert().success(); + let result: &str = std::str::from_utf8(&output.get_output().stdout)?; + + assert!(result.contains("Created node config file")); + let home = PathBuf::from(node_init_home_cli); + // Verification: check that the config file was created + let config_path = home + .join("node") + .join(node_init_name_cli) + .join("config.toml"); + assert!(config_path.exists()); + // Check if config file params are according to cli params + let config_contents = std::fs::read_to_string(&config_path).unwrap(); + assert!(config_contents.contains("name = \"TEST_NODE_CLI\"")); + assert!(config_contents.contains("role = \"sequencer\"")); + assert!(config_contents.contains("subnet = \"topos-cli\"")); + std::fs::remove_dir_all(node_init_home_cli)?; + + Ok(()) +} From 37f450272832fdf182822b9e7098fef5d8485f4a Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Wed, 8 Nov 2023 12:18:44 +0100 Subject: [PATCH 2/4] fix: minor --- crates/topos/tests/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/topos/tests/config.rs b/crates/topos/tests/config.rs index ba47fb18a..9665dc020 100644 --- a/crates/topos/tests/config.rs +++ b/crates/topos/tests/config.rs @@ -164,6 +164,7 @@ async fn test_command_init_name_precedence() -> Result<(), Box Date: Wed, 8 Nov 2023 12:21:16 +0100 Subject: [PATCH 3/4] fix: rename --- crates/topos/tests/config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/topos/tests/config.rs b/crates/topos/tests/config.rs index 9665dc020..363e4bdbf 100644 --- a/crates/topos/tests/config.rs +++ b/crates/topos/tests/config.rs @@ -127,9 +127,9 @@ async fn test_handle_command_init_with_custom_name() -> Result<(), Box Result<(), Box> { +async fn test_command_init_precedence() -> Result<(), Box> { // Test node init with env variables - let node_init_home_env = "/tmp/topos/test_command_init_name_precedence_env"; + let node_init_home_env = "/tmp/topos/test_command_init_precedence_env"; let node_edge_path_env = polygon_edge_path(node_init_home_env).await; let node_init_name_env = "TEST_NODE_ENV"; let node_init_role_env = "full-node"; @@ -165,7 +165,7 @@ async fn test_command_init_name_precedence() -> Result<(), Box Date: Wed, 8 Nov 2023 14:47:28 +0100 Subject: [PATCH 4/4] fix: review --- Cargo.lock | 1 + crates/topos/Cargo.toml | 1 + crates/topos/tests/config.rs | 27 ++++++++++++++++++++------- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f4ea92f4..0e4c3e69e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7352,6 +7352,7 @@ dependencies = [ "serde", "serde_json", "tar", + "tempfile", "test-log", "thiserror", "tokio", diff --git a/crates/topos/Cargo.toml b/crates/topos/Cargo.toml index 71dbc080e..06001077d 100644 --- a/crates/topos/Cargo.toml +++ b/crates/topos/Cargo.toml @@ -65,6 +65,7 @@ libp2p = { workspace = true, features = ["identify"] } assert_cmd = "2.0.6" insta = { version = "1.21", features = ["json", "redactions"] } rstest = { workspace = true, features = ["async-timeout"] } +tempfile = "3.8.0" [features] default = ["tce", "sequencer", "network", "node", "setup", "subnet"] diff --git a/crates/topos/tests/config.rs b/crates/topos/tests/config.rs index 363e4bdbf..9da6bdefe 100644 --- a/crates/topos/tests/config.rs +++ b/crates/topos/tests/config.rs @@ -1,6 +1,7 @@ use assert_cmd::prelude::*; use std::path::PathBuf; use std::process::Command; +use tempfile::tempdir; use topos::install_polygon_edge; async fn polygon_edge_path(path: &str) -> String { @@ -124,12 +125,13 @@ async fn test_handle_command_init_with_custom_name() -> Result<(), Box Result<(), Box> { +async fn test_command_init_precedence_env() -> Result<(), Box> { + let tmp_home_directory = tempdir()?; + // Test node init with env variables - let node_init_home_env = "/tmp/topos/test_command_init_precedence_env"; + let node_init_home_env = tmp_home_directory.path().to_str().unwrap(); let node_edge_path_env = polygon_edge_path(node_init_home_env).await; let node_init_name_env = "TEST_NODE_ENV"; let node_init_role_env = "full-node"; @@ -161,12 +163,24 @@ async fn test_command_init_precedence() -> Result<(), Box assert!(config_contents.contains("name = \"TEST_NODE_ENV\"")); assert!(config_contents.contains("role = \"fullnode\"")); assert!(config_contents.contains("subnet = \"topos-env\"")); - std::fs::remove_dir_all(node_init_home_env)?; + + Ok(()) +} + +/// Test node cli arguments precedence over env arguments +#[tokio::test] +async fn test_command_init_precedence_cli_env() -> Result<(), Box> { + let tmp_home_dir = tempdir()?; // Test node init with both cli and env flags // Cli arguments should take precedence over env variables + let node_init_home_env = tmp_home_dir.path().to_str().unwrap(); + let node_edge_path_env = polygon_edge_path(node_init_home_env).await; + let node_init_name_env = "TEST_NODE_ENV"; + let node_init_role_env = "full-node"; + let node_init_subnet_env = "topos-env"; let node_init_home_cli = "/tmp/topos/test_command_init_precedence_cli"; - let node_edge_path_cli = polygon_edge_path(node_init_home_cli).await; + let node_edge_path_cli = node_edge_path_env.clone(); let node_init_name_cli = "TEST_NODE_CLI"; let node_init_role_cli = "sequencer"; let node_init_subnet_cli = "topos-cli"; @@ -206,7 +220,6 @@ async fn test_command_init_precedence() -> Result<(), Box assert!(config_contents.contains("name = \"TEST_NODE_CLI\"")); assert!(config_contents.contains("role = \"sequencer\"")); assert!(config_contents.contains("subnet = \"topos-cli\"")); - std::fs::remove_dir_all(node_init_home_cli)?; Ok(()) }