]> git.proxmox.com Git - pve-manager.git/blob - test/vzdump_new_retention_test.pl
569419fb30455f58cdbe4de92fd74ce8eba85897
[pve-manager.git] / test / vzdump_new_retention_test.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '..';
7
8 use Test::More;
9 use Test::MockModule;
10
11 use PVE::VZDump;
12
13 my $vzdump_config;
14 my $storage_config;
15
16 sub prepare_storage_config {
17 my ($param) = @_;
18
19 $storage_config = "dir: local\n";
20 $storage_config .= "\tcontent backup\n";
21 $storage_config .= "\tpath /var/lib/vz\n";
22
23 foreach my $key (keys %{$param}) {
24 my $value = $param->{$key};
25 $storage_config .= "\t${key} ${value}\n";
26 }
27 }
28
29 sub prepare_vzdump_config {
30 my ($param) = @_;
31
32 $vzdump_config = "";
33 foreach my $key (keys %{$param}) {
34 my $value = $param->{$key};
35 $vzdump_config .= "${key}: ${value}\n";
36 }
37 }
38
39 my $pve_vzdump_module = Test::MockModule->new('PVE::VZDump');
40 $pve_vzdump_module->mock(
41 mkpath => sub {
42 return;
43 },
44 check_bin => sub {
45 return;
46 },
47 );
48
49 my $pve_storage_module = Test::MockModule->new('PVE::Storage');
50 $pve_storage_module->mock(
51 activate_storage => sub {
52 return;
53 },
54 );
55
56 my $pve_cluster_module = Test::MockModule->new('PVE::Cluster');
57 $pve_cluster_module->mock(
58 get_config => sub {
59 my ($filename) = @_;
60
61 die "unexpected filename '$filename'\n" if $filename ne 'storage.cfg';
62 return $storage_config;
63 },
64 );
65
66 my $pve_tools_module = Test::MockModule->new('PVE::Tools');
67 $pve_tools_module->mock(
68 file_get_contents => sub {
69 my ($filename) = @_;
70
71 die "unexpected filename '$filename'\n" if $filename ne '/etc/vzdump.conf';
72 return $vzdump_config;
73 },
74 );
75
76 my @tested_options = qw(prune-backups remove);
77
78 # each test consists of the following:
79 # name - unique name for the test
80 # cli_param - CLI parameters to be passed to new(); vmid and storage are hardcoded
81 # storage_param - parameters for the mocked storage configuration
82 # vzdump_param - parameters for the mocked /etc/vzdump.conf
83 # expected - expected options
84 my @tests = (
85 {
86 description => 'no params',
87 expected => {
88 'prune-backups' => {
89 'keep-last' => 1,
90 },
91 remove => 1,
92 },
93 },
94 # TODO make parse error critical?
95 {
96 description => 'maxfiles vzdump 1',
97 vzdump_param => {
98 maxfiles => 0,
99 },
100 expected => {
101 'prune-backups' => {
102 'keep-last' => 1,
103 },
104 remove => 1,
105 },
106 },
107 {
108 description => 'maxfiles vzdump 2',
109 vzdump_param => {
110 maxfiles => 7,
111 },
112 expected => {
113 'prune-backups' => {
114 'keep-last' => 7,
115 },
116 remove => 1,
117 },
118 },
119 {
120 description => 'maxfiles storage 1',
121 storage_param => {
122 maxfiles => 0,
123 },
124 expected => {
125 'prune-backups' => {
126 'keep-all' => 1,
127 },
128 remove => 0,
129 },
130 },
131 {
132 description => 'maxfiles storage 2',
133 storage_param => {
134 maxfiles => 7,
135 },
136 expected => {
137 'prune-backups' => {
138 'keep-last' => 7,
139 },
140 remove => 1,
141 },
142 },
143 {
144 description => 'maxfiles CLI 1',
145 cli_param => {
146 maxfiles => 0,
147 },
148 expected => {
149 'prune-backups' => {
150 'keep-all' => 1,
151 },
152 remove => 0,
153 },
154 },
155 {
156 description => 'maxfiles CLI 2',
157 cli_param => {
158 maxfiles => 7,
159 },
160 expected => {
161 'prune-backups' => {
162 'keep-last' => 7,
163 },
164 remove => 1,
165 },
166 },
167 {
168 description => 'prune-backups vzdump 1',
169 vzdump_param => {
170 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
171 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
172 },
173 expected => {
174 'prune-backups' => {
175 'keep-last' => 1,
176 'keep-hourly' => 2,
177 'keep-daily' => 3,
178 'keep-weekly' => 4,
179 'keep-monthly' => 5,
180 'keep-yearly' => 6,
181 },
182 remove => 1,
183 },
184 },
185 {
186 description => 'prune-backups vzdump 2',
187 vzdump_param => {
188 'prune-backups' => 'keep-all=1',
189 },
190 expected => {
191 'prune-backups' => {
192 'keep-all' => 1,
193 },
194 remove => 0,
195 },
196 },
197 {
198 description => 'prune-backups vzdump 3',
199 vzdump_param => {
200 'prune-backups' => 'keep-hourly=0,keep-monthly=0,keep-yearly=0',
201 },
202 expected => {
203 'prune-backups' => {
204 'keep-all' => 1,
205 },
206 remove => 0,
207 },
208 },
209 {
210 description => 'both vzdump 1',
211 vzdump_param => {
212 'prune-backups' => 'keep-all=1',
213 maxfiles => 7,
214 },
215 expected => {
216 'prune-backups' => {
217 'keep-all' => 1,
218 },
219 remove => 0,
220 },
221 },
222 {
223 description => 'prune-backups storage 1',
224 storage_param => {
225 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
226 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
227 },
228 expected => {
229 'prune-backups' => {
230 'keep-last' => 1,
231 'keep-hourly' => 2,
232 'keep-daily' => 3,
233 'keep-weekly' => 4,
234 'keep-monthly' => 5,
235 'keep-yearly' => 6,
236 },
237 remove => 1,
238 },
239 },
240 {
241 description => 'prune-backups storage 2',
242 storage_param => {
243 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
244 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
245 },
246 expected => {
247 'prune-backups' => {
248 'keep-all' => 1,
249 },
250 remove => 0,
251 },
252 },
253 {
254 description => 'prune-backups storage 3',
255 storage_param => {
256 'prune-backups' => 'keep-hourly=0,keep-monthly=0,keep-yearly=0',
257 },
258 expected => {
259 'prune-backups' => {
260 'keep-all' => 1,
261 },
262 remove => 0,
263 },
264 },
265 {
266 description => 'both storage 1',
267 storage_param => {
268 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
269 maxfiles => 0,
270 },
271 expected => {
272 'prune-backups' => {
273 'keep-hourly' => 1,
274 'keep-monthly' => 2,
275 'keep-yearly' => 3,
276 },
277 remove => 1,
278 },
279 },
280 {
281 description => 'prune-backups CLI 1',
282 cli_param => {
283 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
284 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
285 },
286 expected => {
287 'prune-backups' => {
288 'keep-last' => 1,
289 'keep-hourly' => 2,
290 'keep-daily' => 3,
291 'keep-weekly' => 4,
292 'keep-monthly' => 5,
293 'keep-yearly' => 6,
294 },
295 remove => 1,
296 },
297 },
298 {
299 description => 'prune-backups CLI 2',
300 cli_param => {
301 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
302 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
303 },
304 expected => {
305 'prune-backups' => {
306 'keep-all' => 1,
307 },
308 remove => 0,
309 },
310 },
311 {
312 description => 'prune-backups CLI 3',
313 cli_param => {
314 'prune-backups' => 'foo=bar',
315 },
316 expected => "format error\n" .
317 "foo: property is not defined in schema and the schema does not allow additional properties\n",
318 },
319 {
320 description => 'both CLI 1',
321 cli_param => {
322 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
323 maxfiles => 4,
324 },
325 expected => "400 Parameter verification failed.\n" .
326 "prune-backups: option conflicts with option 'maxfiles'\n",
327 },
328 {
329 description => 'mixed 1',
330 vzdump_param => {
331 maxfiles => 7,
332 },
333 storage_param => {
334 'prune-backups' => 'keep-hourly=24',
335 },
336 expected => {
337 'prune-backups' => {
338 'keep-hourly' => 24,
339 },
340 remove => 1,
341 },
342 },
343 # TODO make parse error critical?
344 {
345 description => 'mixed 2',
346 vzdump_param => {
347 maxfiles => 7,
348 },
349 storage_param => {
350 'prune-backups' => 'keephourly=24',
351 },
352 expected => {
353 'prune-backups' => {
354 'keep-last' => 7,
355 },
356 remove => 1,
357 },
358 },
359 {
360 description => 'mixed 3',
361 vzdump_param => {
362 maxfiles => 7,
363 },
364 cli_param => {
365 'prune-backups' => 'keep-all=1',
366 },
367 expected => {
368 'prune-backups' => {
369 'keep-all' => 1,
370 },
371 remove => 0,
372 },
373 },
374 {
375 description => 'mixed 4',
376 vzdump_param => {
377 maxfiles => 7,
378 },
379 storage_param => {
380 'prune-backups' => 'keep-all=0,keep-last=10',
381 },
382 cli_param => {
383 'prune-backups' => 'keep-all=1',
384 },
385 expected => {
386 'prune-backups' => {
387 'keep-all' => 1,
388 },
389 remove => 0,
390 },
391 },
392 {
393 description => 'mixed 5',
394 vzdump_param => {
395 maxfiles => 7,
396 },
397 storage_param => {
398 'prune-backups' => 'keep-all=0,keep-last=10',
399 },
400 expected => {
401 'prune-backups' => {
402 'keep-last' => 10,
403 },
404 remove => 1,
405 },
406 },
407 {
408 description => 'mixed 6',
409 storage_param => {
410 'prune-backups' => 'keep-last=10',
411 },
412 cli_param => {
413 'prune-backups' => 'keep-all=1',
414 },
415 expected => {
416 'prune-backups' => {
417 'keep-all' => 1,
418 },
419 remove => 0,
420 },
421 },
422 {
423 description => 'mixed 7',
424 storage_param => {
425 'prune-backups' => 'keep-all=1',
426 },
427 cli_param => {
428 'prune-backups' => 'keep-last=10',
429 },
430 expected => {
431 'prune-backups' => {
432 'keep-last' => 10,
433 },
434 remove => 1,
435 },
436 },
437 {
438 description => 'mixed 8',
439 storage_param => {
440 'prune-backups' => 'keep-last=10',
441 },
442 vzdump_param => {
443 'prune-backups' => 'keep-all=1',
444 },
445 expected => {
446 'prune-backups' => {
447 'keep-last' => 10,
448 },
449 remove => 1,
450 },
451 },
452 {
453 description => 'mixed 9',
454 vzdump_param => {
455 'prune-backups' => 'keep-last=10',
456 },
457 cli_param => {
458 'prune-backups' => 'keep-all=1',
459 },
460 expected => {
461 'prune-backups' => {
462 'keep-all' => 1,
463 },
464 remove => 0,
465 },
466 },
467 );
468
469 plan tests => scalar @tests;
470
471 foreach my $test (@tests) {
472 prepare_storage_config($test->{storage_param});
473 prepare_vzdump_config($test->{vzdump_param});
474
475 $test->{cli_param}->{vmid} = 100;
476 $test->{cli_param}->{storage} = 'local';
477
478 my $got = eval {
479 PVE::VZDump::verify_vzdump_parameters($test->{cli_param}, 1);
480
481 my $vzdump = PVE::VZDump->new('fake cmdline', $test->{cli_param}, undef);
482
483 my $opts = $vzdump->{opts} or die "did not get options\n";
484 die "maxfiles is defined" if defined($opts->{maxfiles});
485
486 my $res = {};
487 foreach my $opt (@tested_options) {
488 next if !defined($opts->{$opt});
489 $res->{$opt} = $opts->{$opt};
490 }
491 return $res;
492 };
493 $got = $@ if $@;
494
495 is_deeply($got, $test->{expected}, $test->{description}) || diag(explain($got));
496 }
497
498 done_testing();