[libvirt] [tck PATCH 2/9] Use $net->get_dhcp_leases() when available

Laine Stump posted 9 patches 7 years, 3 months ago
There is a newer version of this series
[libvirt] [tck PATCH 2/9] Use $net->get_dhcp_leases() when available
Posted by Laine Stump 7 years, 3 months ago
Newer versions of libvirt no longer let dnsmasq create a leases file,
they keep track of it themselves and provide an API to retrieve the
current list of leases. Use that to get the guest's IP address when
it's available.
---

I later realized that it might be more appropriate to use
$dom->get_interface_addresses(), but I'd already rewritten the
existing function this way and it works, so I left it.

 lib/Sys/Virt/TCK/NetworkHelpers.pm        | 9 +++++++++
 scripts/nwfilter/100-ping-still-working.t | 2 +-
 scripts/nwfilter/210-no-mac-spoofing.t    | 2 +-
 scripts/nwfilter/220-no-ip-spoofing.t     | 2 +-
 scripts/nwfilter/230-no-mac-broadcast.t   | 2 +-
 scripts/nwfilter/240-no-arp-spoofing.t    | 2 +-
 scripts/nwfilter/nwfilter_concurrent.sh   | 4 ++--
 7 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/lib/Sys/Virt/TCK/NetworkHelpers.pm b/lib/Sys/Virt/TCK/NetworkHelpers.pm
