| 1 |
# -*- perl -*- |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 25; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('Sys::Virt'); |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
my $conn = Sys::Virt->new(uri => "test:///default"); |
| 14 |
|
| 15 |
isa_ok($conn, "Sys::Virt"); |
| 16 |
|
| 17 |
|
| 18 |
my $nid = $conn->num_of_interfaces(); |
| 19 |
is($nid, 1, "1 active interface"); |
| 20 |
|
| 21 |
my @ifacenames = $conn->list_interface_names($nid); |
| 22 |
is_deeply(\@ifacenames, ["eth1"], "interface names"); |
| 23 |
|
| 24 |
my $iface = $conn->get_interface_by_name($ifacenames[0]); |
| 25 |
isa_ok($iface, "Sys::Virt::Interface"); |
| 26 |
|
| 27 |
is($iface->get_name, "eth1", "name"); |
| 28 |
|
| 29 |
my @ifaces; |
| 30 |
SKIP: { |
| 31 |
skip "Impl missing in test driver in libvirt 0.10.2", 2; |
| 32 |
|
| 33 |
@ifaces = $conn->list_all_interfaces(); |
| 34 |
is(int(@ifaces), 1, "1 active interface"); |
| 35 |
is($ifaces[0]->get_name, "2eth1", "interface name matches"); |
| 36 |
} |
| 37 |
|
| 38 |
ok($iface->is_active(), "interface is active"); |
| 39 |
|
| 40 |
# Lookup again via MAC to verify we get the same |
| 41 |
my $mac = $iface->get_mac(); |
| 42 |
|
| 43 |
my $iface2 = $conn->get_interface_by_mac($mac); |
| 44 |
isa_ok($iface2, "Sys::Virt::Interface"); |
| 45 |
is($iface2->get_name, "eth1", "name"); |
| 46 |
|
| 47 |
@ifaces = $conn->list_interfaces(); |
| 48 |
is($#ifaces, 0, "one interface"); |
| 49 |
isa_ok($ifaces[0], "Sys::Virt::Interface"); |
| 50 |
|
| 51 |
|
| 52 |
my $nname = $conn->num_of_defined_networks(); |
| 53 |
is($nname, 0, "0 defined interfaces"); |
| 54 |
|
| 55 |
my $xml = " |
| 56 |
<interface type='ethernet' name='eth2'> |
| 57 |
<start mode='onboot'/> |
| 58 |
<mac address='aa:bb:cc:ff:ee:ff'/> |
| 59 |
<mtu size='1492'/> |
| 60 |
<protocol family='ipv4'> |
| 61 |
<ip address='192.168.0.6' prefix='24'/> |
| 62 |
<route gateway='192.168.0.1'/> |
| 63 |
</protocol> |
| 64 |
</interface>"; |
| 65 |
|
| 66 |
$iface = $conn->define_interface($xml); |
| 67 |
|
| 68 |
$nname = $conn->num_of_defined_interfaces(); |
| 69 |
is($nname, 1, "1 defined interface"); |
| 70 |
|
| 71 |
my @names = $conn->list_defined_interface_names($nname); |
| 72 |
is_deeply(\@names, ["eth2"], "names"); |
| 73 |
|
| 74 |
@ifaces = $conn->list_defined_interfaces(); |
| 75 |
is($#ifaces, 0, "1 defined interface"); |
| 76 |
isa_ok($ifaces[0], "Sys::Virt::Interface"); |
| 77 |
|
| 78 |
$iface = $conn->get_interface_by_name("eth2"); |
| 79 |
isa_ok($iface, "Sys::Virt::Interface"); |
| 80 |
|
| 81 |
|
| 82 |
$iface->create(); |
| 83 |
|
| 84 |
my $nids = $conn->num_of_interfaces(); |
| 85 |
is($nids, 2, "2 active interfaces"); |
| 86 |
|
| 87 |
my @ids = sort { $a cmp $b } $conn->list_interface_names($nids); |
| 88 |
is_deeply(\@ids, ["eth1", "eth2"], "interface names"); |
| 89 |
|
| 90 |
$iface->destroy(); |
| 91 |
|
| 92 |
|
| 93 |
$nids = $conn->num_of_interfaces(); |
| 94 |
is($nids, 1, "1 active interfaces"); |
| 95 |
|
| 96 |
@ids = $conn->list_interface_names($nids); |
| 97 |
is_deeply(\@ids, ["eth1"], "interface names"); |
| 98 |
|
| 99 |
$iface = $conn->get_interface_by_name("eth2"); |
| 100 |
|
| 101 |
$iface->undefine(); |
| 102 |
|
| 103 |
|
| 104 |
$nname = $conn->num_of_defined_interfaces(); |
| 105 |
is($nname, 0, "0 defined interface"); |
| 106 |
|
| 107 |
@names = $conn->list_defined_interface_names($nname); |
| 108 |
is_deeply(\@names, [], "names"); |
| 109 |
|