]>
Commit | Line | Data |
---|---|---|
fe824061 TL |
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 | }, | |
c29213f0 TL |
63 | { |
64 | method => 'print_pending_delete', | |
65 | subtests => [ | |
66 | { | |
67 | params => [{ | |
68 | cpu => { force => 0, }, | |
69 | memory => { force => 0, }, | |
70 | }], | |
6a6ad855 | 71 | expect => "cpu,memory", |
c29213f0 TL |
72 | }, |
73 | { | |
74 | params => [{ # we have no double negation, only simple stuff | |
75 | '!memory' => { force => 1, }, | |
76 | }], | |
77 | expect => "!!memory", | |
78 | }, | |
79 | ] | |
80 | }, | |
6f38ab5a TL |
81 | { |
82 | method => 'add_to_pending_delete', # $conf, $key, $force | |
83 | subtests => [ | |
84 | { # simple test addition to of a pending deletion to the empty config | |
85 | params => [ {}, 'memory', ], | |
86 | expect => { pending => { delete => 'memory', }, }, | |
87 | , | |
88 | }, | |
89 | { | |
90 | params => [ { pending => { delete => 'cpu', }, }, 'memory', 1 ], | |
91 | expect => { pending => { delete => 'cpu,!memory', }, }, | |
92 | , | |
93 | }, | |
94 | { | |
95 | params => [ { pending => { delete => 'cpu', }, }, 'cpu', 1 ], | |
96 | expect => { pending => { delete => '!cpu', }, }, | |
97 | }, | |
98 | { | |
99 | params => [ { pending => { delete => 'cpu', }, }, 'cpu', ], | |
100 | expect => { pending => { delete => 'cpu', }, }, | |
101 | }, | |
102 | ] | |
103 | }, | |
e33cb615 TL |
104 | { |
105 | method => 'remove_from_pending_delete', # $conf, $key | |
106 | subtests => [ | |
107 | { # simple test addition to of a pending deletion to the empty config | |
108 | params => [ { pending => { delete => 'memory', } }, 'memory' ], | |
109 | expect => { pending => {} }, | |
110 | , | |
111 | }, | |
112 | { | |
113 | params => [ { pending => { delete => 'cpu,!memory', } }, 'memory' ], | |
114 | expect => { pending => { delete => 'cpu' } }, | |
115 | , | |
116 | }, | |
117 | { | |
118 | params => [ { pending => { delete => 'cpu', } }, 'memory' ], | |
119 | expect => { pending => { delete => 'cpu' } }, | |
120 | , | |
121 | }, | |
122 | ] | |
123 | }, | |
fe824061 TL |
124 | ]; # tests definition end |
125 | ||
126 | ||
127 | sub do_test($$;$) { | |
128 | my ($method, $test, $name) = @_; | |
129 | ||
130 | fail("incomplete test, params or expected missing") | |
131 | if !exists $test->{params} || !exists $test->{expect}; | |
132 | ||
133 | my ($params, $expect) = $test->@{qw(params expect)}; | |
134 | ||
135 | my $res = eval { PVE::AbstractConfig->$method(@$params) }; | |
136 | if (my $err = $@) { | |
137 | is ($err, $expect, $name); | |
138 | } else { | |
139 | is_deeply($res, $expect, $name); | |
140 | } | |
141 | } | |
142 | ||
143 | for my $test (@$tests) { | |
144 | my $method = $test->{method} or fail("missing AbstractConfig method to test") and next; | |
145 | ||
146 | my $name = $test->{name} // $method; | |
147 | ||
148 | if (defined(my $subtests = $test->{subtests})) { | |
149 | subtest $name => sub { | |
150 | for my $subtest (@$subtests) { | |
151 | do_test($method, $subtest); | |
152 | } | |
153 | } | |
154 | } else { | |
155 | do_test($method, $test, $name); | |
156 | } | |
157 | #my $expected = $test->{expect}; | |
158 | } | |
159 | ||
160 | done_testing(); |