]>
git.proxmox.com Git - pve-guest-common.git/blob - tests/abstract-config-tests.pl
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 specify 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 indentation 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
111 params
=> [ { pending
=> { delete => 'memory', } }, 'memory' ],
112 expect
=> { pending
=> {} },
115 params
=> [ { pending
=> { delete => 'cpu,!memory', } }, 'memory' ],
116 expect
=> { pending
=> { delete => 'cpu' } },
119 params
=> [ { pending
=> { delete => 'cpu', } }, 'memory' ],
120 expect
=> { pending
=> { delete => 'cpu' } },
125 method => 'cleanup_pending', # $conf
127 { # want to delete opt which is not in config? -> all done, cleanup!
129 pending
=> { delete => 'memory', }
131 map_expect_to_param_id
=> 0,
132 expect
=> { pending
=> {} },
134 { # should do nothing, delete is pending and memory is set after all
137 pending
=> { delete => 'memory', }
139 map_expect_to_param_id
=> 0,
142 pending
=> { delete => 'memory', }
145 { # the pending change is the same as the currents config value, cleanup pending!
148 pending
=> { memory
=> 128, }
150 map_expect_to_param_id
=> 0,
158 ]; # tests definition end
162 my ($method, $test, $name) = @_;
164 fail
("incomplete test, params or expected missing")
165 if !exists $test->{params
} || !exists $test->{expect
};
167 my ($params, $expect) = $test->@{qw(params expect)};
169 my $res = eval { PVE
::AbstractConfig-
>$method(@$params) };
171 if (defined(my $param_id = $test->{map_expect_to_param_id
})) {
172 # it's a /cool/ hack, sometimes we have the interesting result in
173 # "call-by-reference" param, and the return value is just some "I did
174 # something" or plain undef value. So allow to map the result to one of
176 $res = $params->[$param_id];
180 is ($err, $expect, $name);
182 is_deeply
($res, $expect, $name);
186 for my $test (@$tests) {
187 my $method = $test->{method} or fail
("missing AbstractConfig method to test") and next;
189 my $name = $test->{name
} // $method;
191 if (defined(my $subtests = $test->{subtests
})) {
192 subtest
$name => sub {
193 for my $subtest (@$subtests) {
194 do_test
($method, $subtest);
198 do_test
($method, $test, $name);
200 #my $expected = $test->{expect};