[libvirt] [tck PATCH v3 2/2] new NetworkHelper function get_network_ip()

Laine Stump laine at laine.org
Tue Mar 6 14:17:52 UTC 2018


This function gets the first IP address for the named virtual
network. It is returned as a Net::IP object, so that we will have info
about its netmask/prefix and can easily get it broadcast address and
perform arithmetic on the address.

Signed-off-by: Laine Stump <laine at laine.org>
---
 Build.PL                           |  1 +
 lib/Sys/Virt/TCK/NetworkHelpers.pm | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/Build.PL b/Build.PL
index 9593021..bf2d7a9 100644
--- a/Build.PL
+++ b/Build.PL
@@ -79,6 +79,7 @@ my $b = $class->new(
         'IO::String' => 0,
         'IO::Uncompress::Gunzip' => 0,
         'IO::Uncompress::Bunzip2' => 0,
+        'NetAddr::IP' => 0,
         'TAP::Formatter::HTML' => 0,
         'TAP::Formatter::JUnit' => 0,
         'TAP::Harness' => 3.11,
diff --git a/lib/Sys/Virt/TCK/NetworkHelpers.pm b/lib/Sys/Virt/TCK/NetworkHelpers.pm
index 5f563e5..7bbce62 100644
--- a/lib/Sys/Virt/TCK/NetworkHelpers.pm
+++ b/lib/Sys/Virt/TCK/NetworkHelpers.pm
@@ -1,4 +1,5 @@
 use Sys::Virt::TCK qw(xpath);
+use NetAddr::IP qw(:lower);
 use strict;
 use utf8;
 
@@ -9,6 +10,27 @@ sub get_first_macaddress {
     return $mac;
 }
 
+sub get_network_ip {
+    my $conn = shift;
+    my $netname = shift;
+    diag "getting ip for network $netname";
+    my $net = $conn->get_network_by_name($netname);
+    my $net_ip = xpath($net, "string(/network/ip[1]/\@address");
+    my $net_mask = xpath($net, "string(/network/ip[1]/\@netmask");
+    my $net_prefix = xpath($net, "string(/network/ip[1]/\@prefix");
+    my $ip;
+
+    if ($net_mask) {
+        $ip = NetAddr::IP->new($net_ip, $net_mask);
+    } elsif ($net_prefix) {
+        $ip = NetAddr::IP->new("$net_ip/$net_mask");
+    } else {
+        $ip = NetAddr::IP->new("$net_ip");
+    }
+    return $ip;
+}
+
+
 sub get_ip_from_leases{
     my $conn = shift;
     my $netname = shift;
-- 
2.14.3




More information about the libvir-list mailing list