]>
Commit | Line | Data |
---|---|---|
cd554b79 AA |
1 | package PVE::Storage::TestArchiveInfo; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use lib qw(..); | |
7 | ||
8 | use PVE::Storage; | |
9 | use Test::More; | |
10 | ||
11 | my $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 | |
19 | my $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 | }, | |
4b26f814 AA |
41 | { |
42 | description => 'Backup archive, none, tgz', | |
43 | archive => "backup/whatever-the-name_is_here.tgz", | |
44 | expected => { | |
45 | 'type' => 'unknown', | |
46 | 'format' => 'tar', | |
47 | 'decompressor' => ['tar', '-z'], | |
48 | 'compression' => 'gz', | |
49 | }, | |
50 | }, | |
cd554b79 AA |
51 | ]; |
52 | ||
53 | # add new compression fromats to test | |
54 | my $decompressor = { | |
55 | tar => { | |
56 | gz => ['tar', '-z'], | |
57 | lzo => ['tar', '--lzop'], | |
014d36db | 58 | zst => ['tar', '--zstd'], |
cd554b79 AA |
59 | }, |
60 | vma => { | |
61 | gz => ['zcat'], | |
62 | lzo => ['lzop', '-d', '-c'], | |
014d36db | 63 | zst => ['zstd', '-q', '-d', '-c'], |
cd554b79 AA |
64 | }, |
65 | }; | |
66 | ||
67 | my $bkp_suffix = { | |
68 | qemu => [ 'vma', $decompressor->{vma}, ], | |
69 | lxc => [ 'tar', $decompressor->{tar}, ], | |
70 | openvz => [ 'tar', $decompressor->{tar}, ], | |
71 | }; | |
72 | ||
73 | # create more test cases for backup files matches | |
5df46bf2 TL |
74 | for my $virt (sort keys %$bkp_suffix) { |
75 | my ($format, $decomp) = $bkp_suffix->{$virt}->@*; | |
cd554b79 | 76 | |
5df46bf2 | 77 | for my $suffix (sort keys %$decomp) { |
cd554b79 AA |
78 | my @arr = ( |
79 | { | |
80 | description => "Backup archive, $virt, $format.$suffix", | |
81 | archive => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$format.$suffix", | |
82 | expected => { | |
83 | 'type' => "$virt", | |
84 | 'format' => "$format", | |
85 | 'decompressor' => $decomp->{$suffix}, | |
86 | 'compression' => "$suffix", | |
87 | }, | |
88 | }, | |
89 | ); | |
90 | ||
91 | push @$tests, @arr; | |
92 | } | |
93 | } | |
94 | ||
95 | ||
96 | # add compression formats to test failed matches | |
97 | my $non_bkp_suffix = { | |
98 | 'openvz' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ], | |
99 | 'lxc' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ], | |
014d36db | 100 | 'qemu' => [ 'vma.xz', 'vms.gz', 'vmx.zst', '', ], |
cd554b79 AA |
101 | }; |
102 | ||
103 | # create tests for failed matches | |
5df46bf2 | 104 | for my $virt (sort keys %$non_bkp_suffix) { |
cd554b79 AA |
105 | my $suffix = $non_bkp_suffix->{$virt}; |
106 | foreach my $s (@$suffix) { | |
107 | my @arr = ( | |
108 | { | |
109 | description => "Failed match: Backup archive, $virt, $s", | |
110 | archive => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s", | |
111 | expected => "ERROR: couldn't determine format and compression type\n", | |
112 | }, | |
113 | ); | |
114 | ||
115 | push @$tests, @arr; | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | plan tests => scalar @$tests; | |
121 | ||
3dde6a9c TL |
122 | for my $tt (@$tests) { |
123 | ||
124 | my $got = eval { PVE::Storage::archive_info($tt->{archive}) }; | |
cd554b79 AA |
125 | $got = $@ if $@; |
126 | ||
3dde6a9c | 127 | is_deeply($got, $tt->{expected}, $tt->{description}) || diag(explain($got)); |
cd554b79 AA |
128 | } |
129 | ||
130 | done_testing(); | |
131 | ||
132 | 1; |