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