-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmock_container.rs
More file actions
166 lines (143 loc) · 4.62 KB
/
Copy pathmock_container.rs
File metadata and controls
166 lines (143 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Mock SSH server container for fast testing
use std::net::{IpAddr, Ipv4Addr};
use super::config::SshServerConfig;
use super::error::SshServerError;
/// Mock SSH server container for fast testing
///
/// This implementation doesn't start a real container but provides the same
/// interface as a real SSH server. Use this for tests that only need to verify
/// configuration, timeouts, or client behavior without actual SSH connectivity.
pub struct MockSshServerContainer {
config: SshServerConfig,
host_ip: IpAddr,
ssh_port: u16,
}
impl MockSshServerContainer {
/// Create a mock SSH server container with custom configuration
///
/// This doesn't start any actual container, making it very fast for tests
/// that don't need real SSH connectivity.
///
/// # Arguments
///
/// * `config` - Configuration for the mock container
///
/// # Returns
///
/// A mock container configured with the provided settings.
///
/// # Errors
///
/// This function is infallible but returns a Result to match the interface
/// of `RealSshServerContainer::start_with_config()`.
///
/// # Example
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::{
/// MockSshServerContainer, SshServerConfig
/// };
///
/// let config = SshServerConfig::builder()
/// .username("customuser")
/// .password("custompass")
/// .build();
///
/// let container = MockSshServerContainer::start_with_config(config).unwrap();
/// ```
pub fn start_with_config(config: SshServerConfig) -> Result<Self, SshServerError> {
let ssh_port = config.mock_port;
Ok(Self {
config,
host_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
ssh_port,
})
}
/// Create a mock SSH server container with default configuration
///
/// This is a convenience method that uses default configuration values.
///
/// # Returns
///
/// A mock container configured with default test credentials.
///
/// # Errors
///
/// This function is infallible but returns a Result to match the interface
/// of `RealSshServerContainer::start()`.
///
/// # Example
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::MockSshServerContainer;
///
/// let container = MockSshServerContainer::start().unwrap();
/// ```
pub fn start() -> Result<Self, SshServerError> {
Self::start_with_config(SshServerConfig::default())
}
/// Get the SSH port mapped by the container
///
/// Returns the host port that maps to the container's SSH port (22).
#[must_use]
pub fn ssh_port(&self) -> u16 {
self.ssh_port
}
/// Get the container's host IP address
///
/// Returns the IP address to connect to the container from the host.
#[must_use]
pub fn host_ip(&self) -> IpAddr {
self.host_ip
}
/// Get the test username configured in the container
#[must_use]
pub fn test_username(&self) -> &str {
&self.config.username
}
/// Get the test password configured in the container
#[must_use]
pub fn test_password(&self) -> &str {
&self.config.password
}
}
impl super::SshServerContainer for MockSshServerContainer {
fn ssh_port(&self) -> u16 {
self.ssh_port
}
fn host_ip(&self) -> IpAddr {
self.host_ip
}
fn test_username(&self) -> &str {
&self.config.username
}
fn test_password(&self) -> &str {
&self.config.password
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_use_default_mock_port_with_default_config() {
let container = MockSshServerContainer::start().expect("Failed to create mock container");
assert_eq!(container.ssh_port(), 2222);
}
#[test]
fn it_should_use_custom_mock_port_from_config() {
let config = SshServerConfig::builder().mock_port(5555).build();
let container = MockSshServerContainer::start_with_config(config)
.expect("Failed to create mock container");
assert_eq!(container.ssh_port(), 5555);
}
#[test]
fn it_should_allow_testing_with_different_port_configurations() {
let ports = vec![3000, 4000, 5000];
for port in ports {
let config = SshServerConfig::builder().mock_port(port).build();
let container = MockSshServerContainer::start_with_config(config)
.expect("Failed to create mock container");
assert_eq!(container.ssh_port(), port);
}
}
}