|
| 1 | +use serde::Serialize; |
| 2 | +use thiserror::Error; |
| 3 | + |
| 4 | +/// Errors that can occur when creating an `AnsibleVariablesContext` |
| 5 | +#[derive(Debug, Error)] |
| 6 | +pub enum AnsibleVariablesContextError { |
| 7 | + /// Invalid SSH port |
| 8 | + #[error("Invalid SSH port: {0}")] |
| 9 | + InvalidSshPort(#[from] crate::infrastructure::external_tools::ansible::template::wrappers::inventory::context::AnsiblePortError), |
| 10 | +} |
| 11 | + |
| 12 | +/// Context for rendering the variables.yml.tera template |
| 13 | +/// |
| 14 | +/// This context contains system configuration variables used across |
| 15 | +/// Ansible playbooks (but NOT inventory connection variables). |
| 16 | +#[derive(Serialize, Debug, Clone)] |
| 17 | +pub struct AnsibleVariablesContext { |
| 18 | + /// SSH port to configure in firewall and other services |
| 19 | + ssh_port: u16, |
| 20 | +} |
| 21 | + |
| 22 | +impl AnsibleVariablesContext { |
| 23 | + /// Creates a new context with the specified SSH port |
| 24 | + /// |
| 25 | + /// # Errors |
| 26 | + /// |
| 27 | + /// Returns an error if the SSH port is invalid (0 or out of range) |
| 28 | + pub fn new(ssh_port: u16) -> Result<Self, AnsibleVariablesContextError> { |
| 29 | + // Validate SSH port using existing validation |
| 30 | + crate::infrastructure::external_tools::ansible::template::wrappers::inventory::context::AnsiblePort::new(ssh_port)?; |
| 31 | + |
| 32 | + Ok(Self { ssh_port }) |
| 33 | + } |
| 34 | + |
| 35 | + /// Get the SSH port |
| 36 | + #[must_use] |
| 37 | + pub fn ssh_port(&self) -> u16 { |
| 38 | + self.ssh_port |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn it_should_create_context_with_valid_ssh_port() { |
| 48 | + let context = AnsibleVariablesContext::new(22).unwrap(); |
| 49 | + assert_eq!(context.ssh_port(), 22); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn it_should_create_context_with_custom_ssh_port() { |
| 54 | + let context = AnsibleVariablesContext::new(2222).unwrap(); |
| 55 | + assert_eq!(context.ssh_port(), 2222); |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn it_should_create_context_with_high_port() { |
| 60 | + let context = AnsibleVariablesContext::new(65535).unwrap(); |
| 61 | + assert_eq!(context.ssh_port(), 65535); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn it_should_fail_with_port_zero() { |
| 66 | + let result = AnsibleVariablesContext::new(0); |
| 67 | + assert!(result.is_err()); |
| 68 | + let error_msg = result.unwrap_err().to_string(); |
| 69 | + assert!(error_msg.contains("Invalid SSH port")); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn it_should_implement_clone() { |
| 74 | + let context1 = AnsibleVariablesContext::new(22).unwrap(); |
| 75 | + let context2 = context1.clone(); |
| 76 | + assert_eq!(context1.ssh_port(), context2.ssh_port()); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn it_should_serialize_to_json() { |
| 81 | + let context = AnsibleVariablesContext::new(8022).unwrap(); |
| 82 | + let json = serde_json::to_string(&context).unwrap(); |
| 83 | + assert!(json.contains("\"ssh_port\":8022")); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn it_should_display_error_message_correctly() { |
| 88 | + let error = AnsibleVariablesContext::new(0).unwrap_err(); |
| 89 | + let error_msg = format!("{error}"); |
| 90 | + assert!(error_msg.contains("Invalid SSH port")); |
| 91 | + assert!(error_msg.contains("Invalid port number: 0")); |
| 92 | + } |
| 93 | +} |
0 commit comments