]> git.proxmox.com Git - pve-common.git/blob - test/section_config_test.pl
bump version to 8.2.1
[pve-common.git] / test / section_config_test.pl
1 #!/usr/bin/perl
2
3 use lib '../src';
4
5 package Conf;
6 use strict;
7 use warnings;
8
9 use Test::More;
10
11 use base qw(PVE::SectionConfig);
12
13 my $defaultData = {
14 propertyList => {
15 type => { description => "Section type." },
16 id => {
17 description => "ID",
18 type => 'string',
19 format => 'pve-configid',
20 maxLength => 64,
21 },
22 common => {
23 type => 'string',
24 description => 'common value',
25 maxLength => 512,
26 },
27 },
28 };
29
30 sub private {
31 return $defaultData;
32 }
33
34 sub expect_success {
35 my ($class, $filename, $expected, $raw, $allow_unknown) = @_;
36
37 my $res = $class->parse_config($filename, $raw, $allow_unknown);
38 delete $res->{digest};
39
40 is_deeply($res, $expected, $filename);
41
42 my $written = $class->write_config($filename, $res, $allow_unknown);
43 my $res2 = $class->parse_config($filename, $written, $allow_unknown);
44 delete $res2->{digest};
45
46 is_deeply($res, $res2, "$filename - verify rewritten data");
47 }
48
49 sub expect_fail {
50 my ($class, $filename, $expected, $raw) = @_;
51
52 eval { $class->parse_config($filename, $raw) };
53 die "test '$filename' succeeded unexpectedly\n" if !$@;
54 ok(1, "$filename should fail to parse");
55 }
56
57 package Conf::One;
58 use strict;
59 use warnings;
60
61 use base 'Conf';
62
63 sub type {
64 return 'one';
65 }
66
67 sub properties {
68 return {
69 field1 => {
70 description => 'Field One',
71 type => 'integer',
72 minimum => 3,
73 maximum => 9,
74 },
75 another => {
76 description => 'Another field',
77 type => 'string',
78 },
79 };
80 }
81
82 sub options {
83 return {
84 common => { optional => 1 },
85 field1 => {},
86 another => { optional => 1 },
87 };
88 }
89
90 package Conf::Two;
91 use strict;
92 use warnings;
93
94 use base 'Conf';
95
96 sub type {
97 return 'two';
98 }
99
100 sub properties {
101 return {
102 field2 => {
103 description => 'Field Two',
104 type => 'integer',
105 minimum => 3,
106 maximum => 9,
107 },
108 };
109 }
110
111 sub options {
112 return {
113 common => { optional => 1 },
114 field2 => {},
115 another => {},
116 };
117 }
118
119 package main;
120
121 use strict;
122 use warnings;
123
124 use Test::More;
125
126 Conf::One->register();
127 Conf::Two->register();
128 Conf->init();
129
130 # FIXME: allow development debug warnings?!
131 local $SIG{__WARN__} = sub { die @_; };
132
133 my sub enum {
134 my $n = 1;
135 return { map { $_ => $n++ } @_ };
136 }
137
138 Conf->expect_success(
139 'test1',
140 {
141 ids => {
142 t1 => {
143 type => 'one',
144 common => 'foo',
145 field1 => 3,
146 },
147 t2 => {
148 type => 'one',
149 common => 'foo2',
150 field1 => 4,
151 another => 'more-text',
152 },
153 t3 => {
154 type => 'two',
155 field2 => 5,
156 another => 'even more text',
157 },
158 },
159 order => { t1 => 1, t2 => 2, t3 => 3 },
160 },
161 <<"EOF");
162 one: t1
163 common foo
164 field1 3
165
166 one: t2
167 common foo2
168 field1 4
169 another more-text
170
171 two: t3
172 field2 5
173 another even more text
174 EOF
175
176 my $with_unknown_data = {
177 ids => {
178 t1 => {
179 type => 'one',
180 common => 'foo',
181 field1 => 3,
182 },
183 t2 => {
184 type => 'one',
185 common => 'foo2',
186 field1 => 4,
187 another => 'more-text',
188 },
189 t3 => {
190 type => 'two',
191 field2 => 5,
192 another => 'even more text',
193 },
194 invalid => {
195 type => 'bad',
196 common => 'omg',
197 },
198 },
199 order => enum(qw(t1 t2 invalid t3)),
200 };
201 my $with_unknown_text = <<"EOF";
202 one: t1
203 common foo
204 field1 3
205
206 one: t2
207 common foo2
208 field1 4
209 another more-text
210
211 bad: invalid
212 common omg
213
214 two: t3
215 field2 5
216 another even more text
217 EOF
218
219 Conf->expect_fail('unknown-forbidden', $with_unknown_data, $with_unknown_text);
220 Conf->expect_success('unknown-allowed', $with_unknown_data, $with_unknown_text, 1);
221
222
223 done_testing();
224
225 1;