]> git.proxmox.com Git - pve-storage.git/blame - test/path_to_volume_id_test.pm
Fix: path_to_volume_id returned wrong content
[pve-storage.git] / test / path_to_volume_id_test.pm
CommitLineData
e4dbfde3
AA
1package PVE::Storage::TestPathToVolumeId;
2
3use strict;
4use warnings;
5
6use lib qw(..);
7
8use PVE::Storage;
9
10use Test::More;
11
12use Cwd;
13use File::Basename;
14use File::Path qw(make_path remove_tree);
15use File::Temp;
16
17my $storage_dir = File::Temp->newdir();
18my $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
45my @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 },
103
104 {
105 description => 'ISO file',
106 volname => "$storage_dir/template/iso/yet-again-a-installation-disk.iso",
107 expected => [
108 'iso',
109 'local:iso/yet-again-a-installation-disk.iso',
110 ],
111 },
112 {
113 description => 'CT template, tar.gz',
114 volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.gz",
115 expected => [
116 'vztmpl',
117 'local:vztmpl/debian-10.0-standard_10.0-1_amd64.tar.gz',
118 ],
119 },
120
121 {
122 description => 'Rootdir',
123 volname => "$storage_dir/private/1234/", # fileparse needs / at the end
124 expected => [
125 'rootdir',
126 'local:rootdir/1234',
127 ],
128 },
129 {
130 description => 'Rootdir, folder subvol',
131 volname => "$storage_dir/images/1234/subvol-1234-disk-0.subvol/", # fileparse needs / at the end
132 expected => [
133 'images',
134 'local:1234/subvol-1234-disk-0.subvol'
135 ],
136 },
137
138 # no matches
139 {
140 description => 'Snippets, yaml',
141 volname => "$storage_dir/snippets/userconfig.yaml",
142 expected => [''],
143 },
144 {
145 description => 'Snippets, hookscript',
146 volname => "$storage_dir/snippets/hookscript.pl",
147 expected => [''],
148 },
149 {
150 description => 'CT template, tar.xz',
151 volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.xz",
152 expected => [''],
153 },
154
155 # no matches, path or files with failures
156 {
157 description => 'Base template, string as vmid in folder name',
158 volname => "$storage_dir/images/ssss/base-4321-disk-0.raw",
159 expected => [''],
160 },
161 {
162 description => 'ISO file, wrong ending',
163 volname => "$storage_dir/template/iso/yet-again-a-installation-disk.dvd",
164 expected => [''],
165 },
166 {
167 description => 'CT template, wrong ending, zip.gz',
168 volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.zip.gz",
169 expected => [''],
170 },
171 {
172 description => 'CT template, wrong ending, tar bz2',
173 volname => "$storage_dir/template/cache/debian-10.0-standard_10.0-1_amd64.tar.bz2",
174 expected => [''],
175 },
176 {
177 description => 'Rootdir as subvol, wrong path',
178 volname => "$storage_dir/private/subvol-19254-disk-0/",
179 expected => [''],
180 },
181 {
182 description => 'Backup, wrong ending, openvz, tar.bz2',
183 volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.tar.bz2",
184 expected => [''],
185 },
186 {
187 description => 'Backup, wrong format, openvz, zip.gz',
188 volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.zip.gz",
189 expected => [''],
190 },
191 {
192 description => 'Backup, wrong format, openvz, tgz.lzo',
193 volname => "$storage_dir/dump/vzdump-openvz-16112-2020_03_30-21_39_30.tgz.lzo",
194 expected => [''],
195 },
196 {
197 description => 'Backup, wrong ending, qemu, vma.xz',
198 volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_12_40.vma.xz",
199 expected => [''],
200 },
201 {
202 description => 'Backup, wrong format, qemu, vms.gz',
203 volname => "$storage_dir/dump/vzdump-qemu-16110-2020_03_30-21_12_40.vms.gz",
204 expected => [''],
205 },
206 {
207 description => 'Image, string as vmid in folder name',
208 volname => "$storage_dir/images/ssss/vm-1234-disk-0.qcow2",
209 expected => [''],
210 },
211);
212
213plan tests => scalar @tests;
214
215foreach my $tt (@tests) {
216 my $file = $tt->{volname};
217 my $expected = $tt->{expected};
218 my $description = $tt->{description};
219
220 # prepare environment
221 my ($name, $dir, $suffix) = fileparse($file);
222 make_path($dir, { verbose => 1, mode => 0755 });
223
224 if ($name) {
225 open(my $fh, ">>", "$file") || die "Error open file: $!";
226 close($fh);
227 }
228
229 # run tests
230 my $got;
231 eval { $got = [ PVE::Storage::path_to_volume_id($scfg, $file) ] };
232 $got = $@ if $@;
233
234 is_deeply($got, $expected, $description) || diag(explain($got));
235}
236
237#cleanup
238# File::Temp unlinks tempdir on exit
239
240done_testing();
241
2421;