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