fix: opnsense dhcp test and formatting
All checks were successful
Run Check Script / check (pull_request) Successful in 1m12s
All checks were successful
Run Check Script / check (pull_request) Successful in 1m12s
This commit is contained in:
@@ -361,7 +361,9 @@ mod test {
|
||||
let mac = "00:11:22:33:44:55";
|
||||
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;
|
||||
assert_eq!(hosts.len(), 1);
|
||||
@@ -381,7 +383,7 @@ mod test {
|
||||
let domain = "some.domain";
|
||||
|
||||
dhcp_config
|
||||
.add_static_mapping(mac, ip, &format!("{hostname}.{domain}"))
|
||||
.add_static_mapping(&vec![mac.to_string()], &ip, &format!("{hostname}.{domain}"))
|
||||
.unwrap();
|
||||
|
||||
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
|
||||
@@ -408,7 +410,7 @@ mod test {
|
||||
let hostname = "existing-host";
|
||||
|
||||
dhcp_config
|
||||
.add_static_mapping(new_mac, ip, hostname)
|
||||
.add_static_mapping(&vec![new_mac.to_string()], &ip, hostname)
|
||||
.unwrap();
|
||||
|
||||
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";
|
||||
|
||||
// Using a different hostname should still find the host by IP and log a warning.
|
||||
let new_hostname = "different-host-name";
|
||||
dhcp_config
|
||||
.add_static_mapping(new_mac, ip, "different-host-name")
|
||||
.add_static_mapping(&vec![new_mac.to_string()], &ip, new_hostname)
|
||||
.unwrap();
|
||||
|
||||
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
|
||||
@@ -444,7 +447,7 @@ mod test {
|
||||
host.hwaddr.content_string(),
|
||||
"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]
|
||||
@@ -461,7 +464,11 @@ mod test {
|
||||
|
||||
// Using a different IP should still find the host by hostname and log a warning.
|
||||
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();
|
||||
|
||||
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
|
||||
@@ -471,7 +478,7 @@ mod test {
|
||||
host.hwaddr.content_string(),
|
||||
"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]
|
||||
@@ -481,7 +488,11 @@ mod test {
|
||||
let mut dhcp_config = setup_test_env(vec![initial_host]);
|
||||
|
||||
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();
|
||||
|
||||
let hosts = &dhcp_config.opnsense.dnsmasq.as_ref().unwrap().hosts;
|
||||
@@ -492,8 +503,11 @@ mod test {
|
||||
#[test]
|
||||
fn test_add_invalid_mac_address() {
|
||||
let mut dhcp_config = setup_test_env(vec![]);
|
||||
let result =
|
||||
dhcp_config.add_static_mapping("invalid-mac", Ipv4Addr::new(10, 0, 0, 1), "host");
|
||||
let result = dhcp_config.add_static_mapping(
|
||||
&vec!["invalid-mac".to_string()],
|
||||
&Ipv4Addr::new(10, 0, 0, 1),
|
||||
"host",
|
||||
);
|
||||
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 result = dhcp_config.add_static_mapping(
|
||||
"CC:CC:CC:CC:CC:CC",
|
||||
Ipv4Addr::new(192, 168, 1, 10),
|
||||
&vec!["CC:CC:CC:CC:CC:CC".to_string()],
|
||||
&Ipv4Addr::new(192, 168, 1, 10),
|
||||
"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.
|
||||
let result = dhcp_config.add_static_mapping(
|
||||
"CC:CC:CC:CC:CC:CC",
|
||||
Ipv4Addr::new(192, 168, 1, 30),
|
||||
&vec!["CC:CC:CC:CC:CC:CC".to_string()],
|
||||
&Ipv4Addr::new(192, 168, 1, 30),
|
||||
"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())));
|
||||
|
||||
Reference in New Issue
Block a user