Commit 0cab9223 authored by Daniel P. Berrangé's avatar Daniel P. Berrangé

Add tests for virtual network <-> guest connections

Tested-by: 's avatarLaine Stump <laine@laine.org>
Reviewed-by: 's avatarLaine Stump <laine@laine.org>
Signed-off-by: 's avatarDaniel P. Berrangé <berrange@redhat.com>
parent 75aa5d74
......@@ -29,6 +29,7 @@ use File::Path qw(mkpath);
use File::Spec::Functions qw(catfile catdir rootdir);
use Cwd qw(cwd);
use LWP::UserAgent;
use IO::Interface::Simple;
use IO::Uncompress::Gunzip qw(gunzip);
use IO::Uncompress::Bunzip2 qw(bunzip2);
use XML::XPath;
......@@ -1285,6 +1286,38 @@ sub get_ip_from_leases{
}
sub find_free_ipv4_subnet {
my $index;
my %used;
foreach my $iface (IO::Interface::Simple->interfaces()) {
if ($iface->netmask eq "255.255.255.0" &&
$iface->address =~ /^192.168.(\d+).\d+/) {
$used{"$1"} = 1;
print "Used $1\n";
} else {
print "Not used ", $iface->address, "\n";
}
}
for (my $i = 1; $i < 255; $i++) {
if (!exists $used{"$i"}) {
$index = $i;
last;
}
}
return () unless defined $index;
return (
address => "192.168.$index.1",
netmask => "255.255.255.0",
dhcpstart => "192.168.$index.100",
dhcpend => "192.168.$index.200"
);
}
sub shutdown_vm_gracefully {
my $dom = shift;
......
......@@ -61,6 +61,22 @@ sub forward {
return $self;
}
sub interfaces {
my $self = shift;
$self->{interfaces} = [@_];
return $self;
}
sub host_devices {
my $self = shift;
$self->{host_devices} = [@_];
return $self;
}
sub ipaddr {
my $self = shift;
my $address = shift;
......@@ -98,8 +114,21 @@ sub as_xml {
$w->emptyTag("bridge", %{$self->{bridge}})
if $self->{bridge};
$w->emptyTag("forward", %{$self->{forward}})
if exists $self->{forward};
if (exists $self->{forward}) {
$w->startTag("forward", %{$self->{forward}});
foreach (@{$self->{interfaces}}) {
$w->emptyTag("interface", dev => $_);
}
foreach (@{$self->{host_devices}}) {
$w->emptyTag("address",
type => "pci",
domain => $_->[0],
bus => $_->[1],
slot => $_->[2],
function => $_->[3]);
}
$w->endTag("forward");
}
if ($self->{ipaddr}) {
$w->startTag("ip",
......
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/300-guest-network-isolated.t - guest connect to isolated network
=head1 DESCRIPTION
This test case validates that a guest is connected to an isolated
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my %subnet = Sys::Virt::TCK->find_free_ipv4_subnet();
SKIP: {
skip "No available IPv4 subnet", 4 unless defined $subnet{address};
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->bridge("tck");
$b->ipaddr($subnet{address}, $subnet{netmask});
$b->dhcp_range($subnet{dhcpstart}, $subnet{dhcpend});
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/310-guest-network-nat.t - guest connect to nat network
=head1 DESCRIPTION
This test case validates that a guest is connected to a nat
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my %subnet = Sys::Virt::TCK->find_free_ipv4_subnet();
SKIP: {
skip "No available IPv4 subnet", 4 unless defined $subnet{address};
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->bridge("tck");
$b->forward(mode => "nat");
$b->ipaddr($subnet{address}, $subnet{netmask});
$b->dhcp_range($subnet{dhcpstart}, $subnet{dhcpend});
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/320-guest-network-route.t - guest connect to routed network
=head1 DESCRIPTION
This test case validates that a guest is connected to a routed
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my %subnet = Sys::Virt::TCK->find_free_ipv4_subnet();
SKIP: {
skip "No available IPv4 subnet", 4 unless defined $subnet{address};
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->bridge("tck");
$b->forward(mode => "route");
$b->ipaddr($subnet{address}, $subnet{netmask});
$b->dhcp_range($subnet{dhcpstart}, $subnet{dhcpend});
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/330-guest-network-open.t - guest connect to open network
=head1 DESCRIPTION
This test case validates that a guest is connected to an open
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my %subnet = Sys::Virt::TCK->find_free_ipv4_subnet();
SKIP: {
skip "No available IPv4 subnet", 4 unless defined $subnet{address};
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->bridge("tck");
$b->forward(mode => "open");
$b->ipaddr($subnet{address}, $subnet{netmask});
$b->dhcp_range($subnet{dhcpstart}, $subnet{dhcpend});
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/340-guest-network-bridge.t - guest connect to bridge network
=head1 DESCRIPTION
This test case validates that a guest is connected to a bridge
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
((system "brctl addbr tck") == 0) or die "cannot create bridge 'tck'";
END { system "brctl delbr tck" }
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->bridge("tck");
$b->forward(mode => "bridge");
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/350-guest-network-private.t - guest connect to private network
=head1 DESCRIPTION
This test case validates that a guest is connected to a private
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my $hostnet = $tck->get_host_network_device();
SKIP: {
skip "No host device available", 4 unless $hostnet;
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->forward(mode => "private");
$b->interfaces($hostnet);
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/360-guest-network-vepa.t - guest connect to vepa network
=head1 DESCRIPTION
This test case validates that a guest is connected to a VEPA
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my $hostnet = $tck->get_host_network_device();
SKIP: {
skip "No host device available", 4 unless $hostnet;
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->forward(mode => "vepa");
$b->interfaces($hostnet);
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");
$b = $tck->generic_domain(name => "tck");
$b->interface(type => "network",
source => "tck",
model => "virtio",
mac => "52:54:00:11:11:11");
$xml = $b->as_xml();
diag "Creating a new transient domain";
diag $xml;
my $dom;
ok_domain(sub { $dom = $conn->create_domain($xml) }, "created transient domain object");
diag "Destroying the transient guest";
$dom->destroy;
diag "Checking that transient domain has gone away";
ok_error(sub { $conn->get_domain_by_name("tck") }, "NO_DOMAIN error raised from missing domain",
Sys::Virt::Error::ERR_NO_DOMAIN);
diag "Destroying the transient network";
$net->destroy;
diag "Checking that transient network has gone away";
ok_error(sub { $conn->get_network_by_name("tck") }, "NO_network error raised from missing network",
Sys::Virt::Error::ERR_NO_NETWORK);
}
# end
# -*- perl -*-
#
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
=pod
=head1 NAME
network/370-guest-network-passthrough.t - guest connect to passthrough network
=head1 DESCRIPTION
This test case validates that a guest is connected to a passthrough
virtual network
=cut
use strict;
use warnings;
use Test::More tests => 4;
use Sys::Virt::TCK;
my $tck = Sys::Virt::TCK->new();
my $conn = eval { $tck->setup(); };
BAIL_OUT "failed to setup test harness: $@" if $@;
END { $tck->cleanup if $tck; }
my $hostnet = $tck->get_host_network_device();
SKIP: {
skip "No host device available", 4 unless $hostnet;
my $b = Sys::Virt::TCK::NetworkBuilder->new(name => "tck");
$b->forward(mode => "passthrough");
$b->interfaces($hostnet);
my $xml = $b->as_xml();
diag "Creating a new transient network";
diag $xml;
my $net;
ok_network(sub { $net = $conn->create_network($xml) }, "created transient network object");