Skip to content

Commit

Permalink
refactor: [torrust#634] no need to change dir to temp dir
Browse files Browse the repository at this point in the history
We only need to create the config file for the tracker_checker in a temp dir but it can be executed from the root dir.
  • Loading branch information
josecelano committed Jan 24, 2024
1 parent ef80b0a commit af65b59
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/e2e/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct RunningContainer {
}

impl Drop for RunningContainer {
/// Ensures that the temporary container is stopped and removed when the
/// Ensures that the temporary container is stopped and removed when the
/// struct goes out of scope.
fn drop(&mut self) {
let _unused = Docker::stop(self);
Expand Down
42 changes: 22 additions & 20 deletions src/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,30 @@ pub const NUMBER_OF_ARGUMENTS: usize = 2;
///
/// Will panic if it can't not perform any of the operations.
pub fn run() {
// todo: const
let container_tag: &str = "torrust-tracker:local";
let tracker_checker_config_file = "tracker_checker.json";

setup_logging(LevelFilter::Info);

let args = parse_arguments();

// Setup tracker configuration
info!("Reading tracker configuration from file: {} ...", args.tracker_config_path);

let tracker_config = read_tracker_config(&args.tracker_config_path);

let container_tag: &str = "torrust-tracker:local";
let tracker_checker_config_file = "tracker_checker.json";

// Build tracker container image
Docker::build("./Containerfile", container_tag).expect("A tracker local docker image should be built");

// Create temp dir
info!(
"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);

info!("Change dir to: {:?}", temp_dir_handler.temp_dir);
temp_dir_handler
.change_to_temp_dir()
.expect("The app should change dir to the temp dir");

// Run the tracker container
let container_name = generate_random_container_name("tracker_");

// code-review: if we want to use port 0 we don't know which ports we have to open.
Expand All @@ -68,6 +66,8 @@ pub fn run() {

info!("Container {container_name} is healthy ...");

// Extract running services from container logs

let logs = Docker::logs(&container_name).expect("Logs should be captured from running container");

debug!("Logs after starting the container:\n{logs}");
Expand All @@ -77,20 +77,20 @@ pub fn run() {

let json = serde_json::to_string_pretty(&config).expect("Running services should be serialized into JSON");

let tracker_checker_config_path = format!("./{tracker_checker_config_file}");
// Write tracker_checker configuration file

let mut file = File::create(tracker_checker_config_path.clone()).expect("Tracker checker config file to be created");
let mut absolute_tracker_checker_config_path = PathBuf::from(&temp_dir_handler.temp_dir.path());
absolute_tracker_checker_config_path.push(tracker_checker_config_file);

let mut file = File::create(absolute_tracker_checker_config_path.clone()).expect("Tracker checker config file to be created");
file.write_all(json.as_bytes())
.expect("Tracker checker config file to be written");
info!("Tracker checker configuration file: {tracker_checker_config_path} \n{json}");

info!("Revert current dir to: {:?}", temp_dir_handler.original_dir);
temp_dir_handler
.revert_to_original_dir()
.expect("The app should revert dir from temp dir to the original one");
info!(
"Tracker checker configuration file: {:?} \n{json}",
absolute_tracker_checker_config_path
);

let mut absolute_tracker_checker_config_path = PathBuf::from(&temp_dir_handler.temp_dir.path());
absolute_tracker_checker_config_path.push(tracker_checker_config_file);
// Run the tracker_checker

info!(
"Running tacker checker: cargo --bin tracker_checker {}",
Expand All @@ -99,6 +99,8 @@ pub fn run() {

run_tracker_checker(&absolute_tracker_checker_config_path).expect("Tracker checker should check running services");

// End: container will be removed automatically when the `RunningContainer` is dropped.

info!("Running container `{}` will be automatically removed", container.name);
}

Expand Down
11 changes: 11 additions & 0 deletions tracker_checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"udp_trackers": [
"127.0.0.1:6969"
],
"http_trackers": [
"http://127.0.0.1:7070"
],
"health_checks": [
"http://127.0.0.1:1313/health_check"
]
}

0 comments on commit af65b59

Please sign in to comment.