|
1 | | -//! Pre-flight cleanup module for E2E tests |
| 1 | +//! Generic preflight cleanup functionality |
2 | 2 | //! |
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. |
13 | 6 |
|
14 | 7 | use std::fmt; |
15 | 8 |
|
| 9 | +use crate::e2e::environment::TestEnvironment; |
16 | 10 | use crate::infrastructure::adapters::opentofu::EmergencyDestroyError; |
| 11 | +use tracing::{info, warn}; |
17 | 12 |
|
18 | 13 | // Re-export functions from the new modular structure for backward compatibility |
19 | 14 | pub use crate::e2e::tasks::container::preflight_cleanup::cleanup_lingering_resources; |
@@ -52,3 +47,146 @@ impl std::error::Error for PreflightCleanupError { |
52 | 47 | } |
53 | 48 | } |
54 | 49 | } |
| 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 | +} |
0 commit comments