]> git.proxmox.com Git - pve-guest-common.git/blob - tests/abstract-config-tests.pl
d/control: depend on pve-storage with new storage_migrate
[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 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.
21
22 # note that the indentation 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 {
111 params => [ { pending => { delete => 'memory', } }, 'memory' ],
112 expect => { pending => {} },
113 },
114 {
115 params => [ { pending => { delete => 'cpu,!memory', } }, 'memory' ],
116 expect => { pending => { delete => 'cpu' } },
117 },
118 {
119 params => [ { pending => { delete => 'cpu', } }, 'memory' ],
120 expect => { pending => { delete => 'cpu' } },
121 },
122 ]
123 },
124 {
125 method => 'cleanup_pending', # $conf
126 subtests => [
127 { # want to delete opt which is not in config? -> all done, cleanup!
128 params => [{
129 pending => { delete => 'memory', }
130 }],
131 map_expect_to_param_id => 0,
132 expect => { pending => {} },
133 },
134 { # should do nothing, delete is pending and memory is set after all
135 params => [{
136 memory => 128,
137 pending => { delete => 'memory', }
138 }],
139 map_expect_to_param_id => 0,
140 expect => {
141 memory => 128,
142 pending => { delete => 'memory', }
143 },
144 },
145 { # the pending change is the same as the currents config value, cleanup pending!
146 params => [{
147 memory => 128,
148 pending => { memory => 128, }
149 }],
150 map_expect_to_param_id => 0,
151 expect => {
152 memory => 128,
153 pending => {}
154 },
155 },
156 ]
157 },
158 ]; # tests definition end
159
160
161 sub do_test($$;$) {
162 my ($method, $test, $name) = @_;
163
164 fail("incomplete test, params or expected missing")
165 if !exists $test->{params} || !exists $test->{expect};
166
167 my ($params, $expect) = $test->@{qw(params expect)};
168
169 my $res = eval { PVE::AbstractConfig->$method(@$params) };
170
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
175 # the parameters
176 $res = $params->[$param_id];
177 }
178
179 if (my $err = $@) {
180 is ($err, $expect, $name);
181 } else {
182 is_deeply($res, $expect, $name);
183 }
184 }
185
186 for my $test (@$tests) {
187 my $method = $test->{method} or fail("missing AbstractConfig method to test") and next;
188
189 my $name = $test->{name} // $method;
190
191 if (defined(my $subtests = $test->{subtests})) {
192 subtest $name => sub {
193 for my $subtest (@$subtests) {
194 do_test($method, $subtest);
195 }
196 }
197 } else {
198 do_test($method, $test, $name);
199 }
200 #my $expected = $test->{expect};
201 }
202
203 done_testing();