-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreflight_cleanup.rs
More file actions
364 lines (338 loc) · 12.4 KB
/
Copy pathpreflight_cleanup.rs
File metadata and controls
364 lines (338 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! Generic preflight cleanup functionality
//!
//! This module provides directory cleanup functions that are used by both
//! container-based and VM-based E2E testing workflows. These functions handle
//! the cleanup of build and template directories to ensure test isolation.
use std::fmt;
use crate::adapters::tofu::EmergencyDestroyError;
use crate::testing::e2e::context::TestContext;
use tracing::{info, warn};
// Re-export functions from the new modular structure for backward compatibility
pub use crate::testing::e2e::tasks::container::preflight_cleanup::preflight_cleanup_previous_resources;
/// Errors that can occur during pre-flight cleanup operations
#[derive(Debug)]
pub enum PreflightCleanupError {
/// Emergency destroy operation failed
EmergencyDestroyFailed { source: EmergencyDestroyError },
/// Resource conflicts detected that would prevent new test runs
ResourceConflicts { details: String },
}
impl fmt::Display for PreflightCleanupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmergencyDestroyFailed { source } => {
write!(f, "Emergency destroy operation failed: {source}")
}
Self::ResourceConflicts { details } => {
write!(
f,
"Resource conflicts detected that would prevent new test runs: {details}"
)
}
}
}
}
impl std::error::Error for PreflightCleanupError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::EmergencyDestroyFailed { source } => Some(source),
Self::ResourceConflicts { .. } => None,
}
}
}
// TODO: Refactor TestContext to eliminate the need for this workaround function
//
// Current issue: TestContext requires an Environment, but we need to clean up data
// directories BEFORE creating the Environment (because CreateCommandHandler checks
// if the environment already exists in the repository).
//
// Proposed solutions:
// 1. Make Environment optional in TestContext (TestContext { environment: Option<Environment> })
// 2. Move Environment out of TestContext (preferred - better separation of concerns)
//
// The second option is better because:
// - TestContext should manage test infrastructure (services, config, temp directories)
// - Environment is a domain entity that represents deployment state
// - Separating them provides clearer responsibilities and easier testing
//
// After refactoring, we could eliminate this standalone function and have all cleanup
// go through a single preflight_cleanup_previous_resources() that doesn't require
// a fully initialized TestContext with an Environment.
/// Cleans the data directory for a specific environment name before `TestContext` creation
///
/// This helper function removes the `data/{environment_name}` directory to prevent
/// "environment already exists" errors when `CreateCommandHandler` checks the repository.
/// Unlike `cleanup_data_environment`, this function works without a `TestContext` and is
/// intended to be called early in the test setup before environment creation.
///
/// # Safety
///
/// This function is only intended for E2E test environments and should never
/// be called in production code paths. It's designed to provide test isolation
/// by ensuring environments from previous test runs don't interfere.
///
/// # Arguments
///
/// * `environment_name` - The name of the environment to clean up
///
/// # Returns
///
/// Returns `Ok(())` if cleanup succeeds or if the directory doesn't exist.
///
/// # Errors
///
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the data directory
/// cannot be removed due to permission issues or file locks.
pub fn cleanup_previous_test_data(environment_name: &str) -> Result<(), PreflightCleanupError> {
use std::path::Path;
let data_dir = Path::new("data").join(environment_name);
if !data_dir.exists() {
info!(
operation = "preflight_data_cleanup",
status = "clean",
path = %data_dir.display(),
"No previous data directory found, skipping cleanup"
);
return Ok(());
}
info!(
operation = "preflight_data_cleanup",
path = %data_dir.display(),
"Cleaning data directory from previous test run"
);
match std::fs::remove_dir_all(&data_dir) {
Ok(()) => {
info!(
operation = "preflight_data_cleanup",
status = "success",
path = %data_dir.display(),
"Data directory cleaned successfully"
);
Ok(())
}
Err(e) => {
warn!(
operation = "preflight_data_cleanup",
status = "failed",
path = %data_dir.display(),
error = %e,
"Failed to clean data directory"
);
Err(PreflightCleanupError::ResourceConflicts {
details: format!(
"Failed to clean data directory '{}': {}",
data_dir.display(),
e
),
})
}
}
}
/// Cleans the build directory to ensure fresh template state for E2E tests
///
/// This function removes the build directory if it exists, ensuring that
/// E2E tests start with a clean state and don't use stale cached template files.
///
/// # Safety
///
/// This function is only intended for E2E test environments and should never
/// be called in production code paths. It's designed to provide test isolation
/// by ensuring fresh template rendering for each test run.
///
/// # Arguments
///
/// * `env` - The test environment containing configuration paths
///
/// # Returns
///
/// Returns `Ok(())` if cleanup succeeds or if the build directory doesn't exist.
///
/// # Errors
///
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the build directory
/// cannot be removed due to permission issues or file locks.
pub fn cleanup_build_directory(test_context: &TestContext) -> Result<(), PreflightCleanupError> {
let build_dir = &test_context.config.build_dir;
if !build_dir.exists() {
info!(
operation = "build_directory_cleanup",
status = "clean",
path = %build_dir.display(),
"Build directory doesn't exist, skipping cleanup"
);
return Ok(());
}
info!(
operation = "build_directory_cleanup",
path = %build_dir.display(),
"Cleaning build directory to ensure fresh template state"
);
match std::fs::remove_dir_all(build_dir) {
Ok(()) => {
info!(
operation = "build_directory_cleanup",
status = "success",
path = %build_dir.display(),
"Build directory cleaned successfully"
);
Ok(())
}
Err(e) => {
warn!(
operation = "build_directory_cleanup",
status = "failed",
path = %build_dir.display(),
error = %e,
"Failed to clean build directory"
);
Err(PreflightCleanupError::ResourceConflicts {
details: format!(
"Failed to clean build directory '{}': {}",
build_dir.display(),
e
),
})
}
}
}
/// Cleans the templates directory to ensure fresh embedded template extraction for E2E tests
///
/// This function removes the templates directory if it exists, ensuring that
/// E2E tests start with fresh embedded templates and don't use stale cached template files.
/// This is critical for testing template changes and instance name parameterization.
///
/// # Safety
///
/// This function is only intended for E2E test environments and should never
/// be called in production code paths. It's designed to provide test isolation
/// by ensuring fresh template extraction for each test run.
///
/// # Arguments
///
/// * `env` - The test environment containing configuration paths
///
/// # Returns
///
/// Returns `Ok(())` if cleanup succeeds or if the templates directory doesn't exist.
///
/// # Errors
///
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the templates directory
/// cannot be removed due to permission issues or file locks.
pub fn cleanup_templates_directory(
test_context: &TestContext,
) -> Result<(), PreflightCleanupError> {
let templates_dir = std::path::Path::new(&test_context.config.templates_dir);
if !templates_dir.exists() {
info!(
operation = "templates_directory_cleanup",
status = "clean",
path = %templates_dir.display(),
"Templates directory doesn't exist, skipping cleanup"
);
return Ok(());
}
info!(
operation = "templates_directory_cleanup",
path = %templates_dir.display(),
"Cleaning templates directory to ensure fresh embedded template extraction"
);
match std::fs::remove_dir_all(templates_dir) {
Ok(()) => {
info!(
operation = "templates_directory_cleanup",
status = "success",
path = %templates_dir.display(),
"Templates directory cleaned successfully"
);
Ok(())
}
Err(e) => {
warn!(
operation = "templates_directory_cleanup",
status = "failed",
path = %templates_dir.display(),
error = %e,
"Failed to clean templates directory"
);
Err(PreflightCleanupError::ResourceConflicts {
details: format!(
"Failed to clean templates directory '{}': {}",
templates_dir.display(),
e
),
})
}
}
}
/// Cleans the data directory for the test environment to ensure fresh state for E2E tests
///
/// This function removes the environment's data directory if it exists, ensuring that
/// E2E tests start with a clean state and don't encounter conflicts with stale
/// environment data from previous test runs. This prevents "environment already exists"
/// errors and ensures proper test isolation.
///
/// # Safety
///
/// This function is only intended for E2E test environments and should never
/// be called in production code paths. It's designed to provide test isolation
/// by ensuring fresh environment state for each test run.
///
/// # Arguments
///
/// * `test_context` - The test context containing the environment configuration
///
/// # Returns
///
/// Returns `Ok(())` if cleanup succeeds or if the data directory doesn't exist.
///
/// # Errors
///
/// Returns a `PreflightCleanupError::ResourceConflicts` error if the data directory
/// cannot be removed due to permission issues or file locks.
pub fn cleanup_data_environment(test_context: &TestContext) -> Result<(), PreflightCleanupError> {
use std::path::Path;
// Construct the data directory path: data/{environment_name}
let data_dir = Path::new("data").join(test_context.environment.name().as_str());
if !data_dir.exists() {
info!(
operation = "data_directory_cleanup",
status = "clean",
path = %data_dir.display(),
"Data directory doesn't exist, skipping cleanup"
);
return Ok(());
}
info!(
operation = "data_directory_cleanup",
path = %data_dir.display(),
"Cleaning data directory for previous test environment"
);
match std::fs::remove_dir_all(&data_dir) {
Ok(()) => {
info!(
operation = "data_directory_cleanup",
status = "success",
path = %data_dir.display(),
"Data directory cleaned successfully"
);
Ok(())
}
Err(e) => {
warn!(
operation = "data_directory_cleanup",
status = "failed",
path = %data_dir.display(),
error = %e,
"Failed to clean data directory"
);
Err(PreflightCleanupError::ResourceConflicts {
details: format!(
"Failed to clean data directory '{}': {}",
data_dir.display(),
e
),
})
}
}
}