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 all 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/topos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
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
100 changes: 100 additions & 0 deletions crates/topos/tests/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -123,3 +124,102 @@ async fn test_handle_command_init_with_custom_name() -> Result<(), Box<dyn std::

Ok(())
}

/// Test node init env arguments
#[tokio::test]
async fn test_command_init_precedence_env() -> Result<(), Box<dyn std::error::Error>> {
let tmp_home_directory = tempdir()?;

// Test node init with env variables
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";
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\""));

Ok(())
}

/// Test node cli arguments precedence over env arguments
#[tokio::test]
async fn test_command_init_precedence_cli_env() -> Result<(), Box<dyn std::error::Error>> {
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 = 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";

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\""));

Ok(())
}