[pve-devel] [PATCH storage v3 1/7] storage: test: split archive format/compressor

Alwin Antreich a.antreich at proxmox.com
Wed Apr 8 12:25:55 CEST 2020


detection into separate functions so they are reusable and easier
modifiable.

Signed-off-by: Alwin Antreich <a.antreich at proxmox.com>
---
 test/Makefile             |  5 ++-
 PVE/Storage.pm            | 79 ++++++++++++++++++++++++++++-----------
 test/run_parser_tests.pl  | 12 ++++++
 test/test_archive_info.pm | 54 ++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 22 deletions(-)
 create mode 100755 test/run_parser_tests.pl
 create mode 100644 test/test_archive_info.pm

diff --git a/test/Makefile b/test/Makefile
index 833a597..838449f 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,6 +1,6 @@
 all: test
 
-test: test_zfspoolplugin test_disklist test_bwlimit
+test: test_zfspoolplugin test_disklist test_bwlimit test_parsers
 
 test_zfspoolplugin: run_test_zfspoolplugin.pl
 	./run_test_zfspoolplugin.pl
@@ -10,3 +10,6 @@ test_disklist: run_disk_tests.pl
 
 test_bwlimit: run_bwlimit_tests.pl
 	./run_bwlimit_tests.pl
+
+test_parsers: run_parser_tests.pl
+	./run_parser_tests.pl
diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 60b8310..0bbd168 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -1284,6 +1284,53 @@ sub foreach_volid {
     }
 }
 
