fix: opnsense dhcp test and formatting
All checks were successful
Run Check Script / check (pull_request) Successful in 1m12s

This commit is contained in:
Jean-Gabriel Gill-Couture 2025-09-03 08:53:47 -04:00
parent 5142e2dd2d
commit 160939de21
7 changed files with 34 additions and 31 deletions

View File

@ -5,7 +5,7 @@ use std::{
use cidr::Ipv4Cidr; use cidr::Ipv4Cidr;
use harmony::{ use harmony::{
hardware::{FirewallGroup, HostCategory, Location, PhysicalHost, SwitchGroup}, hardware::{HostCategory, Location, PhysicalHost, SwitchGroup},
infra::opnsense::OPNSenseManagementInterface, infra::opnsense::OPNSenseManagementInterface,
inventory::Inventory, inventory::Inventory,
modules::{ modules::{

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use derive_new::new; use derive_new::new;
use harmony_inventory_agent::hwinfo::{CPU, MemoryModule, NetworkInterface, StorageDrive}; use harmony_inventory_agent::hwinfo::{CPU, MemoryModule, NetworkInterface, StorageDrive};
use harmony_types::net::MacAddress; use harmony_types::net::MacAddress;
@ -275,9 +273,6 @@ pub enum HostCategory {
Switch, Switch,
} }
#[cfg(test)]
use harmony_macros::mac_address;
use harmony_types::id::Id; use harmony_types::id::Id;
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]

View File

@ -23,7 +23,7 @@ use crate::hardware::{ManagementInterface, ManualManagementInterface};
use super::{ use super::{
filter::Filter, filter::Filter,
hardware::{FirewallGroup, HostGroup, Location, SwitchGroup}, hardware::{HostGroup, Location, SwitchGroup},
}; };
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,4 +1,3 @@
use crate::infra::opnsense::Host;
use crate::infra::opnsense::LogicalHost; use crate::infra::opnsense::LogicalHost;
use crate::{ use crate::{
executors::ExecutorError, executors::ExecutorError,

View File

@ -47,19 +47,15 @@
//! - public_domain: External wildcard/apps domain (e.g., apps.example.com). //! - public_domain: External wildcard/apps domain (e.g., apps.example.com).
//! - internal_domain: Internal cluster domain (e.g., cluster.local or harmony.mcd). //! - internal_domain: Internal cluster domain (e.g., cluster.local or harmony.mcd).
use std::{fmt::Write, path::PathBuf, process::ExitStatus}; use std::{fmt::Write, path::PathBuf};
use async_trait::async_trait; use async_trait::async_trait;
use derive_new::new; use derive_new::new;
use harmony_secret::SecretManager; use harmony_secret::SecretManager;
use harmony_types::{id::Id, net::Url}; use harmony_types::id::Id;
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::{ use tokio::{fs::File, io::AsyncWriteExt, process::Command};
fs::File,
io::{AsyncReadExt, AsyncWriteExt},
process::Command,
};
use crate::{ use crate::{
config::secret::{RedhatSecret, SshKeyPair}, config::secret::{RedhatSecret, SshKeyPair},
@ -75,7 +71,6 @@ use crate::{
inventory::LaunchDiscoverInventoryAgentScore, inventory::LaunchDiscoverInventoryAgentScore,
okd::{ okd::{
bootstrap_load_balancer::OKDBootstrapLoadBalancerScore, bootstrap_load_balancer::OKDBootstrapLoadBalancerScore,
dns::OKDDnsScore,
templates::{BootstrapIpxeTpl, InstallConfigYaml}, templates::{BootstrapIpxeTpl, InstallConfigYaml},
}, },
}, },

View File

@ -1,4 +1,4 @@
use std::{sync::Arc, time::Duration}; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use log::debug; use log::debug;

View File

@ -361,7 +361,9 @@ mod test {
let mac = "00:11:22:33:44:55"; let mac = "00:11:22:33:44:55";
let hostname = "new-host"; let hostname = "new-host";
dhcp_config.add_static_mapping(mac, ip, hostname).unwrap(); dhcp_config
.add_static_mapping(&vec![mac.to_string()], &ip, hostname)
.unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
assert_eq!(hosts.len(), 1); assert_eq!(hosts.len(), 1);
@ -381,7 +383,7 @@ mod test {
let domain = "some.domain"; let domain = "some.domain";
dhcp_config dhcp_config
.add_static_mapping(mac, ip, &format!("{hostname}.{domain}")) .add_static_mapping(&vec![mac.to_string()], &ip, &format!("{hostname}.{domain}"))
.unwrap(); .unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
@ -408,7 +410,7 @@ mod test {
let hostname = "existing-host"; let hostname = "existing-host";
dhcp_config dhcp_config
.add_static_mapping(new_mac, ip, hostname) .add_static_mapping(&vec![new_mac.to_string()], &ip, hostname)
.unwrap(); .unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
@ -433,8 +435,9 @@ mod test {
let new_mac = "00:11:22:33:44:55"; let new_mac = "00:11:22:33:44:55";
// Using a different hostname should still find the host by IP and log a warning. // Using a different hostname should still find the host by IP and log a warning.
let new_hostname = "different-host-name";
dhcp_config dhcp_config
.add_static_mapping(new_mac, ip, "different-host-name") .add_static_mapping(&vec![new_mac.to_string()], &ip, new_hostname)
.unwrap(); .unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
@ -444,7 +447,7 @@ mod test {
host.hwaddr.content_string(), host.hwaddr.content_string(),
"AA:BB:CC:DD:EE:FF,00:11:22:33:44:55" "AA:BB:CC:DD:EE:FF,00:11:22:33:44:55"
); );
assert_eq!(host.host, "existing-host"); // Original hostname should be preserved. assert_eq!(host.host, new_hostname); // hostname should be updated
} }
#[test] #[test]
@ -461,7 +464,11 @@ mod test {
// Using a different IP should still find the host by hostname and log a warning. // Using a different IP should still find the host by hostname and log a warning.
dhcp_config dhcp_config
.add_static_mapping(new_mac, Ipv4Addr::new(192, 168, 1, 99), hostname) .add_static_mapping(
&vec![new_mac.to_string()],
&Ipv4Addr::new(192, 168, 1, 99),
hostname,
)
.unwrap(); .unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
@ -471,7 +478,7 @@ mod test {
host.hwaddr.content_string(), host.hwaddr.content_string(),
"AA:BB:CC:DD:EE:FF,00:11:22:33:44:55" "AA:BB:CC:DD:EE:FF,00:11:22:33:44:55"
); );
assert_eq!(host.ip.content_string(), "192.168.1.20"); // Original IP should be preserved. assert_eq!(host.ip.content_string(), "192.168.1.99"); // Original IP should be preserved.
} }
#[test] #[test]
@ -481,7 +488,11 @@ mod test {
let mut dhcp_config = setup_test_env(vec![initial_host]); let mut dhcp_config = setup_test_env(vec![initial_host]);
dhcp_config dhcp_config
.add_static_mapping(initial_mac, Ipv4Addr::new(192, 168, 1, 20), "host-1") .add_static_mapping(
&vec![initial_mac.to_string()],
&Ipv4Addr::new(192, 168, 1, 20),
"host-1",
)
.unwrap(); .unwrap();
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts; let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
@ -492,8 +503,11 @@ mod test {
#[test] #[test]
fn test_add_invalid_mac_address() { fn test_add_invalid_mac_address() {
let mut dhcp_config = setup_test_env(vec![]); let mut dhcp_config = setup_test_env(vec![]);
let result = let result = dhcp_config.add_static_mapping(
dhcp_config.add_static_mapping("invalid-mac", Ipv4Addr::new(10, 0, 0, 1), "host"); &vec!["invalid-mac".to_string()],
&Ipv4Addr::new(10, 0, 0, 1),
"host",
);
assert!(matches!(result, Err(DhcpError::InvalidMacAddress(_)))); assert!(matches!(result, Err(DhcpError::InvalidMacAddress(_))));
} }
@ -504,8 +518,8 @@ mod test {
let mut dhcp_config = setup_test_env(vec![host_a, host_b]); let mut dhcp_config = setup_test_env(vec![host_a, host_b]);
let result = dhcp_config.add_static_mapping( let result = dhcp_config.add_static_mapping(
"CC:CC:CC:CC:CC:CC", &vec!["CC:CC:CC:CC:CC:CC".to_string()],
Ipv4Addr::new(192, 168, 1, 10), &Ipv4Addr::new(192, 168, 1, 10),
"host-b", "host-b",
); );
// This IP belongs to host-a, but the hostname belongs to host-b. // This IP belongs to host-a, but the hostname belongs to host-b.
@ -520,8 +534,8 @@ mod test {
// This IP is ambiguous. // This IP is ambiguous.
let result = dhcp_config.add_static_mapping( let result = dhcp_config.add_static_mapping(
"CC:CC:CC:CC:CC:CC", &vec!["CC:CC:CC:CC:CC:CC".to_string()],
Ipv4Addr::new(192, 168, 1, 30), &Ipv4Addr::new(192, 168, 1, 30),
"new-host", "new-host",
); );
assert_eq!(result, Err(DhcpError::Configuration("Configuration conflict: Found multiple host entries matching IP 192.168.1.30 and/or hostname 'new-host'. Cannot resolve automatically.".to_string()))); assert_eq!(result, Err(DhcpError::Configuration("Configuration conflict: Found multiple host entries matching IP 192.168.1.30 and/or hostname 'new-host'. Cannot resolve automatically.".to_string())));