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