Skip to content

Commit

Permalink
ci: [#647] E2E tests. Make sure there are not panics in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 26, 2024
1 parent 670927c commit ddad4a4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
54 changes: 52 additions & 2 deletions src/e2e/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ impl Drop for RunningContainer {
/// 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);
let _unused = Docker::remove(&self.name);
if Docker::is_container_running(&self.name) {
let _unused = Docker::stop(self);
}
if Docker::container_exist(&self.name) {
let _unused = Docker::remove(&self.name);
}

Check warning on line 26 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L21-L26

Added lines #L21 - L26 were not covered by tests
}
}

Expand Down Expand Up @@ -180,4 +184,50 @@ impl Docker {

false
}

/// Checks if a Docker container is running.
///
/// # Arguments
///
/// * `container` - The name of the Docker container.
///
/// # Returns
///
/// `true` if the container is running, `false` otherwise.
#[must_use]
pub fn is_container_running(container: &str) -> bool {
match Command::new("docker")
.args(["ps", "-f", &format!("name={container}"), "--format", "{{.Names}}"])

Check warning on line 200 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L198-L200

Added lines #L198 - L200 were not covered by tests
.output()
{
Ok(output) => {
let output_str = String::from_utf8_lossy(&output.stdout);
output_str.contains(container)
}
Err(_) => false,

Check warning on line 207 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L203-L207

Added lines #L203 - L207 were not covered by tests
}
}

Check warning on line 209 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L209

Added line #L209 was not covered by tests

/// Checks if a Docker container exists.
///
/// # Arguments
///
/// * `container` - The name of the Docker container.
///
/// # Returns
///
/// `true` if the container exists, `false` otherwise.
#[must_use]
pub fn container_exist(container: &str) -> bool {
match Command::new("docker")
.args(["ps", "-a", "-f", &format!("name={container}"), "--format", "{{.Names}}"])

Check warning on line 223 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L221-L223

Added lines #L221 - L223 were not covered by tests
.output()
{
Ok(output) => {
let output_str = String::from_utf8_lossy(&output.stdout);
output_str.contains(container)
}
Err(_) => false,

Check warning on line 230 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L226-L230

Added lines #L226 - L230 were not covered by tests
}
}

Check warning on line 232 in src/e2e/docker.rs

View check run for this annotation

Codecov / codecov/patch

src/e2e/docker.rs#L232

Added line #L232 was not covered by tests
}
32 changes: 30 additions & 2 deletions src/e2e/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub fn run() {

let running_services = parse_running_services_from_logs(&container);

assert_there_are_no_panics_in_logs(&container);

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L60

Added line #L60 was not covered by tests

assert_there_is_at_least_one_service_per_type(&running_services);

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L62

Added line #L62 was not covered by tests

let tracker_checker_config =
Expand All @@ -69,9 +71,12 @@ pub fn run() {

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

// More E2E tests could be executed here in the future. For example: `cargo test ...`.
// More E2E tests could be added here in the future.
// For example: `cargo test ...` for only E2E tests, using this shared test env.

stop_tracker_container(&container);

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L77

Added line #L77 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L79

Added line #L79 was not covered by tests
}

fn setup_runner_logging(level: LevelFilter) {
Expand Down Expand Up @@ -164,6 +169,29 @@ fn run_tracker_container(image: &str, container_name: &str, options: &RunOptions
container
}

fn stop_tracker_container(container: &RunningContainer) {
info!("Stopping docker tracker image: {} ...", container.name);
Docker::stop(container).expect("Container should be stopped");
assert_there_are_no_panics_in_logs(container);
}

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L172-L176

Added lines #L172 - L176 were not covered by tests

fn remove_tracker_container(container_name: &str) {
info!("Removing docker tracker image: {container_name} ...");
Docker::remove(container_name).expect("Container should be removed");
}

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L178-L181

Added lines #L178 - L181 were not covered by tests

fn assert_there_are_no_panics_in_logs(container: &RunningContainer) -> RunningServices {
let logs = Docker::logs(&container.name).expect("Logs should be captured from running container");

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L183-L184

Added lines #L183 - L184 were not covered by tests

assert!(
!(logs.contains(" panicked at ") || logs.contains("RUST_BACKTRACE=1")),

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L186-L187

Added lines #L186 - L187 were not covered by tests
"{}",
format!("Panics found is logs:\n{logs}")

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L189

Added line #L189 was not covered by tests
);

RunningServices::parse_from_logs(&logs)
}

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

View check run for this annotation

Codecov / codecov/patch

src/e2e/runner.rs#L192-L193

Added lines #L192 - L193 were not covered by tests

fn parse_running_services_from_logs(container: &RunningContainer) -> RunningServices {
let logs = Docker::logs(&container.name).expect("Logs should be captured from running container");

Expand Down

0 comments on commit ddad4a4

Please sign in to comment.