Fixing test indeterminacy

This commit is contained in:
Dave Hrycyszyn
2024-06-13 11:34:41 +01:00
parent 7bb672f4b8
commit 481b041554
3 changed files with 42 additions and 23 deletions

10
Cargo.lock generated
View File

@@ -2065,6 +2065,7 @@ dependencies = [
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"url", "url",
"uuid",
] ]
[[package]] [[package]]
@@ -2527,6 +2528,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom 0.2.15",
]
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.0" version = "0.1.0"

View File

@@ -25,6 +25,8 @@ tracing-subscriber = "0.3.9"
toml = "0.8.14" toml = "0.8.14"
url = "2.2.2" url = "2.2.2"
[dev-dependencies]
uuid = { version = "1.8.0", features = ["v4"] }
[features] [features]
default = ["bft", "logging-list", "logging-json"] default = ["bft", "logging-list", "logging-json"]

View File

@@ -34,7 +34,7 @@ fn ensure_side_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs, path::Path}; use std::{fs, path::Path, str::FromStr};
use fastcrypto::{ use fastcrypto::{
ed25519::Ed25519KeyPair, ed25519::Ed25519KeyPair,
@@ -43,50 +43,57 @@ mod tests {
use super::*; use super::*;
fn default_side_node_config() -> SideNodeConfig { /// Generates a SideNodeConfig with a unique name for each test.
SideNodeConfig { /// This is necessary because the tests run in parallel and we
name: "alice".to_string(), /// don't want them to interfere with each other - without a unique
} /// name, the tests would all try to write to the same directory and we
/// get test indeterminacy
fn side_node_config() -> (SideNodeConfig, String) {
let name = format!("test-{}", uuid::Uuid::new_v4()).to_string();
(SideNodeConfig { name: name.clone() }, name)
} }
#[test] #[test]
fn creates_side_node_directory() { fn creates_side_node_directory() {
let mut test_home = PathBuf::new(); let (config, name) = side_node_config();
let side_dir = "/tmp/side"; let side_dir = format!("/tmp/side/{name}");
// clean up any previous test runs let mut test_home = PathBuf::new();
fs::remove_dir_all(side_dir).expect("couldn't remove side directory during test");
test_home.push(side_dir); test_home.push(side_dir);
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap(); let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
let _ = init(test_home.clone(), default_side_node_config()); let _ = init(test_home.clone(), config);
assert!(std::path::Path::new(node_dir).exists()); assert!(std::path::Path::new(node_dir).exists());
} }
#[test] #[test]
fn creates_key_file() { fn creates_key_file() {
let mut file_path = PathBuf::new(); let (config, name) = side_node_config();
file_path.push("/tmp/side"); let side_dir = format!("/tmp/side/{name}");
let side_dir = file_path.clone();
file_path.push(utils::KEY_FILE);
let _ = init(side_dir.clone(), default_side_node_config()); let mut key_file_path = PathBuf::new();
assert!(file_path.exists()); key_file_path.push(side_dir.clone());
key_file_path.push(utils::KEY_FILE);
let _ = init(PathBuf::from_str(&side_dir).unwrap(), config);
assert!(key_file_path.exists());
// check that the pem is readable // check that the pem is readable
let data = fs::read_to_string(file_path).expect("couldn't read key file"); let data = fs::read_to_string(key_file_path).expect("couldn't read key file");
let keys = Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file"); let keys = Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file");
assert_eq!(keys.public().as_bytes().len(), 32); assert_eq!(keys.public().as_bytes().len(), 32);
} }
#[test] #[test]
fn creates_config_file() { fn creates_config_file() {
let mut file_path = PathBuf::new(); let (config, name) = side_node_config();
file_path.push("/tmp/side"); let side_dir = format!("/tmp/side/{name}");
let side_dir = file_path.clone();
file_path.push(utils::CONFIG_FILE);
let _ = init(side_dir.clone(), default_side_node_config()); let mut config_file_path = PathBuf::new();
assert!(file_path.exists()); config_file_path.push(side_dir.clone());
config_file_path.push(utils::CONFIG_FILE);
let _ = init(PathBuf::from_str(&side_dir).unwrap(), config);
assert!(config_file_path.exists());
} }
} }