-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstance_name.rs
More file actions
413 lines (361 loc) · 12.9 KB
/
Copy pathinstance_name.rs
File metadata and controls
413 lines (361 loc) · 12.9 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Instance name validation and management for LXD VMs and containers
//!
//! This module provides the `InstanceName` type which ensures instance names
//! follow the strict naming requirements imposed by LXD for security and
//! compatibility reasons. The validated names are used for:
//!
//! - **LXD Virtual Machines**: Names for provisioned VMs in production/deployment environments
//! - **Testing Containers**: Names for Docker containers used in end-to-end tests
//!
//! ## Naming Requirements
//!
//! - Length: 1-63 characters
//! - Characters: ASCII letters, numbers, and dashes only
//! - Cannot start with digit or dash
//! - Cannot end with dash
//!
//! These restrictions ensure compatibility with DNS records, file systems,
//! security profiles, and host names across different environments.
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// Errors that can occur during instance name validation
#[derive(Debug, Error, PartialEq)]
pub enum InstanceNameError {
#[error("Instance name cannot be empty")]
Empty,
#[error("Instance name must be 63 characters or less, got {length} characters")]
TooLong { length: usize },
#[error("Instance name must not start with a digit or dash")]
InvalidFirstCharacter,
#[error("Instance name must not end with a dash")]
InvalidLastCharacter,
#[error("Instance name must contain only ASCII letters, numbers, and dashes")]
InvalidCharacters,
}
/// A validated instance name following LXD naming requirements.
///
/// This type ensures that names are valid for both LXD virtual machines used in
/// production deployments and Docker containers used in end-to-end testing.
///
/// Valid instance names must fulfill the following requirements:
/// - The name must be between 1 and 63 characters long
/// - The name must contain only letters, numbers and dashes from the ASCII table
/// - The name must not start with a digit or a dash
/// - The name must not end with a dash
///
/// These requirements ensure that the instance name can be used in DNS records,
/// on the file system, in various security profiles and as the host name.
///
/// # Use Cases
///
/// - **LXD Virtual Machines**: Naming VMs provisioned for deployment environments
/// - **Testing Containers**: Naming Docker containers in E2E test scenarios
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::InstanceName;
///
/// // Valid instance names - accepts both &str and String
/// let vm_instance = InstanceName::new("torrust-vm-prod")?;
/// let test_container = InstanceName::new("test-container-01".to_string())?;
/// let dynamic_name = InstanceName::new(format!("app-{}", "staging"))?;
///
/// // Invalid instance names
/// assert!(InstanceName::new("").is_err());
/// assert!(InstanceName::new("test-").is_err());
/// assert!(InstanceName::new("-test").is_err());
/// assert!(InstanceName::new("1test").is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String")]
pub struct InstanceName(String);
impl InstanceName {
/// Creates a new `InstanceName` from a string if it's valid.
///
/// This method validates that the provided name meets the requirements for both
/// LXD virtual machines and testing containers.
///
/// # Arguments
///
/// * `name` - The instance name to validate (accepts `&str`, `String`, or anything that implements `Into<String>`)
///
/// # Returns
///
/// * `Ok(InstanceName)` - If the name is valid
/// * `Err(InstanceNameError)` - If the name violates LXD naming requirements
///
/// # Errors
///
/// This function will return an error if the name violates any LXD naming requirements:
/// * Empty name
/// * Name longer than 63 characters
/// * Name contains non-ASCII letters, numbers, or dashes
/// * Name starts with a digit or dash
/// * Name ends with a dash
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::domain::InstanceName;
///
/// // Valid names for VMs and containers - accepts both &str and String
/// let vm_name = InstanceName::new("torrust-vm-prod")?;
/// let container_name = InstanceName::new("test-web-01".to_string())?;
/// let dynamic_name = InstanceName::new(format!("app-{}", "staging"))?;
///
/// // Invalid names
/// assert!(InstanceName::new("").is_err());
/// assert!(InstanceName::new("test-").is_err());
/// assert!(InstanceName::new("-test").is_err());
/// assert!(InstanceName::new("1test").is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn new<S: Into<String>>(name: S) -> Result<Self, InstanceNameError> {
let name = name.into();
Self::validate(&name)?;
Ok(Self(name))
}
/// Returns the instance name as a string slice.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
/// Validates an instance name according to LXD requirements.
///
/// # Arguments
///
/// * `name` - The name to validate
///
/// # Returns
///
/// * `Ok(())` - If the name is valid
/// * `Err(InstanceNameError)` - If the name violates any requirement
fn validate(name: &str) -> Result<(), InstanceNameError> {
// Check length: must be between 1 and 63 characters
if name.is_empty() {
return Err(InstanceNameError::Empty);
}
if name.len() > 63 {
return Err(InstanceNameError::TooLong { length: name.len() });
}
// Check characters: only ASCII letters, numbers, and dashes
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
return Err(InstanceNameError::InvalidCharacters);
}
// Check first character: must not be a digit or dash
if let Some(first_char) = name.chars().next() {
if first_char.is_ascii_digit() || first_char == '-' {
return Err(InstanceNameError::InvalidFirstCharacter);
}
}
// Check last character: must not be a dash
if name.ends_with('-') {
return Err(InstanceNameError::InvalidLastCharacter);
}
Ok(())
}
}
impl fmt::Display for InstanceName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for InstanceName {
type Err = InstanceNameError;
fn from_str(s: &str) -> Result<Self, InstanceNameError> {
Self::new(s.to_string())
}
}
impl AsRef<str> for InstanceName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl TryFrom<String> for InstanceName {
type Error = InstanceNameError;
fn try_from(value: String) -> Result<Self, InstanceNameError> {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_valid_instance_name() {
let name = InstanceName::new("test-instance").unwrap();
assert_eq!(name.as_str(), "test-instance");
}
#[test]
fn it_should_accept_string_slice_and_owned_string() {
// Test with &str
let name1 = InstanceName::new("test-instance").unwrap();
assert_eq!(name1.as_str(), "test-instance");
// Test with String
let name2 = InstanceName::new("test-instance".to_string()).unwrap();
assert_eq!(name2.as_str(), "test-instance");
// Test with String from format!
let name3 = InstanceName::new(format!("app-{}", "prod")).unwrap();
assert_eq!(name3.as_str(), "app-prod");
// Test that both are equal
assert_eq!(name1, name2);
}
#[test]
fn it_should_create_instance_name_with_numbers() {
let name = InstanceName::new("test123").unwrap();
assert_eq!(name.as_str(), "test123");
}
#[test]
fn it_should_create_instance_name_with_dashes() {
let name = InstanceName::new("test-instance-name").unwrap();
assert_eq!(name.as_str(), "test-instance-name");
}
#[test]
fn it_should_create_single_character_name() {
let name = InstanceName::new("a").unwrap();
assert_eq!(name.as_str(), "a");
}
#[test]
fn it_should_create_63_character_name() {
let long_name = "a".repeat(63);
let name = InstanceName::new(long_name.clone()).unwrap();
assert_eq!(name.as_str(), long_name);
}
#[test]
fn it_should_reject_empty_name() {
let result = InstanceName::new("");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InstanceNameError::Empty);
}
#[test]
fn it_should_reject_name_longer_than_63_characters() {
let long_name = "a".repeat(64);
let result = InstanceName::new(long_name);
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
InstanceNameError::TooLong { length: 64 }
);
}
#[test]
fn it_should_reject_name_starting_with_digit() {
let result = InstanceName::new("1test");
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
InstanceNameError::InvalidFirstCharacter
);
}
#[test]
fn it_should_reject_name_starting_with_dash() {
let result = InstanceName::new("-test");
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
InstanceNameError::InvalidFirstCharacter
);
}
#[test]
fn it_should_reject_name_ending_with_dash() {
let result = InstanceName::new("test-");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InstanceNameError::InvalidLastCharacter);
}
#[test]
fn it_should_reject_name_with_invalid_characters() {
let invalid_chars = vec![
"test@instance",
"test.instance",
"test_instance",
"test instance",
"test#instance",
"test$instance",
"test%instance",
"test*instance",
"test+instance",
"test=instance",
"test[instance]",
"test{instance}",
"test|instance",
"test\\instance",
"test:instance",
"test;instance",
"test\"instance",
"test'instance",
"test<instance>",
"test,instance",
"test?instance",
"test/instance",
];
for invalid_name in invalid_chars {
let result = InstanceName::new(invalid_name);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InstanceNameError::InvalidCharacters);
}
}
#[test]
fn it_should_reject_name_with_unicode_characters() {
let result = InstanceName::new("tést-instance");
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InstanceNameError::InvalidCharacters);
}
#[test]
fn it_should_display_instance_name() {
let name = InstanceName::new("test-instance").unwrap();
assert_eq!(format!("{name}"), "test-instance");
}
#[test]
fn it_should_parse_valid_name_from_string() {
let name: InstanceName = "test-instance".parse().unwrap();
assert_eq!(name.as_str(), "test-instance");
}
#[test]
fn it_should_fail_parsing_invalid_name_from_string() {
let result: Result<InstanceName, _> = "test-".parse();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), InstanceNameError::InvalidLastCharacter);
}
#[test]
fn it_should_implement_as_ref() {
let name = InstanceName::new("test-instance").unwrap();
let as_ref: &str = name.as_ref();
assert_eq!(as_ref, "test-instance");
}
#[test]
fn it_should_be_cloneable_and_comparable() {
let name1 = InstanceName::new("test-instance").unwrap();
let name2 = name1.clone();
assert_eq!(name1, name2);
}
#[test]
fn it_should_be_hashable() {
use std::collections::HashSet;
let mut set = HashSet::new();
let name1 = InstanceName::new("test-instance").unwrap();
let name2 = InstanceName::new("test-instance").unwrap();
let name3 = InstanceName::new("other-instance").unwrap();
set.insert(name1);
set.insert(name2); // Should not increase size due to equality
set.insert(name3);
assert_eq!(set.len(), 2);
}
#[test]
fn it_should_serialize_and_deserialize() {
let name = InstanceName::new("test-instance").unwrap();
// Serialize
let json = serde_json::to_string(&name).unwrap();
assert_eq!(json, "\"test-instance\"");
// Deserialize
let deserialized: InstanceName = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, name);
}
#[test]
fn it_should_fail_deserializing_invalid_name() {
let invalid_json = "\"test-\"";
let result: Result<InstanceName, _> = serde_json::from_str(invalid_json);
assert!(result.is_err());
}
}