]>
Commit | Line | Data |
---|---|---|
e4dbfde3 AA |
1 | package PVE::Storage::TestPathToVolumeId; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use lib qw(..); | |
7 | ||
8 | use PVE::Storage; | |
9 | ||
10 | use Test::More; | |
11 | ||
12 | use Cwd; | |
13 | use File::Basename; | |
14 | use File::Path qw(make_path remove_tree); | |
15 | use File::Temp; | |
16 | ||
17 | my $storage_dir = File::Temp->newdir(); | |
18 | my $scfg = { | |
19 | 'digest' => 'd29306346b8b25b90a4a96165f1e8f52d1af1eda', | |
20 | 'ids' => { | |
21 | 'local' => { | |
22 | 'shared' => 0, | |
23 | 'path' => "$storage_dir", | |
24 | 'type' => 'dir', | |
25 | 'maxfiles' => 0, | |
26 | 'content' => { | |
27 | 'snippets' => 1, | |
28 | 'rootdir' => 1, | |
29 | 'images' => 1, | |
30 | 'iso' => 1, | |
31 | 'backup' => 1, | |
32 | 'vztmpl' => 1, | |
33 | }, | |
34 | }, | |
35 | }, | |
36 | 'order' => { | |
37 | 'local' => 1, | |
38 | }, | |
39 | }; | |
40 | ||
41 | # the tests array consists of hashes with the following keys: | |
42 | # description => to identify the test case | |
43 | # volname => to create the test file | |
44 | # expected => the result that path_to_volume_id should return | |
45 | my @tests = ( | |
46 | { | |
47 | description => 'Image, qcow2', | |
48 | volname => "$storage_dir/images/16110/vm-16110-disk-0.qcow2", | |
49 | expected => [ | |
50 | 'images', | |
51 | 'local:16110/vm-16110-disk-0.qcow2', | |
52 | ], | |
53 | }, | |
54 | { | |
55 | description => 'Image, raw', | |
56 | volname => "$storage_dir/images/16112/vm-16112-disk-0.raw", | |
57 | expected => [ | |
58 | 'images', | |
59 | 'local:16112/vm-16112-disk-0.raw', | |
60 | ], | |
61 | }, | |
62 | { | |
63 | description => 'Image template, qcow2', | |
64 | volname => "$storage_dir/images/9004/base-9004-disk-0.qcow2", | |
65 | expected => [ | |
66 | 'images', | |
67 | 'local:9004/base-9004-disk-0.qcow2', | |
68 | ], | |
69 | }, | |
70 | ||
71 | { | |
72 | description => 'Backup, vma.gz', | |
73 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_11_40.vma.gz", | |
74 | expected => [ | |
892dc992 | 75 | 'backup', |
e4dbfde3 AA |
76 | 'local:backup/vzdump-qemu-16110-2020_03_30-21_11_40.vma.gz', |
77 | ], | |
78 | }, | |
79 | { | |
80 | description => 'Backup, vma.lzo', | |
81 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_12_45.vma.lzo", | |
82 | expected => [ | |
892dc992 | 83 | 'backup', |
e4dbfde3 AA |
84 | 'local:backup/vzdump-qemu-16110-2020_03_30-21_12_45.vma.lzo', |
85 | ], | |
86 | }, | |
87 | { | |
88 | description => 'Backup, vma', | |
89 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_13_55.vma", | |
90 | expected => [ | |
892dc992 | 91 | 'backup', |
e4dbfde3 AA |
92 | 'local:backup/vzdump-qemu-16110-2020_03_30-21_13_55.vma', |
93 | ], | |
94 | }, | |
95 | { | |
96 | description => 'Backup, tar.lzo', | |
97 | volname => "$storage_dir/dump/vzdump-lxc-16112-2020_03_30-21_39_30.tar.lzo", | |
98 | expected => [ | |
892dc992 | 99 | 'backup', |
e4dbfde3 AA |
100 | 'local:backup/vzdump-lxc-16112-2020_03_30-21_39_30.tar.lzo', |
101 | ], | |
102 | }, | |
014d36db AA |
103 | { |
104 | description => 'Backup, vma.zst', | |
105 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_13_55.vma.zst", | |
106 | expected => [ | |
107 | 'backup', | |
108 | 'local:backup/vzdump-qemu-16110-2020_03_30-21_13_55.vma.zst' | |
109 | ], | |
110 | }, | |
111 | { | |
112 | description => 'Backup, tar.zst', | |
113 | volname => "$storage_dir/dump/vzdump-lxc-16112-2020_03_30-21_39_30.tar.zst", | |
114 | expected => [ | |
115 | 'backup', | |
116 | 'local:backup/vzdump-lxc-16112-2020_03_30-21_39_30.tar.zst' | |
117 | ], | |
118 | }, | |
e4dbfde3 AA |
119 | |
120 | { | |
121 | description => 'ISO file', | |
122 | volname => "$storage_dir/template/iso/yet-again-a-installation-disk.iso", | |
123 | expected => [ | |
124 | 'iso', | |
125 | 'local:iso/yet-again-a-installation-disk.iso', | |
126 | ], | |
127 | }, | |
128 | { | |
129 | description => 'CT template, tar.gz', | |
130 | volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.gz", | |
131 | expected => [ | |
132 | 'vztmpl', | |
133 | 'local:vztmpl/debian-10.0-standard_10.0-1_amd64.tar.gz', | |
134 | ], | |
135 | }, | |
136 | ||
137 | { | |
138 | description => 'Rootdir', | |
139 | volname => "$storage_dir/private/1234/", # fileparse needs / at the end | |
140 | expected => [ | |
141 | 'rootdir', | |
142 | 'local:rootdir/1234', | |
143 | ], | |
144 | }, | |
145 | { | |
146 | description => 'Rootdir, folder subvol', | |
147 | volname => "$storage_dir/images/1234/subvol-1234-disk-0.subvol/", # fileparse needs / at the end | |
148 | expected => [ | |
149 | 'images', | |
150 | 'local:1234/subvol-1234-disk-0.subvol' | |
151 | ], | |
152 | }, | |
e4dbfde3 AA |
153 | { |
154 | description => 'Snippets, yaml', | |
155 | volname => "$storage_dir/snippets/userconfig.yaml", | |
73fcb7bf AA |
156 | expected => [ |
157 | 'snippets', | |
158 | 'local:snippets/userconfig.yaml', | |
159 | ], | |
e4dbfde3 AA |
160 | }, |
161 | { | |
162 | description => 'Snippets, hookscript', | |
163 | volname => "$storage_dir/snippets/hookscript.pl", | |
73fcb7bf AA |
164 | expected => [ |
165 | 'snippets', | |
166 | 'local:snippets/hookscript.pl', | |
167 | ], | |
e4dbfde3 | 168 | }, |
73fcb7bf AA |
169 | |
170 | # no matches | |
e4dbfde3 AA |
171 | { |
172 | description => 'CT template, tar.xz', | |
173 | volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.xz", | |
174 | expected => [''], | |
175 | }, | |
176 | ||
177 | # no matches, path or files with failures | |
178 | { | |
179 | description => 'Base template, string as vmid in folder name', | |
180 | volname => "$storage_dir/images/ssss/base-4321-disk-0.raw", | |
181 | expected => [''], | |
182 | }, | |
183 | { | |
184 | description => 'ISO file, wrong ending', | |
185 | volname => "$storage_dir/template/iso/yet-again-a-installation-disk.dvd", | |
186 | expected => [''], | |
187 | }, | |
188 | { | |
189 | description => 'CT template, wrong ending, zip.gz', | |
190 | volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.zip.gz", | |
191 | expected => [''], | |
192 | }, | |
193 | { | |
194 | description => 'CT template, wrong ending, tar bz2', | |
195 | volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.bz2", | |
196 | expected => [''], | |
197 | }, | |
198 | { | |
199 | description => 'Rootdir as subvol, wrong path', | |
200 | volname => "$storage_dir/private/subvol-19254-disk-0/", | |
201 | expected => [''], | |
202 | }, | |
203 | { | |
204 | description => 'Backup, wrong ending, openvz, tar.bz2', | |
205 | volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.tar.bz2", | |
206 | expected => [''], | |
207 | }, | |
208 | { | |
209 | description => 'Backup, wrong format, openvz, zip.gz', | |
210 | volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.zip.gz", | |
211 | expected => [''], | |
212 | }, | |
213 | { | |
214 | description => 'Backup, wrong format, openvz, tgz.lzo', | |
215 | volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.tgz.lzo", | |
216 | expected => [''], | |
217 | }, | |
218 | { | |
219 | description => 'Backup, wrong ending, qemu, vma.xz', | |
220 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_12_40.vma.xz", | |
221 | expected => [''], | |
222 | }, | |
223 | { | |
224 | description => 'Backup, wrong format, qemu, vms.gz', | |
225 | volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_12_40.vms.gz", | |
226 | expected => [''], | |
227 | }, | |
228 | { | |
229 | description => 'Image, string as vmid in folder name', | |
230 | volname => "$storage_dir/images/ssss/vm-1234-disk-0.qcow2", | |
231 | expected => [''], | |
232 | }, | |
233 | ); | |
234 | ||
73fcb7bf AA |
235 | plan tests => scalar @tests + 1; |
236 | ||
237 | my $seen_vtype; | |
238 | my $vtype_subdirs = { map { $_ => 1 } keys %{ PVE::Storage::Plugin::get_vtype_subdirs() } }; | |
e4dbfde3 AA |
239 | |
240 | foreach my $tt (@tests) { | |
241 | my $file = $tt->{volname}; | |
242 | my $expected = $tt->{expected}; | |
243 | my $description = $tt->{description}; | |
244 | ||
245 | # prepare environment | |
246 | my ($name, $dir, $suffix) = fileparse($file); | |
247 | make_path($dir, { verbose => 1, mode => 0755 }); | |
248 | ||
249 | if ($name) { | |
250 | open(my $fh, ">>", "$file") || die "Error open file: $!"; | |
251 | close($fh); | |
252 | } | |
253 | ||
254 | # run tests | |
255 | my $got; | |
256 | eval { $got = [ PVE::Storage::path_to_volume_id($scfg, $file) ] }; | |
257 | $got = $@ if $@; | |
258 | ||
259 | is_deeply($got, $expected, $description) || diag(explain($got)); | |
73fcb7bf AA |
260 | |
261 | $seen_vtype->{@$expected[0]} = 1 | |
262 | if ( @$expected[0] ne '' && scalar @$expected > 1); | |
e4dbfde3 AA |
263 | } |
264 | ||
73fcb7bf AA |
265 | # to check if all $vtype_subdirs are defined in path_to_volume_id |
266 | # or have a test | |
267 | is_deeply($seen_vtype, $vtype_subdirs, "vtype_subdir check"); | |
268 | ||
e4dbfde3 AA |
269 | #cleanup |
270 | # File::Temp unlinks tempdir on exit | |
271 | ||
272 | done_testing(); | |
273 | ||
274 | 1; |