Skip to content

Commit 933956c

Browse files
committed
refactor: move Username from domain to shared module and update SshCredentials
- Move src/domain/username.rs to src/shared/username.rs - Update SshCredentials to use Username type instead of String for ssh_username field - Update all usage sites across the codebase to use Username::new() - Update SSH key setup functions to accept &Username parameters - All tests pass: 351 unit tests, full e2e test suite, all linters
1 parent 54498b5 commit 933956c

File tree

22 files changed

+87
-47
lines changed

22 files changed

+87
-47
lines changed

src/application/commands/provision.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ mod tests {
178178
use super::*;
179179
use tempfile::TempDir;
180180

181+
use crate::shared::Username;
182+
181183
// Helper function to create mock dependencies for testing
182184
fn create_mock_dependencies() -> (
183185
Arc<TofuTemplateRenderer>,
@@ -195,7 +197,7 @@ mod tests {
195197
let ssh_credentials = SshCredentials::new(
196198
"dummy_key".into(),
197199
"dummy_key.pub".into(),
198-
"testuser".to_string(),
200+
Username::new("testuser").unwrap(),
199201
);
200202

201203
let tofu_renderer = Arc::new(TofuTemplateRenderer::new(
@@ -219,8 +221,11 @@ mod tests {
219221

220222
let ssh_key_path = temp_dir.path().join("test_key");
221223
let ssh_pub_key_path = temp_dir.path().join("test_key.pub");
222-
let ssh_credentials =
223-
SshCredentials::new(ssh_key_path, ssh_pub_key_path, "test_user".to_string());
224+
let ssh_credentials = SshCredentials::new(
225+
ssh_key_path,
226+
ssh_pub_key_path,
227+
Username::new("test_user").unwrap(),
228+
);
224229

225230
(
226231
tofu_renderer,

src/application/commands/test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,18 @@ mod tests {
111111
use std::net::{IpAddr, Ipv4Addr};
112112
use tempfile::TempDir;
113113

114+
use crate::shared::Username;
115+
114116
// Helper function to create mock dependencies for testing
115117
fn create_mock_dependencies() -> (SshCredentials, IpAddr, TempDir) {
116118
let temp_dir = TempDir::new().expect("Failed to create temp dir");
117119
let ssh_key_path = temp_dir.path().join("test_key");
118120
let ssh_pub_key_path = temp_dir.path().join("test_key.pub");
119-
let ssh_credentials =
120-
SshCredentials::new(ssh_key_path, ssh_pub_key_path, "test_user".to_string());
121+
let ssh_credentials = SshCredentials::new(
122+
ssh_key_path,
123+
ssh_pub_key_path,
124+
Username::new("test_user").unwrap(),
125+
);
121126
let instance_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100));
122127

123128
(ssh_credentials, instance_ip, temp_dir)

src/application/steps/connectivity/wait_ssh_connectivity.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ impl WaitForSSHConnectivityStep {
7878
mod tests {
7979
use std::net::{IpAddr, Ipv4Addr};
8080

81-
use crate::shared::ssh::SshCredentials;
81+
use crate::shared::ssh::{SshConnection, SshCredentials};
82+
use crate::shared::Username;
8283

8384
use super::*;
8485

@@ -88,7 +89,7 @@ mod tests {
8889
let credentials = SshCredentials::new(
8990
"/tmp/test_key".into(),
9091
"/tmp/test_key.pub".into(),
91-
"testuser".to_string(),
92+
Username::new("testuser").unwrap(),
9293
);
9394
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
9495

@@ -108,7 +109,7 @@ mod tests {
108109
let credentials = SshCredentials::new(
109110
"/home/user/.ssh/id_rsa".into(),
110111
"/home/user/.ssh/id_rsa.pub".into(),
111-
"torrust".to_string(),
112+
Username::new("torrust").unwrap(),
112113
);
113114
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
114115

@@ -128,7 +129,7 @@ mod tests {
128129
let credentials = SshCredentials::new(
129130
"/path/to/ssh/key".into(),
130131
"/path/to/ssh/key.pub".into(),
131-
"admin".to_string(),
132+
Username::new("admin").unwrap(),
132133
);
133134
let ssh_connection = SshConnection::with_default_port(credentials, instance_ip);
134135

src/application/steps/validation/cloud_init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ mod tests {
7575
use std::path::PathBuf;
7676

7777
use crate::shared::ssh::SshCredentials;
78+
use crate::shared::Username;
7879

7980
use super::*;
8081

@@ -83,7 +84,7 @@ mod tests {
8384
let ssh_credentials = SshCredentials::new(
8485
PathBuf::from("test_key"),
8586
PathBuf::from("test_key.pub"),
86-
"test_user".to_string(),
87+
Username::new("test_user").unwrap(),
8788
);
8889
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
8990
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);

src/application/steps/validation/docker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mod tests {
7474
use std::path::PathBuf;
7575

7676
use crate::shared::ssh::SshCredentials;
77+
use crate::shared::Username;
7778

7879
use super::*;
7980

@@ -82,7 +83,7 @@ mod tests {
8283
let ssh_credentials = SshCredentials::new(
8384
PathBuf::from("test_key"),
8485
PathBuf::from("test_key.pub"),
85-
"test_user".to_string(),
86+
Username::new("test_user").unwrap(),
8687
);
8788
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
8889
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);

src/application/steps/validation/docker_compose.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod tests {
8080
use std::path::PathBuf;
8181

8282
use crate::shared::ssh::SshCredentials;
83+
use crate::shared::Username;
8384

8485
use super::*;
8586

@@ -88,7 +89,7 @@ mod tests {
8889
let ssh_credentials = SshCredentials::new(
8990
PathBuf::from("test_key"),
9091
PathBuf::from("test_key.pub"),
91-
"test_user".to_string(),
92+
Username::new("test_user").unwrap(),
9293
);
9394
let host_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
9495
let ssh_connection = SshConnection::with_default_port(ssh_credentials, host_ip);

src/bin/e2e_config_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use torrust_tracker_deploy::e2e::tasks::run_configure_command::run_configure_com
5151
use tracing::{error, info};
5252

5353
use torrust_tracker_deploy::config::InstanceName;
54-
use torrust_tracker_deploy::domain::Username;
5554
use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
5655
use torrust_tracker_deploy::e2e::tasks::{
5756
container::{
@@ -62,6 +61,7 @@ use torrust_tracker_deploy::e2e::tasks::{
6261
run_configuration_validation::run_configuration_validation,
6362
};
6463
use torrust_tracker_deploy::logging::{self, LogFormat};
64+
use torrust_tracker_deploy::shared::Username;
6565

6666
#[derive(Parser)]
6767
#[command(name = "e2e-config-tests")]

src/bin/e2e_provision_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use tracing::{error, info};
2323

2424
// Import E2E testing infrastructure
2525
use torrust_tracker_deploy::config::InstanceName;
26-
use torrust_tracker_deploy::domain::Username;
2726
use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
2827
use torrust_tracker_deploy::e2e::tasks::{
2928
preflight_cleanup::cleanup_lingering_resources,
@@ -33,6 +32,7 @@ use torrust_tracker_deploy::e2e::tasks::{
3332
},
3433
};
3534
use torrust_tracker_deploy::logging::{self, LogFormat};
35+
use torrust_tracker_deploy::shared::Username;
3636

3737
#[derive(Parser)]
3838
#[command(name = "e2e-provision-tests")]

src/bin/e2e_tests_full.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use tracing::{error, info};
2828

2929
// Import E2E testing infrastructure
3030
use torrust_tracker_deploy::config::InstanceName;
31-
use torrust_tracker_deploy::domain::Username;
3231
use torrust_tracker_deploy::e2e::environment::{TestEnvironment, TestEnvironmentType};
3332
use torrust_tracker_deploy::e2e::tasks::{
3433
preflight_cleanup::cleanup_lingering_resources,
@@ -40,6 +39,7 @@ use torrust_tracker_deploy::e2e::tasks::{
4039
},
4140
};
4241
use torrust_tracker_deploy::logging::{self, LogFormat};
42+
use torrust_tracker_deploy::shared::Username;
4343

4444
#[derive(Parser)]
4545
#[command(name = "e2e-tests")]

src/config/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ impl Config {
9090
/// # use std::net::{IpAddr, Ipv4Addr};
9191
/// # use std::path::PathBuf;
9292
/// # use torrust_tracker_deploy::config::{Config, SshCredentials, InstanceName};
93+
/// # use torrust_tracker_deploy::shared::Username;
9394
/// let ssh_credentials = SshCredentials::new(
9495
/// PathBuf::from("/home/user/.ssh/deploy_key"),
9596
/// PathBuf::from("/home/user/.ssh/deploy_key.pub"),
96-
/// "ubuntu".to_string(),
97+
/// Username::new("ubuntu").unwrap(),
9798
/// );
9899
/// let instance_name = InstanceName::new("my-instance".to_string()).unwrap();
99100
/// let config = Config::new(

0 commit comments

Comments
 (0)