Debian Codegraph @

lemonldap-ng-common/t/TestConfBackend.pm

raw
1 package t::TestConfBackend;
2
3 use strict;
4 use Lemonldap::NG::Common::Conf::Constants;
5 use Test::More;
6 our %stats;
7 our %defaultConf = (
8 cfgNum => 1,
9 cfgDate => time,
10 cfgAuthor => 'LLNG Team',
11 );
12 our @conf = ( \%defaultConf );
13 our $fail_prereq = 0;
14 our $fail_lastCfg = 0;
15 our $fail_load = 0;
16
17 our $VERSION = '2.19.0';
18
19 sub prereq {
20 my $self = shift;
21
22 if ($fail_prereq) {
23 $Lemonldap::NG::Common::Conf::msg .= "Module was set to fail\n";
24 return 0;
25 }
26 return 1;
27 }
28
29 sub available {
30 $stats{'available'} += 1;
31 my @res = map { $_->{cfgNum} } @conf;
32 return @res;
33 }
34
35 sub lastCfg {
36 $stats{'lastCfg'} += 1;
37 return undef if $fail_lastCfg;
38 my $res = @conf[-1]->{cfgNum};
39 return $res;
40 }
41
42 sub isLocked {
43 $stats{'isLocked'} += 1;
44 return 1;
45 }
46
47 sub unlock {
48 $stats{'unlock'} += 1;
49 return 1;
50 }
51
52 sub store {
53 $stats{'store'} += 1;
54 $Lemonldap::NG::Common::Conf::msg = 'Read-only backend!';
55 return DATABASE_LOCKED;
56 }
57
58 sub load {
59 my ( $self, $num ) = @_;
60 $stats{'load'} += 1;
61 return undef if $fail_load;
62 my @res = grep { $_->{cfgNum} == $num } @conf;
63 return { %{ $res[0] } };
64 }
65
66 sub delete {
67 $stats{'delete'} += 1;
68 $Lemonldap::NG::Common::Conf::msg = 'Read-only backend!';
69 return 0;
70 }
71
72 # Reset state, used by tests
73 sub testReset {
74 @conf = ( \%defaultConf );
75 %stats = ();
76 $fail_prereq = 0;
77 $fail_lastCfg = 0;
78 $fail_load = 0;
79 }
80
81 sub fail_prereq { $fail_prereq = 1; }
82 sub fail_lastCfg { $fail_lastCfg = 1; }
83 sub fail_load { $fail_load = 1; }
84 sub recover { $fail_lastCfg = 0; $fail_load = 0; }
85
86 1;