+sub decompressor_info {
+    my ($format, $comp) = @_;
+
+    if ($format eq 'tgz' && !defined($comp)) {
+	($format, $comp) = ('tar', 'gz');
+    }
+
+    my $decompressor = {
+	tar => {
+	    gz => ['tar', '-z'],
+	    lzo => ['tar', '--lzop'],
+	},
+	vma => {
+	    gz => ['zcat'],
+	    lzo => ['lzop', '-d', '-c'],
+	},
+    };
+
+    die "ERROR: archive format not defined\n"
+	if !defined($decompressor->{$format});
+
+    my $decomp = $decompressor->{$format}->{$comp} if $comp;
+
+    my $info = {
+	format => $format,
+	compression => $comp,
+	decompressor => $decomp,
+    };
+
+    return $info;
+}
+
+sub archive_info {
+    my ($archive) = shift;
+    my $info;
+
+    my $volid = basename($archive);
+    if ($volid =~ /vzdump-(lxc|openvz|qemu)-\d+-(?:\d{4})_(?:\d{2})_(?:\d{2})-(?:\d{2})_(?:\d{2})_(?:\d{2})\.(tgz$|tar|vma)(?:\.(gz|lzo))?$/) {
+	$info = decompressor_info($2, $3);
+	$info->{type} = $1;
+    } else {
+	die "ERROR: couldn't determine format and compression type\n";
+    }
+
+    return $info;
+}
+
 sub extract_vzdump_config_tar {
     my ($archive, $conf_re) = @_;
 
@@ -1329,16 +1376,12 @@ sub extract_vzdump_config_vma {
     };
 
 
+    my $info = archive_info($archive);
+    $comp //= $info->{compression};
+    my $decompressor = $info->{decompressor};
+
     if ($comp) {
-	my $uncomp;
-	if ($comp eq 'gz') {
-	    $uncomp = ["zcat", $archive];
-	} elsif ($comp eq 'lzo') {
-	    $uncomp = ["lzop", "-d", "-c", $archive];
-	} else {
-	    die "unknown compression method '$comp'\n";
-	}
-	$cmd = [$uncomp, ["vma", "config", "-"]];
+	$cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ];
 
 	# in some cases, lzop/zcat exits with 1 when its stdout pipe is
 	# closed early by vma, detect this and ignore the exit code later
@@ -1388,20 +1431,14 @@ sub extract_vzdump_config {
     }
 
     my $archive = abs_filesystem_path($cfg, $volid);
+    my $info = archive_info($archive);
+    my $format = $info->{format};
+    my $comp = $info->{compression};
+    my $type = $info->{type};
 
-    if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
+    if ($type eq 'lxc' || $type eq 'openvz') {
 	return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
-    } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
-	my $format;
-	my $comp;
-	if ($7 eq 'tgz') {
-	    $format = 'tar';
-	    $comp = 'gz';
-	} else {
-	    $format = $9;
-	    $comp = $11 if defined($11);
-	}
-
+    } elsif ($type eq 'qemu') {
 	if ($format eq 'tar') {
 	    return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!);
 	} else {
diff --git a/test/run_parser_tests.pl b/test/run_parser_tests.pl
new file mode 100755
index 0000000..042112c
--- /dev/null
+++ b/test/run_parser_tests.pl
@@ -0,0 +1,12 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use TAP::Harness;
+
+my $harness = TAP::Harness->new( { verbosity => -1 });
+my $res = $harness->runtests("test_archive_info.pm");
+
+exit -1 if !$res || $res->{failed} || $res->{parse_errors};
+
diff --git a/test/test_archive_info.pm b/test/test_archive_info.pm
new file mode 100644
index 0000000..464cc89
--- /dev/null
+++ b/test/test_archive_info.pm
@@ -0,0 +1,54 @@
+package PVE::Storage::TestArchiveInfo;
+
+use strict;
+use warnings;
+
+use lib qw(..);
+
+use PVE::Storage;
+use Test::More;
+
+my @tests = (
+    # backup archives
+    [ 'backup/vzdump-qemu-16110-2020_03_30-21_12_40.vma',     { 'type' => 'qemu', 'format' => 'vma', 'decompressor' => undef, 'compression' => undef },                'Backup archive, vma' ],
+    [ 'backup/vzdump-qemu-16110-2020_03_30-21_12_40.vma.gz',  { 'type' => 'qemu', 'format' => 'vma', 'decompressor' => ['zcat'], 'compression' => 'gz' },              'Backup archive, vma, gz' ],
+    [ 'backup/vzdump-qemu-16110-2020_03_30-21_12_40.vma.lzo', { 'type' => 'qemu', 'format' => 'vma', 'decompressor' => ['lzop', '-d', '-c'], 'compression' => 'lzo' }, 'Backup archive, vma, lzo' ],
+
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30.tar',     { 'type' => 'lxc', 'format' => 'tar', 'decompressor' => undef, 'compression' => undef },             'Backup archive, lxc' ],
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30.tar.gz',  { 'type' => 'lxc', 'format' => 'tar', 'decompressor' => ['tar', '-z'], 'compression' => 'gz' },      'Backup archive, lxc, gz' ],
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30.tgz',     { 'type' => 'lxc', 'format' => 'tar', 'decompressor' => ['tar', '-z'], 'compression' => 'gz' },      'Backup archive, lxc, tgz' ],
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30.tar.lzo', { 'type' => 'lxc', 'format' => 'tar', 'decompressor' => ['tar', '--lzop'], 'compression' => 'lzo' }, 'Backup archive, lxc, lzo' ],
+
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tar',     { 'type' => 'openvz', 'format' => 'tar', 'decompressor' => undef, 'compression' => undef },             'Backup archive, openvz' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tar.gz',  { 'type' => 'openvz', 'format' => 'tar', 'decompressor' => ['tar', '-z'], 'compression' => 'gz' },      'Backup archive, openvz, gz' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tgz',     { 'type' => 'openvz', 'format' => 'tar', 'decompressor' => ['tar', '-z'], 'compression' => 'gz' },      'Backup archive, openvz, tgz' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tar.lzo', { 'type' => 'openvz', 'format' => 'tar', 'decompressor' => ['tar', '--lzop'], 'compression' => 'lzo' }, 'Backup archive, openvz, lzo' ],
+
+    # failed matches
+    [ 'backup/vzdump-none-16112-2020_03_30-21_39_30.tar',       "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, no virtualization type' ],
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30',            "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, lxc, no ending' ],
+    [ 'backup/vzdump-lxc-16112-2020_03_30-21_39_30.zip',        "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, lxc, zip' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tgz.lzo', "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, openvz, tgz.lzo' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.tar.bz2', "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, openvz, bz2' ],
+    [ 'backup/vzdump-openvz-16112-2020_03_30-21_39_30.zip.gz',  "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, openvz, zip.gz' ],
+    [ 'backup/vzdump-qemu-16110-2020_03_30-21_12_40.vma.xz',    "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, vma, xz' ],
+    [ 'backup/vzdump-qemu-16110-2020_03_30-21_12_40.vms.gz',    "ERROR: couldn't determine format and compression type\n", 'Failed match: Backup archive, vms, gz' ],
+);
+
+
+
+
+plan tests => scalar @tests;
+
+foreach my $t (@tests) {
+    my ($archive, $expected, $description) = @$t;
+    my $got;
+    eval { $got = PVE::Storage::archive_info($archive) };
+    $got = $@ if $@;
+
+    is_deeply($got, $expected, $description) || diag(explain($got));
+}
+
+done_testing();
+
+1;
-- 
2.20.1





More information about the pve-devel mailing list