Skip to content

Commit 494a85d

Browse files
committed
refactor: make SSH key file paths configurable in TestEnvironment
- Update TestEnvironment::setup_ssh_credentials to accept ssh_private_key_path and ssh_public_key_path parameters - Modify TestEnvironment::initialized method to accept SSH key file paths and pass them through - Update all three E2E test binaries (e2e_config_tests, e2e_provision_tests, e2e_tests_full) to initialize SSH key paths from fixtures - Fix doctest in run_provision_simulation.rs to include new SSH key parameters - Improves testability by making SSH key paths configurable instead of hardcoded - Maintains backward compatibility by using the same fixture paths (fixtures/testing_rsa and fixtures/testing_rsa.pub)
1 parent c61930c commit 494a85d

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

src/bin/e2e_config_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,16 @@ pub async fn main() -> Result<()> {
117117

118118
let ssh_user = Username::new("torrust").expect("Valid hardcoded username");
119119

120+
let ssh_private_key_path = std::path::PathBuf::from("fixtures/testing_rsa");
121+
let ssh_public_key_path = std::path::PathBuf::from("fixtures/testing_rsa.pub");
122+
120123
let env = TestEnvironment::initialized(
121124
false,
122125
cli.templates_dir,
123126
&ssh_user,
124127
instance_name,
128+
ssh_private_key_path,
129+
ssh_public_key_path,
125130
TestEnvironmentType::Container,
126131
)?;
127132

src/bin/e2e_provision_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,16 @@ pub async fn main() -> Result<()> {
101101

102102
let ssh_user = Username::new("torrust").expect("Valid hardcoded username");
103103

104+
let ssh_private_key_path = std::path::PathBuf::from("fixtures/testing_rsa");
105+
let ssh_public_key_path = std::path::PathBuf::from("fixtures/testing_rsa.pub");
106+
104107
let env = TestEnvironment::initialized(
105108
cli.keep,
106109
cli.templates_dir,
107110
&ssh_user,
108111
instance_name,
112+
ssh_private_key_path,
113+
ssh_public_key_path,
109114
TestEnvironmentType::VirtualMachine,
110115
)?;
111116

src/bin/e2e_tests_full.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ pub async fn main() -> Result<()> {
100100

101101
let ssh_user = Username::new("torrust").expect("Valid hardcoded username");
102102

103+
let ssh_private_key_path = std::path::PathBuf::from("fixtures/testing_rsa");
104+
let ssh_public_key_path = std::path::PathBuf::from("fixtures/testing_rsa.pub");
105+
103106
let env = TestEnvironment::initialized(
104107
cli.keep,
105108
cli.templates_dir,
106109
&ssh_user,
107110
instance_name,
111+
ssh_private_key_path,
112+
ssh_public_key_path,
108113
TestEnvironmentType::VirtualMachine,
109114
)?;
110115

src/e2e/environment.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ impl TestEnvironment {
9797
/// * `templates_dir` - Path to the templates directory
9898
/// * `ssh_user` - SSH username to use for connections
9999
/// * `instance_name` - Name for the instance to be deployed
100+
/// * `ssh_private_key_path` - Path to the SSH private key file
101+
/// * `ssh_public_key_path` - Path to the SSH public key file
100102
/// * `environment_type` - The type of test environment (Container or `VirtualMachine`)
101103
///
102104
/// # Errors
@@ -112,16 +114,25 @@ impl TestEnvironment {
112114
templates_dir: impl Into<std::path::PathBuf>,
113115
ssh_user: &Username,
114116
instance_name: InstanceName,
117+
ssh_private_key_path: impl Into<std::path::PathBuf>,
118+
ssh_public_key_path: impl Into<std::path::PathBuf>,
115119
environment_type: TestEnvironmentType,
116120
) -> Result<Self, TestEnvironmentError> {
117121
let templates_dir = templates_dir.into();
122+
let ssh_private_key_path = ssh_private_key_path.into();
123+
let ssh_public_key_path = ssh_public_key_path.into();
118124

119125
Self::validate_inputs(&templates_dir)?;
120126

121127
let project_root = Self::get_project_root()?;
122128
let temp_dir = Self::create_temp_directory()?;
123129

124-
let ssh_credentials = Self::setup_ssh_credentials(&project_root, &temp_dir, ssh_user)?;
130+
let ssh_credentials = Self::setup_ssh_credentials(
131+
&ssh_private_key_path,
132+
&ssh_public_key_path,
133+
&temp_dir,
134+
ssh_user,
135+
)?;
125136

126137
let config = Self::create_config(
127138
keep_env,
@@ -188,24 +199,21 @@ impl TestEnvironment {
188199

189200
/// Sets up SSH credentials with temporary keys
190201
fn setup_ssh_credentials(
191-
project_root: &std::path::Path,
202+
ssh_private_key_path: &std::path::Path,
203+
ssh_public_key_path: &std::path::Path,
192204
temp_dir: &TempDir,
193205
ssh_user: &Username,
194206
) -> Result<SshCredentials, TestEnvironmentError> {
195207
let temp_ssh_key = temp_dir.path().join(SSH_PRIVATE_KEY_FILENAME);
196208
let temp_ssh_pub_key = temp_dir.path().join(SSH_PUBLIC_KEY_FILENAME);
197209

198-
// Copy SSH private key from fixtures to temp directory
199-
let fixtures_ssh_key = project_root.join("fixtures/testing_rsa");
200-
201-
std::fs::copy(&fixtures_ssh_key, &temp_ssh_key)
210+
// Copy SSH private key from provided path to temp directory
211+
std::fs::copy(ssh_private_key_path, &temp_ssh_key)
202212
.context("Failed to copy SSH private key to temporary directory")
203213
.map_err(|e| TestEnvironmentError::SshKeySetupError { source: e })?;
204214

205-
// Copy SSH public key from fixtures to temp directory
206-
let fixtures_ssh_pub_key = project_root.join("fixtures/testing_rsa.pub");
207-
208-
std::fs::copy(&fixtures_ssh_pub_key, &temp_ssh_pub_key)
215+
// Copy SSH public key from provided path to temp directory
216+
std::fs::copy(ssh_public_key_path, &temp_ssh_pub_key)
209217
.context("Failed to copy SSH public key to temporary directory")
210218
.map_err(|e| TestEnvironmentError::SshKeySetupError { source: e })?;
211219

src/e2e/tasks/container/run_provision_simulation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ use crate::infrastructure::ansible::AnsibleTemplateRenderer;
7070
/// async fn main() -> anyhow::Result<()> {
7171
/// let instance_name = InstanceName::new("test-container".to_string())?;
7272
/// let ssh_user = Username::new("torrust")?;
73+
/// let ssh_private_key_path = std::path::PathBuf::from("fixtures/testing_rsa");
74+
/// let ssh_public_key_path = std::path::PathBuf::from("fixtures/testing_rsa.pub");
7375
/// let test_env = TestEnvironment::initialized(
7476
/// false,
7577
/// "./templates".to_string(),
7678
/// &ssh_user,
7779
/// instance_name,
80+
/// ssh_private_key_path,
81+
/// ssh_public_key_path,
7882
/// TestEnvironmentType::Container
7983
/// )?;
8084
///

0 commit comments

Comments
 (0)