]> git.proxmox.com Git - pve-storage.git/blob - test/parse_volname_test.pm
bump version to 7.0-4
[pve-storage.git] / test / parse_volname_test.pm
1 package PVE::Storage::TestParseVolname;
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 = 1234;
12
13 # an array of test cases, each test is comprised of the following keys:
14 # description => to identify a single test
15 # volname => the input for parse_volname
16 # expected => the array that parse_volname returns
17 my $tests = [
18 #
19 # VM images
20 #
21 {
22 description => 'VM disk image, linked, qcow2, vm- as base-',
23 volname => "$vmid/vm-$vmid-disk-0.qcow2/$vmid/vm-$vmid-disk-0.qcow2",
24 expected => [ 'images', "vm-$vmid-disk-0.qcow2", "$vmid", "vm-$vmid-disk-0.qcow2", "$vmid", undef, 'qcow2', ],
25 },
26 #
27 # iso
28 #
29 {
30 description => 'ISO image, iso',
31 volname => 'iso/some-installation-disk.iso',
32 expected => ['iso', 'some-installation-disk.iso'],
33 },
34 {
35 description => 'ISO image, img',
36 volname => 'iso/some-other-installation-disk.img',
37 expected => ['iso', 'some-other-installation-disk.img'],
38 },
39 #
40 # container templates
41 #
42 {
43 description => 'Container template tar.gz',
44 volname => 'vztmpl/debian-10.0-standard_10.0-1_amd64.tar.gz',
45 expected => ['vztmpl', 'debian-10.0-standard_10.0-1_amd64.tar.gz'],
46 },
47 {
48 description => 'Container template tar.xz',
49 volname => 'vztmpl/debian-10.0-standard_10.0-1_amd64.tar.xz',
50 expected => ['vztmpl', 'debian-10.0-standard_10.0-1_amd64.tar.xz'],
51 },
52 #
53 # container rootdir
54 #
55 {
56 description => 'Container rootdir, sub directory',
57 volname => "rootdir/$vmid",
58 expected => ['rootdir', "$vmid", "$vmid"],
59 },
60 {
61 description => 'Container rootdir, subvol',
62 volname => "$vmid/subvol-$vmid-disk-0.subvol",
63 expected => [ 'images', "subvol-$vmid-disk-0.subvol", "$vmid", undef, undef, undef, 'subvol' ],
64 },
65 {
66 description => 'Backup archive, no virtualization type',
67 volname => "backup/vzdump-none-$vmid-2020_03_30-21_39_30.tar",
68 expected => ['backup', "vzdump-none-$vmid-2020_03_30-21_39_30.tar"],
69 },
70 #
71 # Snippets
72 #
73 {
74 description => 'Snippets, yaml',
75 volname => 'snippets/userconfig.yaml',
76 expected => ['snippets', 'userconfig.yaml'],
77 },
78 {
79 description => 'Snippets, perl',
80 volname => 'snippets/hookscript.pl',
81 expected => ['snippets', 'hookscript.pl'],
82 },
83 #
84 # failed matches
85 #
86 {
87 description => "Failed match: VM disk image, base, raw",
88 volname => "ssss/base-$vmid-disk-0.raw",
89 expected => "unable to parse directory volume name 'ssss/base-$vmid-disk-0.raw'\n",
90 },
91 {
92 description => 'Failed match: ISO image, dvd',
93 volname => 'iso/yet-again-a-installation-disk.dvd',
94 expected => "unable to parse directory volume name 'iso/yet-again-a-installation-disk.dvd'\n",
95 },
96 {
97 description => 'Failed match: Container template, zip.gz',
98 volname => 'vztmpl/debian-10.0-standard_10.0-1_amd64.zip.gz',
99 expected => "unable to parse directory volume name 'vztmpl/debian-10.0-standard_10.0-1_amd64.zip.gz'\n",
100 },
101 {
102 description => 'Failed match: Container template, tar.bz2',
103 volname => 'vztmpl/debian-10.0-standard_10.0-1_amd64.tar.bz2',
104 expected => "unable to parse directory volume name 'vztmpl/debian-10.0-standard_10.0-1_amd64.tar.bz2'\n",
105 },
106 {
107 description => 'Failed match: Container rootdir, subvol',
108 volname => "rootdir/subvol-$vmid-disk-0",
109 expected => "unable to parse directory volume name 'rootdir/subvol-$vmid-disk-0'\n",
110 },
111 {
112 description => 'Failed match: VM disk image, linked, vhdx',
113 volname => "$vmid/base-$vmid-disk-0.vhdx/$vmid/vm-$vmid-disk-0.vhdx",
114 expected => "unable to parse volume filename 'base-$vmid-disk-0.vhdx'\n",
115 },
116 {
117 description => 'Failed match: VM disk image, linked, qcow2, first vmid',
118 volname => "ssss/base-$vmid-disk-0.qcow2/$vmid/vm-$vmid-disk-0.qcow2",
119 expected => "unable to parse directory volume name 'ssss/base-$vmid-disk-0.qcow2/$vmid/vm-$vmid-disk-0.qcow2'\n",
120 },
121 {
122 description => 'Failed match: VM disk image, linked, qcow2, second vmid',
123 volname => "$vmid/base-$vmid-disk-0.qcow2/ssss/vm-$vmid-disk-0.qcow2",
124 expected => "unable to parse volume filename 'base-$vmid-disk-0.qcow2/ssss/vm-$vmid-disk-0.qcow2'\n",
125 },
126 ];
127
128 # create more test cases for VM disk images matches
129 my $disk_suffix = [ 'raw', 'qcow2', 'vmdk' ];
130 foreach my $s (@$disk_suffix) {
131 my @arr = (
132 {
133 description => "VM disk image, $s",
134 volname => "$vmid/vm-$vmid-disk-1.$s",
135 expected => [
136 'images',
137 "vm-$vmid-disk-1.$s",
138 "$vmid",
139 undef,
140 undef,
141 undef,
142 "$s",
143 ],
144 },
145 {
146 description => "VM disk image, linked, $s",
147 volname => "$vmid/base-$vmid-disk-0.$s/$vmid/vm-$vmid-disk-0.$s",
148 expected => [
149 'images',
150 "vm-$vmid-disk-0.$s",
151 "$vmid",
152 "base-$vmid-disk-0.$s",
153 "$vmid",
154 undef,
155 "$s",
156 ],
157 },
158 {
159 description => "VM disk image, base, $s",
160 volname => "$vmid/base-$vmid-disk-0.$s",
161 expected => [
162 'images',
163 "base-$vmid-disk-0.$s",
164 "$vmid",
165 undef,
166 undef,
167 'base-',
168 "$s"
169 ],
170 },
171 );
172
173 push @$tests, @arr;
174 }
175
176
177 # create more test cases for backup files matches
178 my $bkp_suffix = {
179 qemu => [ 'vma', 'vma.gz', 'vma.lzo', 'vma.zst' ],
180 lxc => [ 'tar', 'tgz', 'tar.gz', 'tar.lzo', 'tar.zst' ],
181 openvz => [ 'tar', 'tgz', 'tar.gz', 'tar.lzo', 'tar.zst' ],
182 };
183
184 foreach my $virt (keys %$bkp_suffix) {
185 my $suffix = $bkp_suffix->{$virt};
186 foreach my $s (@$suffix) {
187 my @arr = (
188 {
189 description => "Backup archive, $virt, $s",
190 volname => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s",
191 expected => [
192 'backup',
193 "vzdump-$virt-$vmid-2020_03_30-21_12_40.$s",
194 "$vmid"
195 ],
196 },
197 );
198
199 push @$tests, @arr;
200 }
201 }
202
203
204 # create more test cases for failed backup files matches
205 my $non_bkp_suffix = {
206 qemu => [ 'vms.gz', 'vma.xz' ],
207 lxc => [ 'tar.bz2', 'zip.gz', 'tgz.lzo' ],
208 };
209 foreach my $virt (keys %$non_bkp_suffix) {
210 my $suffix = $non_bkp_suffix->{$virt};
211 foreach my $s (@$suffix) {
212 my @arr = (
213 {
214 description => "Failed match: Backup archive, $virt, $s",
215 volname => "backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s",
216 expected => "unable to parse directory volume name 'backup/vzdump-$virt-$vmid-2020_03_30-21_12_40.$s'\n",
217 },
218 );
219
220 push @$tests, @arr;
221 }
222 }
223
224
225 #
226 # run through test case array
227 #
228 plan tests => scalar @$tests + 1;
229
230 my $seen_vtype;
231 my $vtype_subdirs = { map { $_ => 1 } keys %{ PVE::Storage::Plugin::get_vtype_subdirs() } };
232
233 foreach my $t (@$tests) {
234 my $description = $t->{description};
235 my $volname = $t->{volname};
236 my $expected = $t->{expected};
237
238 my $got;
239 eval { $got = [ PVE::Storage::Plugin->parse_volname($volname) ] };
240 $got = $@ if $@;
241
242 is_deeply($got, $expected, $description);
243
244 $seen_vtype->{@$expected[0]} = 1 if ref $expected eq 'ARRAY';
245 }
246
247 # to check if all $vtype_subdirs are defined in path_to_volume_id
248 # or have a test
249 is_deeply($seen_vtype, $vtype_subdirs, "vtype_subdir check");
250
251 done_testing();
252
253 1;