Skip to content

Commit 7dcb7ab

Browse files
committed
refactor: reorganize function order in E2E container modules for better logical flow
1 parent 95fca05 commit 7dcb7ab

File tree

3 files changed

+145
-140
lines changed

3 files changed

+145
-140
lines changed

src/bin/e2e_config_tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ async fn run_configuration_tests(test_env: &TestEnvironment) -> Result<()> {
168168
run_configure_command(test_env)?;
169169

170170
// Step 3: Run configuration validation
171-
let socket_addr = running_container.ssh_socket_addr();
172-
run_configuration_validation(socket_addr, &test_env.config.ssh_credentials).await?;
171+
run_configuration_validation(
172+
running_container.ssh_socket_addr(),
173+
&test_env.config.ssh_credentials,
174+
)
175+
.await?;
173176

174177
// Step 4: Cleanup container
175178
cleanup_infrastructure(running_container);

src/e2e/tasks/container/run_provision_simulation.rs

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,6 @@ use crate::e2e::containers::{RunningProvisionedContainer, StoppedProvisionedCont
3232
use crate::e2e::environment::TestEnvironment;
3333
use crate::infrastructure::ansible::AnsibleTemplateRenderer;
3434

35-
/// Create and start a Docker container for E2E testing
36-
///
37-
/// This function creates a new Docker container from the provisioned instance image
38-
/// and starts it, making it ready for SSH connectivity and configuration testing.
39-
///
40-
/// # Returns
41-
///
42-
/// Returns a `RunningProvisionedContainer` that can be used for:
43-
/// - SSH connectivity testing
44-
/// - Ansible configuration
45-
/// - Service validation
46-
/// - Container cleanup
47-
///
48-
/// # Errors
49-
///
50-
/// Returns an error if:
51-
/// - Container creation fails
52-
/// - Container startup fails
53-
/// - Docker daemon is not available
54-
async fn create_and_start_container() -> Result<RunningProvisionedContainer> {
55-
info!("Creating and starting Docker container for E2E testing");
56-
57-
let stopped_container = StoppedProvisionedContainer::default();
58-
let running_container = stopped_container
59-
.start()
60-
.await
61-
.context("Failed to start provisioned instance container")?;
62-
63-
info!(
64-
container_id = %running_container.container_id(),
65-
ssh_socket_addr = %running_container.ssh_socket_addr(),
66-
"Docker container setup completed successfully"
67-
);
68-
69-
Ok(running_container)
70-
}
71-
7235
/// Run provision simulation to prepare templates for container-based testing
7336
///
7437
/// This function simulates the provision phase specifically for Docker containers
@@ -119,6 +82,7 @@ pub async fn run_provision_simulation(
11982

12083
// Step 1: Setup Docker container
12184
let running_container = create_and_start_container().await?;
85+
12286
let socket_addr = running_container.ssh_socket_addr();
12387

12488
// Step 2: Establish SSH connectivity
@@ -151,48 +115,42 @@ pub async fn run_provision_simulation(
151115
Ok(running_container)
152116
}
153117

154-
/// Render Ansible configuration templates for container-based E2E testing
118+
/// Create and start a Docker container for E2E testing
155119
///
156-
/// This function renders Ansible templates with the container's connection details,
157-
/// preparing the configuration files needed for Ansible playbook execution.
158-
/// SSH connectivity is assumed to be already established by the container startup process.
120+
/// This function creates a new Docker container from the provisioned instance image
121+
/// and starts it, making it ready for SSH connectivity and configuration testing.
159122
///
160-
/// # Arguments
123+
/// # Returns
161124
///
162-
/// * `ansible_template_renderer` - Renderer for creating Ansible inventory and configuration
163-
/// * `ssh_credentials` - SSH credentials for connecting to the container
164-
/// * `socket_addr` - Socket address (IP and port) where the container can be reached
125+
/// Returns a `RunningProvisionedContainer` that can be used for:
126+
/// - SSH connectivity testing
127+
/// - Ansible configuration
128+
/// - Service validation
129+
/// - Container cleanup
165130
///
166131
/// # Errors
167132
///
168133
/// Returns an error if:
169-
/// - Ansible template rendering fails
170-
async fn render_ansible_configuration(
171-
ansible_template_renderer: Arc<AnsibleTemplateRenderer>,
172-
ssh_credentials: SshCredentials,
173-
socket_addr: SocketAddr,
174-
) -> Result<()> {
175-
info!(
176-
socket_addr = %socket_addr,
177-
"Rendering Ansible configuration templates"
178-
);
134+
/// - Container creation fails
135+
/// - Container startup fails
136+
/// - Docker daemon is not available
137+
async fn create_and_start_container() -> Result<RunningProvisionedContainer> {
138+
info!("Creating and starting Docker container for E2E testing");
179139

180-
// Step 1: Render Ansible templates with container connection details
181-
info!("Rendering Ansible templates for container");
182-
RenderAnsibleTemplatesStep::new(ansible_template_renderer, ssh_credentials, socket_addr)
183-
.execute()
184-
.await
185-
.context("Failed to render Ansible templates for container")?;
140+
let stopped_container = StoppedProvisionedContainer::default();
186141

187-
// Note: SSH connectivity check is skipped for Docker containers since
188-
// the container setup process already ensures SSH is ready and accessible
142+
let running_container = stopped_container
143+
.start()
144+
.await
145+
.context("Failed to start provisioned instance container")?;
189146

190147
info!(
191-
socket_addr = %socket_addr,
192-
"Ansible configuration templates rendered successfully"
148+
container_id = %running_container.container_id(),
149+
ssh_socket_addr = %running_container.ssh_socket_addr(),
150+
"Docker container setup completed successfully"
193151
);
194152

195-
Ok(())
153+
Ok(running_container)
196154
}
197155

198156
/// Establish SSH connectivity for a running Docker container
@@ -253,3 +211,47 @@ async fn establish_ssh_connectivity(
253211

254212
Ok(())
255213
}
214+
215+
/// Render Ansible configuration templates for container-based E2E testing
216+
///
217+
/// This function renders Ansible templates with the container's connection details,
218+
/// preparing the configuration files needed for Ansible playbook execution.
219+
/// SSH connectivity is assumed to be already established by the container startup process.
220+
///
221+
/// # Arguments
222+
///
223+
/// * `ansible_template_renderer` - Renderer for creating Ansible inventory and configuration
224+
/// * `ssh_credentials` - SSH credentials for connecting to the container
225+
/// * `socket_addr` - Socket address (IP and port) where the container can be reached
226+
///
227+
/// # Errors
228+
///
229+
/// Returns an error if:
230+
/// - Ansible template rendering fails
231+
async fn render_ansible_configuration(
232+
ansible_template_renderer: Arc<AnsibleTemplateRenderer>,
233+
ssh_credentials: SshCredentials,
234+
socket_addr: SocketAddr,
235+
) -> Result<()> {
236+
info!(
237+
socket_addr = %socket_addr,
238+
"Rendering Ansible configuration templates"
239+
);
240+
241+
// Step 1: Render Ansible templates with container connection details
242+
info!("Rendering Ansible templates for container");
243+
RenderAnsibleTemplatesStep::new(ansible_template_renderer, ssh_credentials, socket_addr)
244+
.execute()
245+
.await
246+
.context("Failed to render Ansible templates for container")?;
247+
248+
// Note: SSH connectivity check is skipped for Docker containers since
249+
// the container setup process already ensures SSH is ready and accessible
250+
251+
info!(
252+
socket_addr = %socket_addr,
253+
"Ansible configuration templates rendered successfully"
254+
);
255+
256+
Ok(())
257+
}

src/e2e/tasks/run_configuration_validation.rs

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,78 @@ use crate::infrastructure::remote_actions::{
2929
DockerComposeValidator, DockerValidator, RemoteAction,
3030
};
3131

32+
/// Run configuration validation tests on a configured instance
33+
///
34+
/// This function performs comprehensive validation of a configured instance,
35+
/// checking that all required services and components are properly installed
36+
/// and functioning. It uses SSH to connect to the target instance and run
37+
/// validation commands.
38+
///
39+
/// # Arguments
40+
///
41+
/// * `socket_addr` - Socket address where the target instance can be reached
42+
/// * `ssh_credentials` - SSH credentials for connecting to the instance
43+
///
44+
/// # Returns
45+
///
46+
/// Returns `Ok(())` when all validation tests pass successfully.
47+
///
48+
/// # Errors
49+
///
50+
/// Returns an error if:
51+
/// - SSH connection cannot be established
52+
/// - Docker validation fails (not installed or not working)
53+
/// - Docker Compose validation fails (not installed or not working)
54+
/// - Any other validation checks fail
55+
///
56+
/// # Example
57+
///
58+
/// ```rust,no_run
59+
/// use torrust_tracker_deploy::e2e::tasks::run_configuration_validation::run_configuration_validation;
60+
/// use torrust_tracker_deploy::config::SshCredentials;
61+
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
62+
///
63+
/// #[tokio::main]
64+
/// async fn main() -> anyhow::Result<()> {
65+
/// let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 2222);
66+
/// let ssh_credentials = SshCredentials::new(
67+
/// "./id_rsa".into(),
68+
/// "./id_rsa.pub".into(),
69+
/// "testuser".to_string()
70+
/// );
71+
///
72+
/// run_configuration_validation(socket_addr, &ssh_credentials).await?;
73+
/// println!("All configuration validations passed");
74+
/// Ok(())
75+
/// }
76+
/// ```
77+
pub async fn run_configuration_validation(
78+
socket_addr: SocketAddr,
79+
ssh_credentials: &SshCredentials,
80+
) -> Result<()> {
81+
info!(
82+
socket_addr = %socket_addr,
83+
ssh_user = %ssh_credentials.ssh_username,
84+
"Running configuration validation tests"
85+
);
86+
87+
let ip_addr = socket_addr.ip();
88+
89+
// Validate Docker installation
90+
validate_docker_installation(ip_addr, ssh_credentials, socket_addr.port()).await?;
91+
92+
// Validate Docker Compose installation
93+
validate_docker_compose_installation(ip_addr, ssh_credentials, socket_addr.port()).await?;
94+
95+
info!(
96+
socket_addr = %socket_addr,
97+
status = "success",
98+
"All configuration validation tests passed successfully"
99+
);
100+
101+
Ok(())
102+
}
103+
32104
/// Validate Docker installation on a configured instance
33105
///
34106
/// This function validates that Docker is properly installed and functioning
@@ -103,75 +175,3 @@ async fn validate_docker_compose_installation(
103175

104176
Ok(())
105177
}
106-
107-
/// Run configuration validation tests on a configured instance
108-
///
109-
/// This function performs comprehensive validation of a configured instance,
110-
/// checking that all required services and components are properly installed
111-
/// and functioning. It uses SSH to connect to the target instance and run
112-
/// validation commands.
113-
///
114-
/// # Arguments
115-
///
116-
/// * `socket_addr` - Socket address where the target instance can be reached
117-
/// * `ssh_credentials` - SSH credentials for connecting to the instance
118-
///
119-
/// # Returns
120-
///
121-
/// Returns `Ok(())` when all validation tests pass successfully.
122-
///
123-
/// # Errors
124-
///
125-
/// Returns an error if:
126-
/// - SSH connection cannot be established
127-
/// - Docker validation fails (not installed or not working)
128-
/// - Docker Compose validation fails (not installed or not working)
129-
/// - Any other validation checks fail
130-
///
131-
/// # Example
132-
///
133-
/// ```rust,no_run
134-
/// use torrust_tracker_deploy::e2e::tasks::run_configuration_validation::run_configuration_validation;
135-
/// use torrust_tracker_deploy::config::SshCredentials;
136-
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
137-
///
138-
/// #[tokio::main]
139-
/// async fn main() -> anyhow::Result<()> {
140-
/// let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 2222);
141-
/// let ssh_credentials = SshCredentials::new(
142-
/// "./id_rsa".into(),
143-
/// "./id_rsa.pub".into(),
144-
/// "testuser".to_string()
145-
/// );
146-
///
147-
/// run_configuration_validation(socket_addr, &ssh_credentials).await?;
148-
/// println!("All configuration validations passed");
149-
/// Ok(())
150-
/// }
151-
/// ```
152-
pub async fn run_configuration_validation(
153-
socket_addr: SocketAddr,
154-
ssh_credentials: &SshCredentials,
155-
) -> Result<()> {
156-
info!(
157-
socket_addr = %socket_addr,
158-
ssh_user = %ssh_credentials.ssh_username,
159-
"Running configuration validation tests"
160-
);
161-
162-
let ip_addr = socket_addr.ip();
163-
164-
// Validate Docker installation
165-
validate_docker_installation(ip_addr, ssh_credentials, socket_addr.port()).await?;
166-
167-
// Validate Docker Compose installation
168-
validate_docker_compose_installation(ip_addr, ssh_credentials, socket_addr.port()).await?;
169-
170-
info!(
171-
socket_addr = %socket_addr,
172-
status = "success",
173-
"All configuration validation tests passed successfully"
174-
);
175-
176-
Ok(())
177-
}

0 commit comments

Comments
 (0)