Skip to main content

torrust_tracker_deployer_lib/domain/provider/
lxd.rs

1//! LXD Provider Domain Types
2//!
3//! This module contains domain types specific to the LXD provider.
4//! LXD is used for local development and testing, providing fast VM creation
5//! with no cloud costs, ideal for E2E tests and CI environments.
6
7use serde::{Deserialize, Serialize};
8
9use crate::domain::ProfileName;
10
11/// LXD-specific configuration (Domain Type)
12///
13/// LXD is used for local development and testing. It provides fast VM creation
14/// with no cloud costs, making it ideal for E2E tests and CI environments.
15///
16/// Uses validated domain types (e.g., `ProfileName`).
17///
18/// # Examples
19///
20/// ```rust
21/// use torrust_tracker_deployer_lib::domain::provider::LxdConfig;
22/// use torrust_tracker_deployer_lib::domain::ProfileName;
23///
24/// let config = LxdConfig {
25///     profile_name: ProfileName::new("torrust-profile-dev").unwrap(),
26/// };
27/// assert_eq!(config.profile_name.as_str(), "torrust-profile-dev");
28/// ```
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct LxdConfig {
31    /// LXD profile name for the instance (validated domain type).
32    ///
33    /// This profile must exist in LXD and typically configures
34    /// networking, storage, and resource limits.
35    pub profile_name: ProfileName,
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn it_should_store_validated_profile_name_when_created() {
44        let profile_name = ProfileName::new("test-profile").unwrap();
45        let config = LxdConfig {
46            profile_name: profile_name.clone(),
47        };
48        assert_eq!(config.profile_name, profile_name);
49    }
50
51    #[test]
52    fn it_should_serialize_to_json_when_valid_config_exists() {
53        let config = LxdConfig {
54            profile_name: ProfileName::new("torrust-profile").unwrap(),
55        };
56        let json = serde_json::to_string(&config).unwrap();
57
58        assert!(json.contains("\"profile_name\":\"torrust-profile\""));
59    }
60
61    #[test]
62    fn it_should_deserialize_from_json_when_valid_json_provided() {
63        let json = r#"{"profile_name":"torrust-profile"}"#;
64        let config: LxdConfig = serde_json::from_str(json).unwrap();
65
66        assert_eq!(config.profile_name.as_str(), "torrust-profile");
67    }
68
69    #[test]
70    fn it_should_be_cloneable_when_cloned() {
71        let config = LxdConfig {
72            profile_name: ProfileName::new("test").unwrap(),
73        };
74        let cloned = config.clone();
75        assert_eq!(config, cloned);
76    }
77
78    #[test]
79    fn it_should_implement_debug_trait_when_formatted() {
80        let config = LxdConfig {
81            profile_name: ProfileName::new("test").unwrap(),
82        };
83        let debug = format!("{config:?}");
84        assert!(debug.contains("LxdConfig"));
85        assert!(debug.contains("profile_name"));
86    }
87}