Commit e8cfbbf6 authored by Daniel P. Berrange's avatar Daniel P. Berrange

Add helper for building network XML & tests for XML builders

parent 3b282903
package Sys::Virt::TCK::NetworkBuilder;
use strict;
use warnings;
use Sys::Virt;
use IO::String;
use XML::Writer;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
my $self = {
name => $params{name} ? $params{name} : "test" ,
};
bless $self, $class;
return $self;
}
sub bridge {
my $self = shift;
my $name = shift;
$self->{bridge} = { name => $name,
@_ };
return $self;
}
sub forward {
my $self = shift;
$self->{forward} = { @_ };
return $self;
}
sub ipaddr {
my $self = shift;
my $address = shift;
my $netmask = shift;
$self->{ipaddr} = [$address, $netmask];
return $self;
}
sub dhcp_range {
my $self = shift;
my $start = shift;
my $end = shift;
$self->{dhcp} = [] unless $self->{dhcp};
push @{$self->{dhcp}}, [$start, $end];
return $self;
}
sub as_xml {
my $self = shift;
my $data;
my $fh = IO::String->new(\$data);
my $w = XML::Writer->new(OUTPUT => $fh,
DATA_MODE => 1,
DATA_INDENT => 2);
$w->startTag("network");
$w->dataElement("name" => $self->{name});
$w->emptyTag("bridge", %{$self->{bridge}})
if $self->{bridge};
$w->emptyTag("forward", %{$self->{forward}})
if exists $self->{forward};
if ($self->{ipaddr}) {
$w->startTag("ip",
address => $self->{ipaddr}->[0],
netmask => $self->{ipaddr}->[1]);
if ($self->{dhcp}) {
$w->startTag("dhcp");
foreach my $range (@{$self->{dhcp}}) {
$w->emptyTag("range",
start => $range->[0],
end => $range->[1]);
}
$w->endTag("dhcp");
}
$w->endTag("ip");
}
$w->endTag("network");
return $data;
}
1;
# -*- perl -*-
use Test::More tests => 2;
BEGIN {
use_ok("Sys::Virt::TCK::DomainBuilder");
}
my $xml = <<EOF;
<domain type="xen">
<name>test</name>
<memory>512500</memory>
<currentMemory>512500</currentMemory>
<vcpu>3</vcpu>
<os>
<type>hvm</type>
<boot dev="hd" />
</os>
<features>
<acpi />
</features>
<devices>
<disk type="block">
<source dev="/dev/hda1" />
<target dev="/dev/xvda" bus="xen" />
</disk>
</devices>
</domain>
EOF
chomp $xml;
my $conn = Sys::Virt->new(address => "test:///default");
my $b = Sys::Virt::TCK::DomainBuilder->new(conn => $conn)
->with_acpi->memory(500*1025)->vcpu(3)
->disk(type => 'block', src => "/dev/hda1", dst => "/dev/xvda", bus => "xen")
->as_xml;
is ($b, $xml);
# -*- perl -*-
use Test::More tests => 2;
BEGIN {
use_ok("Sys::Virt::TCK::NetworkBuilder");
}
my $xml = <<EOF;
<network>
<name>test</name>
<bridge name="virbr0" />
<forward dev="eth0" />
<ip address="192.168.100.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.100.50" end="192.168.100.70" />
<range start="192.168.100.200" end="192.168.100.250" />
</dhcp>
</ip>
</network>
EOF
chomp $xml;
my $b = Sys::Virt::TCK::NetworkBuilder->new()
->forward(dev => 'eth0')->bridge("virbr0")
->ipaddr("192.168.100.1", "255.255.255.0")
->dhcp_range("192.168.100.50", "192.168.100.70")
->dhcp_range("192.168.100.200", "192.168.100.250")
->as_xml;
is ($b, $xml);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment