Skip to content

Commit 0e7d5a0

Browse files
Copilotjosecelano
andcommitted
fix: [#37] add data directory cleanup to E2E preflight
- Add cleanup_data_environment() helper to preflight_cleanup.rs - Clean data/{environment_name} directory before E2E test runs - Call from both container and VM preflight cleanup functions - Prevents "environment already exists" errors from stale state - Ensures proper test isolation by starting with clean data directory - Follows same pattern as build and templates directory cleanup Co-authored-by: josecelano <[email protected]>
1 parent ce59c6a commit 0e7d5a0

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

src/testing/e2e/tasks/container/preflight_cleanup.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
use crate::shared::command::CommandExecutor;
99
use crate::testing::e2e::context::TestContext;
1010
use crate::testing::e2e::tasks::preflight_cleanup::{
11-
cleanup_build_directory, cleanup_templates_directory, PreflightCleanupError,
11+
cleanup_build_directory, cleanup_data_environment, cleanup_templates_directory,
12+
PreflightCleanupError,
1213
};
1314
use tracing::{info, warn};
1415

@@ -44,6 +45,9 @@ pub fn preflight_cleanup_previous_resources(
4445
// Clean the templates directory to ensure fresh embedded template extraction for E2E tests
4546
cleanup_templates_directory(env)?;
4647

48+
// Clean the data directory to ensure fresh environment state for E2E tests
49+
cleanup_data_environment(env)?;
50+
4751
// Clean up any hanging Docker containers from interrupted test runs
4852
cleanup_hanging_docker_containers(env);
4953

src/testing/e2e/tasks/preflight_cleanup.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,79 @@ pub fn cleanup_templates_directory(
192192
}
193193
}
194194
}
195+
196+
/// Cleans the data directory for the test environment to ensure fresh state for E2E tests
197+
///
198+
/// This function removes the environment's data directory if it exists, ensuring that
199+
/// E2E tests start with a clean state and don't encounter conflicts with stale
200+
/// environment data from previous test runs. This prevents "environment already exists"
201+
/// errors and ensures proper test isolation.
202+
///
203+
/// # Safety
204+
///
205+
/// This function is only intended for E2E test environments and should never
206+
/// be called in production code paths. It's designed to provide test isolation
207+
/// by ensuring fresh environment state for each test run.
208+
///
209+
/// # Arguments
210+
///
211+
/// * `test_context` - The test context containing the environment configuration
212+
///
213+
/// # Returns
214+
///
215+
/// Returns `Ok(())` if cleanup succeeds or if the data directory doesn't exist.
216+
///
217+
/// # Errors
218+
///
219+
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the data directory
220+
/// cannot be removed due to permission issues or file locks.
221+
pub fn cleanup_data_environment(test_context: &TestContext) -> Result<(), PreflightCleanupError> {
222+
use std::path::Path;
223+
224+
// Construct the data directory path: data/{environment_name}
225+
let data_dir = Path::new("data").join(test_context.environment.name().as_str());
226+
227+
if !data_dir.exists() {
228+
info!(
229+
operation = "data_directory_cleanup",
230+
status = "clean",
231+
path = %data_dir.display(),
232+
"Data directory doesn't exist, skipping cleanup"
233+
);
234+
return Ok(());
235+
}
236+
237+
info!(
238+
operation = "data_directory_cleanup",
239+
path = %data_dir.display(),
240+
"Cleaning data directory for previous test environment"
241+
);
242+
243+
match std::fs::remove_dir_all(&data_dir) {
244+
Ok(()) => {
245+
info!(
246+
operation = "data_directory_cleanup",
247+
status = "success",
248+
path = %data_dir.display(),
249+
"Data directory cleaned successfully"
250+
);
251+
Ok(())
252+
}
253+
Err(e) => {
254+
warn!(
255+
operation = "data_directory_cleanup",
256+
status = "failed",
257+
path = %data_dir.display(),
258+
error = %e,
259+
"Failed to clean data directory"
260+
);
261+
Err(PreflightCleanupError::ResourceConflicts {
262+
details: format!(
263+
"Failed to clean data directory '{}': {}",
264+
data_dir.display(),
265+
e
266+
),
267+
})
268+
}
269+
}
270+
}

src/testing/e2e/tasks/virtual_machine/preflight_cleanup.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::adapters::tofu;
99
use crate::infrastructure::external_tools::tofu::OPENTOFU_SUBFOLDER;
1010
use crate::testing::e2e::context::TestContext;
1111
use crate::testing::e2e::tasks::preflight_cleanup::{
12-
cleanup_build_directory, cleanup_templates_directory, PreflightCleanupError,
12+
cleanup_build_directory, cleanup_data_environment, cleanup_templates_directory,
13+
PreflightCleanupError,
1314
};
1415
use tracing::{info, warn};
1516

@@ -44,6 +45,9 @@ pub fn preflight_cleanup_previous_resources(
4445
// Clean the templates directory to ensure fresh embedded template extraction for E2E tests
4546
cleanup_templates_directory(test_context)?;
4647

48+
// Clean the data directory to ensure fresh environment state for E2E tests
49+
cleanup_data_environment(test_context)?;
50+
4751
// Clean any existing OpenTofu infrastructure from previous test runs
4852
cleanup_opentofu_infrastructure(test_context)?;
4953

0 commit comments

Comments
 (0)