sdn: dnsmasq: extract function that updates dnsmasq lease via dbus

Extract the dbus based interactions with dnsmasq so that it can be
mocked in tests.

Signed-off-by: Stefan Lendl <s.lendl@proxmox.com>
Reviewed-by: Max Carrara <m.carrara@proxmox.com>
Tested-by: Max Carrara <m.carrara@proxmox.com>
Reviewed-by: Stefan Hanreich <s.hanreich@proxmox.com>
Tested-by: Stefan Hanreich <s.hanreich@proxmox.com>
This commit is contained in:
Stefan Lendl 2024-04-05 15:18:01 +02:00 committed by Thomas Lamprecht
parent 23e7fe3197
commit 40f020660d

View file

@ -38,6 +38,17 @@ sub ethers_file {
return "$DNSMASQ_CONFIG_ROOT/$dhcpid/ethers";
}
sub update_lease {
my ($dhcpid, $ip4, $mac) = @_;
#update lease as ip could still be associated to an old removed mac
my $bus = Net::DBus->system();
my $dnsmasq = $bus->get_service("uk.org.thekelleys.dnsmasq.$dhcpid");
my $manager = $dnsmasq->get_object("/uk/org/thekelleys/dnsmasq","uk.org.thekelleys.dnsmasq.$dhcpid");
my @hostname = unpack("C*", "*");
$manager->AddDhcpLease($ip4, $mac, \@hostname, undef, 0, 0, 0) if $ip4;
}
sub add_ip_mapping {
my ($class, $dhcpid, $macdb, $mac, $ip4, $ip6) = @_;
@ -107,16 +118,7 @@ sub add_ip_mapping {
my $service_name = "dnsmasq\@$dhcpid";
systemctl_service('reload', $service_name) if $reload;
#update lease as ip could still be associated to an old removed mac
my $bus = Net::DBus->system();
my $dnsmasq = $bus->get_service("uk.org.thekelleys.dnsmasq.$dhcpid");
my $manager = $dnsmasq->get_object("/uk/org/thekelleys/dnsmasq","uk.org.thekelleys.dnsmasq.$dhcpid");
my @hostname = unpack("C*", "*");
$manager->AddDhcpLease($ip4, $mac, \@hostname, undef, 0, 0, 0) if $ip4;
# $manager->AddDhcpLease($ip6, $mac, \@hostname, undef, 0, 0, 0) if $ip6;
update_lease($dhcpid, $ip4, $mac);
}
sub configure_subnet {