]> git.proxmox.com Git - pve-guest-common.git/blob - tests/abstract-config-tests.pl
tests: add map_expect_to_param_id feature for checking side-effects
[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 # 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.
21
22 # note that the indentaion level below is "wrong" by design
23 my $tests = [
24 {
25 method => 'parse_pending_delete',
26 subtests => [
27 {
28 params => [ "memory,cpu" ],
29 expect => {
30 cpu => { force => 0, },
31 memory => { force => 0, },
32 },
33 },
34 {
35 params => [ "memory;cpu,!mp0" ],
36 expect => {
37 cpu => { force => 0, },
38 memory => { force => 0, },
39 mp0 => { force => 1, },
40 },
41 },
42 {
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, },
49 },
50 },
51 {
52 params => [ "!!memory" ],
53 expect => { # we have no double negation, only simple stuff
54 '!memory' => { force => 1, },
55 },
56 },
57 {
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, },
62 },
63 },
64 ]
65 },
66 {
67 method => 'print_pending_delete',
68 subtests => [
69 {
70 params => [{
71 cpu => { force => 0, },
72 memory => { force => 0, },
73 }],
74 expect => "cpu,memory",
75 },
76 {
77 params => [{ # we have no double negation, only simple stuff
78 '!memory' => { force => 1, },
79 }],
80 expect => "!!memory",
81 },
82 ]
83 },
84 {
85 method => 'add_to_pending_delete', # $conf, $key, $force
86 subtests => [
87 { # simple test addition to of a pending deletion to the empty config
88 params => [ {}, 'memory', ],
89 expect => { pending => { delete => 'memory', }, },
90 ,
91 },
92 {
93 params => [ { pending => { delete => 'cpu', }, }, 'memory', 1 ],
94 expect => { pending => { delete => 'cpu,!memory', }, },
95 ,
96 },
97 {
98 params => [ { pending => { delete => 'cpu', }, }, 'cpu', 1 ],
99 expect => { pending => { delete => '!cpu', }, },
100 },
101 {
102 params => [ { pending => { delete => 'cpu', }, }, 'cpu', ],
103 expect => { pending => { delete => 'cpu', }, },
104 },
105 ]
106 },
107 {
108 method => 'remove_from_pending_delete', # $conf, $key
109 subtests => [
110 { # simple test addition to of a pending deletion to the empty config
111 params => [ { pending => { delete => 'memory', } }, 'memory' ],
112 expect => { pending => {} },
113 ,
114 },
115 {
116 params => [ { pending => { delete => 'cpu,!memory', } }, 'memory' ],
117 expect => { pending => { delete => 'cpu' } },
118 ,
119 },
120 {
121 params => [ { pending => { delete => 'cpu', } }, 'memory' ],
122 expect => { pending => { delete => 'cpu' } },
123 ,
124 },
125 ]
126 },
127 ]; # tests definition end
128
129
130 sub do_test($$;$) {
131 my ($method, $test, $name) = @_;
132
133 fail("incomplete test, params or expected missing")
134 if !exists $test->{params} || !exists $test->{expect};
135
136 my ($params, $expect) = $test->@{qw(params expect)};
137
138 my $res = eval { PVE::AbstractConfig->$method(@$params) };
139
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
144 # the parameters
145 $res = $params->[$param_id];
146 }
147
148 if (my $err = $@) {
149 is ($err, $expect, $name);
150 } else {
151 is_deeply($res, $expect, $name);
152 }
153 }
154
155 for my $test (@$tests) {
156 my $method = $test->{method} or fail("missing AbstractConfig method to test") and next;
157
158 my $name = $test->{name} // $method;
159
160 if (defined(my $subtests = $test->{subtests})) {
161 subtest $name => sub {
162 for my $subtest (@$subtests) {
163 do_test($method, $subtest);
164 }
165 }
166 } else {
167 do_test($method, $test, $name);
168 }
169 #my $expected = $test->{expect};
170 }
171
172 done_testing();