Skip to content

Commit 90329e6

Browse files
committed
refactor: consolidate preflight cleanup modules
Move contents from preflight_cleanup_common.rs to preflight_cleanup.rs to consolidate the preflight cleanup functionality into a single module. This simplifies the module structure by: - Removing the preflight_cleanup_common module - Moving cleanup_build_directory and cleanup_templates_directory functions - Preserving existing error types and compatibility - Maintaining backward compatibility for imports All tests, linting, and E2E functionality verified as working.
1 parent b73a742 commit 90329e6

File tree

5 files changed

+154
-175
lines changed

5 files changed

+154
-175
lines changed

src/e2e/tasks/container/preflight_cleanup.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
//! directory cleanup operations.
77
88
use crate::e2e::environment::TestEnvironment;
9-
use crate::e2e::tasks::preflight_cleanup::PreflightCleanupError;
10-
use crate::e2e::tasks::preflight_cleanup_common::{
11-
cleanup_build_directory, cleanup_templates_directory,
9+
use crate::e2e::tasks::preflight_cleanup::{
10+
cleanup_build_directory, cleanup_templates_directory, PreflightCleanupError,
1211
};
1312
use tracing::info;
1413

src/e2e/tasks/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@
2222
//! ### Virtual machine-specific tasks (`virtual_machine` submodule):
2323
//! - `run_provision_command` - Infrastructure provisioning via `OpenTofu`
2424
//! - `cleanup_infrastructure` - Infrastructure resource cleanup
25-
//! - `preflight_cleanup` - VM-specific preflight cleanup (`OpenTofu` + LXD)
26-
//!
27-
//! ### Common functionality:
28-
//! - `preflight_cleanup_common` - Shared directory cleanup functions
29-
//! - `preflight_cleanup` - Legacy module with common error types and functions
25+
//! - `preflight_cleanup` - Shared directory cleanup functions and error types for both VM and container tests
3026
//!
3127
//! These tasks are orchestrated by the E2E test binaries to provide comprehensive
3228
//! testing coverage of the entire deployment system.
3329
3430
pub mod container;
3531
pub mod preflight_cleanup;
36-
pub mod preflight_cleanup_common;
3732
pub mod run_configure_command;
3833
pub mod run_deployment_validation;
3934
pub mod run_test_command;

src/e2e/tasks/preflight_cleanup.rs

Lines changed: 149 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
//! Pre-flight cleanup module for E2E tests
1+
//! Generic preflight cleanup functionality
22
//!
3-
//! This module provides functionality to clean up any lingering resources
4-
//! from previous test runs that may have been interrupted before cleanup.
5-
//!
6-
//! ## Migration Notice
7-
//!
8-
//! This module now serves as a compatibility layer that re-exports functions
9-
//! from the new modularized structure:
10-
//! - Container-specific functions are in `container::preflight_cleanup`
11-
//! - VM-specific functions are in `virtual_machine::preflight_cleanup`
12-
//! - Common directory cleanup functions are in `preflight_cleanup_common`
3+
//! This module provides directory cleanup functions that are used by both
4+
//! container-based and VM-based E2E testing workflows. These functions handle
5+
//! the cleanup of build and template directories to ensure test isolation.
136
147
use std::fmt;
158

9+
use crate::e2e::environment::TestEnvironment;
1610
use crate::infrastructure::adapters::opentofu::EmergencyDestroyError;
11+
use tracing::{info, warn};
1712

1813
// Re-export functions from the new modular structure for backward compatibility
1914
pub use crate::e2e::tasks::container::preflight_cleanup::cleanup_lingering_resources;
@@ -52,3 +47,146 @@ impl std::error::Error for PreflightCleanupError {
5247
}
5348
}
5449
}
50+
51+
/// Cleans the build directory to ensure fresh template state for E2E tests
52+
///
53+
/// This function removes the build directory if it exists, ensuring that
54+
/// E2E tests start with a clean state and don't use stale cached template files.
55+
///
56+
/// # Safety
57+
///
58+
/// This function is only intended for E2E test environments and should never
59+
/// be called in production code paths. It's designed to provide test isolation
60+
/// by ensuring fresh template rendering for each test run.
61+
///
62+
/// # Arguments
63+
///
64+
/// * `env` - The test environment containing configuration paths
65+
///
66+
/// # Returns
67+
///
68+
/// Returns `Ok(())` if cleanup succeeds or if the build directory doesn't exist.
69+
///
70+
/// # Errors
71+
///
72+
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the build directory
73+
/// cannot be removed due to permission issues or file locks.
74+
pub fn cleanup_build_directory(env: &TestEnvironment) -> Result<(), PreflightCleanupError> {
75+
let build_dir = &env.config.build_dir;
76+
77+
if !build_dir.exists() {
78+
info!(
79+
operation = "build_directory_cleanup",
80+
status = "clean",
81+
path = %build_dir.display(),
82+
"Build directory doesn't exist, skipping cleanup"
83+
);
84+
return Ok(());
85+
}
86+
87+
info!(
88+
operation = "build_directory_cleanup",
89+
path = %build_dir.display(),
90+
"Cleaning build directory to ensure fresh template state"
91+
);
92+
93+
match std::fs::remove_dir_all(build_dir) {
94+
Ok(()) => {
95+
info!(
96+
operation = "build_directory_cleanup",
97+
status = "success",
98+
path = %build_dir.display(),
99+
"Build directory cleaned successfully"
100+
);
101+
Ok(())
102+
}
103+
Err(e) => {
104+
warn!(
105+
operation = "build_directory_cleanup",
106+
status = "failed",
107+
path = %build_dir.display(),
108+
error = %e,
109+
"Failed to clean build directory"
110+
);
111+
Err(PreflightCleanupError::ResourceConflicts {
112+
details: format!(
113+
"Failed to clean build directory '{}': {}",
114+
build_dir.display(),
115+
e
116+
),
117+
})
118+
}
119+
}
120+
}
121+
122+
/// Cleans the templates directory to ensure fresh embedded template extraction for E2E tests
123+
///
124+
/// This function removes the templates directory if it exists, ensuring that
125+
/// E2E tests start with fresh embedded templates and don't use stale cached template files.
126+
/// This is critical for testing template changes and instance name parameterization.
127+
///
128+
/// # Safety
129+
///
130+
/// This function is only intended for E2E test environments and should never
131+
/// be called in production code paths. It's designed to provide test isolation
132+
/// by ensuring fresh template extraction for each test run.
133+
///
134+
/// # Arguments
135+
///
136+
/// * `env` - The test environment containing configuration paths
137+
///
138+
/// # Returns
139+
///
140+
/// Returns `Ok(())` if cleanup succeeds or if the templates directory doesn't exist.
141+
///
142+
/// # Errors
143+
///
144+
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the templates directory
145+
/// cannot be removed due to permission issues or file locks.
146+
pub fn cleanup_templates_directory(env: &TestEnvironment) -> Result<(), PreflightCleanupError> {
147+
let templates_dir = std::path::Path::new(&env.config.templates_dir);
148+
149+
if !templates_dir.exists() {
150+
info!(
151+
operation = "templates_directory_cleanup",
152+
status = "clean",
153+
path = %templates_dir.display(),
154+
"Templates directory doesn't exist, skipping cleanup"
155+
);
156+
return Ok(());
157+
}
158+
159+
info!(
160+
operation = "templates_directory_cleanup",
161+
path = %templates_dir.display(),
162+
"Cleaning templates directory to ensure fresh embedded template extraction"
163+
);
164+
165+
match std::fs::remove_dir_all(templates_dir) {
166+
Ok(()) => {
167+
info!(
168+
operation = "templates_directory_cleanup",
169+
status = "success",
170+
path = %templates_dir.display(),
171+
"Templates directory cleaned successfully"
172+
);
173+
Ok(())
174+
}
175+
Err(e) => {
176+
warn!(
177+
operation = "templates_directory_cleanup",
178+
status = "failed",
179+
path = %templates_dir.display(),
180+
error = %e,
181+
"Failed to clean templates directory"
182+
);
183+
Err(PreflightCleanupError::ResourceConflicts {
184+
details: format!(
185+
"Failed to clean templates directory '{}': {}",
186+
templates_dir.display(),
187+
e
188+
),
189+
})
190+
}
191+
}
192+
}

src/e2e/tasks/preflight_cleanup_common.rs

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/e2e/tasks/virtual_machine/preflight_cleanup.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
//! infrastructure resources including `OpenTofu` state and LXD instances.
66
77
use crate::e2e::environment::TestEnvironment;
8-
use crate::e2e::tasks::preflight_cleanup::PreflightCleanupError;
9-
use crate::e2e::tasks::preflight_cleanup_common::{
10-
cleanup_build_directory, cleanup_templates_directory,
8+
use crate::e2e::tasks::preflight_cleanup::{
9+
cleanup_build_directory, cleanup_templates_directory, PreflightCleanupError,
1110
};
1211
use crate::infrastructure::adapters::lxd::client::LxdClient;
1312
use crate::infrastructure::adapters::opentofu::{self};

0 commit comments

Comments
 (0)