Skip to content

Commit 7407c8b

Browse files
committed
refactor: [#200] extract run_preflight_cleanup to shared task
- Create preflight_cleanup.rs in src/testing/e2e/tasks/black_box/ - Function removes artifacts from previous test runs - Remove duplicate implementations from both E2E binaries
1 parent 1df7ff1 commit 7407c8b

File tree

4 files changed

+89
-102
lines changed

4 files changed

+89
-102
lines changed

src/bin/e2e_provision_and_destroy_tests.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ use torrust_dependency_installer::Dependency;
4646
use tracing::{error, info, warn};
4747

4848
use torrust_tracker_deployer_lib::bootstrap::logging::{LogFormat, LogOutput, LoggingBuilder};
49-
use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::verify_required_dependencies;
50-
use torrust_tracker_deployer_lib::testing::e2e::tasks::virtual_machine::preflight_cleanup::{
51-
preflight_cleanup_previous_resources, PreflightCleanupContext,
49+
use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::{
50+
run_preflight_cleanup, verify_required_dependencies,
5251
};
5352
use torrust_tracker_deployer_lib::testing::e2e::ProcessRunner;
5453

@@ -143,54 +142,6 @@ fn main() -> Result<()> {
143142
test_result
144143
}
145144

146-
/// Performs preflight cleanup to remove artifacts from previous test runs.
147-
///
148-
/// This ensures a clean slate before starting new tests by removing:
149-
/// - Build directory
150-
/// - Templates directory
151-
/// - Data directory for this environment
152-
/// - LXD resources (instance and profile)
153-
///
154-
/// # Arguments
155-
///
156-
/// * `environment_name` - The name of the environment to clean up
157-
///
158-
/// # Errors
159-
///
160-
/// Returns an error if cleanup fails.
161-
fn run_preflight_cleanup(environment_name: &str) -> Result<()> {
162-
use torrust_tracker_deployer_lib::domain::EnvironmentName;
163-
164-
info!(
165-
operation = "preflight_cleanup",
166-
environment = environment_name,
167-
"Running preflight cleanup"
168-
);
169-
170-
// Create preflight cleanup context with paths for the environment
171-
let cleanup_context = PreflightCleanupContext::new(
172-
format!("./build/{environment_name}").into(),
173-
format!("./templates/{environment_name}").into(),
174-
EnvironmentName::new(environment_name).expect("Valid environment name"),
175-
format!("torrust-tracker-vm-{environment_name}")
176-
.try_into()
177-
.expect("Valid instance name"),
178-
format!("torrust-profile-{environment_name}")
179-
.try_into()
180-
.expect("Valid profile name"),
181-
);
182-
183-
preflight_cleanup_previous_resources(&cleanup_context)?;
184-
185-
info!(
186-
operation = "preflight_cleanup",
187-
status = "success",
188-
"Preflight cleanup completed"
189-
);
190-
191-
Ok(())
192-
}
193-
194145
/// Runs the E2E test workflow using CLI commands.
195146
///
196147
/// Executes the following commands in sequence:

src/bin/e2e_tests_full.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ use torrust_dependency_installer::Dependency;
5353
use tracing::{error, info, warn};
5454

5555
use torrust_tracker_deployer_lib::bootstrap::logging::{LogFormat, LogOutput, LoggingBuilder};
56-
use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::verify_required_dependencies;
57-
use torrust_tracker_deployer_lib::testing::e2e::tasks::virtual_machine::preflight_cleanup::{
58-
preflight_cleanup_previous_resources, PreflightCleanupContext,
56+
use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::{
57+
run_preflight_cleanup, verify_required_dependencies,
5958
};
6059
use torrust_tracker_deployer_lib::testing::e2e::ProcessRunner;
6160

@@ -150,54 +149,6 @@ fn main() -> Result<()> {
150149
test_result
151150
}
152151

153-
/// Performs preflight cleanup to remove artifacts from previous test runs.
154-
///
155-
/// This ensures a clean slate before starting new tests by removing:
156-
/// - Build directory
157-
/// - Templates directory
158-
/// - Data directory for this environment
159-
/// - LXD resources (instance and profile)
160-
///
161-
/// # Arguments
162-
///
163-
/// * `environment_name` - The name of the environment to clean up
164-
///
165-
/// # Errors
166-
///
167-
/// Returns an error if cleanup fails.
168-
fn run_preflight_cleanup(environment_name: &str) -> Result<()> {
169-
use torrust_tracker_deployer_lib::domain::EnvironmentName;
170-
171-
info!(
172-
operation = "preflight_cleanup",
173-
environment = environment_name,
174-
"Running preflight cleanup"
175-
);
176-
177-
// Create preflight cleanup context with paths for the environment
178-
let cleanup_context = PreflightCleanupContext::new(
179-
format!("./build/{environment_name}").into(),
180-
format!("./templates/{environment_name}").into(),
181-
EnvironmentName::new(environment_name).expect("Valid environment name"),
182-
format!("torrust-tracker-vm-{environment_name}")
183-
.try_into()
184-
.expect("Valid instance name"),
185-
format!("torrust-profile-{environment_name}")
186-
.try_into()
187-
.expect("Valid profile name"),
188-
);
189-
190-
preflight_cleanup_previous_resources(&cleanup_context)?;
191-
192-
info!(
193-
operation = "preflight_cleanup",
194-
status = "success",
195-
"Preflight cleanup completed"
196-
);
197-
198-
Ok(())
199-
}
200-
201152
/// Runs the full E2E test workflow using CLI commands.
202153
///
203154
/// Executes the following commands in sequence:

src/testing/e2e/tasks/black_box/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
//!
66
//! ## Module Structure
77
//!
8+
//! - `preflight_cleanup` - Remove artifacts from previous test runs
89
//! - `verify_dependencies` - Verify required system dependencies are installed
910
11+
pub mod preflight_cleanup;
1012
pub mod verify_dependencies;
1113

1214
// Re-export commonly used items
15+
pub use preflight_cleanup::run_preflight_cleanup;
1316
pub use verify_dependencies::verify_required_dependencies;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//! Preflight cleanup task for black-box E2E tests.
2+
//!
3+
//! This module provides a shared function to perform preflight cleanup
4+
//! before running E2E tests, ensuring a clean slate by removing artifacts
5+
//! from previous test runs.
6+
//!
7+
//! # Example
8+
//!
9+
//! ```rust,ignore
10+
//! use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::run_preflight_cleanup;
11+
//!
12+
//! // Clean up artifacts from previous "e2e-provision" test runs
13+
//! run_preflight_cleanup("e2e-provision")?;
14+
//! ```
15+
16+
use anyhow::Result;
17+
use tracing::info;
18+
19+
use crate::domain::EnvironmentName;
20+
use crate::testing::e2e::tasks::virtual_machine::preflight_cleanup::{
21+
preflight_cleanup_previous_resources, PreflightCleanupContext,
22+
};
23+
24+
/// Performs preflight cleanup to remove artifacts from previous test runs.
25+
///
26+
/// This ensures a clean slate before starting new tests by removing:
27+
/// - Build directory
28+
/// - Templates directory
29+
/// - Data directory for this environment
30+
/// - LXD resources (instance and profile)
31+
///
32+
/// # Arguments
33+
///
34+
/// * `environment_name` - The name of the environment to clean up
35+
///
36+
/// # Errors
37+
///
38+
/// Returns an error if cleanup fails.
39+
///
40+
/// # Panics
41+
///
42+
/// Panics if the environment name, instance name, or profile name is invalid.
43+
/// This should not happen with valid E2E test environment names.
44+
///
45+
/// # Example
46+
///
47+
/// ```rust,ignore
48+
/// use torrust_tracker_deployer_lib::testing::e2e::tasks::black_box::run_preflight_cleanup;
49+
///
50+
/// run_preflight_cleanup("e2e-provision")?;
51+
/// run_preflight_cleanup("e2e-full")?;
52+
/// ```
53+
pub fn run_preflight_cleanup(environment_name: &str) -> Result<()> {
54+
info!(
55+
operation = "preflight_cleanup",
56+
environment = environment_name,
57+
"Running preflight cleanup"
58+
);
59+
60+
// Create preflight cleanup context with paths for the environment
61+
let cleanup_context = PreflightCleanupContext::new(
62+
format!("./build/{environment_name}").into(),
63+
format!("./templates/{environment_name}").into(),
64+
EnvironmentName::new(environment_name).expect("Valid environment name"),
65+
format!("torrust-tracker-vm-{environment_name}")
66+
.try_into()
67+
.expect("Valid instance name"),
68+
format!("torrust-profile-{environment_name}")
69+
.try_into()
70+
.expect("Valid profile name"),
71+
);
72+
73+
preflight_cleanup_previous_resources(&cleanup_context)?;
74+
75+
info!(
76+
operation = "preflight_cleanup",
77+
status = "success",
78+
"Preflight cleanup completed"
79+
);
80+
81+
Ok(())
82+
}

0 commit comments

Comments
 (0)