Skip to content

Commit

Permalink
refactor: [#659] use clap and anyhow in E2E test runner
Browse files Browse the repository at this point in the history
You can execute the E2E runner with:

```bash
cargo run --bin e2e_tests_runner -- --config-toml-path "./share/default/config/tracker.e2e.container.sqlite3.toml"
```

Or:

```bash
TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.e2e.container.sqlite3.toml" cargo run --bin e2e_tests_runner
```

Or:

```bash
TORRUST_TRACKER_CONFIG_TOML=$(cat "./share/default/config/tracker.e2e.container.sqlite3.toml") cargo run --bin e2e_tests_runner
```
  • Loading branch information
josecelano committed Jun 10, 2024
1 parent dc171c1 commit c08de75
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ jobs:

- id: test
name: Run E2E Tests
run: cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml
run: cargo run --bin e2e_tests_runner -- --config-toml-path "./share/default/config/tracker.e2e.container.sqlite3.toml"
4 changes: 2 additions & 2 deletions src/bin/e2e_tests_runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Program to run E2E tests.
use torrust_tracker::console::ci::e2e;

fn main() {
e2e::runner::run();
fn main() -> anyhow::Result<()> {
e2e::runner::run()
}
85 changes: 58 additions & 27 deletions src/console/ci/e2e/runner.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
//! Program to run E2E tests.
//!
//! You can execute it with (passing a TOML config file path):
//!
//! ```text
//! cargo run --bin e2e_tests_runner -- --config-toml-path "./share/default/config/tracker.e2e.container.sqlite3.toml"
//! ```
//!
//! Or:
//!
//! ```text
//! cargo run --bin e2e_tests_runner share/default/config/tracker.e2e.container.sqlite3.toml
//! TORRUST_TRACKER_CONFIG_TOML_PATH="./share/default/config/tracker.e2e.container.sqlite3.toml" cargo run --bin e2e_tests_runner"
//! ```
//!
//! You can execute it with (directly passing TOML config):
//!
//! ```text
//! TORRUST_TRACKER_CONFIG_TOML=$(cat "./share/default/config/tracker.e2e.container.sqlite3.toml") cargo run --bin e2e_tests_runner
//! ```
use std::path::PathBuf;

use anyhow::Context;
use clap::Parser;
use tracing::info;
use tracing::level_filters::LevelFilter;

Expand All @@ -19,25 +37,38 @@ use crate::console::ci::e2e::tracker_checker::{self};
Should we remove the image too?
*/

const NUMBER_OF_ARGUMENTS: usize = 2;
const CONTAINER_IMAGE: &str = "torrust-tracker:local";
const CONTAINER_NAME_PREFIX: &str = "tracker_";

pub struct Arguments {
pub tracker_config_path: String,
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]

Check warning on line 44 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L43-L44

Added lines #L43 - L44 were not covered by tests
struct Args {
/// Path to the JSON configuration file.

Check warning on line 46 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L46

Added line #L46 was not covered by tests
#[clap(short, long, env = "TORRUST_TRACKER_CONFIG_TOML_PATH")]
config_toml_path: Option<PathBuf>,

/// Direct configuration content in JSON.

Check warning on line 50 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L50

Added line #L50 was not covered by tests
#[clap(env = "TORRUST_TRACKER_CONFIG_TOML", hide_env_values = true)]
config_toml: Option<String>,

Check warning on line 52 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L52

Added line #L52 was not covered by tests
}

/// Script to run E2E tests.
///
/// # Errors
///
/// Will return an error if it can't load the tracker configuration from arguments.
///
/// # Panics
///
/// Will panic if it can't not perform any of the operations.
pub fn run() {
pub fn run() -> anyhow::Result<()> {

Check warning on line 64 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L64

Added line #L64 was not covered by tests
tracing_stdout_init(LevelFilter::INFO);

let args = parse_arguments();
let args = Args::parse();

Check warning on line 67 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L67

Added line #L67 was not covered by tests

let tracker_config = load_tracker_configuration(&args.tracker_config_path);
let tracker_config = load_tracker_configuration(&args)?;

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

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L69

Added line #L69 was not covered by tests

info!("tracker config:\n{tracker_config}");

Check warning on line 71 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L71

Added line #L71 was not covered by tests

let mut tracker_container = TrackerContainer::new(CONTAINER_IMAGE, CONTAINER_NAME_PREFIX);

Expand Down Expand Up @@ -80,36 +111,36 @@ pub fn run() {
tracker_container.remove();

info!("Tracker container final state:\n{:#?}", tracker_container);

Ok(())

Check warning on line 115 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L115

Added line #L115 was not covered by tests
}

fn tracing_stdout_init(filter: LevelFilter) {
tracing_subscriber::fmt().with_max_level(filter).with_ansi(false).init();
info!("logging initialized.");
info!("Logging initialized.");

Check warning on line 120 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L120

Added line #L120 was not covered by tests
}

fn parse_arguments() -> Arguments {
let args: Vec<String> = std::env::args().collect();

if args.len() < NUMBER_OF_ARGUMENTS {
eprintln!("Usage: cargo run --bin e2e_tests_runner <PATH_TO_TRACKER_CONFIG_FILE>");
eprintln!("For example: cargo run --bin e2e_tests_runner ./share/default/config/tracker.e2e.container.sqlite3.toml");
std::process::exit(1);
}

let config_path = &args[1];

Arguments {
tracker_config_path: config_path.to_string(),
fn load_tracker_configuration(args: &Args) -> anyhow::Result<String> {
match (args.config_toml_path.clone(), args.config_toml.clone()) {
(Some(config_path), _) => {
info!(

Check warning on line 126 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L123-L126

Added lines #L123 - L126 were not covered by tests
"Reading tracker configuration from file: {} ...",
config_path.to_string_lossy()
);
load_config_from_file(&config_path)
}
(_, Some(config_content)) => {
info!("Reading tracker configuration from env var ...");
Ok(config_content)
}
_ => Err(anyhow::anyhow!("No configuration provided")),

Check warning on line 136 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L130-L136

Added lines #L130 - L136 were not covered by tests
}
}

fn load_tracker_configuration(tracker_config_path: &str) -> String {
info!("Reading tracker configuration from file: {} ...", tracker_config_path);
read_file(tracker_config_path)
}
fn load_config_from_file(path: &PathBuf) -> anyhow::Result<String> {
let config = std::fs::read_to_string(path).with_context(|| format!("CSan't read config file {path:?}"))?;

Check warning on line 141 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L140-L141

Added lines #L140 - L141 were not covered by tests

fn read_file(path: &str) -> String {
std::fs::read_to_string(path).unwrap_or_else(|_| panic!("Can't read file {path}"))
Ok(config)

Check warning on line 143 in src/console/ci/e2e/runner.rs

View check run for this annotation

Codecov / codecov/patch

src/console/ci/e2e/runner.rs#L143

Added line #L143 was not covered by tests
}

fn assert_there_is_at_least_one_service_per_type(running_services: &RunningServices) {
Expand Down

0 comments on commit c08de75

Please sign in to comment.