Skip to content

Commit 18e275b

Browse files
committed
refactor: standardize TestEnvironment API with unified 'initialized' method
- Replace multiple constructor methods (new, with_ssh_user_and_init) with single 'initialized' method - All e2e test binaries now use TestEnvironment::initialized with explicit SSH user 'torrust' - Update e2e_provision_tests.rs and e2e_tests_full.rs to use consistent API - Fix outdated documentation comments to reflect current implementation - Update doctest examples to use new API - Add TestEnvironmentType parameter to all environment creation calls Benefits: - Consistent API across all e2e tests - Explicit SSH user configuration for all tests - Clearer error messages with specific failure conditions - Single entry point for test environment creation
1 parent f459446 commit 18e275b

File tree

5 files changed

+43
-92
lines changed

5 files changed

+43
-92
lines changed

src/bin/e2e_config_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub async fn main() -> Result<()> {
114114
let instance_name =
115115
InstanceName::new("torrust-tracker-vm".to_string()).expect("Valid hardcoded instance name");
116116

117-
let env = TestEnvironment::with_ssh_user_and_init(
117+
let env = TestEnvironment::initialized(
118118
false,
119119
cli.templates_dir,
120120
"torrust",

src/bin/e2e_provision_tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tracing::{error, info};
2323

2424
// Import E2E testing infrastructure
2525
use torrust_tracker_deploy::config::InstanceName;
26-
use torrust_tracker_deploy::e2e::environment::TestEnvironment;
26+
use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
2727
use torrust_tracker_deploy::e2e::tasks::{
2828
preflight_cleanup::cleanup_lingering_resources,
2929
virtual_machine::{
@@ -98,7 +98,13 @@ pub async fn main() -> Result<()> {
9898
let instance_name =
9999
InstanceName::new("torrust-tracker-vm".to_string()).expect("Valid hardcoded instance name");
100100

101-
let env = TestEnvironment::new(cli.keep, cli.templates_dir, instance_name)?;
101+
let env = TestEnvironment::initialized(
102+
cli.keep,
103+
cli.templates_dir,
104+
"torrust",
105+
instance_name,
106+
TestEnvironmentType::VirtualMachine,
107+
)?;
102108

103109
// Perform pre-flight cleanup to remove any lingering resources from interrupted tests
104110
cleanup_lingering_resources(&env)?;

src/bin/e2e_tests_full.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use tracing::{error, info};
2828

2929
// Import E2E testing infrastructure
3030
use torrust_tracker_deploy::config::InstanceName;
31-
use torrust_tracker_deploy::e2e::environment::TestEnvironment;
31+
use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
3232
use torrust_tracker_deploy::e2e::tasks::{
3333
preflight_cleanup::cleanup_lingering_resources,
3434
run_configure_command::run_configure_command,
@@ -97,7 +97,13 @@ pub async fn main() -> Result<()> {
9797
let instance_name =
9898
InstanceName::new("torrust-tracker-vm".to_string()).expect("Valid hardcoded instance name");
9999

100-
let env = TestEnvironment::new(cli.keep, cli.templates_dir, instance_name)?;
100+
let env = TestEnvironment::initialized(
101+
cli.keep,
102+
cli.templates_dir,
103+
"torrust",
104+
instance_name,
105+
TestEnvironmentType::VirtualMachine,
106+
)?;
101107

102108
// Perform pre-flight cleanup to remove any lingering resources from interrupted tests
103109
cleanup_lingering_resources(&env)?;

src/e2e/environment.rs

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ pub enum TestEnvironmentError {
5555
EnvironmentPreparationError { source: anyhow::Error },
5656
}
5757

58-
/// Default SSH username for test environments
59-
const DEFAULT_SSH_USER: &str = "torrust";
60-
6158
/// SSH private key filename for testing
6259
const SSH_PRIVATE_KEY_FILENAME: &str = "testing_rsa";
6360

@@ -88,83 +85,10 @@ pub struct TestEnvironment {
8885
}
8986

9087
impl TestEnvironment {
91-
/// Creates a new test environment with configuration and services
88+
/// Creates and initializes a new test environment with custom SSH user
9289
///
93-
/// This method only performs basic construction without side effects.
94-
/// Call `init()` to perform environment initialization.
95-
///
96-
/// # Arguments
97-
///
98-
/// * `keep_env` - Whether to keep the environment after tests complete
99-
/// * `templates_dir` - Path to the templates directory
100-
/// * `instance_name` - Name for the instance to be deployed
101-
///
102-
/// # Errors
103-
///
104-
/// Returns an error if:
105-
/// - Current directory cannot be determined
106-
/// - Temporary directory creation fails
107-
/// - SSH key setup fails
108-
/// - Templates directory is invalid
109-
///
110-
/// # Panics
111-
///
112-
/// Panics if the provided `instance_name` is invalid (contains invalid characters
113-
/// or is empty). This should not happen with well-formed instance names.
114-
pub fn new(
115-
keep_env: bool,
116-
templates_dir: impl Into<std::path::PathBuf>,
117-
instance_name: InstanceName,
118-
) -> Result<Self, TestEnvironmentError> {
119-
Self::with_ssh_user(
120-
keep_env,
121-
templates_dir,
122-
DEFAULT_SSH_USER,
123-
instance_name,
124-
TestEnvironmentType::VirtualMachine,
125-
)
126-
}
127-
128-
/// Creates and initializes a new test environment with custom SSH user in one step
129-
///
130-
/// This is a convenience method that combines `with_ssh_user()` and `init()`.
131-
/// Use this when you want the full setup in one call.
132-
///
133-
/// # Arguments
134-
///
135-
/// * `keep_env` - Whether to keep the environment after tests complete
136-
/// * `templates_dir` - Path to the templates directory
137-
/// * `ssh_user` - SSH username to use for connections
138-
/// * `instance_name` - Name for the instance to be deployed
139-
/// * `environment_type` - The type of test environment (Container or `VirtualMachine`)
140-
///
141-
/// # Errors
142-
///
143-
/// Returns an error if:
144-
/// - Construction fails (see `with_ssh_user()` for details)
145-
/// - Initialization fails (see `init()` for details)
146-
pub fn with_ssh_user_and_init(
147-
keep_env: bool,
148-
templates_dir: impl Into<std::path::PathBuf>,
149-
ssh_user: &str,
150-
instance_name: InstanceName,
151-
environment_type: TestEnvironmentType,
152-
) -> Result<Self, TestEnvironmentError> {
153-
let env = Self::with_ssh_user(
154-
keep_env,
155-
templates_dir,
156-
ssh_user,
157-
instance_name,
158-
environment_type,
159-
)?;
160-
env.init()?;
161-
Ok(env)
162-
}
163-
164-
/// Creates a new test environment with a custom SSH user
165-
///
166-
/// This method only performs basic construction without side effects.
167-
/// Call `init()` to perform environment initialization.
90+
/// This method performs the complete setup including validation, SSH key setup,
91+
/// template preparation, and environment initialization in a single call.
16892
///
16993
/// # Arguments
17094
///
@@ -177,44 +101,53 @@ impl TestEnvironment {
177101
/// # Errors
178102
///
179103
/// Returns an error if:
104+
/// - Input validation fails (empty or invalid templates directory)
180105
/// - Current directory cannot be determined
181106
/// - Temporary directory creation fails
182107
/// - SSH key setup fails
183-
/// - Templates directory is invalid
184-
fn with_ssh_user(
108+
/// - Template preparation fails
109+
pub fn initialized(
185110
keep_env: bool,
186111
templates_dir: impl Into<std::path::PathBuf>,
187112
ssh_user: &str,
188113
instance_name: InstanceName,
189114
environment_type: TestEnvironmentType,
190115
) -> Result<Self, TestEnvironmentError> {
191116
let templates_dir = templates_dir.into();
117+
192118
Self::validate_inputs(&templates_dir)?;
193119

194120
let project_root = Self::get_project_root()?;
195121
let temp_dir = Self::create_temp_directory()?;
122+
196123
let ssh_credentials = Self::setup_ssh_credentials(&project_root, &temp_dir, ssh_user)?;
124+
197125
let config = Self::create_config(
198126
keep_env,
199127
ssh_credentials,
200128
instance_name,
201129
&templates_dir,
202130
&project_root,
203131
);
132+
204133
let services = Services::new(&config);
205134

206-
Ok(Self {
135+
let env = Self {
207136
config,
208137
services,
209138
temp_dir: Some(temp_dir),
210139
environment_type,
211-
})
140+
};
141+
142+
env.init()?;
143+
144+
Ok(env)
212145
}
213146

214147
/// Initializes the test environment by preparing templates and logging setup
215148
///
216-
/// This method performs the actual environment setup with side effects.
217-
/// Must be called after construction to make the environment ready for use.
149+
/// This method performs the final environment setup with side effects.
150+
/// It is called internally by `initialized()` as part of the initialization process.
218151
///
219152
/// # Errors
220153
///

src/e2e/tasks/container/run_provision_simulation.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ use crate::infrastructure::ansible::AnsibleTemplateRenderer;
6262
///
6363
/// ```rust,no_run
6464
/// use torrust_tracker_deploy::e2e::tasks::container::run_provision_simulation::run_provision_simulation;
65-
/// use torrust_tracker_deploy::e2e::environment::TestEnvironment;
65+
/// use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
6666
/// use torrust_tracker_deploy::config::InstanceName;
6767
///
6868
/// #[tokio::main]
6969
/// async fn main() -> anyhow::Result<()> {
7070
/// let instance_name = InstanceName::new("test-container".to_string())?;
71-
/// let test_env = TestEnvironment::new(false, "./templates".to_string(), instance_name)?;
71+
/// let test_env = TestEnvironment::initialized(
72+
/// false,
73+
/// "./templates".to_string(),
74+
/// "torrust",
75+
/// instance_name,
76+
/// TestEnvironmentType::Container
77+
/// )?;
7278
///
7379
/// let running_container = run_provision_simulation(&test_env).await?;
7480
/// println!("Container provision simulation completed: {}", running_container.ssh_socket_addr());

0 commit comments

Comments
 (0)