Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat: topos node init test #367

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/topos/src/components/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
);

Expand All @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion crates/topos/src/components/node/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) fn spawn_sequencer_process(
shutdown: (CancellationToken, mpsc::Sender<()>),
) -> JoinHandle<Result<(), Errors>> {
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,
Expand Down
4 changes: 2 additions & 2 deletions crates/topos/src/config/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -44,7 +44,7 @@ fn default_secrets_config() -> Option<String> {

impl BaseConfig {
pub fn need_tce(&self) -> bool {
self.subnet_id == "topos"
self.subnet == "topos"
}

pub fn need_sequencer(&self) -> bool {
Expand Down
87 changes: 87 additions & 0 deletions crates/topos/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,90 @@ async fn test_handle_command_init_with_custom_name() -> Result<(), Box<dyn std::

Ok(())
}

/// Test node init arguments precedence
/// • CLI flag should overwrite ENV variable
#[tokio::test]
async fn test_command_init_precedence() -> Result<(), Box<dyn std::error::Error>> {
// Test node init with env variables
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";
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
// Cli arguments should take precedence over env variables
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_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(())
}