Fix CLI unit tests (#2103)

This commit is contained in:
Maxime Besson 2020-02-24 14:29:31 +01:00
parent 53ac31e3c3
commit fd2747b1e4
3 changed files with 101 additions and 90 deletions

1
debian/control vendored
View File

@ -42,6 +42,7 @@ Build-Depends-Indep: libapache-session-perl <!nocheck>,
libstring-random-perl <!nocheck>, libstring-random-perl <!nocheck>,
libtest-mockobject-perl <!nocheck>, libtest-mockobject-perl <!nocheck>,
libtest-pod-perl <!nocheck>, libtest-pod-perl <!nocheck>,
libtest-output-perl <!nocheck>,
libtext-unidecode-perl <!nocheck>, libtext-unidecode-perl <!nocheck>,
libtime-fake-perl <!nocheck>, libtime-fake-perl <!nocheck>,
libunicode-string-perl <!nocheck>, libunicode-string-perl <!nocheck>,

View File

@ -1,104 +1,113 @@
use Test::More; use Test::More;
use Test::Output;
use JSON; use JSON;
use strict; use strict;
require 't/test-lib.pm'; require 't/test-lib.pm';
my $tests = 14; my $tests = 17;
use_ok('Lemonldap::NG::Common::Cli'); use_ok('Lemonldap::NG::Common::Cli');
use_ok('Lemonldap::NG::Manager::Cli'); use_ok('Lemonldap::NG::Manager::Cli');
&cleanConfFiles; &cleanConfFiles;
SKIP: { sub llclient {
eval 'use Test::Output;'; return Lemonldap::NG::Manager::Cli->new( iniFile => 't/lemonldap-ng.ini' );
if ($@) { }
skip 'Test::Output is missing, skipping', $tests - 2;
}
my $client =
Lemonldap::NG::Manager::Cli->new( iniFile => 't/lemonldap-ng.ini' );
my $commonClient =
Lemonldap::NG::Common::Cli->new( iniFile => 't/lemonldap-ng.ini' );
my @cmd;
my $res;
# Test 'set' command sub llcommonClient {
@cmd = qw(-yes 1 set notification 1); return Lemonldap::NG::Common::Cli->new( iniFile => 't/lemonldap-ng.ini' );
Capture::Tiny::capture_stdout( sub { $client->run(@cmd) } ); }
# Test 'get' command my @cmd;
@cmd = qw(get notification); my $res;
$res = Capture::Tiny::capture_stdout( sub { $client->run(@cmd) } );
ok( $res =~ /^notification\s+=\s+1$/, '"get notification" OK' ) # Test 'set' command
@cmd = qw(-yes 1 set notification 1);
combined_like(
sub { llclient->run(@cmd) },
qr/Saved under/,
'"addKey" OK'
);
# Test 'get' command
@cmd = qw(get notification);
$res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
ok( $res =~ /^notification\s+=\s+1$/, '"get notification" OK' )
or diag " $res"; or diag " $res";
# Test 'addKey' command # Test 'addKey' command
@cmd = qw(-yes 1 addKey locationRules/test1.example.com ^/reject deny); @cmd = qw(-yes 1 addKey locationRules/test1.example.com ^/reject deny);
Test::Output::combined_like( combined_like(
sub { $client->run(@cmd) }, sub { llclient->run(@cmd) },
qr#'\^/reject' => 'deny'#s, qr/Saved under/,
'"addKey" OK' '"addKey" OK'
); );
# Test 'delKey' command # Test 'delKey' command
@cmd = qw(-yes 1 delKey locationRules/test1.example.com ^/reject); @cmd = qw(-yes 1 delKey locationRules/test1.example.com ^/reject);
Test::Output::combined_unlike( combined_unlike(
sub { $client->run(@cmd) }, sub { llclient->run(@cmd) },
qr#'\^/reject' => 'deny'#s, qr#'\^/reject' => 'deny'#s,
'"delKey" OK' '"delKey" OK'
); );
# Test 'get' command with key/subkey # Test 'get' command with key/subkey
@cmd = qw(get locationRules/test1.example.com); @cmd = qw(get locationRules/test1.example.com/default);
$res = Capture::Tiny::capture_stdout( sub { $client->run(@cmd) } ); $res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
ok( $res =~ m#(?:/logout|default)#, '"get key/subkey" OK' ) ok( $res =~ m#accept#, '"get key/subkey" OK' )
or diag "$res"; or diag "$res";
# Test 'set' command with key/subkey # Test 'set' command with key/subkey
@cmd = qw(-yes 1 set locationRules/test1.example.com/default deny); @cmd = qw(-yes 1 set locationRules/test1.example.com/default deny);
Capture::Tiny::capture_stdout( sub { $client->run(@cmd) } ); combined_like(
sub { llclient->run(@cmd) },
qr/Saved under/,
'"addKey" OK'
);
# Test 'save' command # Test 'save' command
@cmd = ('save'); @cmd = qw(-cfgNum 1 save);
$res = Capture::Tiny::capture_stdout( sub { $client->run(@cmd) } ); $res = Test::Output::stdout_from( sub { llclient->run(@cmd) } );
ok( $res =~ /^\s*(\{.*\})\s*$/s, '"save" result looks like JSON' ); ok( $res =~ /^\s*(\{.*\})\s*$/s, '"save" result looks like JSON' );
eval { JSON::from_json($res) }; my $j;
ok( not($@), ' result is JSON' ) or diag "error: $@"; eval { $j = JSON::from_json($res) };
is( $j->{cfgNum}, 1, "correct version number" );
ok( not($@), ' result is JSON' ) or diag "error: $@";
# Test 'restore' command # Test 'restore' command
close STDIN; my $tmpFile = File::Temp->new();
open STDIN, '<', \$res; print $tmpFile $res;
@cmd = ( 'restore', '-' ); @cmd = ( 'restore', $tmpFile->filename );
Test::Output::combined_like( sub { $client->run(@cmd) }, combined_like( sub { llclient->run(@cmd) },
qr/"cfgNum"\s*:\s*"3"/s, 'New config: 3' ); qr/"cfgNum"\s*:\s*\d*/s, 'New config' );
# Test 'set' command with force # Test 'set' command with force
@cmd = qw(-yes 1 -force 1 -cfgNum 2 set useSafeJail 0); @cmd = qw(-yes 1 -force 1 -cfgNum 2 set useSafeJail 0);
Test::Output::combined_like( combined_like(
sub { $client->run(@cmd) }, sub { llclient->run(@cmd) },
qr#cfgNum forced with 2#s, qr#cfgNum forced with 2#s,
'"Force cfgNum" OK' '"Force cfgNum" OK'
); );
# Test 'update-cache' command with force # Test 'info' command with force
@cmd = qw(update-cache); @cmd = qw(info);
Test::Output::combined_like( combined_like(
sub { $commonClient->run(@cmd) }, sub { llcommonClient->run(@cmd) },
qr#Cache updated to configuration 3#s,
'"update-cache" OK'
);
# Test 'info' command with force
@cmd = qw(info);
Test::Output::combined_like(
$res = sub { $commonClient->run(@cmd) },
qr#\bAuthor IP\b#s, qr#\bAuthor IP\b#s,
'"Author IP" OK' '"Author IP" OK'
); );
Test::Output::combined_like( $res = sub { $commonClient->run(@cmd) }, combined_like( sub { llcommonClient->run(@cmd) },
qr#\bLog\b#s, '"Log" OK' ); qr#\bLog\b#s, '"Log" OK' );
Test::Output::combined_like( $res = sub { $commonClient->run(@cmd) }, combined_like( sub { llcommonClient->run(@cmd) },
qr#\bVersion\b#s, '"Version" OK' ); qr#\bVersion\b#s, '"Version" OK' );
}
# Test 'rollback' command
@cmd = qw(rollback);
combined_like(
sub { llclient->run(@cmd) },
qr/Configuration \d+ has been rolled back/,
'"Author IP" OK'
);
count($tests); count($tests);
done_testing( count() ); done_testing( count() );

View File

@ -153,6 +153,7 @@ BuildRequires: perl(strict)
BuildRequires: perl(String::Random) BuildRequires: perl(String::Random)
BuildRequires: perl(Sys::Syslog) BuildRequires: perl(Sys::Syslog)
BuildRequires: perl(Test::MockObject) BuildRequires: perl(Test::MockObject)
BuildRequires: perl(Test::Output)
BuildRequires: perl(Test::Pod) >= 1.00 BuildRequires: perl(Test::Pod) >= 1.00
BuildRequires: perl(Text::Unidecode) BuildRequires: perl(Text::Unidecode)
BuildRequires: perl(Time::Fake) BuildRequires: perl(Time::Fake)