diff --git a/src/e2e/mod.rs b/src/e2e/mod.rs index deba8971..e4384e16 100644 --- a/src/e2e/mod.rs +++ b/src/e2e/mod.rs @@ -1,5 +1,5 @@ pub mod docker; pub mod logs_parser; pub mod runner; -pub mod temp_dir; +pub mod tracker_checker; pub mod tracker_container; diff --git a/src/e2e/runner.rs b/src/e2e/runner.rs index 90c98608..a4bcb3aa 100644 --- a/src/e2e/runner.rs +++ b/src/e2e/runner.rs @@ -1,15 +1,9 @@ -use std::fs::File; -use std::io::Write; -use std::path::{Path, PathBuf}; -use std::process::Command; -use std::{env, io}; - use log::{debug, info, LevelFilter}; use super::tracker_container::TrackerContainer; use crate::e2e::docker::RunOptions; use crate::e2e::logs_parser::RunningServices; -use crate::e2e::temp_dir::Handler; +use crate::e2e::tracker_checker::{self}; /* code-review: - We use always the same docker image name. Should we use a random image name (tag)? @@ -19,10 +13,9 @@ use crate::e2e::temp_dir::Handler; Should we remove the image too? */ -pub const NUMBER_OF_ARGUMENTS: usize = 2; +const NUMBER_OF_ARGUMENTS: usize = 2; const CONTAINER_IMAGE: &str = "torrust-tracker:local"; const CONTAINER_NAME_PREFIX: &str = "tracker_"; -const TRACKER_CHECKER_CONFIG_FILE: &str = "tracker_checker.json"; pub struct Arguments { pub tracker_config_path: String, @@ -63,14 +56,10 @@ pub fn run() { assert_there_is_at_least_one_service_per_type(&running_services); - let temp_dir = create_temp_dir(); - - let tracker_checker_config_path = - create_tracker_checker_config_file(&running_services, temp_dir.temp_dir.path(), TRACKER_CHECKER_CONFIG_FILE); + let tracker_checker_config = + serde_json::to_string_pretty(&running_services).expect("Running services should be serialized into JSON"); - // todo: inject the configuration with an env variable so that we don't have - // to create the temporary directory/file. - run_tracker_checker(&tracker_checker_config_path).expect("All tracker services should be running correctly"); + tracker_checker::run(&tracker_checker_config).expect("All tracker services should be running correctly"); // More E2E tests could be added here in the future. // For example: `cargo test ...` for only E2E tests, using this shared test env. @@ -128,19 +117,6 @@ fn read_file(path: &str) -> String { std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Can't read file {path}")) } -fn create_temp_dir() -> Handler { - debug!( - "Current dir: {:?}", - env::current_dir().expect("It should return the current dir") - ); - - let temp_dir_handler = Handler::new().expect("A temp dir should be created"); - - info!("Temp dir created: {:?}", temp_dir_handler.temp_dir); - - temp_dir_handler -} - fn assert_there_is_at_least_one_service_per_type(running_services: &RunningServices) { assert!( !running_services.udp_trackers.is_empty(), @@ -155,64 +131,3 @@ fn assert_there_is_at_least_one_service_per_type(running_services: &RunningServi "At least one Health Check should be enabled in E2E tests configuration" ); } - -fn create_tracker_checker_config_file(running_services: &RunningServices, config_path: &Path, config_name: &str) -> PathBuf { - let tracker_checker_config = - serde_json::to_string_pretty(&running_services).expect("Running services should be serialized into JSON"); - - let mut tracker_checker_config_path = PathBuf::from(&config_path); - tracker_checker_config_path.push(config_name); - - write_tracker_checker_config_file(&tracker_checker_config_path, &tracker_checker_config); - - tracker_checker_config_path -} - -fn write_tracker_checker_config_file(config_file_path: &Path, config: &str) { - info!( - "Writing Tracker Checker configuration file: {:?} \n{config}", - config_file_path - ); - - let mut file = File::create(config_file_path).expect("Tracker checker config file to be created"); - - file.write_all(config.as_bytes()) - .expect("Tracker checker config file to be written"); -} - -/// Runs the Tracker Checker. -/// -/// For example: -/// -/// ```text -/// cargo run --bin tracker_checker "./share/default/config/tracker_checker.json" -/// ``` -/// -/// # Errors -/// -/// Will return an error if the tracker checker fails. -/// -/// # Panics -/// -/// Will panic if the config path is not a valid string. -pub fn run_tracker_checker(config_path: &Path) -> io::Result<()> { - info!( - "Running Tracker Checker: cargo run --bin tracker_checker -- --config-path \"{}\"", - config_path.display() - ); - - let path = config_path.to_str().expect("The path should be a valid string"); - - let status = Command::new("cargo") - .args(["run", "--bin", "tracker_checker", "--", "--config-path", path]) - .status()?; - - if status.success() { - Ok(()) - } else { - Err(io::Error::new( - io::ErrorKind::Other, - format!("Failed to run Tracker Checker with config file {path}"), - )) - } -} diff --git a/src/e2e/temp_dir.rs b/src/e2e/temp_dir.rs deleted file mode 100644 index 8433e305..00000000 --- a/src/e2e/temp_dir.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! Temp dir which is automatically removed when it goes out of scope. -use std::path::PathBuf; -use std::{env, io}; - -use tempfile::TempDir; - -pub struct Handler { - pub temp_dir: TempDir, - pub original_dir: PathBuf, -} - -impl Handler { - /// Creates a new temporary directory and remembers the current working directory. - /// - /// # Errors - /// - /// Will error if: - /// - /// - It can't create the temp dir. - /// - It can't get the current dir. - pub fn new() -> io::Result { - let temp_dir = TempDir::new()?; - let original_dir = env::current_dir()?; - - Ok(Handler { temp_dir, original_dir }) - } - - /// Changes the current working directory to the temporary directory. - /// - /// # Errors - /// - /// Will error if it can't change the current di to the temp dir. - pub fn change_to_temp_dir(&self) -> io::Result<()> { - env::set_current_dir(self.temp_dir.path()) - } - - /// Changes the current working directory back to the original directory. - /// - /// # Errors - /// - /// Will error if it can't revert the current dir to the original one. - pub fn revert_to_original_dir(&self) -> io::Result<()> { - env::set_current_dir(&self.original_dir) - } -} - -impl Drop for Handler { - /// Ensures that the temporary directory is deleted when the struct goes out of scope. - fn drop(&mut self) { - // The temporary directory is automatically deleted when `TempDir` is dropped. - // We can add additional cleanup here if necessary. - } -} diff --git a/src/e2e/tracker_checker.rs b/src/e2e/tracker_checker.rs new file mode 100644 index 00000000..edc67980 --- /dev/null +++ b/src/e2e/tracker_checker.rs @@ -0,0 +1,25 @@ +use std::io; +use std::process::Command; + +use log::info; + +/// Runs the Tracker Checker. +/// +/// # Errors +/// +/// Will return an error if the Tracker Checker fails. +pub fn run(config_content: &str) -> io::Result<()> { + info!("Running Tracker Checker: TORRUST_CHECKER_CONFIG=[config] cargo run --bin tracker_checker"); + info!("Tracker Checker config:\n{config_content}"); + + let status = Command::new("cargo") + .env("TORRUST_CHECKER_CONFIG", config_content) + .args(["run", "--bin", "tracker_checker"]) + .status()?; + + if status.success() { + Ok(()) + } else { + Err(io::Error::new(io::ErrorKind::Other, "Failed to run Tracker Checker")) + } +}