5
0
mirror of git://git.proxmox.com/git/qemu-server.git synced 2024-12-23 17:34:19 +03:00

add tests for qemu_img_convert

Add tests for the qemu_img_convert parameters to the resulting
'qemu-img convert' call

we mock the 'run_command' and extract the 'cmd' parameter to
compare with what we expect

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2019-10-17 13:32:33 +02:00 committed by Thomas Lamprecht
parent a447e92c09
commit 458a2b2640
2 changed files with 214 additions and 1 deletions

View File

@ -1,6 +1,6 @@
all: test
test: test_snapshot test_ovf test_cfg_to_cmd test_pci_addr_conflicts
test: test_snapshot test_ovf test_cfg_to_cmd test_pci_addr_conflicts test_qemu_img_convert
test_snapshot: run_snapshot_tests.pl
./run_snapshot_tests.pl
@ -12,5 +12,8 @@ test_ovf: run_ovf_tests.pl
test_cfg_to_cmd: run_config2command_tests.pl cfg2cmd/*.conf
perl -I../ ./run_config2command_tests.pl
test_qemu_img_convert: run_qemu_img_convert_tests.pl
perl -I../ ./run_qemu_img_convert_tests.pl
test_pci_addr_conflicts: run_pci_addr_checks.pl
./run_pci_addr_checks.pl

View File

@ -0,0 +1,210 @@
#!/usr/bin/perl
use strict;
use warnings;
use lib qw(..);
use Test::More;
use Test::MockModule;
use PVE::QemuServer;
my $vmid = 8006;
my $storage_config = {
ids => {
local => {
content => {
images => 1,
},
path => "/var/lib/vz",
type => "dir",
shared => 0,
},
"rbd-store" => {
monhost => "127.0.0.42,127.0.0.21,::1",
content => {
images => 1
},
type => "rbd",
pool => "cpool",
username => "admin",
shared => 1
},
"local-lvm" => {
vgname => "pve",
bwlimit => "restore=1024",
type => "lvmthin",
thinpool => "data",
content => {
images => 1,
}
},
"zfs-over-iscsi" => {
type => "zfs",
iscsiprovider => "LIO",
lio_tpg => "tpg1",
portal => "127.0.0.1",
target => "iqn.2019-10.org.test:foobar",
pool => "tank",
}
}
};
my $tests = [
{
name => 'qcow2raw',
parameters => [ "local:$vmid/vm-$vmid-disk-0.qcow2", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "qcow2", "-O", "raw",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.qcow2", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw"
],
},
{
name => "raw2qcow2",
parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "local:$vmid/vm-$vmid-disk-0.qcow2", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "qcow2",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.qcow2"
]
},
{
name => "local2rbd",
parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "rbd-store:vm-$vmid-disk-0", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw", "rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none"
]
},
{
name => "rbd2local",
parameters => [ "rbd-store:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
"rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none", "/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw"
]
},
{
name => "local2zos",
parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "zfs-over-iscsi:vm-$vmid-disk-0", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "--target-image-opts",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
"file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw"
]
},
{
name => "zos2local",
parameters => [ "zfs-over-iscsi:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "--image-opts", "-O", "raw",
"file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
]
},
{
name => "zos2rbd",
parameters => [ "zfs-over-iscsi:vm-$vmid-disk-0", "rbd-store:vm-$vmid-disk-0", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "--image-opts", "-O", "raw",
"file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
"rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none"
]
},
{
name => "rbd2zos",
parameters => [ "rbd-store:vm-$vmid-disk-0", "zfs-over-iscsi:vm-$vmid-disk-0", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "--target-image-opts",
"rbd:cpool/vm-$vmid-disk-0:mon_host=127.0.0.42;127.0.0.21;[\\:\\:1]:auth_supported=none",
"file.driver=iscsi,file.transport=tcp,file.initiator-name=foobar,file.portal=127.0.0.1,file.target=iqn.2019-10.org.test:foobar,file.lun=1,driver=raw",
]
},
{
name => "local2lvmthin",
parameters => [ "local:$vmid/vm-$vmid-disk-0.raw", "local-lvm:vm-$vmid-disk-0", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
"/dev/pve/vm-$vmid-disk-0",
]
},
{
name => "lvmthin2local",
parameters => [ "local-lvm:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 0 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
"/dev/pve/vm-$vmid-disk-0",
"/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
]
},
{
name => "zeroinit",
parameters => [ "local-lvm:vm-$vmid-disk-0", "local:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 1 ],
expected => [
"/usr/bin/qemu-img", "convert", "-p", "-n", "-f", "raw", "-O", "raw",
"/dev/pve/vm-$vmid-disk-0",
"zeroinit:/var/lib/vz/images/$vmid/vm-$vmid-disk-0.raw",
]
},
{
name => "notexistingstorage",
parameters => [ "local-lvm:vm-$vmid-disk-0", "not-existing:$vmid/vm-$vmid-disk-0.raw", 1024*10, undef, 1 ],
expected => "storage 'not-existing' does not exists\n",
},
];
my $command;
my $storage_module = Test::MockModule->new("PVE::Storage");
$storage_module->mock(
config => sub {
return $storage_config;
},
activate_volumes => sub {
return 1;
}
);
my $lio_module = Test::MockModule->new("PVE::Storage::LunCmd::LIO");
$lio_module->mock(
run_lun_command => sub {
return 1;
}
);
# we use the exported run_command so we have to mock it there
my $zfsplugin_module = Test::MockModule->new("PVE::Storage::ZFSPlugin");
$zfsplugin_module->mock(
run_command => sub {
return 1;
}
);
# we use the exported run_command so we have to mock it there
my $qemu_server_module = Test::MockModule->new("PVE::QemuServer");
$qemu_server_module->mock(
run_command => sub {
$command = shift;
},
get_initiator_name => sub {
return "foobar";
}
);
foreach my $test (@$tests) {
my $name = $test->{name};
my $expected = $test->{expected};
eval { PVE::QemuServer::qemu_img_convert(@{$test->{parameters}}) };
if (my $err = $@) {
is ($err, $expected, $name);
} elsif (defined($command)) {
is_deeply($command, $expected, $name);
$command = undef;
} else {
fail($name);
note("no command")
}
}
done_testing();