]> git.proxmox.com Git - pve-storage.git/blob - test/archive_info_test.pm
archive_info: use timelocal correctly
[pve-storage.git] / test / archive_info_test.pm
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, future millenium',
23 archive => "backup/vzdump-lxc-$vmid-3070_01_01-00_00_00.tgz",
24 expected => {
25 'filename' => "vzdump-lxc-$vmid-3070_01_01-00_00_00.tgz",
26 'logfilename' => "vzdump-lxc-$vmid-3070_01_01-00_00_00.log",
27 'type' => 'lxc',
28 'format' => 'tar',
29 'decompressor' => ['tar', '-z'],
30 'compression' => 'gz',
31 'vmid' => $vmid,
32 'ctime' => 60*60*24 * (365*1100 + 267),
33 'is_std_name' => 1,
34 },
35 },
36 {
37 description => 'Backup archive, lxc, tgz, very old',
38 archive => "backup/vzdump-lxc-$vmid-1970_01_01-02_00_30.tgz",
39 expected => {
40 'filename' => "vzdump-lxc-$vmid-1970_01_01-02_00_30.tgz",
41 'logfilename' => "vzdump-lxc-$vmid-1970_01_01-02_00_30.log",
42 'type' => 'lxc',
43 'format' => 'tar',
44 'decompressor' => ['tar', '-z'],
45 'compression' => 'gz',
46 'vmid' => $vmid,
47 'ctime' => 60*60*2 + 30,
48 'is_std_name' => 1,
49 },
50 },
51 {
52 description => 'Backup archive, lxc, tgz',
53 archive => "backup/vzdump-lxc-$vmid-2020_03_30-21_39_30.tgz",
54 expected => {
55 'filename' => "vzdump-lxc-$vmid-2020_03_30-21_39_30.tgz",
56 'logfilename' => "vzdump-lxc-$vmid-2020_03_30-21_39_30.log",
57 'type' => 'lxc',
58 'format' => 'tar',
59 'decompressor' => ['tar', '-z'],
60 'compression' => 'gz',
61 'vmid' => $vmid,
62 'ctime' => 1585604370,
63 'is_std_name' => 1,
64 },
65 },
66 {
67 description => 'Backup archive, openvz, tgz',
68 archive => "backup/vzdump-openvz-$vmid-2020_03_30-21_39_30.tgz",
69 expected => {
70 'filename' => "vzdump-openvz-$vmid-2020_03_30-21_39_30.tgz",
71 'logfilename' => "vzdump-openvz-$vmid-2020_03_30-21_39_30.log",
72 'type' => 'openvz',
73 'format' => 'tar',
74 'decompressor' => ['tar', '-z'],
75 'compression' => 'gz',
76 'vmid' => $vmid,
77 'ctime' => 1585604370,
78 'is_std_name' => 1,
79 },
80 },
81 {
82 description => 'Backup archive, custom dump directory, qemu, tgz',
83 archive => "/here/be/Back-ups/vzdump-qemu-$vmid-2020_03_30-21_39_30.tgz",
84 expected => {
85 'filename' => "vzdump-qemu-$vmid-2020_03_30-21_39_30.tgz",
86 'logfilename' => "vzdump-qemu-$vmid-2020_03_30-21_39_30.log",
87 'type' => 'qemu',
88 'format' => 'tar',
89 'decompressor' => ['tar', '-z'],
90 'compression' => 'gz',
91 'vmid' => $vmid,
92 'ctime' => 1585604370,
93 'is_std_name' => 1,
94 },
95 },
96 {
97 description => 'Backup archive, none, tgz',
98 archive => "backup/vzdump-qemu-$vmid-whatever-the-name_is_here.tgz",
99 expected => {
100 'filename' => "vzdump-qemu-$vmid-whatever-the-name_is_here.tgz",
101 'type' => 'qemu',
102 'format' => 'tar',
103 'decompressor' => ['tar', '-z'],
104 'compression' => 'gz',
105 'is_std_name' => 0,
106 },
107 },
108 ];
109
110 # add new compression fromats to test
111 my $decompressor = {
112 tar => {
113 gz => ['tar', '-z'],
114 lzo => ['tar', '--lzop'],
115 zst => ['tar', '--zstd'],
116 },
117 vma => {
118 gz => ['zcat'],
119 lzo => ['lzop', '-d', '-c'],
120 zst => ['zstd', '-q', '-d', '-c'],
121 },
122 };
123
124 my $bkp_suffix = {
125 qemu => [ 'vma', $decompressor->{vma}, ],
126 lxc => [ 'tar', $decompressor->{tar}, ],
127 openvz => [ 'tar', $decompressor->{tar}, ],
128 };
129
130 # create more test cases for backup files matches
131 for my $virt (sort keys %$bkp_suffix) {
132 my ($format, $decomp) = $bkp_suffix->{$virt}->@*;
133
134 for my $suffix (sort keys %$decomp) {
135 push @$tests, {
136 description => "Backup archive, $virt, $format.$suffix",
137 archive => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$format.$suffix",
138 expected => {
139 'filename' => "vzdump-$virt-$vmid-2020_03_30-21_12_40.$format.$suffix",
140 'logfilename' => "vzdump-$virt-$vmid-2020_03_30-21_12_40.log",
141 'type' => "$virt",
142 'format' => "$format",
143 'decompressor' => $decomp->{$suffix},
144 'compression' => "$suffix",
145 'vmid' => $vmid,
146 'ctime' => 1585602760,
147 'is_std_name' => 1,
148 },
149 };
150 }
151 }
152
153
154 # add compression formats to test failed matches
155 my $non_bkp_suffix = {
156 'openvz' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ],
157 'lxc' => [ 'zip', 'tgz.lzo', 'tar.bz2', 'zip.gz', '', ],
158 'qemu' => [ 'vma.xz', 'vms.gz', 'vmx.zst', '', ],
159 'none' => [ 'tar.gz', ],
160 };
161
162 # create tests for failed matches
163 for my $virt (sort keys %$non_bkp_suffix) {
164 my $suffix = $non_bkp_suffix->{$virt};
165 for my $s (@$suffix) {
166 my $archive = "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s";
167 push @$tests, {
168 description => "Failed match: Backup archive, $virt, $s",
169 archive => $archive,
170 expected => "ERROR: couldn't determine archive info from '$archive'\n",
171 };
172 }
173 }
174
175
176 plan tests => scalar @$tests;
177
178 for my $tt (@$tests) {
179
180 my $got = eval { PVE::Storage::archive_info($tt->{archive}) };
181 $got = $@ if $@;
182
183 is_deeply($got, $tt->{expected}, $tt->{description}) || diag(explain($got));
184 }
185
186 done_testing();
187
188 1;