]> git.proxmox.com Git - pve-storage.git/blame - test/archive_info_test.pm
backup: compact regex for backup file filter
[pve-storage.git] / test / archive_info_test.pm
CommitLineData
cd554b79
AA
1package PVE::Storage::TestArchiveInfo;
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use PVE::Storage;
9use Test::More;
10
11my $vmid = 16110;
12
13# an array of test cases, each test is comprised of the following keys:
14# description => to identify a single test
15# archive => the input filename for archive_info
16# expected => the hash that archive_info returns
17#
18# most of them are created further below
19my $tests = [
20 # backup archives
21 {
22 description => 'Backup archive, lxc, tgz',
23 archive => "backup/vzdump-lxc-$vmid-2020_03_30-21_39_30.tgz",
24 expected => {
25 'type' => 'lxc',
26 'format' => 'tar',
27 'decompressor' => ['tar', '-z'],
28 'compression' => 'gz',
29 },
30 },
31 {
32 description => 'Backup archive, openvz, tgz',
33 archive => "backup/vzdump-openvz-$vmid-2020_03_30-21_39_30.tgz",
34 expected => {
35 'type' => 'openvz',
36 'format' => 'tar',
37 'decompressor' => ['tar', '-z'],
38 'compression' => 'gz',
39 },
40 },
41];
42
43# add new compression fromats to test
44my $decompressor = {
45 tar => {
46 gz => ['tar', '-z'],
47 lzo => ['tar', '--lzop'],
48 },
49 vma => {
50 gz => ['zcat'],
51 lzo => ['lzop', '-d', '-c'],
52 },
53};
54
55my $bkp_suffix = {
56 qemu => [ 'vma', $decompressor->{vma}, ],
57 lxc => [ 'tar', $decompressor->{tar}, ],
58 openvz => [ 'tar', $decompressor->{tar}, ],
59};
60
61# create more test cases for backup files matches
62foreach my $virt (keys %$bkp_suffix) {
63 my ($format, $decomp) = @{ $bkp_suffix->{$virt} };
64
65 foreach my $suffix (keys %$decomp) {
66 my @arr = (
67 {
68 description => "Backup archive, $virt, $format.$suffix",
69 archive => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$format.$suffix",
70 expected => {
71 'type' => "$virt",
72 'format' => "$format",
73 'decompressor' => $decomp->{$suffix},
74 'compression' => "$suffix",
75 },
76 },
77 );
78
79 push @$tests, @arr;
80 }
81}
82
83
84# add compression formats to test failed matches
85my $non_bkp_suffix = {
86 'openvz' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ],
87 'lxc' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ],
88 'qemu' => [ 'vma.xz', 'vms.gz', '', ],
89 'none' => [ 'tar.gz', ],
90};
91
92# create tests for failed matches
93foreach my $virt (keys %$non_bkp_suffix) {
94 my $suffix = $non_bkp_suffix->{$virt};
95 foreach my $s (@$suffix) {
96 my @arr = (
97 {
98 description => "Failed match: Backup archive, $virt, $s",
99 archive => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s",
100 expected => "ERROR: couldn't determine format and compression type\n",
101 },
102 );
103
104 push @$tests, @arr;
105 }
106}
107
108
109plan tests => scalar @$tests;
110
111# run through tests array
112foreach my $tt (@$tests) {
113 my $description = $tt->{description};
114 my $archive = $tt->{archive};
115 my $expected = $tt->{expected};
116 my $got;
117 eval { $got = PVE::Storage::archive_info($archive) };
118 $got = $@ if $@;
119
120 is_deeply($got, $expected, $description) || diag(explain($got));
121}
122
123done_testing();
124
1251;