From b9d2fb97c97cf13f516072a7e199292c92758a49 Mon Sep 17 00:00:00 2001 From: Gyubong Date: Wed, 9 Oct 2024 22:04:10 +0900 Subject: [PATCH] fix: Accidentally nested log directory config --- harness/src/raft.rs | 4 +--- harness/src/test_environment.rs | 27 +++++++++++++++------------ harness/src/utils.rs | 4 ++-- harness/tests/bootstrap.rs | 7 ++++--- harness/tests/data_replication.rs | 4 ++-- harness/tests/leader_election.rs | 6 +++--- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/harness/src/raft.rs b/harness/src/raft.rs index 3522421..3e1b99e 100644 --- a/harness/src/raft.rs +++ b/harness/src/raft.rs @@ -73,11 +73,9 @@ fn run_raft( let store = HashStore::new(); let logger = build_file_logger(base_storage_path); - let storage_pth = get_storage_path(cfg.get_log_dir(), *node_id); - ensure_directory_exist(storage_pth.as_str())?; let storage = HeedStorage::create( - &storage_pth, + &cfg.get_log_dir(), &cfg, Arc::new(Slogger { slog: logger.clone(), diff --git a/harness/src/test_environment.rs b/harness/src/test_environment.rs index 58429f3..c6fa494 100644 --- a/harness/src/test_environment.rs +++ b/harness/src/test_environment.rs @@ -8,7 +8,7 @@ use std::{ }; lazy_static! { - pub static ref COUNTER: AtomicUsize = AtomicUsize::new(1); + static ref IP_COUNTER: AtomicUsize = AtomicUsize::new(1); } const COUNTER_FILE: &str = ".ip_counter"; @@ -21,23 +21,26 @@ pub struct TestEnvironment { pub base_storage_path: String, } -pub fn get_test_environment(test_name: &str) -> TestEnvironment { +pub fn prepare_test_environment(test_name: &str) -> TestEnvironment { let test_environment_path = format!("logs/{}", test_name); let test_environment_path = Path::new(&test_environment_path); // Remove if the previous test environment exists if test_environment_path.exists() { fs::remove_dir_all(test_environment_path) - .expect("Failed to remove previous logs directory"); + .expect("Failed to remove previous test environment"); } // Caution: Storing ip_counter in the tempdir result in different test binaries using duplicate loopback IP addresses, // so we store the ip_counter file in the harness/logs directory. - get_test_environment_with_counter_file(test_name, COUNTER_FILE) + prepare_test_environment_with_counter_file(test_name, COUNTER_FILE) } // Function with adjustable counter file path for testing -fn get_test_environment_with_counter_file(test_name: &str, counter_file: &str) -> TestEnvironment { +fn prepare_test_environment_with_counter_file( + test_name: &str, + counter_file: &str, +) -> TestEnvironment { let path = Path::new(counter_file); let mut file = OpenOptions::new() @@ -123,11 +126,11 @@ mod tests { write!(file, "{}", MAX_COUNTER - 1).expect("Failed to write to counter file"); // First call: should return last IP address 127.255.255.254 - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.255.255.254"); // Second call: counter wraps around, should return first IP address 127.0.0.1 - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.0.0.1"); } @@ -140,16 +143,16 @@ mod tests { let counter_file_str = counter_file_path.to_str().unwrap(); // First allocation - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.0.0.1"); assert_eq!(env.base_storage_path, "./logs/test_basic"); // Second allocation - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.0.0.2"); // Third allocation - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.0.0.3"); } @@ -167,7 +170,7 @@ mod tests { write!(file, "{}", counter_value).expect("Failed to write to counter file"); // First call: should return IP address 127.1.1.1 - let env = get_test_environment_with_counter_file(test_name, counter_file_str); + let env = prepare_test_environment_with_counter_file(test_name, counter_file_str); assert_eq!(env.loopback_address, "127.1.1.1"); } @@ -184,6 +187,6 @@ mod tests { let counter_file_str = counter_file_path.to_str().unwrap(); // Function call should panic - get_test_environment_with_counter_file(test_name, counter_file_str); + prepare_test_environment_with_counter_file(test_name, counter_file_str); } } diff --git a/harness/src/utils.rs b/harness/src/utils.rs index 7d6b412..137b0b4 100644 --- a/harness/src/utils.rs +++ b/harness/src/utils.rs @@ -140,7 +140,7 @@ mod tests { use crate::{ state_machine::{HashStore, LogEntry}, - test_environment::get_test_environment, + test_environment::prepare_test_environment, utils::{gather_rafts_when_leader_elected, load_peers}, }; @@ -153,7 +153,7 @@ mod tests { use std::sync::mpsc; let test_environment = - get_test_environment(stringify!(test_gather_rafts_when_leader_elected)); + prepare_test_environment(stringify!(test_gather_rafts_when_leader_elected)); let (tx_raft, rx_raft) = mpsc::channel::<(u64, Raft)>(); let peers = load_peers(&test_environment.loopback_address, FIVE_NODE_EXAMPLE) diff --git a/harness/tests/bootstrap.rs b/harness/tests/bootstrap.rs index e3c5586..c527584 100644 --- a/harness/tests/bootstrap.rs +++ b/harness/tests/bootstrap.rs @@ -2,7 +2,7 @@ use harness::constant::RAFT_PORTS; use std::{process::exit, sync::mpsc, time::Duration}; use tokio::time::sleep; -use harness::test_environment::get_test_environment; +use harness::test_environment::prepare_test_environment; use harness::{ constant::{ONE_NODE_EXAMPLE, THREE_NODE_EXAMPLE}, raft::{build_raft_cluster, spawn_and_join_extra_node, wait_until_rafts_ready, Raft}, @@ -11,7 +11,7 @@ use harness::{ #[tokio::test] pub async fn test_static_bootstrap() { - let test_environment = get_test_environment(stringify!(test_static_bootstrap)); + let test_environment = prepare_test_environment(stringify!(test_static_bootstrap)); let (tx_raft, rx_raft) = mpsc::channel::<(u64, Raft)>(); @@ -34,7 +34,8 @@ pub async fn test_static_bootstrap() { #[tokio::test] pub async fn test_dynamic_bootstrap() { - let test_environment = get_test_environment(stringify!(test_dynamic_bootstrap)); + assert!(false); + let test_environment = prepare_test_environment(stringify!(test_dynamic_bootstrap)); let (tx_raft, rx_raft) = mpsc::channel::<(u64, Raft)>(); diff --git a/harness/tests/data_replication.rs b/harness/tests/data_replication.rs index 8b65cd5..45c6e46 100644 --- a/harness/tests/data_replication.rs +++ b/harness/tests/data_replication.rs @@ -6,13 +6,13 @@ use harness::{ constant::{RAFT_PORTS, THREE_NODE_EXAMPLE}, raft::{build_raft_cluster, spawn_and_join_extra_node, wait_until_rafts_ready, Raft}, state_machine::LogEntry, - test_environment::get_test_environment, + test_environment::prepare_test_environment, utils::load_peers, }; #[tokio::test] pub async fn test_data_replication() { - let test_environment = get_test_environment(stringify!(test_data_replication)); + let test_environment = prepare_test_environment(stringify!(test_data_replication)); let peers = load_peers(&test_environment.loopback_address, THREE_NODE_EXAMPLE) .await diff --git a/harness/tests/leader_election.rs b/harness/tests/leader_election.rs index 4d809ac..c5ed488 100644 --- a/harness/tests/leader_election.rs +++ b/harness/tests/leader_election.rs @@ -4,7 +4,7 @@ use std::{process::exit, sync::mpsc}; use harness::{ constant::{FIVE_NODE_EXAMPLE, THREE_NODE_EXAMPLE}, raft::{build_raft_cluster, wait_until_rafts_ready, Raft}, - test_environment::{get_test_environment, TestEnvironment}, + test_environment::{prepare_test_environment, TestEnvironment}, utils::{gather_rafts_when_leader_elected, load_peers}, }; @@ -94,7 +94,7 @@ async fn run_leader_election_test( #[tokio::test] pub async fn test_leader_election_in_five_node_cluster() { run_leader_election_test( - get_test_environment(stringify!(test_leader_election_in_five_node_cluster)), + prepare_test_environment(stringify!(test_leader_election_in_five_node_cluster)), FIVE_NODE_EXAMPLE, 2, ) @@ -104,7 +104,7 @@ pub async fn test_leader_election_in_five_node_cluster() { #[tokio::test] pub async fn test_leader_election_in_three_node_cluster() { run_leader_election_test( - get_test_environment(stringify!(test_leader_election_in_three_node_cluster)), + prepare_test_environment(stringify!(test_leader_election_in_three_node_cluster)), THREE_NODE_EXAMPLE, 1, )