-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe2e_provision_and_destroy_tests.rs
More file actions
281 lines (252 loc) · 10.2 KB
/
e2e_provision_and_destroy_tests.rs
File metadata and controls
281 lines (252 loc) · 10.2 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
//! End-to-End Provisioning and Destruction Tests for Torrust Tracker Deployer
//!
//! This binary tests the complete infrastructure lifecycle: provisioning and destruction.
//! It creates VMs/containers using `OpenTofu`, validates infrastructure provisioning,
//! and then destroys the infrastructure using the `DestroyCommandHandler` (with fallback to
//! manual cleanup on failure). This does NOT test software configuration or installation.
//!
//! ## Usage
//!
//! Run the E2E provisioning and destruction tests:
//!
//! ```bash
//! cargo run --bin e2e-provision-and-destroy-tests
//! ```
//!
//! Run with custom options:
//!
//! ```bash
//! # Keep test environment after completion (for debugging)
//! cargo run --bin e2e-provision-and-destroy-tests -- --keep
//!
//! # Change logging format
//! cargo run --bin e2e-provision-and-destroy-tests -- --log-format json
//!
//! # Show help
//! cargo run --bin e2e-provision-and-destroy-tests -- --help
//! ```
//!
//! ## Test Workflow
//!
//! 1. **Preflight cleanup** - Remove any artifacts from previous test runs that may have failed to clean up
//! 2. **Infrastructure provisioning** - Create VMs/containers using `OpenTofu`
//! 3. **Basic validation** - Verify VM is created and cloud-init completed
//! 4. **Infrastructure destruction** - Destroy infrastructure using `DestroyCommandHandler` (with fallback to manual cleanup)
//!
//! ## Two-Phase Cleanup Strategy
//!
//! The cleanup process happens in two distinct phases:
//!
//! - **Phase 1 - Preflight cleanup**: Removes artifacts from previous test runs that may have
//! failed to clean up properly (executed at the start in main function)
//! - **Phase 2 - Infrastructure destruction**: Destroys resources created specifically during
//! the current test run using `DestroyCommandHandler`, with fallback to manual cleanup on failure
//! (executed at the end in main function)
//!
//! This approach provides comprehensive E2E testing of the full provision+destroy lifecycle
//! while ensuring reliable cleanup in CI environments.
use anyhow::Result;
use clap::Parser;
use std::net::IpAddr;
use std::time::Instant;
use tracing::{error, info};
// Import E2E testing infrastructure
use torrust_tracker_deployer_lib::adapters::ssh::{SshCredentials, DEFAULT_SSH_PORT};
use torrust_tracker_deployer_lib::bootstrap::logging::{LogFormat, LogOutput, LoggingBuilder};
use torrust_tracker_deployer_lib::domain::{Environment, EnvironmentName};
use torrust_tracker_deployer_lib::shared::Username;
use torrust_tracker_deployer_lib::testing::e2e::context::{TestContext, TestContextType};
use torrust_tracker_deployer_lib::testing::e2e::tasks::virtual_machine::{
cleanup_infrastructure::cleanup_test_infrastructure,
preflight_cleanup::preflight_cleanup_previous_resources,
run_destroy_command::run_destroy_command, run_provision_command::run_provision_command,
};
#[derive(Parser)]
#[command(name = "e2e-provision-and-destroy-tests")]
#[command(about = "E2E provisioning and destruction tests for Torrust Tracker Deployer")]
struct Cli {
/// Keep the test environment after completion
#[arg(long)]
keep: bool,
/// Logging format to use
#[arg(
long,
default_value = "pretty",
help = "Logging format: pretty, json, or compact"
)]
log_format: LogFormat,
}
/// Main entry point for the E2E provisioning and destruction test suite
///
/// This function orchestrates the complete provision+destroy test workflow:
/// 1. Initializes logging and test environment
/// 2. Performs pre-flight cleanup
/// 3. Runs provisioning tests (infrastructure creation only)
/// 4. Destroys infrastructure using `DestroyCommandHandler` (with fallback to manual cleanup)
/// 5. Reports results
///
/// Returns `Ok(())` if all tests pass, `Err` otherwise.
///
/// # Errors
///
/// This function may return errors in the following cases:
/// - Invalid environment name provided via CLI
/// - Test environment setup fails
/// - Pre-flight cleanup encounters issues
/// - Infrastructure provisioning fails
/// - Destruction operations fail (both `DestroyCommandHandler` and manual cleanup fallback)
///
/// # Panics
///
/// This function may panic if the hardcoded username "torrust" is invalid,
/// which should never happen in normal operation.
#[tokio::main]
pub async fn main() -> Result<()> {
let cli = Cli::parse();
// Initialize logging based on the chosen format with stderr output for test visibility
// E2E tests use production log location: ./data/logs using the builder pattern
LoggingBuilder::new(std::path::Path::new("./data/logs"))
.with_format(cli.log_format.clone())
.with_output(LogOutput::FileAndStderr)
.init();
info!(
application = "torrust_tracker_deployer",
test_suite = "e2e_provision_and_destroy_tests",
log_format = ?cli.log_format,
"Starting E2E provisioning and destruction tests"
);
// Create environment entity for e2e-provision testing
let environment_name = EnvironmentName::new("e2e-provision".to_string())?;
// Use absolute paths to project root for SSH keys to ensure they can be found by Ansible
let project_root = std::env::current_dir().expect("Failed to get current directory");
let ssh_private_key_path = project_root.join("fixtures/testing_rsa");
let ssh_public_key_path = project_root.join("fixtures/testing_rsa.pub");
let ssh_user = Username::new("torrust").expect("Valid hardcoded username");
let ssh_credentials = SshCredentials::new(
ssh_private_key_path.clone(),
ssh_public_key_path.clone(),
ssh_user.clone(),
);
let ssh_port = DEFAULT_SSH_PORT;
let environment = Environment::new(environment_name, ssh_credentials, ssh_port);
let mut test_context =
TestContext::from_environment(cli.keep, environment, TestContextType::VirtualMachine)?
.init()?;
// Cleanup any artifacts from previous test runs that may have failed to clean up
// This ensures a clean slate before starting new tests
preflight_cleanup_previous_resources(&test_context)?;
let test_start = Instant::now();
let provision_result = run_provisioning_test(&mut test_context).await;
// Always cleanup test infrastructure created during this test run
// Try using DestroyCommandHandlerHandler first, fallback to manual cleanup on failure
// This ensures proper resource cleanup regardless of test success or failure
run_infrastructure_destroy(&mut test_context);
let test_duration = test_start.elapsed();
info!(
performance = "test_execution",
duration_secs = test_duration.as_secs_f64(),
duration = ?test_duration,
"Provisioning and destruction test execution completed"
);
// Handle provisioning test results
match provision_result {
Ok(_) => {
info!(
test_suite = "e2e_provision_and_destroy_tests",
status = "success",
"All provisioning and destruction tests passed successfully"
);
Ok(())
}
Err(provision_err) => {
error!(
test_suite = "e2e_provision_and_destroy_tests",
status = "failed",
error = %provision_err,
"Infrastructure provisioning failed"
);
Err(provision_err)
}
}
}
/// Runs the provisioning test workflow
///
/// This function focuses exclusively on infrastructure provisioning and validation.
/// It does NOT attempt to configure software or install applications.
///
/// # Test Phases
///
/// 1. **Provision Infrastructure**: Creates VMs/containers using `OpenTofu`
/// 2. **Basic Validation**: Verifies infrastructure is ready (cloud-init completed)
///
/// Returns the provisioned instance IP address on success.
async fn run_provisioning_test(env: &mut TestContext) -> Result<IpAddr> {
info!(
test_type = "provision_only",
workflow = "infrastructure_provisioning",
"Starting infrastructure provisioning E2E test"
);
run_provision_command(env)
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
// Extract instance IP from the updated TestContext
let instance_ip = env
.environment
.instance_ip()
.expect("Instance IP must be set after successful provisioning");
info!(
status = "success",
instance_ip = %instance_ip,
"Infrastructure provisioning completed successfully"
);
info!(
test_type = "provision_only",
status = "success",
note = "VM/container created and cloud-init completed - ready for configuration",
"Provisioning E2E test completed successfully"
);
// Return the instance IP for potential future validation
Ok(instance_ip)
}
/// Runs the infrastructure destruction workflow
///
/// This function destroys infrastructure using the `DestroyCommandHandler` with fallback
/// to manual cleanup if the command fails. This ensures reliable cleanup in all scenarios.
///
/// # Destruction Strategy
///
/// 1. **Try `DestroyCommandHandler`**: Use the application layer command for destruction
/// 2. **Fallback to Manual Cleanup**: If `DestroyCommandHandler` fails, use manual cleanup functions
fn run_infrastructure_destroy(test_context: &mut TestContext) {
use tracing::warn;
info!(
test_type = "destroy",
workflow = "infrastructure_destruction",
"Starting infrastructure destruction E2E test"
);
// Try using the DestroyCommandHandler first
match run_destroy_command(test_context) {
Ok(()) => {
info!(
status = "success",
method = "destroy_command",
"Infrastructure destroyed successfully using DestroyCommandHandler"
);
}
Err(e) => {
warn!(
status = "failed",
method = "destroy_command",
error = %e,
"DestroyCommandHandler failed, falling back to manual cleanup"
);
// Fallback to manual cleanup
cleanup_test_infrastructure(test_context);
info!(
status = "success",
method = "manual_cleanup",
"Infrastructure destroyed using manual cleanup fallback"
);
}
}
}