-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.rs
More file actions
284 lines (248 loc) · 8.45 KB
/
config.rs
File metadata and controls
284 lines (248 loc) · 8.45 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Configuration for SSH server containers
use std::path::PathBuf;
use crate::shared::secrets::PlainPassword;
use super::constants::{
CONTAINER_STARTUP_WAIT_SECS, DEFAULT_TEST_PASSWORD, DEFAULT_TEST_USERNAME, DOCKERFILE_DIR,
MOCK_SSH_PORT, SSH_SERVER_IMAGE_NAME, SSH_SERVER_IMAGE_TAG,
};
/// Configuration for SSH server containers
///
/// This struct defines all configurable parameters for SSH server containers.
/// Use the builder pattern to create custom configurations, or use `default()`
/// for standard test scenarios.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::SshServerConfig;
///
/// // Use default configuration
/// let config = SshServerConfig::default();
///
/// // Customize configuration
/// let config = SshServerConfig::builder()
/// .username("customuser")
/// .password("custompass")
/// .startup_wait_secs(15)
/// .build();
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SshServerConfig {
/// Docker image name for the SSH server
pub image_name: String,
/// Docker image tag for the SSH server
pub image_tag: String,
/// Test username configured in the SSH server
pub username: String,
/// Test password configured in the SSH server
pub password: PlainPassword,
/// Container startup wait time in seconds
pub startup_wait_secs: u64,
/// Path to the Dockerfile directory
pub dockerfile_dir: PathBuf,
/// Port to use for mock container (default: 2222)
pub mock_port: u16,
}
impl SshServerConfig {
/// Create a builder for custom configuration
///
/// # Example
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::SshServerConfig;
///
/// let config = SshServerConfig::builder()
/// .username("testuser")
/// .password("testpass")
/// .build();
/// ```
#[must_use]
pub fn builder() -> SshServerConfigBuilder {
SshServerConfigBuilder::default()
}
}
impl Default for SshServerConfig {
/// Create configuration with default values from constants
///
/// Default values:
/// - Image: `torrust-ssh-server:latest`
/// - Username: `testuser`
/// - Password: `testpass`
/// - Startup wait: 10 seconds
/// - Dockerfile: `docker/ssh-server`
/// - Mock port: 2222
///
/// Note: The SSH container always uses port 22 internally (this is not configurable)
fn default() -> Self {
Self {
image_name: SSH_SERVER_IMAGE_NAME.to_string(),
image_tag: SSH_SERVER_IMAGE_TAG.to_string(),
username: DEFAULT_TEST_USERNAME.to_string(),
password: DEFAULT_TEST_PASSWORD.to_string(),
startup_wait_secs: CONTAINER_STARTUP_WAIT_SECS,
dockerfile_dir: PathBuf::from(DOCKERFILE_DIR),
mock_port: MOCK_SSH_PORT,
}
}
}
/// Builder for SSH server configuration
///
/// Provides a fluent API for constructing custom SSH server configurations.
/// Any field not explicitly set will use the default value.
///
/// # Example
///
/// ```rust
/// use torrust_tracker_deployer_lib::testing::integration::ssh_server::SshServerConfig;
///
/// let config = SshServerConfig::builder()
/// .image_name("custom-ssh-server")
/// .image_tag("v2.0")
/// .username("admin")
/// .password("secret123")
/// .startup_wait_secs(20)
/// .build();
/// ```
#[derive(Debug, Default)]
pub struct SshServerConfigBuilder {
image_name: Option<String>,
image_tag: Option<String>,
username: Option<String>,
password: Option<PlainPassword>,
startup_wait_secs: Option<u64>,
dockerfile_dir: Option<PathBuf>,
mock_port: Option<u16>,
}
impl SshServerConfigBuilder {
/// Set the Docker image name
#[must_use]
pub fn image_name(mut self, name: impl Into<String>) -> Self {
self.image_name = Some(name.into());
self
}
/// Set the Docker image tag
#[must_use]
pub fn image_tag(mut self, tag: impl Into<String>) -> Self {
self.image_tag = Some(tag.into());
self
}
/// Set the test username
#[must_use]
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
/// Set the test password
#[must_use]
pub fn password(mut self, password: impl Into<PlainPassword>) -> Self {
self.password = Some(password.into());
self
}
/// Set the container startup wait time in seconds
#[must_use]
pub fn startup_wait_secs(mut self, secs: u64) -> Self {
self.startup_wait_secs = Some(secs);
self
}
/// Set the Dockerfile directory path
#[must_use]
pub fn dockerfile_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.dockerfile_dir = Some(dir.into());
self
}
/// Set the mock SSH server port
#[must_use]
pub fn mock_port(mut self, port: u16) -> Self {
self.mock_port = Some(port);
self
}
/// Build the configuration
///
/// Any fields not explicitly set will use default values from constants.
#[must_use]
pub fn build(self) -> SshServerConfig {
let defaults = SshServerConfig::default();
SshServerConfig {
image_name: self.image_name.unwrap_or(defaults.image_name),
image_tag: self.image_tag.unwrap_or(defaults.image_tag),
username: self.username.unwrap_or(defaults.username),
password: self.password.unwrap_or(defaults.password),
startup_wait_secs: self.startup_wait_secs.unwrap_or(defaults.startup_wait_secs),
dockerfile_dir: self.dockerfile_dir.unwrap_or(defaults.dockerfile_dir),
mock_port: self.mock_port.unwrap_or(defaults.mock_port),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_config_with_default_values() {
let config = SshServerConfig::default();
assert_eq!(config.image_name, "torrust-ssh-server");
assert_eq!(config.image_tag, "latest");
assert_eq!(config.username, "testuser");
assert_eq!(config.password, "testpass");
assert_eq!(config.startup_wait_secs, 10);
assert_eq!(config.dockerfile_dir, PathBuf::from("docker/ssh-server"));
assert_eq!(config.mock_port, 2222);
}
#[test]
fn it_should_build_config_with_custom_values() {
let config = SshServerConfig::builder()
.image_name("custom-ssh")
.image_tag("v2.0")
.username("admin")
.password("secret")
.startup_wait_secs(15)
.dockerfile_dir("custom/path")
.mock_port(3333)
.build();
assert_eq!(config.image_name, "custom-ssh");
assert_eq!(config.image_tag, "v2.0");
assert_eq!(config.username, "admin");
assert_eq!(config.password, "secret");
assert_eq!(config.startup_wait_secs, 15);
assert_eq!(config.dockerfile_dir, PathBuf::from("custom/path"));
assert_eq!(config.mock_port, 3333);
}
#[test]
fn it_should_use_defaults_for_unset_builder_fields() {
let config = SshServerConfig::builder().username("customuser").build();
// Custom value
assert_eq!(config.username, "customuser");
// Default values for unset fields
assert_eq!(config.image_name, "torrust-ssh-server");
assert_eq!(config.image_tag, "latest");
assert_eq!(config.password, "testpass");
assert_eq!(config.startup_wait_secs, 10);
assert_eq!(config.mock_port, 2222);
}
#[test]
fn it_should_allow_chaining_builder_methods() {
let config = SshServerConfig::builder()
.image_name("test")
.image_tag("v1")
.username("user1")
.password("pass1")
.startup_wait_secs(5)
.build();
assert_eq!(config.image_name, "test");
assert_eq!(config.username, "user1");
assert_eq!(config.startup_wait_secs, 5);
}
#[test]
fn it_should_be_cloneable() {
let config1 = SshServerConfig::default();
let config2 = config1.clone();
assert_eq!(config1, config2);
}
#[test]
fn it_should_allow_customizing_mock_port() {
let config = SshServerConfig::builder().mock_port(5555).build();
assert_eq!(config.mock_port, 5555);
// Other fields should use defaults
assert_eq!(config.username, "testuser");
assert_eq!(config.password, "testpass");
}
}