[pve-devel] [PATCH v2 qemu-server 1/4] test: add tests for restoring config

Fabian Ebner f.ebner at proxmox.com
Thu Mar 18 10:44:49 CET 2021


Suggested-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---

New in v2

 PVE/QemuServer.pm                      | 10 ++--
 test/Makefile                          |  5 +-
 test/restore-config-expected/139.conf  | 16 ++++++
 test/restore-config-expected/142.conf  | 15 ++++++
 test/restore-config-expected/1422.conf | 14 ++++++
 test/restore-config-expected/179.conf  | 17 +++++++
 test/restore-config-input/139.conf     | 18 +++++++
 test/restore-config-input/142.conf     | 16 ++++++
 test/restore-config-input/1422.conf    | 18 +++++++
 test/restore-config-input/179.conf     | 21 ++++++++
 test/run_qemu_restore_config_tests.pl  | 67 ++++++++++++++++++++++++++
 11 files changed, 211 insertions(+), 6 deletions(-)
 create mode 100644 test/restore-config-expected/139.conf
 create mode 100644 test/restore-config-expected/142.conf
 create mode 100644 test/restore-config-expected/1422.conf
 create mode 100644 test/restore-config-expected/179.conf
 create mode 100644 test/restore-config-input/139.conf
 create mode 100644 test/restore-config-input/142.conf
 create mode 100644 test/restore-config-input/1422.conf
 create mode 100644 test/restore-config-input/179.conf
 create mode 100755 test/run_qemu_restore_config_tests.pl

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 1c0b5c2..a6e6ae0 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5899,7 +5899,7 @@ my $restore_allocate_devices = sub {
     return $map;
 };
 
