]>
git.proxmox.com Git - pve-guest-common.git/blob - tests/abstract-config-tests.pl
8f58e42aa02a2c1016bcb5b55900612e2abd6a8f
11 use PVE
::AbstractConfig
;
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 # sometimes the return value is less interesting to check than a parameter
19 # reference, so one can use "map_expect_to_param_id" to tell the test system to
20 # use that as expected result.
22 # note that the indentaion level below is "wrong" by design
25 method => 'parse_pending_delete',
28 params
=> [ "memory,cpu" ],
30 cpu
=> { force
=> 0, },
31 memory
=> { force
=> 0, },
35 params
=> [ "memory;cpu,!mp0" ],
37 cpu
=> { force
=> 0, },
38 memory
=> { force
=> 0, },
39 mp0
=> { force
=> 1, },
43 params
=> [ " memory ; cpu, !mp0, !mp1" ],
44 expect
=> { # can separate with comma, semicolon, spaces
45 cpu
=> { force
=> 0, },
46 memory
=> { force
=> 0, },
47 mp0
=> { force
=> 1, },
48 mp1
=> { force
=> 1, },
52 params
=> [ "!!memory" ],
53 expect
=> { # we have no double negation, only simple stuff
54 '!memory' => { force
=> 1, },
58 params
=> [ " mem ory" ],
59 expect
=> { # we do not support keys with spaces, seens as two different ones
60 'mem' => { force
=> 0, },
61 'ory' => { force
=> 0, },
67 method => 'print_pending_delete',
71 cpu
=> { force
=> 0, },
72 memory
=> { force
=> 0, },
74 expect
=> "cpu,memory",
77 params
=> [{ # we have no double negation, only simple stuff
78 '!memory' => { force
=> 1, },
85 method => 'add_to_pending_delete', # $conf, $key, $force
87 { # simple test addition to of a pending deletion to the empty config
88 params
=> [ {}, 'memory', ],
89 expect
=> { pending
=> { delete => 'memory', }, },
93 params
=> [ { pending
=> { delete => 'cpu', }, }, 'memory', 1 ],
94 expect
=> { pending
=> { delete => 'cpu,!memory', }, },
98 params
=> [ { pending
=> { delete => 'cpu', }, }, 'cpu', 1 ],
99 expect
=> { pending
=> { delete => '!cpu', }, },
102 params
=> [ { pending
=> { delete => 'cpu', }, }, 'cpu', ],
103 expect
=> { pending
=> { delete => 'cpu', }, },
108 method => 'remove_from_pending_delete', # $conf, $key
110 { # simple test addition to of a pending deletion to the empty config
111 params
=> [ { pending
=> { delete => 'memory', } }, 'memory' ],
112 expect
=> { pending
=> {} },
116 params
=> [ { pending
=> { delete => 'cpu,!memory', } }, 'memory' ],
117 expect
=> { pending
=> { delete => 'cpu' } },
121 params
=> [ { pending
=> { delete => 'cpu', } }, 'memory' ],
122 expect
=> { pending
=> { delete => 'cpu' } },
127 ]; # tests definition end
131 my ($method, $test, $name) = @_;
133 fail
("incomplete test, params or expected missing")
134 if !exists $test->{params
} || !exists $test->{expect
};
136 my ($params, $expect) = $test->@{qw(params expect)};
138 my $res = eval { PVE
::AbstractConfig-
>$method(@$params) };
140 if (defined(my $param_id = $test->{map_expect_to_param_id
})) {
141 # it's a /cool/ hack, sometimes we have the interesting result in
142 # "call-by-reference" param, and the return value is just some "I did
143 # someting" or plain undef value. So allow to map the result to one of
145 $res = $params->[$param_id];
149 is ($err, $expect, $name);
151 is_deeply
($res, $expect, $name);
155 for my $test (@$tests) {
156 my $method = $test->{method} or fail
("missing AbstractConfig method to test") and next;
158 my $name = $test->{name
} // $method;
160 if (defined(my $subtests = $test->{subtests
})) {
161 subtest
$name => sub {
162 for my $subtest (@$subtests) {
163 do_test
($method, $subtest);
167 do_test
($method, $test, $name);
169 #my $expected = $test->{expect};