]> git.proxmox.com Git - pve-guest-common.git/blob - tests/abstract-config-tests.pl
f640171007b72f4368d8785e9facb29e8f577465
[pve-guest-common.git] / tests / abstract-config-tests.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib qw(..);
7
8 use Test::More;
9 #use Test::MockModule;
10
11 use PVE::AbstractConfig;
12
13
14 # tests for different top level method implementations of AbstractConfig
15 # tests need to specify the method, the parameter and expected result
16 # for neatly doing more tests per single method you can specifiy a subtests
17 # array, which then only has params and expected result
18
19 # note that the indentaion level below is "wrong" by design
20 my $tests = [
21 {
22 method => 'parse_pending_delete',
23 subtests => [
24 {
25 params => [ "memory,cpu" ],
26 expect => {
27 cpu => { force => 0, },
28 memory => { force => 0, },
29 },
30 },
31 {
32 params => [ "memory;cpu,!mp0" ],
33 expect => {
34 cpu => { force => 0, },
35 memory => { force => 0, },
36 mp0 => { force => 1, },
37 },
38 },
39 {
40 params => [ " memory ; cpu, !mp0, !mp1" ],
41 expect => { # can separate with comma, semicolon, spaces
42 cpu => { force => 0, },
43 memory => { force => 0, },
44 mp0 => { force => 1, },
45 mp1 => { force => 1, },
46 },
47 },
48 {
49 params => [ "!!memory" ],
50 expect => { # we have no double negation, only simple stuff
51 '!memory' => { force => 1, },
52 },
53 },
54 {
55 params => [ " mem ory" ],
56 expect => { # we do not support keys with spaces, seens as two different ones
57 'mem' => { force => 0, },
58 'ory' => { force => 0, },
59 },
60 },
61 ]
62 },
63 ]; # tests definition end
64
65
66 sub do_test($$;$) {
67 my ($method, $test, $name) = @_;
68
69 fail("incomplete test, params or expected missing")
70 if !exists $test->{params} || !exists $test->{expect};
71
72 my ($params, $expect) = $test->@{qw(params expect)};
73
74 my $res = eval { PVE::AbstractConfig->$method(@$params) };
75 if (my $err = $@) {
76 is ($err, $expect, $name);
77 } else {
78 is_deeply($res, $expect, $name);
79 }
80 }
81
82 for my $test (@$tests) {
83 my $method = $test->{method} or fail("missing AbstractConfig method to test") and next;
84
85 my $name = $test->{name} // $method;
86
87 if (defined(my $subtests = $test->{subtests})) {
88 subtest $name => sub {
89 for my $subtest (@$subtests) {
90 do_test($method, $subtest);
91 }
92 }
93 } else {
94 do_test($method, $test, $name);
95 }
96 #my $expected = $test->{expect};
97 }
98
99 done_testing();