Skip to content

Commit

Permalink
fix: tracker checker return error code when it fails
Browse files Browse the repository at this point in the history
This command:

```
cargo run --bin tracker_checker "./share/default/config/tracker_checker.json" && echo "OK"
```

should not print OK when it fails.
  • Loading branch information
josecelano committed Jan 26, 2024
1 parent e5cd81b commit f18e68c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bin/tracker_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ use torrust_tracker::checker::app;

#[tokio::main]
async fn main() {
app::run().await;
app::run().await.expect("Some checks fail");
}
10 changes: 7 additions & 3 deletions src/checker/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ use std::sync::Arc;

use super::config::Configuration;
use super::console::Console;
use super::service::{CheckError, Service};
use crate::checker::config::parse_from_json;
use crate::checker::service::Service;

pub const NUMBER_OF_ARGUMENTS: usize = 2;

/// # Errors
///
/// If some checks fails it will return a vector with all failing checks.
///
/// # Panics
///
/// Will panic if:
///
/// - It can't read the json configuration file.
/// - The configuration file is invalid.
pub async fn run() {
pub async fn run() -> Result<(), Vec<CheckError>> {

Check warning on line 20 in src/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/app.rs#L20

Added line #L20 was not covered by tests
let args = parse_arguments();
let config = setup_config(&args);
let console_printer = Console {};
Expand All @@ -22,7 +26,7 @@ pub async fn run() {
console: console_printer,
};

service.run_checks().await;
service.run_checks().await
}

pub struct Arguments {
Expand Down
36 changes: 31 additions & 5 deletions src/checker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ pub struct Service {
pub(crate) console: Console,
}

#[derive(Debug)]

Check warning on line 17 in src/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/service.rs#L17

Added line #L17 was not covered by tests
pub enum CheckError {
UdpError,
HttpError,
HealthCheckError { url: Url },

Check warning on line 21 in src/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/service.rs#L21

Added line #L21 was not covered by tests
}

impl Service {
pub async fn run_checks(&self) {
/// # Errors
///
/// Will return OK is all checks pass or an array with the check errors.
pub async fn run_checks(&self) -> Result<(), Vec<CheckError>> {

Check warning on line 28 in src/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/service.rs#L28

Added line #L28 was not covered by tests
self.console.println("Running checks for trackers ...");

self.check_udp_trackers();
self.check_http_trackers();
self.run_health_checks().await;

self.run_health_checks().await
}

fn check_udp_trackers(&self) {
Expand All @@ -38,11 +50,22 @@ impl Service {
}
}

async fn run_health_checks(&self) {
async fn run_health_checks(&self) -> Result<(), Vec<CheckError>> {

Check warning on line 53 in src/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/service.rs#L53

Added line #L53 was not covered by tests
self.console.println("Health checks ...");

let mut check_errors = vec![];

for health_check_url in &self.config.health_checks {
self.run_health_check(health_check_url.clone()).await;
match self.run_health_check(health_check_url.clone()).await {
Ok(()) => {}
Err(err) => check_errors.push(err),
}
}

if check_errors.is_empty() {
Ok(())
} else {
Err(check_errors)
}
}

Expand All @@ -62,22 +85,25 @@ impl Service {
.println(&format!("{} - HTTP tracker at {} is OK (TODO)", "✓".green(), url));
}

async fn run_health_check(&self, url: Url) {
async fn run_health_check(&self, url: Url) -> Result<(), CheckError> {

Check warning on line 88 in src/checker/service.rs

View check run for this annotation

Codecov / codecov/patch

src/checker/service.rs#L88

Added line #L88 was not covered by tests
let client = Client::builder().timeout(Duration::from_secs(5)).build().unwrap();

match client.get(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
self.console
.println(&format!("{} - Health API at {} is OK", "✓".green(), url));
Ok(())
} else {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, response));
Err(CheckError::HealthCheckError { url })
}
}
Err(err) => {
self.console
.eprintln(&format!("{} - Health API at {} failing: {:?}", "✗".red(), url, err));
Err(CheckError::HealthCheckError { url })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn run() {
let tracker_checker_config_path =
create_tracker_checker_config_file(&running_services, temp_dir.temp_dir.path(), TRACKER_CHECKER_CONFIG_FILE);

Check warning on line 69 in src/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L68-L69

Added lines #L68 - L69 were not covered by tests

// todo: inject the configuration with an env variable so that we don't have
// 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");

Check warning on line 73 in src/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L73

Added line #L73 was not covered by tests

Expand Down

0 comments on commit f18e68c

Please sign in to comment.