Skip to content

Commit

Permalink
fix: Accidentally nested log directory config
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Oct 9, 2024
1 parent bdc900d commit b9d2fb9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
4 changes: 1 addition & 3 deletions harness/src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
27 changes: 15 additions & 12 deletions harness/src/test_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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()
Expand Down Expand Up @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions harness/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions harness/tests/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)>();

Expand All @@ -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)>();

Expand Down
4 changes: 2 additions & 2 deletions harness/tests/data_replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions harness/tests/leader_election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down

0 comments on commit b9d2fb9

Please sign in to comment.