Commit 586f8506 authored by Daniel P. Berrange's avatar Daniel P. Berrange

Add helpers for building storage pool and storage volume XML

parent f2b850a5
......@@ -6,6 +6,9 @@ use warnings;
use Sys::Virt;
use Sys::Virt::TCK::DomainBuilder;
use Sys::Virt::TCK::NetworkBuilder;
use Sys::Virt::TCK::StoragePoolBuilder;
use Sys::Virt::TCK::StorageVolBuilder;
use Sys::Virt::TCK::Capabilities;
use Config::Record;
......
package Sys::Virt::TCK::StoragePoolBuilder;
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" ,
type => $params{type} ? $params{type} : "dir",
source => {},
};
bless $self, $class;
return $self;
}
sub source_host {
my $self = shift;
$self->{source}->{host} = shift;
return $self;
}
sub source_dir {
my $self = shift;
$self->{source}->{dir} = shift;
return $self;
}
sub source_device {
my $self = shift;
my @devs = @_;
$self->{source}->{device} = \@devs;
return $self;
}
sub source_adapter {
my $self = shift;
$self->{source}->{adapter} = shift;
return $self;
}
sub source_name {
my $self = shift;
$self->{source}->{name} = shift;
return $self;
}
sub target {
my $self = shift;
$self->{target} = shift;
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("pool", type => $self->{type});
$w->dataElement("name" => $self->{name});
$w->startTag("source");
if ($self->{source}->{host}) {
$w->emptyTag("host", name => $self->{source}->{host});
}
if ($self->{source}->{dir}) {
$w->emptyTag("dir", path => $self->{source}->{dir});
}
if ($self->{source}->{device}) {
foreach my $dev (@{$self->{source}->{device}}) {
$w->emptyTag("dev", path => $dev);
}
}
if ($self->{source}->{adapter}) {
$w->emptyTag("adapter", name => $self->{source}->{adapter});
}
if ($self->{source}->{name}) {
$w->dataElement("name", $self->{source}->{name});
}
$w->endTag("source");
$w->startTag("target");
$w->dataElement("path", $self->{target});
$w->endTag("target");
$w->endTag("pool");
return $data;
}
1;
package Sys::Virt::TCK::StorageVolBuilder;
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 capacity {
my $self = shift;
$self->{capacity} = shift;
return $self;
}
sub allocation {
my $self = shift;
$self->{allocation} = shift;
return $self;
}
sub format {
my $self = shift;
$self->{format} = shift;
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("volume");
$w->dataElement("name" => $self->{name});
$w->dataElement("capacity", $self->{capacity});
$w->dataElement("allocation", $self->{allocation});
if ($self->{format}) {
$w->startTag("target");
$w->emptyTag("format", type => $self->{format});
$w->endTag("target");
}
$w->endTag("volume");
return $data;
}
1;
# -*- perl -*-
use Test::More tests => 8;
use strict;
use warnings;
BEGIN {
use_ok("Sys::Virt::TCK::StoragePoolBuilder");
}
my $xmlDir = <<EOF;
<pool type="dir">
<name>test</name>
<source>
<dir path="/var/lib/libvirt/images" />
</source>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool>
EOF
chomp $xmlDir;
my $builtDir = Sys::Virt::TCK::StoragePoolBuilder->new()
->source_dir("/var/lib/libvirt/images")
->target("/var/lib/libvirt/images")
->as_xml;
is ($builtDir, $xmlDir);
my $xmlFS = <<EOF;
<pool type="fs">
<name>test</name>
<source>
<dev path="/dev/sda1" />
</source>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool>
EOF
chomp $xmlFS;
my $builtFS = Sys::Virt::TCK::StoragePoolBuilder->new(type => "fs")
->source_device("/dev/sda1")
->target("/var/lib/libvirt/images")
->as_xml;
is ($builtFS, $xmlFS);
my $xmlNetFS = <<EOF;
<pool type="netfs">
<name>test</name>
<source>
<host name="nfs.example.com" />
<dir path="/var/lib/libvirt/images" />
</source>
<target>
<path>/var/lib/libvirt/images</path>
</target>
</pool>
EOF
chomp $xmlNetFS;
my $builtNetFS = Sys::Virt::TCK::StoragePoolBuilder->new(type => "netfs")
->source_host("nfs.example.com")
->source_dir("/var/lib/libvirt/images")
->target("/var/lib/libvirt/images")
->as_xml;
is ($builtNetFS, $xmlNetFS);
my $xmlLogical = <<EOF;
<pool type="logical">
<name>test</name>
<source>
<dev path="/dev/sda1" />
<dev path="/dev/sdb1" />
<dev path="/dev/sdc1" />
<name>test</name>
</source>
<target>
<path>/dev/test</path>
</target>
</pool>
EOF
chomp $xmlLogical;
my $builtLogical = Sys::Virt::TCK::StoragePoolBuilder->new(type => "logical")
->source_device("/dev/sda1", "/dev/sdb1", "/dev/sdc1")
->source_name("test")
->target("/dev/test")
->as_xml;
is ($builtLogical, $xmlLogical);
my $xmlDisk = <<EOF;
<pool type="disk">
<name>test</name>
<source>
<dev path="/dev/sda" />
</source>
<target>
<path>/dev</path>
</target>
</pool>
EOF
chomp $xmlDisk;
my $builtDisk = Sys::Virt::TCK::StoragePoolBuilder->new(type => "disk")
->source_device("/dev/sda")
->target("/dev")
->as_xml;
is ($builtDisk, $xmlDisk);
my $xmlSCSI = <<EOF;
<pool type="scsi">
<name>test</name>
<source>
<adapter name="scsi1" />
</source>
<target>
<path>/dev</path>
</target>
</pool>
EOF
chomp $xmlSCSI;
my $builtSCSI = Sys::Virt::TCK::StoragePoolBuilder->new(type => "scsi")
->source_adapter("scsi1")
->target("/dev")
->as_xml;
is ($builtSCSI, $xmlSCSI);
my $xmlISCSI = <<EOF;
<pool type="iscsi">
<name>test</name>
<source>
<host name="iscsi.example.com" />
<dev path="test.target" />
</source>
<target>
<path>/dev</path>
</target>
</pool>
EOF
chomp $xmlISCSI;
my $builtISCSI = Sys::Virt::TCK::StoragePoolBuilder->new(type => "iscsi")
->source_host("iscsi.example.com")
->source_device("test.target")
->target("/dev")
->as_xml;
is ($builtISCSI, $xmlISCSI);
# -*- perl -*-
use Test::More tests => 2;
BEGIN {
use_ok("Sys::Virt::TCK::StorageVolBuilder");
}
my $xml = <<EOF;
<volume>
<name>test</name>
<capacity>1000000</capacity>
<allocation>1000000</allocation>
<target>
<format type="qcow2" />
</target>
</volume>
EOF
chomp $xml;
my $b = Sys::Virt::TCK::StorageVolBuilder->new()
->capacity(1000000)->allocation(1000000)
->format("qcow2")
->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