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