Skip to content

Commit bd739f7

Browse files
committed
ci: [#634] E2E test runner: open services port in docker run and run tracker checker
1 parent 5c3f07c commit bd739f7

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

src/e2e/docker.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,29 @@ impl Docker {
3939
/// # Errors
4040
///
4141
/// Will fail if the docker run command fails.
42-
pub fn run(image: &str, container: &str, env_vars: &[(String, String)]) -> io::Result<Output> {
42+
pub fn run(image: &str, container: &str, env_vars: &[(String, String)], ports: &[String]) -> io::Result<Output> {
4343
let initial_args = vec![
4444
"run".to_string(),
4545
"--detach".to_string(),
4646
"--name".to_string(),
4747
container.to_string(),
4848
];
4949

50+
// Add environment variables
5051
let mut env_var_args: Vec<String> = vec![];
5152
for (key, value) in env_vars {
5253
env_var_args.push("--env".to_string());
5354
env_var_args.push(format!("{key}={value}"));
5455
}
5556

56-
let args = [initial_args, env_var_args, [image.to_string()].to_vec()].concat();
57+
// Add port mappings
58+
let mut port_args: Vec<String> = vec![];
59+
for port in ports {
60+
port_args.push("--publish".to_string());
61+
port_args.push(port.to_string());
62+
}
63+
64+
let args = [initial_args, env_var_args, port_args, [image.to_string()].to_vec()].concat();
5765

5866
debug!("Docker run args: {:?}", args);
5967

src/e2e/logs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ impl RunningServices {
1717

1818
if let Some(start) = line.find(heal_check_pattern) {
1919
let address = &line[start + heal_check_pattern.len()..].trim();
20-
self.health_checks.push(Self::replace_wildcard_ip_with_localhost(address));
20+
self.health_checks
21+
.push(format!("{}/health_check", Self::replace_wildcard_ip_with_localhost(address)));
2122
} else if let Some(start) = line.find(udp_tracker_pattern) {
2223
let address = &line[start + udp_tracker_pattern.len()..].trim();
2324
self.udp_trackers.push(Self::replace_wildcard_ip_with_localhost(address));

src/e2e/runner.rs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use std::env;
21
use std::fs::File;
32
use std::io::Write;
3+
use std::path::{Path, PathBuf};
4+
use std::process::Command;
45
use std::time::Duration;
6+
use std::{env, io};
57

68
use log::{debug, LevelFilter};
79
use rand::distributions::Alphanumeric;
@@ -22,17 +24,7 @@ pub const NUMBER_OF_ARGUMENTS: usize = 2;
2224
/// - It can't change to the new temp dir.
2325
/// - It can't revert the dit to the previous one.
2426
pub fn run() {
25-
/* todo:
26-
27-
- [x] Build the docker image.
28-
- [x] Run the docker image.
29-
- [x] Wait until the container is healthy.
30-
- [x] Parse logs to get running services.
31-
- [x] Build config file for the tracker_checker.
32-
- [ ] Run the tracker_checker.
33-
- [x] Stop the container.
34-
35-
*/
27+
// todo: stop container or revert current dir on panics.
3628

3729
setup_logging(LevelFilter::Debug);
3830

@@ -42,7 +34,8 @@ pub fn run() {
4234

4335
let tracker_config = read_tracker_config(&args.tracker_config_path);
4436

45-
let container_tag = "torrust-tracker:local";
37+
let container_tag: &str = "torrust-tracker:local";
38+
let tracker_checker_config_file = "tracker_checker.json";
4639

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

@@ -64,7 +57,13 @@ pub fn run() {
6457

6558
println!("Running docker tracker image: {container_name} ...");
6659
let env_vars = [("TORRUST_TRACKER_CONFIG".to_string(), tracker_config.to_string())];
67-
Docker::run(container_tag, &container_name, &env_vars).expect("A tracker local docker image should be running");
60+
let ports = [
61+
"6969:6969/udp".to_string(),
62+
"7070:7070/tcp".to_string(),
63+
"1212:1212/tcp".to_string(),
64+
"1313:1313/tcp".to_string(),
65+
];
66+
Docker::run(container_tag, &container_name, &env_vars, &ports).expect("A tracker local docker image should be running");
6867

6968
println!("Waiting for the container {container_name} to be healthy ...");
7069
let is_healthy = Docker::wait_until_is_healthy(&container_name, Duration::from_secs(10));
@@ -88,20 +87,30 @@ pub fn run() {
8887
let json = serde_json::to_string_pretty(&config).expect("Running services should be serialized into JSON");
8988
println!("Tracker checker configuration: {json}");
9089

91-
let tracker_checker_config_path = "./tracker_checker.json";
90+
let tracker_checker_config_path = format!("./{tracker_checker_config_file}");
9291

93-
let mut file = File::create(tracker_checker_config_path).expect("Tracker checker config file to be created");
92+
let mut file = File::create(tracker_checker_config_path.clone()).expect("Tracker checker config file to be created");
9493
file.write_all(json.as_bytes())
9594
.expect("Tracker checker config file to be written");
9695
println!("Tracker checker configuration file: {tracker_checker_config_path} \n{json}");
9796

98-
println!("Stopping docker tracker image: {container_name} ...");
99-
Docker::stop(&container_name).expect("A tracker local docker image should be stopped");
100-
10197
println!("Revert current dir to: {:?}", temp_dir_handler.original_dir);
10298
temp_dir_handler
10399
.revert_to_original_dir()
104100
.expect("The app should revert dir from temp dir to the original one");
101+
102+
let mut absolute_tracker_checker_config_path = PathBuf::from(&temp_dir_handler.temp_dir.path());
103+
absolute_tracker_checker_config_path.push(tracker_checker_config_file);
104+
105+
println!(
106+
"Running tacker checker: cargo --bin tracker_checker {}",
107+
absolute_tracker_checker_config_path.display()
108+
);
109+
110+
run_tracker_checker(&absolute_tracker_checker_config_path).expect("Tracker checker should check running services");
111+
112+
println!("Stopping docker tracker image: {container_name} ...");
113+
Docker::stop(&container_name).expect("A tracker local docker image should be stopped");
105114
}
106115

107116
fn setup_logging(level: LevelFilter) {
@@ -159,3 +168,33 @@ fn generate_random_container_name(prefix: &str) -> String {
159168

160169
format!("{prefix}{rand_string}")
161170
}
171+
172+
/// Runs the tracker checker
173+
///
174+
/// ```text
175+
/// cargo run --bin tracker_checker "./share/default/config/tracker_checker.json"
176+
/// ```
177+
///
178+
/// # Errors
179+
///
180+
/// Will return an error if the tracker checker fails.
181+
///
182+
/// # Panics
183+
///
184+
/// Will panic if the config path is not a valid string.
185+
pub fn run_tracker_checker(config_path: &Path) -> io::Result<()> {
186+
let path = config_path.to_str().expect("The path should be a valid string");
187+
188+
let status = Command::new("cargo")
189+
.args(["run", "--bin", "tracker_checker", path])
190+
.status()?;
191+
192+
if status.success() {
193+
Ok(())
194+
} else {
195+
Err(io::Error::new(
196+
io::ErrorKind::Other,
197+
format!("Failed to run tracker checker with config file {path}"),
198+
))
199+
}
200+
}

0 commit comments

Comments
 (0)