[pve-devel] [PATCH qemu-server v2 1/2] add tests for qemu_img_convert

Dominik Csapak d.csapak at proxmox.com
Thu Oct 17 13:32:33 CEST 2019


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 at proxmox.com>
---
changes from v1:
* changed /usr/bin/env perl to /usr/bin/perl
* better commit message
* better test structure

 test/Makefile                      |   5 +-
 test/run_qemu_img_convert_tests.pl | 210 +++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+), 1 deletion(-)
 create mode 100755 test/run_qemu_img_convert_tests.pl

diff --git a/test/Makefile b/test/Makefile
index 902d9e3..d88cbd2 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -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
diff --git a/test/run_qemu_img_convert_tests.pl b/test/run_qemu_img_convert_tests.pl
new file mode 100755
index 0000000..eb38bf6
--- /dev/null
+++ b/test/run_qemu_img_convert_tests.pl
@@ -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();
-- 
2.20.1





More information about the pve-devel mailing list