index 133064b..f6bf8f9 100644
--- a/lib/Sys/Virt/TCK/NetworkHelpers.pm
+++ b/lib/Sys/Virt/TCK/NetworkHelpers.pm
@@ -10,7 +10,16 @@ sub get_first_macaddress {
 }
 
 sub get_ip_from_leases{
+    my $conn = shift;
+    my $netname = shift;
     my $mac = shift;
+
+    my $net = $conn->get_network_by_name($netname);
+    if ($net->can('get_dhcp_leases')) {
+        my @leases = $net->get_dhcp_leases($mac);
+        return @leases ? @leases[0]->{'ipaddr'} : undef;
+    }
+
     my $tmp = `grep $mac /var/lib/libvirt/dnsmasq/default.leases`;
     my @fields = split(/ /, $tmp);
     my $ip = $fields[2];
diff --git a/scripts/nwfilter/100-ping-still-working.t b/scripts/nwfilter/100-ping-still-working.t
index a20b95d..dc1efd2 100644
--- a/scripts/nwfilter/100-ping-still-working.t
+++ b/scripts/nwfilter/100-ping-still-working.t
@@ -69,7 +69,7 @@ sleep(10);
 my $mac = get_first_macaddress($dom);
 diag "mac is $mac";
 
-my $guestip = get_ip_from_leases($mac);
+my $guestip = get_ip_from_leases($conn, "default", $mac);
 diag "ip is $guestip";
 
 # check ebtables entry
diff --git a/scripts/nwfilter/210-no-mac-spoofing.t b/scripts/nwfilter/210-no-mac-spoofing.t
index b81fc4a..03001a8 100644
--- a/scripts/nwfilter/210-no-mac-spoofing.t
+++ b/scripts/nwfilter/210-no-mac-spoofing.t
@@ -69,7 +69,7 @@ sleep(10);
 my $mac =  get_first_macaddress($dom);
 diag "mac is $mac";
 
-my $guestip = get_ip_from_leases($mac);
+my $guestip = get_ip_from_leases($conn, "default", $mac);
 diag "ip is $guestip";
 
 # check ebtables entry
diff --git a/scripts/nwfilter/220-no-ip-spoofing.t b/scripts/nwfilter/220-no-ip-spoofing.t
index 3a0213d..d447a19 100644
--- a/scripts/nwfilter/220-no-ip-spoofing.t
+++ b/scripts/nwfilter/220-no-ip-spoofing.t
@@ -60,7 +60,7 @@ sleep(30);
 my $mac =  get_first_macaddress($dom);
 diag "mac is $mac";
 
-my $guestip = get_ip_from_leases($mac);
+my $guestip = get_ip_from_leases($conn, "default", $mac);
 diag "ip is $guestip";
 
 # check ebtables entry
diff --git a/scripts/nwfilter/230-no-mac-broadcast.t b/scripts/nwfilter/230-no-mac-broadcast.t
index 16ce60d..9d00dc4 100644
--- a/scripts/nwfilter/230-no-mac-broadcast.t
+++ b/scripts/nwfilter/230-no-mac-broadcast.t
@@ -68,7 +68,7 @@ sleep(10);
 my $mac =  get_first_macaddress($dom);
 diag "mac is $mac";
 
-my $guestip = get_ip_from_leases($mac);
+my $guestip = get_ip_from_leases($conn, "default", $mac);
 diag "ip is $guestip";
 
 # check ebtables entry
diff --git a/scripts/nwfilter/240-no-arp-spoofing.t b/scripts/nwfilter/240-no-arp-spoofing.t
index 284033d..f1e6870 100644
--- a/scripts/nwfilter/240-no-arp-spoofing.t
+++ b/scripts/nwfilter/240-no-arp-spoofing.t
@@ -70,7 +70,7 @@ sleep(10);
 my $mac =  get_first_macaddress($dom);
 diag "mac is $mac";
 
-my $guestip = get_ip_from_leases($mac);
+my $guestip = get_ip_from_leases($conn, "default", $mac);
 diag "ip is $guestip";
 
 # check ebtables entry
diff --git a/scripts/nwfilter/nwfilter_concurrent.sh b/scripts/nwfilter/nwfilter_concurrent.sh
index 4c9b878..359e2ab 100644
--- a/scripts/nwfilter/nwfilter_concurrent.sh
+++ b/scripts/nwfilter/nwfilter_concurrent.sh
@@ -242,9 +242,9 @@ runTest()
 
   [ $? -ne 0 ] && rm -rf "${tmpdir}" && return 1;
 
-  # Test runs for a maximum of 5 minutes
+  # Test runs for a maximum of 10 minutes
   now=`date +%s`
-  test_end=$(($now + 5 * 60))
+  test_end=$(($now + 10 * 60))
 
   while :;
   do
-- 
2.13.6

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [tck PATCH 2/9] Use $net->get_dhcp_leases() when available
Posted by Daniel P. Berrangé 7 years, 3 months ago
On Wed, Feb 07, 2018 at 09:04:52PM -0500, Laine Stump wrote:
> Newer versions of libvirt no longer let dnsmasq create a leases file,
> they keep track of it themselves and provide an API to retrieve the
> current list of leases. Use that to get the guest's IP address when
> it's available.
> ---
> 
> I later realized that it might be more appropriate to use
> $dom->get_interface_addresses(), but I'd already rewritten the
> existing function this way and it works, so I left it.
> 
>  lib/Sys/Virt/TCK/NetworkHelpers.pm        | 9 +++++++++
>  scripts/nwfilter/100-ping-still-working.t | 2 +-
>  scripts/nwfilter/210-no-mac-spoofing.t    | 2 +-
>  scripts/nwfilter/220-no-ip-spoofing.t     | 2 +-
>  scripts/nwfilter/230-no-mac-broadcast.t   | 2 +-
>  scripts/nwfilter/240-no-arp-spoofing.t    | 2 +-
>  scripts/nwfilter/nwfilter_concurrent.sh   | 4 ++--
>  7 files changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/lib/Sys/Virt/TCK/NetworkHelpers.pm b/lib/Sys/Virt/TCK/NetworkHelpers.pm
> index 133064b..f6bf8f9 100644
> --- a/lib/Sys/Virt/TCK/NetworkHelpers.pm
> +++ b/lib/Sys/Virt/TCK/NetworkHelpers.pm
> @@ -10,7 +10,16 @@ sub get_first_macaddress {
>  }
>  
>  sub get_ip_from_leases{
> +    my $conn = shift;
> +    my $netname = shift;
>      my $mac = shift;
> +
> +    my $net = $conn->get_network_by_name($netname);
> +    if ($net->can('get_dhcp_leases')) {
> +        my @leases = $net->get_dhcp_leases($mac);
> +        return @leases ? @leases[0]->{'ipaddr'} : undef;
> +    }
> +
>      my $tmp = `grep $mac /var/lib/libvirt/dnsmasq/default.leases`;
>      my @fields = split(/ /, $tmp);
>      my $ip = $fields[2];
> diff --git a/scripts/nwfilter/100-ping-still-working.t b/scripts/nwfilter/100-ping-still-working.t
> index a20b95d..dc1efd2 100644
> --- a/scripts/nwfilter/100-ping-still-working.t
> +++ b/scripts/nwfilter/100-ping-still-working.t
> @@ -69,7 +69,7 @@ sleep(10);
>  my $mac = get_first_macaddress($dom);
>  diag "mac is $mac";
>  
> -my $guestip = get_ip_from_leases($mac);
> +my $guestip = get_ip_from_leases($conn, "default", $mac);
>  diag "ip is $guestip";
>  
>  # check ebtables entry
> diff --git a/scripts/nwfilter/210-no-mac-spoofing.t b/scripts/nwfilter/210-no-mac-spoofing.t
> index b81fc4a..03001a8 100644
> --- a/scripts/nwfilter/210-no-mac-spoofing.t
> +++ b/scripts/nwfilter/210-no-mac-spoofing.t
> @@ -69,7 +69,7 @@ sleep(10);
>  my $mac =  get_first_macaddress($dom);
>  diag "mac is $mac";
>  
> -my $guestip = get_ip_from_leases($mac);
> +my $guestip = get_ip_from_leases($conn, "default", $mac);
>  diag "ip is $guestip";
>  
>  # check ebtables entry
> diff --git a/scripts/nwfilter/220-no-ip-spoofing.t b/scripts/nwfilter/220-no-ip-spoofing.t
> index 3a0213d..d447a19 100644
> --- a/scripts/nwfilter/220-no-ip-spoofing.t
> +++ b/scripts/nwfilter/220-no-ip-spoofing.t
> @@ -60,7 +60,7 @@ sleep(30);
>  my $mac =  get_first_macaddress($dom);
>  diag "mac is $mac";
>  
> -my $guestip = get_ip_from_leases($mac);
> +my $guestip = get_ip_from_leases($conn, "default", $mac);
>  diag "ip is $guestip";
>  
>  # check ebtables entry
> diff --git a/scripts/nwfilter/230-no-mac-broadcast.t b/scripts/nwfilter/230-no-mac-broadcast.t
> index 16ce60d..9d00dc4 100644
> --- a/scripts/nwfilter/230-no-mac-broadcast.t
> +++ b/scripts/nwfilter/230-no-mac-broadcast.t
> @@ -68,7 +68,7 @@ sleep(10);
>  my $mac =  get_first_macaddress($dom);
>  diag "mac is $mac";
>  
> -my $guestip = get_ip_from_leases($mac);
> +my $guestip = get_ip_from_leases($conn, "default", $mac);
>  diag "ip is $guestip";
>  
>  # check ebtables entry
> diff --git a/scripts/nwfilter/240-no-arp-spoofing.t b/scripts/nwfilter/240-no-arp-spoofing.t
> index 284033d..f1e6870 100644
> --- a/scripts/nwfilter/240-no-arp-spoofing.t
> +++ b/scripts/nwfilter/240-no-arp-spoofing.t
> @@ -70,7 +70,7 @@ sleep(10);
>  my $mac =  get_first_macaddress($dom);
>  diag "mac is $mac";
>  
> -my $guestip = get_ip_from_leases($mac);
> +my $guestip = get_ip_from_leases($conn, "default", $mac);
>  diag "ip is $guestip";
>  
>  # check ebtables entry

Upto this point

Reviewed-by: Daniel P. Berrange <berrange@redhat.com>



> diff --git a/scripts/nwfilter/nwfilter_concurrent.sh b/scripts/nwfilter/nwfilter_concurrent.sh
> index 4c9b878..359e2ab 100644
> --- a/scripts/nwfilter/nwfilter_concurrent.sh
> +++ b/scripts/nwfilter/nwfilter_concurrent.sh
> @@ -242,9 +242,9 @@ runTest()
>  
>    [ $? -ne 0 ] && rm -rf "${tmpdir}" && return 1;
>  
> -  # Test runs for a maximum of 5 minutes
> +  # Test runs for a maximum of 10 minutes
>    now=`date +%s`
> -  test_end=$(($now + 5 * 60))
> +  test_end=$(($now + 10 * 60))
>  
>    while :;
>    do

Just split that into a second patch if still needed

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [tck PATCH 2/9] Use $net->get_dhcp_leases() when available
Posted by Laine Stump 7 years, 3 months ago
On 02/08/2018 04:15 AM, Daniel P. Berrangé wrote:
>> diff --git a/scripts/nwfilter/nwfilter_concurrent.sh b/scripts/nwfilter/nwfilter_concurrent.sh
>> index 4c9b878..359e2ab 100644
>> --- a/scripts/nwfilter/nwfilter_concurrent.sh
>> +++ b/scripts/nwfilter/nwfilter_concurrent.sh
>> @@ -242,9 +242,9 @@ runTest()
>>  
>>    [ $? -ne 0 ] && rm -rf "${tmpdir}" && return 1;
>>  
>> -  # Test runs for a maximum of 5 minutes
>> +  # Test runs for a maximum of 10 minutes
>>    now=`date +%s`
>> -  test_end=$(($now + 5 * 60))
>> +  test_end=$(($now + 10 * 60))
>>  
>>    while :;
>>    do
> Just split that into a second patch if still needed


Oops. I had that in for debugging and forgot to remove it.

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list