|
17 | 17 | //! - Works with both container and VM-based infrastructure |
18 | 18 | //! - Integrates with the existing `ConfigureCommand` workflow |
19 | 19 | //! |
20 | | -//! ## Notes |
| 20 | +//! ## E2E Config Tests Integration |
21 | 21 | //! |
22 | | -//! Currently, this task has limitations when used with containers due to |
23 | | -//! inventory addressing differences. Container-specific inventory templates |
24 | | -//! may be needed for full container support. |
| 22 | +//! In E2E config tests, this module works seamlessly with provision simulation. |
| 23 | +//! The provision simulation ensures that Ansible config files are generated with |
| 24 | +//! the correct configuration even without executing the actual provision phase, |
| 25 | +//! allowing the configuration command to run successfully on simulated infrastructure. |
25 | 26 |
|
26 | | -use std::net::SocketAddr; |
27 | 27 | use std::sync::Arc; |
28 | 28 |
|
29 | 29 | use anyhow::{Context, Result}; |
30 | | -use tracing::{info, warn}; |
| 30 | +use tracing::info; |
31 | 31 |
|
32 | 32 | use crate::application::commands::ConfigureCommand; |
33 | | -use crate::container::Services; |
34 | | -use crate::e2e::environment::TestEnvironment; |
35 | 33 |
|
36 | | -/// Run Ansible configuration on a target instance |
37 | | -/// |
38 | | -/// This function executes Ansible playbooks to configure services and applications |
39 | | -/// on the target instance. It uses the existing `ConfigureCommand` workflow and |
40 | | -/// handles both successful configurations and expected failures. |
41 | | -/// |
42 | | -/// # Arguments |
43 | | -/// |
44 | | -/// * `socket_addr` - Socket address where the target instance can be reached |
45 | | -/// * `test_env` - Test environment containing configuration and services |
46 | | -/// * `expect_success` - Whether to expect configuration to succeed (for testing purposes) |
47 | | -/// |
48 | | -/// # Returns |
49 | | -/// |
50 | | -/// Returns `Ok(())` when: |
51 | | -/// - Configuration succeeds (if `expect_success` is true) |
52 | | -/// - Expected failure occurs (if `expect_success` is false) |
53 | | -/// |
54 | | -/// # Errors |
55 | | -/// |
56 | | -/// Returns an error if: |
57 | | -/// - Configuration fails unexpectedly (when `expect_success` is true) |
58 | | -/// - Services cannot be initialized |
59 | | -/// - `ConfigureCommand` execution encounters unexpected errors |
60 | | -/// |
61 | | -/// # Example |
62 | | -/// |
63 | | -/// ```rust,no_run |
64 | | -/// use torrust_tracker_deploy::e2e::tasks::run_configure_command::run_ansible_configuration; |
65 | | -/// use torrust_tracker_deploy::e2e::environment::TestEnvironment; |
66 | | -/// use torrust_tracker_deploy::config::InstanceName; |
67 | | -/// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; |
68 | | -/// |
69 | | -/// fn main() -> anyhow::Result<()> { |
70 | | -/// let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 2222); |
71 | | -/// let instance_name = InstanceName::new("test-instance".to_string())?; |
72 | | -/// let test_env = TestEnvironment::new(false, "./templates".to_string(), instance_name)?; |
73 | | -/// |
74 | | -/// run_ansible_configuration(socket_addr, &test_env, true)?; |
75 | | -/// println!("Ansible configuration completed successfully"); |
76 | | -/// Ok(()) |
77 | | -/// } |
78 | | -/// ``` |
79 | | -pub fn run_ansible_configuration( |
80 | | - socket_addr: SocketAddr, |
81 | | - test_env: &TestEnvironment, |
82 | | - expect_success: bool, |
83 | | -) -> Result<()> { |
84 | | - info!( |
85 | | - socket_addr = %socket_addr, |
86 | | - expect_success = expect_success, |
87 | | - "Running Ansible configuration on instance" |
88 | | - ); |
89 | | - |
90 | | - // Initialize services from test environment configuration |
91 | | - let services = Services::new(&test_env.config); |
92 | | - let configure_command = ConfigureCommand::new(Arc::clone(&services.ansible_client)); |
93 | | - |
94 | | - // Execute configuration command |
95 | | - match configure_command.execute().map_err(anyhow::Error::from) { |
96 | | - Ok(()) => { |
97 | | - if expect_success { |
98 | | - info!( |
99 | | - socket_addr = %socket_addr, |
100 | | - status = "success", |
101 | | - "Configuration completed successfully" |
102 | | - ); |
103 | | - } else { |
104 | | - warn!( |
105 | | - socket_addr = %socket_addr, |
106 | | - status = "unexpected_success", |
107 | | - "Configuration succeeded when failure was expected" |
108 | | - ); |
109 | | - } |
110 | | - } |
111 | | - Err(e) => { |
112 | | - if expect_success { |
113 | | - return Err(e.context("Ansible configuration failed unexpectedly")); |
114 | | - } |
115 | | - info!( |
116 | | - socket_addr = %socket_addr, |
117 | | - status = "expected_failure", |
118 | | - error = %e, |
119 | | - "Configuration failed as expected" |
120 | | - ); |
121 | | - } |
122 | | - } |
123 | | - |
124 | | - info!( |
125 | | - socket_addr = %socket_addr, |
126 | | - status = "complete", |
127 | | - "Ansible configuration workflow completed" |
128 | | - ); |
129 | | - |
130 | | - Ok(()) |
131 | | -} |
| 34 | +use crate::e2e::environment::TestEnvironment; |
132 | 35 |
|
133 | | -/// Configure infrastructure using Ansible playbooks (compatibility wrapper) |
| 36 | +/// Configure infrastructure using Ansible playbooks |
134 | 37 | /// |
135 | | -/// This is a simplified wrapper around the `ConfigureCommand` for use in full E2E tests. |
136 | | -/// For more advanced configuration with success/failure handling, use `run_ansible_configuration`. |
| 38 | +/// This function executes Ansible configuration using the `ConfigureCommand` for E2E tests. |
| 39 | +/// It works with both VM and container-based infrastructure, utilizing rendered Ansible |
| 40 | +/// inventories and configuration files generated during the provision simulation phase. |
137 | 41 | /// |
138 | 42 | /// # Errors |
139 | 43 | /// |
140 | 44 | /// Returns an error if: |
141 | 45 | /// - `ConfigureCommand` execution fails |
142 | 46 | /// - Infrastructure configuration fails |
143 | | -pub fn configure_infrastructure(env: &TestEnvironment) -> Result<()> { |
| 47 | +pub fn run_configure_command(env: &TestEnvironment) -> Result<()> { |
144 | 48 | info!("Configuring test infrastructure"); |
145 | 49 |
|
146 | 50 | // Use the new ConfigureCommand to handle all infrastructure configuration steps |
|
0 commit comments