Debian Codegraph @

lemonldap-ng-common/t/02-Common-Conf-Placeholders.t

raw
1 use warnings;
2 use strict;
3 use Test::More tests => 17;
4 use Data::Dumper;
5
6 BEGIN { use_ok('Lemonldap::NG::Common::Conf') }
7
8 use File::Temp;
9 my $dir = File::Temp::tempdir( CLEANUP => 1 );
10 my $h;
11
12 ok(
13 $h = new Lemonldap::NG::Common::Conf( {
14 type => 'File',
15 dirName => $dir,
16 }
17 ),
18 'type => file',
19 );
20 my $conf = {
21 cfgNum => 1,
22 test => '%SERVERENV:A%',
23 test2 => '%SERVERENV:B% %SERVERENV:C%',
24 '%SERVERENV:MYKEY%' => {
25 test => 'Test: %SERVERENV:A%',
26 array => [ 'a', '%SERVERENV:B% %SERVERENV:C%', ],
27 },
28 };
29
30 $ENV{A} = 'Aa';
31 $ENV{B} = 'Bb';
32 $ENV{C} = 'Cc';
33 $ENV{MYKEY} = 'MyKey';
34
35 ok( $h->store($conf) == 1, "Conf is stored" )
36 or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
37 my $cfg;
38 ok( $cfg = $h->getConf( { cfgNum => 1 } ), "Conf can be read" )
39 or print STDERR $Lemonldap::NG::Common::Conf::msg;
40 ok( $cfg->{test} eq '%SERVERENV:A%',
41 '%SERVERENV:A% is not substitued into Aa without useServerEnv' )
42 or print STDERR "Expect $cfg->{test} eq %SERVERENV:A%\n";
43
44 ok(
45 $h = new Lemonldap::NG::Common::Conf( {
46 type => 'File',
47 dirName => "t/",
48 useServerEnv => 1,
49 }
50 ),
51 'type => file',
52 );
53 ok( $h->store($conf) == 1, "Conf is stored" )
54 or print STDERR "$Lemonldap::NG::Common::Conf::msg $!";
55
56 ok( $cfg = $h->getConf( { cfgNum => 1 } ), "Conf can be read" )
57 or print STDERR $Lemonldap::NG::Common::Conf::msg;
58 ok( $cfg->{test} eq 'Aa', '%SERVERENV:A% is substitued into Aa' )
59 or print STDERR "Expect $cfg->{test} eq Aa\n";
60 ok( $cfg->{test2} eq 'Bb Cc',
61 '%SERVERENV:B% %SERVERENV:C% is substitued into Bb Cc' )
62 or print STDERR "Expect $cfg->{test} eq Aa\n";
63
64 ok( ( !$cfg->{'%SERVERENV:MYKEY%'} and $cfg->{MyKey} ),
65 'Keyname is transformed' );
66 ok( (
67 $cfg->{MyKey}->{array}->[0] eq 'a'
68 and $cfg->{MyKey}->{array}->[1] eq 'Bb Cc'
69 ),
70 'Values are substitued into arrays'
71 );
72
73 ok( $cfg = $h->getConf( { cfgNum => 1, raw => 1 } ), 'Get raw conf' );
74 ok( $cfg->{test} eq '%SERVERENV:A%',
75 '%SERVERENV:A% is not substitued into Aa in raw mode' )
76 or print STDERR "Expect $cfg->{test} eq %SERVERENV:A%\n";
77
78 subtest "Check useServerEnv in config file in localConf" => sub {
79
80 my ( $fh, $filename ) =
81 File::Temp::tempfile( "lemonldap-ng.ini.XXXXXX", DIR => $dir );
82 print $fh <<\EOF;
83 [all]
84 useSafeJail = 1
85 logLevel = notice
86 checkTime = 1
87 myA = %SERVERENV:A%
88 myS = %SECRET:myS%
89
90 [configuration]
91 useServerEnv=1
92 configPlaceHolderModules=SECRET=t::Secrets
93 secretPlaceholderConfig=1
94 type=t::TestConfBackend
95 accessoption=%SERVERENV:MYKEY%
96
97 EOF
98 close($fh);
99
100 my $h = Lemonldap::NG::Common::Conf->new( {
101 confFile => $filename,
102 }
103 );
104 is( $h->{accessoption}, "MyKey",
105 'configuration access option correctly replaced' );
106 my $localConf = $h->getLocalConf();
107 {
108 is( $localConf->{myA}, "Aa", 'configuration variable replaced' );
109 is( $localConf->{myS}, "secret-myS",
110 'configuration variable replaced' );
111 }
112 };
113
114 subtest "Check useServerEnv in build option in localConf" => sub {
115
116 my ( $fh, $filename ) =
117 File::Temp::tempfile( "lemonldap-ng.ini.XXXXXX", DIR => $dir );
118 print $fh <<\EOF;
119 [all]
120 useSafeJail = 1
121 logLevel = notice
122 checkTime = 1
123 myA = %SERVERENV:A%
124 myS = %SECRET:myS%
125
126 [configuration]
127 type=t::TestConfBackend
128 accessoption=%SERVERENV:MYKEY%
129
130 EOF
131 close($fh);
132
133 my $h = Lemonldap::NG::Common::Conf->new( {
134 confFile => $filename,
135 useServerEnv => 1,
136 }
137 );
138 is( $h->{accessoption}, "MyKey",
139 'configuration access option correctly replaced' );
140 my $localConf = $h->getLocalConf();
141 {
142 is( $localConf->{myA}, "Aa", 'configuration variable replaced' );
143 }
144 };
145
146 subtest "No useServerEnv in localConf" => sub {
147
148 my ( $fh, $filename ) =
149 File::Temp::tempfile( "lemonldap-ng.ini.XXXXXX", DIR => $dir );
150 print $fh <<\EOF;
151 [all]
152 useSafeJail = 1
153 logLevel = notice
154 checkTime = 1
155 myA = %SERVERENV:A%
156 myS = %SECRET:myS%
157
158 [configuration]
159 type=t::TestConfBackend
160 accessoption=%SERVERENV:MYKEY%
161
162 EOF
163 close($fh);
164
165 my $h = Lemonldap::NG::Common::Conf->new( {
166 confFile => $filename,
167 }
168 );
169 is( $h->{accessoption}, "%SERVERENV:MYKEY%",
170 'configuration access option not replaced' );
171 my $localConf = $h->getLocalConf();
172 {
173 is( $localConf->{myA}, "%SERVERENV:A%",
174 'configuration variable not replaced' );
175 is( $localConf->{myS}, "%SECRET:myS%",
176 'configuration variable not replaced' );
177 }
178 };
179
180 unlink 't/lmConf-1.json';