Write mixnode logs to files in integration tests (#510)

This commit is contained in:
Youngjoon Lee 2024-01-24 16:15:11 +09:00 committed by GitHub
parent f741590315
commit 42d6816b1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 15 deletions

View File

@ -4,8 +4,10 @@ use std::{
time::Duration, time::Duration,
}; };
use super::{create_tempdir, persist_tempdir, LOGS_PREFIX};
use mixnet_node::{MixnetNodeConfig, PRIVATE_KEY_SIZE}; use mixnet_node::{MixnetNodeConfig, PRIVATE_KEY_SIZE};
use mixnet_topology::{Layer, MixnetTopology, Node}; use mixnet_topology::{Layer, MixnetTopology, Node};
use nomos_log::{LoggerBackend, LoggerFormat};
use rand::{thread_rng, RngCore}; use rand::{thread_rng, RngCore};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
@ -14,21 +16,37 @@ use crate::{get_available_port, MixnetConfig};
const MIXNODE_BIN: &str = "../target/debug/mixnode"; const MIXNODE_BIN: &str = "../target/debug/mixnode";
pub struct MixNode { pub struct MixNode {
_tempdir: tempfile::TempDir,
child: Child, child: Child,
} }
impl Drop for MixNode { impl Drop for MixNode {
fn drop(&mut self) { 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 { impl MixNode {
pub async fn spawn(config: MixnetNodeConfig) -> Self { pub async fn spawn(config: MixnetNodeConfig) -> Self {
let config = mixnode::Config { let dir = create_tempdir().unwrap();
let mut config = mixnode::Config {
mixnode: config, mixnode: config,
log: Default::default(), 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 mut file = NamedTempFile::new().unwrap();
let config_path = file.path().to_owned(); 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 //TODO: use a sophisticated way to wait until the node is ready
tokio::time::sleep(Duration::from_secs(1)).await; tokio::time::sleep(Duration::from_secs(1)).await;
Self { child } Self {
_tempdir: dir,
child,
}
} }
pub async fn spawn_nodes(num_nodes: usize) -> (Vec<Self>, MixnetConfig) { pub async fn spawn_nodes(num_nodes: usize) -> (Vec<Self>, MixnetConfig) {

View File

@ -3,3 +3,26 @@ pub mod nomos;
pub use self::mixnode::MixNode; pub use self::mixnode::MixNode;
pub use nomos::NomosNode; pub use nomos::NomosNode;
use tempfile::TempDir;
const LOGS_PREFIX: &str = "__logs";
fn create_tempdir() -> std::io::Result<TempDir> {
// 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(())
}

View File

@ -3,6 +3,7 @@ use std::net::SocketAddr;
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::time::Duration; use std::time::Duration;
// internal // internal
use super::{create_tempdir, persist_tempdir, LOGS_PREFIX};
use crate::{adjust_timeout, get_available_port, ConsensusConfig, MixnetConfig, Node, SpawnConfig}; use crate::{adjust_timeout, get_available_port, ConsensusConfig, MixnetConfig, Node, SpawnConfig};
use carnot_consensus::{CarnotInfo, CarnotSettings}; use carnot_consensus::{CarnotInfo, CarnotSettings};
use carnot_engine::overlay::{RandomBeaconState, RoundRobin, TreeOverlay, TreeOverlaySettings}; use carnot_engine::overlay::{RandomBeaconState, RoundRobin, TreeOverlay, TreeOverlaySettings};
@ -29,7 +30,6 @@ static CLIENT: Lazy<Client> = Lazy::new(Client::new);
const NOMOS_BIN: &str = "../target/debug/nomos-node"; const NOMOS_BIN: &str = "../target/debug/nomos-node";
const CARNOT_INFO_API: &str = "carnot/info"; const CARNOT_INFO_API: &str = "carnot/info";
const STORAGE_BLOCKS_API: &str = "storage/block"; const STORAGE_BLOCKS_API: &str = "storage/block";
const LOGS_PREFIX: &str = "__logs";
const GET_BLOCKS_INFO: &str = "carnot/blocks"; const GET_BLOCKS_INFO: &str = "carnot/blocks";
pub struct NomosNode { pub struct NomosNode {
@ -42,14 +42,14 @@ pub struct NomosNode {
impl Drop for NomosNode { impl Drop for NomosNode {
fn drop(&mut self) { fn drop(&mut self) {
if std::thread::panicking() { if std::thread::panicking() {
println!("persisting directory at {}", self._tempdir.path().display()); if let Err(e) = persist_tempdir(&mut self._tempdir, "nomos-node") {
// we need ownership of the dir to persist it println!("failed to persist tempdir: {e}");
let dir = std::mem::replace(&mut self._tempdir, tempfile::tempdir().unwrap()); }
// a bit confusing but `into_path` persists the directory
let _ = dir.into_path();
} }
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 { pub async fn spawn(mut config: Config) -> Self {
// Waku stores the messages in a db file in the current dir, we need a different // Waku stores the messages in a db file in the current dir, we need a different
// directory for each node to avoid conflicts // directory for each node to avoid conflicts
// let dir = create_tempdir().unwrap();
// 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 mut file = NamedTempFile::new().unwrap(); let mut file = NamedTempFile::new().unwrap();
let config_path = file.path().to_owned(); let config_path = file.path().to_owned();