Debian Codegraph @

lemonldap-ng-common/t/01-Common-Conf.t

raw
1 use warnings;
2 use Time::Fake;
3 use t::TestConfBackend;
4
5 # Before `make install' is performed this script should be runnable with
6 # `make test'. After `make install' it should work as `perl Lemonldap-NG-Manager.t'
7
8 #########################
9
10 # change 'tests => 1' to 'tests => last_test_to_print';
11
12 use Test::More;
13 BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
14
15 #########################
16
17 # Insert your test code below, the Test::More module is used here so read
18 # its man page ( perldoc Test::More ) for help writing this test script.
19
20 my $h;
21 my $inifile = "t/lemonldap-ng.ini";
22 my $confsection = "configuration";
23
24 ok( (
25 Lemonldap::NG::Common::Conf->new( type => 'bad' ) == 0
26 and $Lemonldap::NG::Common::Conf::msg =~
27 /Error: failed to load Lemonldap::NG::Common::Conf::Backends::bad/
28 ),
29 'Bad module'
30 ) or print STDERR "Msg: $Lemonldap::NG::Common::Conf::msg\n";
31
32 $h = bless {}, 'Lemonldap::NG::Common::Conf';
33
34 ok( (
35 %$h = ( %$h, %{ $h->getLocalConf( $confsection, $inifile, 0 ) } )
36 and exists $h->{localStorage}
37 ),
38 "Read $inifile"
39 );
40
41 # A helper function for this test. It calls getConf on a confAccessor with
42 # supplied arguments.
43 # After getConf returns, it runs a series of tests on:
44 # * The resulting status message (msg)
45 # * The obtained configuration (keys)
46 # * How many times Conf::Backend methods were called (stats)
47 sub getConfTest {
48 local $Test::Builder::Level = $Test::Builder::Level + 1;
49 my ( $confAcc, $args, %args ) = @_;
50
51 # Store current stats
52 my %stats;
53 if ( ref( $args{stats} ) eq "HASH" ) {
54 while ( my ( $key, $value ) = each %{ $args{stats} } ) {
55 $stats{$key} = $t::TestConfBackend::stats{$key} || 0;
56 }
57 }
58
59 # Invoke getConf
60 $Lemonldap::NG::Common::Conf::msg = "";
61 note "Invoking getConf with args ", explain $args;
62
63 # The test suite runs as root in CI, we need to explicitely enable caching for root
64 my $conf = $confAcc->getConf($args);
65
66 # Test result keys
67 if ( ref( $args{keys} ) eq "HASH" ) {
68 while ( my ( $key, $value ) = each %{ $args{keys} } ) {
69 is( $conf->{$key}, $value, "Found expected $key in conf" );
70 }
71 }
72
73 # Test message
74 if ( $args{msg} ) {
75 like( $Lemonldap::NG::Common::Conf::msg,
76 $args{msg}, "Found expected message" );
77 }
78
79 # Test stats
80 if ( ref( $args{stats} ) eq "HASH" ) {
81 while ( my ( $key, $value ) = each %{ $args{stats} } ) {
82 is( $t::TestConfBackend::stats{$key} - $stats{$key},
83 $value, "$key counter increased by $value" );
84 }
85 }
86
87 }
88
89 subtest "Invalid backend configuration" => sub {
90 $Lemonldap::NG::Common::Conf::msg = "";
91 t::TestConfBackend::testReset();
92 t::TestConfBackend::fail_prereq();
93
94 ok( !Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
95 "Invalid configuration" );
96 like(
97 $Lemonldap::NG::Common::Conf::msg,
98 qr/Module was set to fail/,
99 "Found correct error message"
100 );
101 };
102
103 subtest "Cache expiration" => sub {
104
105 Time::Fake->reset;
106 t::TestConfBackend::testReset();
107
108 $Lemonldap::NG::Common::Conf::msg = "";
109 ok(
110 my $c =
111 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
112 "Valid configuration"
113 );
114 $c->{refLocalStorage}->Clear();
115
116 getConfTest(
117 $c, { local => 1 },
118 msg => qr/Get configuration 1/,
119 keys => { cfgNum => 1 },
120 stats => { lastCfg => 1, load => 1 }
121 );
122
123 # Publish new configuration
124 push @t::TestConfBackend::conf, { cfgNum => 2, newvalue => "new" };
125
126 # Cache is not expired yet, new value not available
127 getConfTest(
128 $c, { local => 1 },
129 msg => qr/Get configuration from cache without verification/,
130 keys => { newvalue => undef },
131 stats => { lastCfg => 0, load => 0 }
132 );
133
134 # Expire cache
135 Time::Fake->offset('+700s');
136
137 # Cache expired, we fetch conf again and expose new value
138 getConfTest(
139 $c, { local => 1 },
140 msg => qr/Get configuration 2/,
141 keys => { cfgNum => 2, newvalue => 'new' },
142 stats => { lastCfg => 1, load => 1 }
143 );
144 };
145
146 subtest "Configuration reload" => sub {
147
148 Time::Fake->reset;
149 t::TestConfBackend::testReset();
150
151 $Lemonldap::NG::Common::Conf::msg = "";
152 ok(
153 my $c =
154 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
155 "Valid configuration"
156 );
157 $c->{refLocalStorage}->Clear();
158
159 # Load initial configuration in cache
160 getConfTest(
161 $c, { local => 1 },
162 msg => qr/Get configuration 1/,
163 keys => { cfgNum => 1 },
164 stats => { lastCfg => 1, load => 1 }
165 );
166
167 # Publish new configuration
168 push @t::TestConfBackend::conf, { cfgNum => 2, newvalue => "new" };
169
170 # Force reload
171 getConfTest(
172 $c, { local => 0 },
173 msg => qr/Get configuration 2/,
174 keys => { cfgNum => 2, newvalue => "new" },
175 stats => { lastCfg => 1, load => 1 }
176 );
177
178 # A new cache enabled access sees the new values
179 getConfTest(
180 $c, { local => 1 },
181 msg => qr/Get configuration from cache without verification/,
182 keys => { cfgNum => 2, newvalue => "new" },
183 stats => { lastCfg => 0, load => 0 }
184 );
185 };
186
187 subtest "Configuration cache manual clear" => sub {
188
189 Time::Fake->reset;
190 t::TestConfBackend::testReset();
191
192 $Lemonldap::NG::Common::Conf::msg = "";
193 ok(
194 my $c =
195 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
196 "Valid configuration"
197 );
198 $c->{refLocalStorage}->Clear();
199
200 # Load initial configuration in cache
201 getConfTest(
202 $c, { local => 1 },
203 msg => qr/Get configuration 1/,
204 keys => { cfgNum => 1 },
205 stats => { lastCfg => 1, load => 1 }
206 );
207
208 # Publish new configuration
209 $t::TestConfBackend::conf[0]->{'newvalue'} = "new";
210
211 # Not visible yet
212 getConfTest(
213 $c, { local => 1 },
214 msg => qr/Get configuration from cache without verification/,
215 keys => { cfgNum => 1, newvalue => undef },
216 stats => { lastCfg => 0, load => 0 }
217 );
218
219 # Force update-cache
220 getConfTest(
221 $c, { noCache => 2 },
222 msg => qr/Get configuration 1/,
223 keys => { cfgNum => 1, newvalue => "new" },
224 stats => { lastCfg => 1, load => 1 }
225 );
226
227 # Read again directly from cache
228 getConfTest(
229 $c, { local => 1 },
230 msg => qr/Get configuration from cache without verification/,
231 keys => { cfgNum => 1, newvalue => "new" },
232 stats => { lastCfg => 0, load => 0 }
233 );
234 };
235
236 subtest "local param behavior" => sub {
237
238 Time::Fake->reset;
239 t::TestConfBackend::testReset();
240
241 $Lemonldap::NG::Common::Conf::msg = "";
242 ok(
243 my $c =
244 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
245 "Valid configuration"
246 );
247 $c->{refLocalStorage}->Clear();
248
249 # Initial access with a local param
250 getConfTest(
251 $c, { local => 1, localPrm => { myLocalPrm => 1 } },
252 msg => qr/Get configuration 1/,
253 keys => { cfgNum => 1, myLocalPrm => 1 },
254 stats => { lastCfg => 1, load => 1 }
255 );
256
257 # Store new configuration
258 push @t::TestConfBackend::conf, { cfgNum => 2, newvalue => "new" };
259
260 # New access, local param should be kept
261 getConfTest(
262 $c, {},
263 msg => qr/Get configuration 2/,
264 keys => { cfgNum => 2, myLocalPrm => 1 },
265 stats => { lastCfg => 1, load => 1 }
266 );
267
268 ok(
269 my $c2 =
270 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
271 "Another confAccess object from another process"
272 );
273
274 # Store new configuration
275 push @t::TestConfBackend::conf, { cfgNum => 3 };
276
277 # Update cache from another process wihout localPrm
278 getConfTest(
279 $c2, {},
280 msg => qr/Get configuration 3/,
281 keys => { cfgNum => 3, myLocalPrm => undef },
282 stats => { lastCfg => 1, load => 1 }
283 );
284
285 # First process gets config again
286 getConfTest(
287 $c, { local => 1 },
288 msg => qr/Get configuration from cache without verification/,
289 keys => { cfgNum => 3, myLocalPrm => 1 },
290 stats => { lastCfg => 0, load => 0 }
291 );
292 };
293
294 subtest "Backend failure: lastCfg falls back to cached cfgNum" => sub {
295
296 Time::Fake->reset;
297 t::TestConfBackend::testReset();
298
299 $Lemonldap::NG::Common::Conf::msg = "";
300 ok(
301 my $c =
302 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
303 "Valid configuration"
304 );
305 $c->{refLocalStorage}->Clear();
306
307 # Initial load populates the cache
308 getConfTest(
309 $c, { local => 1 },
310 msg => qr/Get configuration 1/,
311 keys => { cfgNum => 1 },
312 stats => { lastCfg => 1, load => 1 }
313 );
314
315 # Backend goes down
316 t::TestConfBackend::fail_lastCfg();
317
318 # Force a remote check: lastCfg fails, but the fallback returns the
319 # cached cfgNum so getConf serves the cached conf without calling load()
320 getConfTest(
321 $c, { local => 0 },
322 msg => qr/falling back to cached configuration/,
323 keys => { cfgNum => 1 },
324 stats => { lastCfg => 1, load => 0 }
325 );
326
327 # Backend recovers, new conf appears
328 t::TestConfBackend::recover();
329 push @t::TestConfBackend::conf, { cfgNum => 2, newvalue => "new" };
330
331 getConfTest(
332 $c, { local => 0 },
333 msg => qr/Get configuration 2/,
334 keys => { cfgNum => 2, newvalue => "new" },
335 stats => { lastCfg => 1, load => 1 }
336 );
337 };
338
339 subtest "noCache disables the fallback (Manager / write paths)" => sub {
340
341 Time::Fake->reset;
342 t::TestConfBackend::testReset();
343
344 $Lemonldap::NG::Common::Conf::msg = "";
345 ok(
346 my $c =
347 Lemonldap::NG::Common::Conf->new( confFile => 't/lemonldap-ng.ini' ),
348 "Valid configuration"
349 );
350 $c->{refLocalStorage}->Clear();
351
352 # Populate the cache
353 getConfTest(
354 $c, { local => 1 },
355 msg => qr/Get configuration 1/,
356 keys => { cfgNum => 1 }
357 );
358
359 # Backend down: with noCache, no fallback, lastCfg must return 0
360 t::TestConfBackend::fail_lastCfg();
361 $Lemonldap::NG::Common::Conf::msg = "";
362 is( $c->lastCfg( { noCache => 1 } ),
363 0, "lastCfg with noCache returns 0 when backend is down" );
364 unlike(
365 $Lemonldap::NG::Common::Conf::msg,
366 qr/falling back to cached configuration/,
367 "No fallback message when noCache is set"
368 );
369
370 # Same path through getConf({ noCache => 1 }) — Manager-style call
371 $Lemonldap::NG::Common::Conf::msg = "";
372 my $conf = $c->getConf( { noCache => 1 } );
373 unlike(
374 $Lemonldap::NG::Common::Conf::msg,
375 qr/falling back to cached configuration/,
376 "getConf with noCache propagates the noCache flag to lastCfg"
377 );
378 };
379
380 done_testing;