]> git.proxmox.com Git - pve-manager.git/blob - test/vzdump_new_test.pl
fix #3430: handle hook script paths better
[pve-manager.git] / test / vzdump_new_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;
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 #
85 # To begin testing for different options, use a fake test like the first one
86 my @tests = (
87 {
88 description => 'BEGIN RETENTION TESTS',
89 tested_options => ['prune-backups', 'remove'],
90 },
91 {
92 description => 'no params',
93 expected => {
94 'prune-backups' => {
95 'keep-last' => 1,
96 },
97 remove => 1,
98 },
99 },
100 # TODO make parse error critical?
101 {
102 description => 'maxfiles vzdump 1',
103 vzdump_param => {
104 maxfiles => 0,
105 },
106 expected => {
107 'prune-backups' => {
108 'keep-last' => 1,
109 },
110 remove => 1,
111 },
112 },
113 {
114 description => 'maxfiles vzdump 2',
115 vzdump_param => {
116 maxfiles => 7,
117 },
118 expected => {
119 'prune-backups' => {
120 'keep-last' => 7,
121 },
122 remove => 1,
123 },
124 },
125 {
126 description => 'maxfiles storage 1',
127 storage_param => {
128 maxfiles => 0,
129 },
130 expected => {
131 'prune-backups' => {
132 'keep-all' => 1,
133 },
134 remove => 0,
135 },
136 },
137 {
138 description => 'maxfiles storage 2',
139 storage_param => {
140 maxfiles => 7,
141 },
142 expected => {
143 'prune-backups' => {
144 'keep-last' => 7,
145 },
146 remove => 1,
147 },
148 },
149 {
150 description => 'maxfiles CLI 1',
151 cli_param => {
152 maxfiles => 0,
153 },
154 expected => {
155 'prune-backups' => {
156 'keep-all' => 1,
157 },
158 remove => 0,
159 },
160 },
161 {
162 description => 'maxfiles CLI 2',
163 cli_param => {
164 maxfiles => 7,
165 },
166 expected => {
167 'prune-backups' => {
168 'keep-last' => 7,
169 },
170 remove => 1,
171 },
172 },
173 {
174 description => 'prune-backups vzdump 1',
175 vzdump_param => {
176 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
177 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
178 },
179 expected => {
180 'prune-backups' => {
181 'keep-last' => 1,
182 'keep-hourly' => 2,
183 'keep-daily' => 3,
184 'keep-weekly' => 4,
185 'keep-monthly' => 5,
186 'keep-yearly' => 6,
187 },
188 remove => 1,
189 },
190 },
191 {
192 description => 'prune-backups vzdump 2',
193 vzdump_param => {
194 'prune-backups' => 'keep-all=1',
195 },
196 expected => {
197 'prune-backups' => {
198 'keep-all' => 1,
199 },
200 remove => 0,
201 },
202 },
203 {
204 description => 'prune-backups vzdump 3',
205 vzdump_param => {
206 'prune-backups' => 'keep-hourly=0,keep-monthly=0,keep-yearly=0',
207 },
208 expected => {
209 'prune-backups' => {
210 'keep-all' => 1,
211 },
212 remove => 0,
213 },
214 },
215 {
216 description => 'both vzdump 1',
217 vzdump_param => {
218 'prune-backups' => 'keep-all=1',
219 maxfiles => 7,
220 },
221 expected => {
222 'prune-backups' => {
223 'keep-all' => 1,
224 },
225 remove => 0,
226 },
227 },
228 {
229 description => 'prune-backups storage 1',
230 storage_param => {
231 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
232 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
233 },
234 expected => {
235 'prune-backups' => {
236 'keep-last' => 1,
237 'keep-hourly' => 2,
238 'keep-daily' => 3,
239 'keep-weekly' => 4,
240 'keep-monthly' => 5,
241 'keep-yearly' => 6,
242 },
243 remove => 1,
244 },
245 },
246 {
247 description => 'prune-backups storage 2',
248 storage_param => {
249 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
250 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
251 },
252 expected => {
253 'prune-backups' => {
254 'keep-all' => 1,
255 },
256 remove => 0,
257 },
258 },
259 {
260 description => 'prune-backups storage 3',
261 storage_param => {
262 'prune-backups' => 'keep-hourly=0,keep-monthly=0,keep-yearly=0',
263 },
264 expected => {
265 'prune-backups' => {
266 'keep-all' => 1,
267 },
268 remove => 0,
269 },
270 },
271 {
272 description => 'both storage 1',
273 storage_param => {
274 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
275 maxfiles => 0,
276 },
277 expected => {
278 'prune-backups' => {
279 'keep-hourly' => 1,
280 'keep-monthly' => 2,
281 'keep-yearly' => 3,
282 },
283 remove => 1,
284 },
285 },
286 {
287 description => 'prune-backups CLI 1',
288 cli_param => {
289 'prune-backups' => 'keep-last=1,keep-hourly=2,keep-daily=3,' .
290 'keep-weekly=4,keep-monthly=5,keep-yearly=6',
291 },
292 expected => {
293 'prune-backups' => {
294 'keep-last' => 1,
295 'keep-hourly' => 2,
296 'keep-daily' => 3,
297 'keep-weekly' => 4,
298 'keep-monthly' => 5,
299 'keep-yearly' => 6,
300 },
301 remove => 1,
302 },
303 },
304 {
305 description => 'prune-backups CLI 2',
306 cli_param => {
307 'prune-backups' => 'keep-last=0,keep-hourly=0,keep-daily=0,' .
308 'keep-weekly=0,keep-monthly=0,keep-yearly=0',
309 },
310 expected => {
311 'prune-backups' => {
312 'keep-all' => 1,
313 },
314 remove => 0,
315 },
316 },
317 {
318 description => 'prune-backups CLI 3',
319 cli_param => {
320 'prune-backups' => 'foo=bar',
321 },
322 expected => "format error\n" .
323 "foo: property is not defined in schema and the schema does not allow additional properties\n",
324 },
325 {
326 description => 'both CLI 1',
327 cli_param => {
328 'prune-backups' => 'keep-hourly=1,keep-monthly=2,keep-yearly=3',
329 maxfiles => 4,
330 },
331 expected => "400 Parameter verification failed.\n" .
332 "prune-backups: option conflicts with option 'maxfiles'\n",
333 },
334 {
335 description => 'mixed 1',
336 vzdump_param => {
337 maxfiles => 7,
338 },
339 storage_param => {
340 'prune-backups' => 'keep-hourly=24',
341 },
342 expected => {
343 'prune-backups' => {
344 'keep-hourly' => 24,
345 },
346 remove => 1,
347 },
348 },
349 # TODO make parse error critical?
350 {
351 description => 'mixed 2',
352 vzdump_param => {
353 maxfiles => 7,
354 },
355 storage_param => {
356 'prune-backups' => 'keephourly=24',
357 },
358 expected => {
359 'prune-backups' => {
360 'keep-last' => 7,
361 },
362 remove => 1,
363 },
364 },
365 {
366 description => 'mixed 3',
367 vzdump_param => {
368 maxfiles => 7,
369 },
370 cli_param => {
371 'prune-backups' => 'keep-all=1',
372 },
373 expected => {
374 'prune-backups' => {
375 'keep-all' => 1,
376 },
377 remove => 0,
378 },
379 },
380 {
381 description => 'mixed 4',
382 vzdump_param => {
383 maxfiles => 7,
384 },
385 storage_param => {
386 'prune-backups' => 'keep-all=0,keep-last=10',
387 },
388 cli_param => {
389 'prune-backups' => 'keep-all=1',
390 },
391 expected => {
392 'prune-backups' => {
393 'keep-all' => 1,
394 },
395 remove => 0,
396 },
397 },
398 {
399 description => 'mixed 5',
400 vzdump_param => {
401 maxfiles => 7,
402 },
403 storage_param => {
404 'prune-backups' => 'keep-all=0,keep-last=10',
405 },
406 expected => {
407 'prune-backups' => {
408 'keep-last' => 10,
409 },
410 remove => 1,
411 },
412 },
413 {
414 description => 'mixed 6',
415 storage_param => {
416 'prune-backups' => 'keep-last=10',
417 },
418 cli_param => {
419 'prune-backups' => 'keep-all=1',
420 },
421 expected => {
422 'prune-backups' => {
423 'keep-all' => 1,
424 },
425 remove => 0,
426 },
427 },
428 {
429 description => 'mixed 7',
430 storage_param => {
431 'prune-backups' => 'keep-all=1',
432 },
433 cli_param => {
434 'prune-backups' => 'keep-last=10',
435 },
436 expected => {
437 'prune-backups' => {
438 'keep-last' => 10,
439 },
440 remove => 1,
441 },
442 },
443 {
444 description => 'mixed 8',
445 storage_param => {
446 'prune-backups' => 'keep-last=10',
447 },
448 vzdump_param => {
449 'prune-backups' => 'keep-all=1',
450 },
451 expected => {
452 'prune-backups' => {
453 'keep-last' => 10,
454 },
455 remove => 1,
456 },
457 },
458 {
459 description => 'mixed 9',
460 vzdump_param => {
461 'prune-backups' => 'keep-last=10',
462 },
463 cli_param => {
464 'prune-backups' => 'keep-all=1',
465 },
466 expected => {
467 'prune-backups' => {
468 'keep-all' => 1,
469 },
470 remove => 0,
471 },
472 },
473 {
474 description => 'BEGIN MAILTO TESTS',
475 tested_options => ['mailto'],
476 },
477 {
478 description => 'mailto vzdump 1',
479 vzdump_param => {
480 'mailto' => 'developer@proxmox.com',
481 },
482 expected => {
483 'mailto' => [
484 'developer@proxmox.com',
485 ],
486 },
487 },
488 {
489 description => 'mailto vzdump 2',
490 vzdump_param => {
491 'mailto' => 'developer@proxmox.com admin@proxmox.com',
492 },
493 expected => {
494 'mailto' => [
495 'developer@proxmox.com',
496 'admin@proxmox.com',
497 ],
498 },
499 },
500 {
501 description => 'mailto vzdump 3',
502 vzdump_param => {
503 'mailto' => 'developer@proxmox.com,admin@proxmox.com',
504 },
505 expected => {
506 'mailto' => [
507 'developer@proxmox.com',
508 'admin@proxmox.com',
509 ],
510 },
511 },
512 {
513 description => 'mailto vzdump 4',
514 vzdump_param => {
515 'mailto' => 'developer@proxmox.com, admin@proxmox.com',
516 },
517 expected => {
518 'mailto' => [
519 'developer@proxmox.com',
520 'admin@proxmox.com',
521 ],
522 },
523 },
524 {
525 description => 'mailto vzdump 5',
526 vzdump_param => {
527 'mailto' => ' ,,; developer@proxmox.com, ; admin@proxmox.com ',
528 },
529 expected => {
530 'mailto' => [
531 'developer@proxmox.com',
532 'admin@proxmox.com',
533 ],
534 },
535 },
536 {
537 description => 'mailto vzdump 6',
538 vzdump_param => {
539 'mailto' => '',
540 },
541 expected => {
542 'mailto' => [],
543 },
544 },
545 {
546 description => 'mailto CLI 1',
547 cli_param => {
548 'mailto' => 'developer@proxmox.com',
549 },
550 expected => {
551 'mailto' => [
552 'developer@proxmox.com',
553 ],
554 },
555 },
556 {
557 description => 'mailto CLI 2',
558 cli_param => {
559 'mailto' => 'developer@proxmox.com admin@proxmox.com',
560 },
561 expected => {
562 'mailto' => [
563 'developer@proxmox.com',
564 'admin@proxmox.com',
565 ],
566 },
567 },
568 {
569 description => 'mailto CLI 3',
570 cli_param => {
571 'mailto' => 'developer@proxmox.com,admin@proxmox.com',
572 },
573 expected => {
574 'mailto' => [
575 'developer@proxmox.com',
576 'admin@proxmox.com',
577 ],
578 },
579 },
580 {
581 description => 'mailto CLI 4',
582 cli_param => {
583 'mailto' => 'developer@proxmox.com, admin@proxmox.com',
584 },
585 expected => {
586 'mailto' => [
587 'developer@proxmox.com',
588 'admin@proxmox.com',
589 ],
590 },
591 },
592 {
593 description => 'mailto CLI 5',
594 cli_param => {
595 'mailto' => ' ,,; developer@proxmox.com, ; admin@proxmox.com ',
596 },
597 expected => {
598 'mailto' => [
599 'developer@proxmox.com',
600 'admin@proxmox.com',
601 ],
602 },
603 },
604 {
605 description => 'mailto both 1',
606 vzdump_param => {
607 'mailto' => 'developer@proxmox.com',
608 },
609 cli_param => {
610 'mailto' => 'admin@proxmox.com',
611 },
612 expected => {
613 'mailto' => [
614 'admin@proxmox.com',
615 ],
616 },
617 },
618 {
619 description => 'mailto both 2',
620 vzdump_param => {
621 'mailto' => 'developer@proxmox.com',
622 },
623 cli_param => {
624 'mailto' => '',
625 },
626 expected => {
627 'mailto' => [],
628 },
629 },
630 );
631
632 plan tests => scalar @tests;
633
634 foreach my $test (@tests) {
635 if (defined($test->{tested_options})) {
636 $tested_options = $test->{tested_options};
637 ok(1, $test->{description});
638 next;
639 }
640
641 prepare_storage_config($test->{storage_param});
642 prepare_vzdump_config($test->{vzdump_param});
643
644 $test->{cli_param}->{vmid} = 100;
645 $test->{cli_param}->{storage} = 'local';
646
647 my $got = eval {
648 PVE::VZDump::verify_vzdump_parameters($test->{cli_param}, 1);
649 PVE::VZDump::parse_mailto_exclude_path($test->{cli_param});
650
651 my $vzdump = PVE::VZDump->new('fake cmdline', $test->{cli_param}, undef);
652
653 my $opts = $vzdump->{opts} or die "did not get options\n";
654 die "maxfiles is defined" if defined($opts->{maxfiles});
655
656 my $res = {};
657 foreach my $opt (@{$tested_options}) {
658 next if !defined($opts->{$opt});
659 $res->{$opt} = $opts->{$opt};
660 }
661 return $res;
662 };
663 $got = $@ if $@;
664
665 is_deeply($got, $test->{expected}, $test->{description}) || diag(explain($got));
666 }
667
668 done_testing();