diff --git a/tests/src/nodes/mixnode.rs b/tests/src/nodes/mixnode.rs index 74678364..87213c1b 100644 --- a/tests/src/nodes/mixnode.rs +++ b/tests/src/nodes/mixnode.rs @@ -4,8 +4,10 @@ use std::{ time::Duration, }; +use super::{create_tempdir, persist_tempdir, LOGS_PREFIX}; use mixnet_node::{MixnetNodeConfig, PRIVATE_KEY_SIZE}; use mixnet_topology::{Layer, MixnetTopology, Node}; +use nomos_log::{LoggerBackend, LoggerFormat}; use rand::{thread_rng, RngCore}; use tempfile::NamedTempFile; @@ -14,21 +16,37 @@ use crate::{get_available_port, MixnetConfig}; const MIXNODE_BIN: &str = "../target/debug/mixnode"; pub struct MixNode { + _tempdir: tempfile::TempDir, child: Child, } impl Drop for MixNode { fn drop(&mut self) { - self.child.kill().unwrap(); + if std::thread::panicking() { + if let Err(e) = persist_tempdir(&mut self._tempdir, "mixnode") { + println!("failed to persist tempdir: {e}"); + } + } + + if let Err(e) = self.child.kill() { + println!("failed to kill the child process: {e}"); + } } } impl MixNode { pub async fn spawn(config: MixnetNodeConfig) -> Self { - let config = mixnode::Config { + let dir = create_tempdir().unwrap(); + + let mut config = mixnode::Config { mixnode: config, log: Default::default(), }; + config.log.backend = LoggerBackend::File { + directory: dir.path().to_owned(), + prefix: Some(LOGS_PREFIX.into()), + }; + config.log.format = LoggerFormat::Json; let mut file = NamedTempFile::new().unwrap(); let config_path = file.path().to_owned(); @@ -43,7 +61,10 @@ impl MixNode { //TODO: use a sophisticated way to wait until the node is ready tokio::time::sleep(Duration::from_secs(1)).await; - Self { child } + Self { + _tempdir: dir, + child, + } } pub async fn spawn_nodes(num_nodes: usize) -> (Vec, MixnetConfig) { diff --git a/tests/src/nodes/mod.rs b/tests/src/nodes/mod.rs index 05c24ae1..bc2a9e61 100644 --- a/tests/src/nodes/mod.rs +++ b/tests/src/nodes/mod.rs @@ -3,3 +3,26 @@ pub mod nomos; pub use self::mixnode::MixNode; pub use nomos::NomosNode; +use tempfile::TempDir; + +const LOGS_PREFIX: &str = "__logs"; + +fn create_tempdir() -> std::io::Result { + // It's easier to use the current location instead of OS-default tempfile location + // because Github Actions can easily access files in the current location using wildcard + // to upload them as artifacts. + tempfile::TempDir::new_in(std::env::current_dir()?) +} + +fn persist_tempdir(tempdir: &mut TempDir, label: &str) -> std::io::Result<()> { + println!( + "{}: persisting directory at {}", + label, + tempdir.path().display() + ); + // we need ownership of the dir to persist it + let dir = std::mem::replace(tempdir, tempfile::tempdir()?); + // a bit confusing but `into_path` persists the directory + let _ = dir.into_path(); + Ok(()) +} diff --git a/tests/src/nodes/nomos.rs b/tests/src/nodes/nomos.rs index af656493..b99c0e61 100644 --- a/tests/src/nodes/nomos.rs +++ b/tests/src/nodes/nomos.rs @@ -3,6 +3,7 @@ use std::net::SocketAddr; use std::process::{Child, Command, Stdio}; use std::time::Duration; // internal +use super::{create_tempdir, persist_tempdir, LOGS_PREFIX}; use crate::{adjust_timeout, get_available_port, ConsensusConfig, MixnetConfig, Node, SpawnConfig}; use carnot_consensus::{CarnotInfo, CarnotSettings}; use carnot_engine::overlay::{RandomBeaconState, RoundRobin, TreeOverlay, TreeOverlaySettings}; @@ -29,7 +30,6 @@ static CLIENT: Lazy = Lazy::new(Client::new); const NOMOS_BIN: &str = "../target/debug/nomos-node"; const CARNOT_INFO_API: &str = "carnot/info"; const STORAGE_BLOCKS_API: &str = "storage/block"; -const LOGS_PREFIX: &str = "__logs"; const GET_BLOCKS_INFO: &str = "carnot/blocks"; pub struct NomosNode { @@ -42,14 +42,14 @@ pub struct NomosNode { impl Drop for NomosNode { fn drop(&mut self) { if std::thread::panicking() { - println!("persisting directory at {}", self._tempdir.path().display()); - // we need ownership of the dir to persist it - let dir = std::mem::replace(&mut self._tempdir, tempfile::tempdir().unwrap()); - // a bit confusing but `into_path` persists the directory - let _ = dir.into_path(); + if let Err(e) = persist_tempdir(&mut self._tempdir, "nomos-node") { + println!("failed to persist tempdir: {e}"); + } } - self.child.kill().unwrap(); + if let Err(e) = self.child.kill() { + println!("failed to kill the child process: {e}"); + } } } @@ -61,11 +61,7 @@ impl NomosNode { pub async fn spawn(mut config: Config) -> Self { // Waku stores the messages in a db file in the current dir, we need a different // directory for each node to avoid conflicts - // - // NOTE: It's easier to use the current location instead of OS-default tempfile location - // because Github Actions can easily access files in the current location using wildcard - // to upload them as artifacts. - let dir = tempfile::TempDir::new_in(std::env::current_dir().unwrap()).unwrap(); + let dir = create_tempdir().unwrap(); let mut file = NamedTempFile::new().unwrap(); let config_path = file.path().to_owned();