Skip to content

Commit 392ffab

Browse files
committed
refactor: [torrust#656] E2E runner. Pass config as env var to Tracker Checker
This was we don't even need a temp dir to run E2E tests.
1 parent 1bab582 commit 392ffab

File tree

4 files changed

+31
-144
lines changed

4 files changed

+31
-144
lines changed

src/e2e/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod docker;
22
pub mod logs_parser;
33
pub mod runner;
4-
pub mod temp_dir;
4+
pub mod tracker_checker;
55
pub mod tracker_container;

src/e2e/runner.rs

+5-90
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
use std::fs::File;
2-
use std::io::Write;
3-
use std::path::{Path, PathBuf};
4-
use std::process::Command;
5-
use std::{env, io};
6-
71
use log::{debug, info, LevelFilter};
82

93
use super::tracker_container::TrackerContainer;
104
use crate::e2e::docker::RunOptions;
115
use crate::e2e::logs_parser::RunningServices;
12-
use crate::e2e::temp_dir::Handler;
6+
use crate::e2e::tracker_checker::{self};
137

148
/* code-review:
159
- 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;
1913
Should we remove the image too?
2014
*/
2115

22-
pub const NUMBER_OF_ARGUMENTS: usize = 2;
16+
const NUMBER_OF_ARGUMENTS: usize = 2;
2317
const CONTAINER_IMAGE: &str = "torrust-tracker:local";
2418
const CONTAINER_NAME_PREFIX: &str = "tracker_";
25-
const TRACKER_CHECKER_CONFIG_FILE: &str = "tracker_checker.json";
2619

2720
pub struct Arguments {
2821
pub tracker_config_path: String,
@@ -63,14 +56,10 @@ pub fn run() {
6356

6457
assert_there_is_at_least_one_service_per_type(&running_services);
6558

66-
let temp_dir = create_temp_dir();
67-
68-
let tracker_checker_config_path =
69-
create_tracker_checker_config_file(&running_services, temp_dir.temp_dir.path(), TRACKER_CHECKER_CONFIG_FILE);
59+
let tracker_checker_config =
60+
serde_json::to_string_pretty(&running_services).expect("Running services should be serialized into JSON");
7061

71-
// todo: inject the configuration with an env variable so that we don't have
72-
// to create the temporary directory/file.
73-
run_tracker_checker(&tracker_checker_config_path).expect("All tracker services should be running correctly");
62+
tracker_checker::run(&tracker_checker_config).expect("All tracker services should be running correctly");
7463

7564
// More E2E tests could be added here in the future.
7665
// For example: `cargo test ...` for only E2E tests, using this shared test env.
@@ -128,19 +117,6 @@ fn read_file(path: &str) -> String {
128117
std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Can't read file {path}"))
129118
}
130119

131-
fn create_temp_dir() -> Handler {
132-
debug!(
133-
"Current dir: {:?}",
134-
env::current_dir().expect("It should return the current dir")
135-
);
136-
137-
let temp_dir_handler = Handler::new().expect("A temp dir should be created");
138-
139-
info!("Temp dir created: {:?}", temp_dir_handler.temp_dir);
140-
141-
temp_dir_handler
142-
}
143-
144120
fn assert_there_is_at_least_one_service_per_type(running_services: &RunningServices) {
145121
assert!(
146122
!running_services.udp_trackers.is_empty(),
@@ -155,64 +131,3 @@ fn assert_there_is_at_least_one_service_per_type(running_services: &RunningServi
155131
"At least one Health Check should be enabled in E2E tests configuration"
156132
);
157133
}
158-
159-
fn create_tracker_checker_config_file(running_services: &RunningServices, config_path: &Path, config_name: &str) -> PathBuf {
160-
let tracker_checker_config =
161-
serde_json::to_string_pretty(&running_services).expect("Running services should be serialized into JSON");
162-
163-
let mut tracker_checker_config_path = PathBuf::from(&config_path);
164-
tracker_checker_config_path.push(config_name);
165-
166-
write_tracker_checker_config_file(&tracker_checker_config_path, &tracker_checker_config);
167-
168-
tracker_checker_config_path
169-
}
170-
171-
fn write_tracker_checker_config_file(config_file_path: &Path, config: &str) {
172-
info!(
173-
"Writing Tracker Checker configuration file: {:?} \n{config}",
174-
config_file_path
175-
);
176-
177-
let mut file = File::create(config_file_path).expect("Tracker checker config file to be created");
178-
179-
file.write_all(config.as_bytes())
180-
.expect("Tracker checker config file to be written");
181-
}
182-
183-
/// Runs the Tracker Checker.
184-
///
185-
/// For example:
186-
///
187-
/// ```text
188-
/// cargo run --bin tracker_checker "./share/default/config/tracker_checker.json"
189-
/// ```
190-
///
191-
/// # Errors
192-
///
193-
/// Will return an error if the tracker checker fails.
194-
///
195-
/// # Panics
196-
///
197-
/// Will panic if the config path is not a valid string.
198-
pub fn run_tracker_checker(config_path: &Path) -> io::Result<()> {
199-
info!(
200-
"Running Tracker Checker: cargo run --bin tracker_checker -- --config-path \"{}\"",
201-
config_path.display()
202-
);
203-
204-
let path = config_path.to_str().expect("The path should be a valid string");
205-
206-
let status = Command::new("cargo")
207-
.args(["run", "--bin", "tracker_checker", "--", "--config-path", path])
208-
.status()?;
209-
210-
if status.success() {
211-
Ok(())
212-
} else {
213-
Err(io::Error::new(
214-
io::ErrorKind::Other,
215-
format!("Failed to run Tracker Checker with config file {path}"),
216-
))
217-
}
218-
}

src/e2e/temp_dir.rs

-53
This file was deleted.

src/e2e/tracker_checker.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::io;
2+
use std::process::Command;
3+
4+
use log::info;
5+
6+
/// Runs the Tracker Checker.
7+
///
8+
/// # Errors
9+
///
10+
/// Will return an error if the Tracker Checker fails.
11+
pub fn run(config_content: &str) -> io::Result<()> {
12+
info!("Running Tracker Checker: TORRUST_CHECKER_CONFIG=[config] cargo run --bin tracker_checker");
13+
info!("Tracker Checker config:\n{config_content}");
14+
15+
let status = Command::new("cargo")
16+
.env("TORRUST_CHECKER_CONFIG", config_content)
17+
.args(["run", "--bin", "tracker_checker"])
18+
.status()?;
19+
20+
if status.success() {
21+
Ok(())
22+
} else {
23+
Err(io::Error::new(io::ErrorKind::Other, "Failed to run Tracker Checker"))
24+
}
25+
}

0 commit comments

Comments
 (0)