-my $restore_update_config_line = sub {
+sub restore_update_config_line {
     my ($cookie, $vmid, $map, $line, $unique) = @_;
 
     return '' if $line =~ m/^\#qmdump\#/;
@@ -5966,7 +5966,7 @@ my $restore_update_config_line = sub {
     }
 
     return $res;
-};
+}
 
 my $restore_deactivate_volumes = sub {
     my ($storecfg, $devinfo) = @_;
@@ -6283,7 +6283,7 @@ sub restore_proxmox_backup_archive {
 
 	my $cookie = { netcount => 0 };
 	while (defined(my $line = <$fh>)) {
-	    $new_conf_raw .= $restore_update_config_line->(
+	    $new_conf_raw .= restore_update_config_line(
 		$cookie,
 		$vmid,
 		$map,
@@ -6456,7 +6456,7 @@ sub restore_vma_archive {
 
 	my $cookie = { netcount => 0 };
 	while (defined(my $line = <$fh>)) {
-	    $new_conf_raw .= $restore_update_config_line->(
+	    $new_conf_raw .= restore_update_config_line(
 		$cookie,
 		$vmid,
 		$map,
@@ -6611,7 +6611,7 @@ sub restore_tar_archive {
 
 	my $cookie = { netcount => 0 };
 	while (defined (my $line = <$srcfd>)) {
-	    $new_conf_raw .= $restore_update_config_line->(
+	    $new_conf_raw .= restore_update_config_line(
 		$cookie,
 		$vmid,
 		$map,
diff --git a/test/Makefile b/test/Makefile
index 5c26382..7a8487c 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -3,7 +3,7 @@ MIGRATION_TEST_TARGETS := $(addprefix test_migration_,$(MIGRATION_TEST_NAMES))
 
 all: test
 
-test: test_snapshot test_ovf test_cfg_to_cmd test_pci_addr_conflicts test_qemu_img_convert test_migration
+test: test_snapshot test_ovf test_cfg_to_cmd test_pci_addr_conflicts test_qemu_img_convert test_migration test_restore_config
 
 test_snapshot: run_snapshot_tests.pl
 	./run_snapshot_tests.pl
@@ -26,6 +26,9 @@ test_migration: run_qemu_migrate_tests.pl MigrationTest/*.pm $(MIGRATION_TEST_TA
 $(MIGRATION_TEST_TARGETS):
 	./run_qemu_migrate_tests.pl $(@:test_migration_%=%)
 
+test_restore_config: run_qemu_restore_config_tests.pl
+	./run_qemu_restore_config_tests.pl
+
 .PHONY: clean
 clean:
 	rm -rf MigrationTest/run
diff --git a/test/restore-config-expected/139.conf b/test/restore-config-expected/139.conf
new file mode 100644
index 0000000..94425f7
--- /dev/null
+++ b/test/restore-config-expected/139.conf
@@ -0,0 +1,16 @@
+# regular VM with an EFI disk
+bios: ovmf
+boot: order=scsi0;ide2;net0
+cores: 1
+efidisk0: target:139/vm-139-disk-0.qcow2,size=128K
+ide2: local:iso/debian-10.6.0-amd64-netinst.iso,media=cdrom
+memory: 2048
+name: eficloneclone
+net0: virtio=7A:6C:A5:8B:11:93,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: target:139/vm-139-disk-1.raw,size=4G
+scsihw: virtio-scsi-pci
+smbios1: uuid=21a7e7bc-3cd2-4232-a009-a41f4ee992ae
+sockets: 1
+vmgenid: 0
diff --git a/test/restore-config-expected/142.conf b/test/restore-config-expected/142.conf
new file mode 100644
index 0000000..ac2d2ad
--- /dev/null
+++ b/test/restore-config-expected/142.conf
@@ -0,0 +1,15 @@
+# plain VM
+bootdisk: scsi0
+cores: 1
+ide2: none,media=cdrom
+memory: 512
+name: apache
+net0: virtio=92:38:11:FD:ED:87,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: target:142/vm-142-disk-0.qcow2,size=4G
+scsihw: virtio-scsi-pci
+smbios1: uuid=ddf91b3f-a597-42be-9a7e-fb6421dcd5cd
+sockets: 1
+vmgenid: 0
+tags: foo bar
diff --git a/test/restore-config-expected/1422.conf b/test/restore-config-expected/1422.conf
new file mode 100644
index 0000000..2d77a44
--- /dev/null
+++ b/test/restore-config-expected/1422.conf
@@ -0,0 +1,14 @@
+# some properties should be filtered
+bootdisk: scsi0
+cores: 1
+ide2: none,media=cdrom
+memory: 512
+name: apache
+net0: virtio=92:38:11:FD:ED:87,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: target:1422/vm-1422-disk-0.qcow2,size=4G
+scsihw: virtio-scsi-pci
+smbios1: uuid=ddf91b3f-a597-42be-9a7e-fb6421dcd5cd
+sockets: 1
+vmgenid: 0
diff --git a/test/restore-config-expected/179.conf b/test/restore-config-expected/179.conf
new file mode 100644
index 0000000..4444efb
--- /dev/null
+++ b/test/restore-config-expected/179.conf
@@ -0,0 +1,17 @@
+# many disks
+boot: order=scsi0;ide2;net0
+cores: 1
+ide2: none,media=cdrom
+memory: 2048
+net0: virtio=26:15:5B:73:3F:7C,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: target:179/vm-179-disk-0.qcow2,cache=none,discard=on,size=32G,ssd=1
+scsi1: target:179/vm-179-disk-1.qcow2,cache=writethrough,size=32G
+scsi2: target:179/vm-179-disk-2.qcow2,mbps_rd=7,mbps_wr=7,replicate=0,size=32G
+scsi3: target:179/vm-179-disk-3.vmdk,size=32G
+#scsi4: myfs:179/vm-179-disk-1.qcow2,backup=0,size=32G
+scsihw: virtio-scsi-pci
+smbios1: uuid=1819ead7-a55d-4544-8d38-29ca94869a9c
+sockets: 1
+vmgenid: 0
diff --git a/test/restore-config-input/139.conf b/test/restore-config-input/139.conf
new file mode 100644
index 0000000..5acb4d4
--- /dev/null
+++ b/test/restore-config-input/139.conf
@@ -0,0 +1,18 @@
+# regular VM with an EFI disk
+bios: ovmf
+boot: order=scsi0;ide2;net0
+cores: 1
+efidisk0: mydir:139/vm-139-disk-0.qcow2,size=128K
+ide2: local:iso/debian-10.6.0-amd64-netinst.iso,media=cdrom
+memory: 2048
+name: eficloneclone
+net0: virtio=7A:6C:A5:8B:11:93,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: rbdkvm:vm-139-disk-1,size=4G
+scsihw: virtio-scsi-pci
+smbios1: uuid=21a7e7bc-3cd2-4232-a009-a41f4ee992ae
+sockets: 1
+vmgenid: 0
+#qmdump#map:efidisk0:drive-efidisk0:mydir:qcow2:
+#qmdump#map:scsi0:drive-scsi0:rbdkvm::
diff --git a/test/restore-config-input/142.conf b/test/restore-config-input/142.conf
new file mode 100644
index 0000000..f3633aa
--- /dev/null
+++ b/test/restore-config-input/142.conf
@@ -0,0 +1,16 @@
+# plain VM
+bootdisk: scsi0
+cores: 1
+ide2: none,media=cdrom
+memory: 512
+name: apache
+net0: virtio=92:38:11:FD:ED:87,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: mydir:142/vm-142-disk-0.qcow2,size=4G
+scsihw: virtio-scsi-pci
+smbios1: uuid=ddf91b3f-a597-42be-9a7e-fb6421dcd5cd
+sockets: 1
+vmgenid: 0
+tags: foo bar
+#qmdump#map:scsi0:drive-scsi0:mydir:qcow2:
diff --git a/test/restore-config-input/1422.conf b/test/restore-config-input/1422.conf
new file mode 100644
index 0000000..d315502
--- /dev/null
+++ b/test/restore-config-input/1422.conf
@@ -0,0 +1,18 @@
+# some properties should be filtered
+bootdisk: scsi0
+cores: 1
+ide2: none,media=cdrom
+memory: 512
+name: apache
+net0: virtio=92:38:11:FD:ED:87,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: mydir:1422/vm-1422-disk-0.qcow2,size=4G
+unused7: mydir:1422/vm-1422-disk-8.qcow2
+parent: snap
+lock: backup
+scsihw: virtio-scsi-pci
+smbios1: uuid=ddf91b3f-a597-42be-9a7e-fb6421dcd5cd
+sockets: 1
+vmgenid: 0
+#qmdump#map:scsi0:drive-scsi0:mydir:qcow2:
diff --git a/test/restore-config-input/179.conf b/test/restore-config-input/179.conf
new file mode 100644
index 0000000..e1ee01a
--- /dev/null
+++ b/test/restore-config-input/179.conf
@@ -0,0 +1,21 @@
+# many disks
+boot: order=scsi0;ide2;net0
+cores: 1
+ide2: none,media=cdrom
+memory: 2048
+net0: virtio=26:15:5B:73:3F:7C,bridge=vmbr0,firewall=1
+numa: 0
+ostype: l26
+scsi0: myfs:179/vm-179-disk-4.qcow2,cache=none,discard=on,size=32G,ssd=1
+scsi1: myfs:179/vm-179-disk-0.qcow2,cache=writethrough,size=32G
+scsi2: myfs:179/vm-179-disk-2.qcow2,mbps_rd=7,mbps_wr=7,replicate=0,size=32G
+scsi3: myfs:179/vm-179-disk-3.vmdk,size=32G
+scsi4: myfs:179/vm-179-disk-1.qcow2,backup=0,size=32G
+scsihw: virtio-scsi-pci
+smbios1: uuid=1819ead7-a55d-4544-8d38-29ca94869a9c
+sockets: 1
+vmgenid: 0
+#qmdump#map:scsi0:drive-scsi0:myfs:qcow2:
+#qmdump#map:scsi1:drive-scsi1:myfs:qcow2:
+#qmdump#map:scsi2:drive-scsi2:myfs:qcow2:
+#qmdump#map:scsi3:drive-scsi3:myfs:vmdk:
diff --git a/test/run_qemu_restore_config_tests.pl b/test/run_qemu_restore_config_tests.pl
new file mode 100755
index 0000000..d829216
--- /dev/null
+++ b/test/run_qemu_restore_config_tests.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use Test::More;
+
+use File::Basename;
+
+use PVE::QemuServer;
+use PVE::Tools qw(dir_glob_foreach file_get_contents);
+
+my $INPUT_DIR = './restore-config-input';
+my $EXPECTED_DIR = './restore-config-expected';
+
+# NOTE update when you add/remove tests
+plan tests => 4;
+
+dir_glob_foreach('./restore-config-input', '[0-9]+.conf', sub {
+    my ($file) = @_;
+
+    my $vmid = basename($file, ('.conf'));
+
+    my $fh = IO::File->new("${INPUT_DIR}/${file}", "r") or
+	die "unable to read '$file' - $!\n";
+
+    my $map = {};
+    my $disknum = 0;
+
+    # NOTE For now, the map is hardcoded to a file-based 'target' storage.
+    # In the future, the test could be extended to include parse_backup_hints
+    # and restore_allocate_devices. Even better if the config-related logic from
+    # the restore_XYZ_archive functions could become a separate function.
+    while (defined(my $line = <$fh>)) {
+	if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
+	    my ($drive, undef, $storeid, $fmt) = ($1, $2, $3, $4);
+
+	    $fmt ||= 'raw';
+
+	    $map->{$drive} = "target:${vmid}/vm-${vmid}-disk-${disknum}.${fmt}";
+	    $disknum++;
+	}
+    }
+
+    $fh->seek(0, 0) or die "seek failed - $!\n";
+
+    my $got = '';
+    my $cookie = { netcount => 0 };
+
+    while (defined(my $line = <$fh>)) {
+	$got .= PVE::QemuServer::restore_update_config_line(
+	    $cookie,
+	    $vmid,
+	    $map,
+	    $line,
+	    0,
+	);
+    }
+
+    my $expected = file_get_contents("${EXPECTED_DIR}/${file}");
+
+    is_deeply($got, $expected, $file);
+});
+
+done_testing();
-- 
2.20.1






More information about the pve-devel mailing list