Skip to content

Commit b59bb34

Browse files
committed
refactor: move test environment setup to main function in e2e config tests
- Move setup_test_environment call from run_configuration_tests to main function - Update run_configuration_tests to accept &TestEnvironment parameter - Eliminate duplicate configuration creation in run_provision_simulation and run_ansible_configuration - Remove unused create_container_config function and Config import - Follow same pattern as e2e_provision_tests.rs for consistency - Ensure configuration is created only once per test run
1 parent ef0412c commit b59bb34

File tree

1 file changed

+13
-49
lines changed

1 file changed

+13
-49
lines changed

src/bin/e2e_config_tests.rs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use std::time::Instant;
5555
use tracing::{error, info};
5656

5757
use torrust_tracker_deploy::application::commands::ConfigureCommand;
58-
use torrust_tracker_deploy::config::{Config, InstanceName, SshCredentials};
58+
use torrust_tracker_deploy::config::{InstanceName, SshCredentials};
5959
use torrust_tracker_deploy::container::Services;
6060
use torrust_tracker_deploy::e2e::containers::actions::{SshKeySetupAction, SshWaitAction};
6161
use torrust_tracker_deploy::e2e::containers::timeout::ContainerTimeouts;
@@ -125,7 +125,10 @@ pub async fn main() -> Result<()> {
125125
let instance_name =
126126
InstanceName::new("torrust-tracker-vm".to_string()).expect("Valid hardcoded instance name");
127127

128-
let test_result = run_configuration_tests(cli.templates_dir, instance_name).await;
128+
// Setup test environment with preflight cleanup
129+
let test_env = setup_test_environment(cli.templates_dir, instance_name)?;
130+
131+
let test_result = run_configuration_tests(&test_env).await;
129132

130133
let test_duration = test_start.elapsed();
131134

@@ -217,29 +220,26 @@ async fn configure_ssh_connectivity(
217220
}
218221

219222
/// Run the complete configuration tests
220-
async fn run_configuration_tests(templates_dir: String, instance_name: InstanceName) -> Result<()> {
223+
async fn run_configuration_tests(test_env: &TestEnvironment) -> Result<()> {
221224
info!("Starting configuration tests with Docker container");
222225

223-
// Step 0: Setup test environment with preflight cleanup
224-
let test_env = setup_test_environment(templates_dir, instance_name)?;
225-
226226
// Step 1: Setup Docker container - start with stopped state
227227
let running_container = setup_docker_container().await?;
228228

229229
// Step 2: Wait for SSH server and setup connectivity (only available when running)
230-
configure_ssh_connectivity(&running_container, &test_env).await?;
230+
configure_ssh_connectivity(&running_container, test_env).await?;
231231

232232
// Step 2.5: Run provision simulation to render Ansible templates
233233
info!("Running provision simulation to prepare container configuration");
234-
run_provision_simulation(running_container.ssh_socket_addr(), &test_env).await?;
234+
run_provision_simulation(running_container.ssh_socket_addr(), test_env).await?;
235235

236236
// Step 3: Run configuration tasks (Ansible playbooks)
237237
info!("Running Ansible configuration tasks");
238-
run_ansible_configuration(running_container.ssh_socket_addr(), &test_env)?;
238+
run_ansible_configuration(running_container.ssh_socket_addr(), test_env)?;
239239

240240
// Step 4: Validate deployment
241241
info!("Validating service deployment");
242-
run_deployment_validation(running_container.ssh_socket_addr(), &test_env).await?;
242+
run_deployment_validation(running_container.ssh_socket_addr(), test_env).await?;
243243

244244
// Step 5: Cleanup - transition back to stopped state
245245
cleanup_container(running_container);
@@ -266,15 +266,10 @@ async fn run_provision_simulation(
266266
"Running provision simulation for container"
267267
);
268268

269-
// Create SSH credentials and configuration for the container
269+
// Create SSH credentials and use configuration from test environment
270270
let ssh_credentials =
271271
create_container_ssh_credentials(&test_env.config.ssh_credentials.ssh_username)?;
272-
let config = create_container_config(
273-
&test_env.config.ssh_credentials.ssh_username,
274-
test_env.config.instance_name.clone(),
275-
test_env.config.templates_dir.clone(),
276-
)?;
277-
let services = Services::new(&config);
272+
let services = Services::new(&test_env.config);
278273

279274
// Run the Docker infrastructure provision simulation
280275
provision_docker_infrastructure(
@@ -314,14 +309,7 @@ fn run_ansible_configuration(
314309
//
315310
// For now, we'll catch the expected connection error and log it:
316311

317-
let config = create_container_config(
318-
&test_env.config.ssh_credentials.ssh_username,
319-
test_env.config.instance_name.clone(),
320-
test_env.config.templates_dir.clone(),
321-
)
322-
.context("Failed to create container configuration")?;
323-
324-
let services = Services::new(&config);
312+
let services = Services::new(&test_env.config);
325313
let configure_command = ConfigureCommand::new(Arc::clone(&services.ansible_client));
326314

327315
match configure_command.execute().map_err(anyhow::Error::from) {
@@ -396,30 +384,6 @@ fn create_test_ssh_credentials(ssh_username: &str) -> Result<SshCredentials> {
396384
))
397385
}
398386

399-
/// Create a minimal configuration for container-based testing
400-
fn create_container_config(
401-
ssh_username: &str,
402-
instance_name: InstanceName,
403-
templates_dir: String,
404-
) -> Result<Config> {
405-
// For container testing, we use fixed test SSH keys from fixtures/
406-
let ssh_credentials = create_test_ssh_credentials(ssh_username)
407-
.context("Failed to create test SSH credentials")?;
408-
409-
let project_root = std::env::current_dir().context("Failed to determine current directory")?;
410-
411-
let build_dir = project_root.join("build");
412-
413-
Ok(Config::new(
414-
false, // Don't keep environment - cleanup after tests
415-
ssh_credentials,
416-
instance_name,
417-
templates_dir,
418-
project_root,
419-
build_dir,
420-
))
421-
}
422-
423387
/// Create SSH credentials for connecting to the container
424388
fn create_container_ssh_credentials(ssh_username: &str) -> Result<SshCredentials> {
425389
// Use the centralized test SSH credentials factory

0 commit comments

Comments
 (0)