| 1 |
# Base library for tests |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Data::Dumper; |
| 5 |
use File::Copy; |
| 6 |
use JSON; |
| 7 |
use File::Temp qw/ tempdir /; |
| 8 |
use IO::String; |
| 9 |
|
| 10 |
use_ok('Lemonldap::NG::Manager::Cli::Lib'); |
| 11 |
|
| 12 |
our $tmpdir = tempdir( DIR => "t/", CLEANUP => 1 ); |
| 13 |
|
| 14 |
mkdir("$tmpdir/conf"); |
| 15 |
mkdir("$tmpdir/sessions"); |
| 16 |
|
| 17 |
copy_subst_file( "t/conf/lmConf-1.json", "conf/lmConf-1.json" ); |
| 18 |
copy_subst_file( "t/lemonldap-ng.ini", "lemonldap-ng.ini" ); |
| 19 |
copy_subst_file( "t/lemonldap-ng-DBI-conf.ini", "lemonldap-ng-DBI-conf.ini" ); |
| 20 |
|
| 21 |
sub substitute_io_handle { |
| 22 |
my ($filename) = @_; |
| 23 |
return get_conf_body_from_fixture($filename); |
| 24 |
} |
| 25 |
|
| 26 |
# Reads a configuration fixture file, replace TMPDIR by the current tmpdir, and |
| 27 |
# apply optional transformation to the decoded JSON data structure |
| 28 |
sub get_conf_body_from_fixture { |
| 29 |
my ( $filename, $transformation ) = @_; |
| 30 |
|
| 31 |
open( my $fh, "<", $filename ); |
| 32 |
my $file_content = do { local $/; <$fh> }; |
| 33 |
close($fh); |
| 34 |
$file_content =~ s/TMPDIR/$main::tmpdir/g; |
| 35 |
|
| 36 |
if ($transformation) { |
| 37 |
my $conf_data = decode_json($file_content); |
| 38 |
$transformation->($conf_data); |
| 39 |
$file_content = encode_json($conf_data); |
| 40 |
} |
| 41 |
|
| 42 |
return ( length($file_content), IO::String->new($file_content) ); |
| 43 |
} |
| 44 |
|
| 45 |
sub copy_subst_file { |
| 46 |
my ( $ini_file, $destname ) = @_; |
| 47 |
|
| 48 |
open my $in, '<', $ini_file or die "Can't open input: $!"; |
| 49 |
my $conf_content = do { local $/; <$in> }; |
| 50 |
close $in; |
| 51 |
|
| 52 |
# Perform regex substitution |
| 53 |
$conf_content =~ s#TMPDIR#$main::tmpdir#g; |
| 54 |
|
| 55 |
# Write to output file |
| 56 |
open my $out, '>', "$main::tmpdir/$destname" |
| 57 |
or die "Can't open output: $!"; |
| 58 |
print $out $conf_content; |
| 59 |
close $out; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
# Legacy way to obtain a test Manager client |
| 64 |
our $client; |
| 65 |
|
| 66 |
sub client { |
| 67 |
if ( !$client ) { |
| 68 |
ok( $client = LLNG::Manager::Test->new, 'Client object' ); |
| 69 |
count(1); |
| 70 |
} |
| 71 |
return $client; |
| 72 |
} |
| 73 |
|
| 74 |
our $count = 1; |
| 75 |
|
| 76 |
sub count { |
| 77 |
my $c = shift; |
| 78 |
$count += $c if ($c); |
| 79 |
return $count; |
| 80 |
} |
| 81 |
|
| 82 |
1; |
| 83 |
|
| 84 |
package LLNG::Manager::Test; |
| 85 |
|
| 86 |
use strict; |
| 87 |
use Mouse; |
| 88 |
use IO::String; |
| 89 |
|
| 90 |
extends 'Lemonldap::NG::Common::PSGI::Cli::Lib'; |
| 91 |
|
| 92 |
our $defaultIni = { protection => "none" }; |
| 93 |
|
| 94 |
has app => ( |
| 95 |
is => 'rw', |
| 96 |
isa => 'CodeRef', |
| 97 |
); |
| 98 |
|
| 99 |
has iniFile => ( is => 'ro', default => "$main::tmpdir/lemonldap-ng.ini" ); |
| 100 |
|
| 101 |
has ini => ( is => 'rw', ); |
| 102 |
has p => ( is => 'rw', ); |
| 103 |
|
| 104 |
sub BUILD { |
| 105 |
my ($self) = @_; |
| 106 |
my $ini = $self->ini; |
| 107 |
foreach my $k ( keys %$defaultIni ) { |
| 108 |
$ini->{$k} //= $defaultIni->{$k}; |
| 109 |
} |
| 110 |
if ( $ENV{DEBUG} ) { |
| 111 |
$ini->{logLevel} = 'debug'; |
| 112 |
} |
| 113 |
if ( $ENV{LLNGLOGLEVEL} ) { |
| 114 |
$ini->{logLevel} = $ENV{LLNGLOGLEVEL}; |
| 115 |
} |
| 116 |
|
| 117 |
$ini->{configStorage} = { confFile => $self->iniFile }; |
| 118 |
$self->ini($ini); |
| 119 |
|
| 120 |
main::ok( $self->p( Lemonldap::NG::Manager->new($ini) ), 'Manager object' ); |
| 121 |
main::ok( $self->p->init($ini), 'Init' ); |
| 122 |
main::ok( $self->app( $self->p->run() ), 'Manager app' ); |
| 123 |
main::count(3); |
| 124 |
return $self; |
| 125 |
} |
| 126 |
|
| 127 |
1; |