diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm index 12d429cfe..be6662bd9 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Cli.pm @@ -30,6 +30,7 @@ has force => ( is => 'rw', isa => 'Bool', default => 0 ); has logger => ( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr->logger } ); has userLogger => ( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr->userLogger } ); +has localConf => ( is => 'ro', lazy => 1, builder => sub { $_[0]->mgr } ); sub get { my ( $self, @keys ) = @_; @@ -275,13 +276,15 @@ sub _save { req => $self->req } ); - unless ( $parser->testNewConf() ) { - $self->logger->error("CLI: Configuration rejected with message: $parser->{message}"); + unless ( $parser->testNewConf( $self->localConf ) ) { + $self->logger->error( + "CLI: Configuration rejected with message: $parser->{message}"); printf STDERR "Modifications rejected: %s:\n", $parser->{message}; } my $saveParams = { force => $self->force }; if ( $self->force and $self->cfgNum ) { - $self->logger->debug("CLI: cfgNum forced with $self->cfgNum()"); + $self->logger->debug( "CLI: cfgNum forced with " . $self->cfgNum ); + print STDERR "cfgNum forced with ", $self->cfgNum; $saveParams->{cfgNum} = $self->cfgNum; $saveParams->{cfgNumFixed} = 1; } @@ -297,7 +300,8 @@ sub _save { my $s = $self->mgr->confAcc->saveConf( $new, %$saveParams ); if ( $s > 0 ) { - $self->logger->debug("CLI: Configuration $s has been saved by $new->{cfgAuthor}"); + $self->logger->debug( + "CLI: Configuration $s has been saved by $new->{cfgAuthor}"); $self->logger->info("CLI: Configuration $s saved"); print STDERR "Saved under number $s\n"; $parser->{status} = [ $self->mgr->applyConf($new) ]; @@ -343,8 +347,7 @@ sub run { $self->cfgNum( $self->lastCfg ) unless ( $self->cfgNum ); my $action = shift; unless ( $action =~ /^(?:get|set|addKey|delKey|save|restore)$/ ) { - die -"Unknown action $action. Only get, set, addKey or delKey allowed"; + die "Unknown action $action. Only get, set, addKey or delKey allowed"; } $self->$action(@_); @@ -355,7 +358,7 @@ package Lemonldap::NG::Manager::Cli::Request; use Mouse; has cfgNum => ( is => 'rw' ); -has error => ( is => 'rw' ); +has error => ( is => 'rw' ); sub params { my ( $self, $key ) = @_; diff --git a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm index 25d3dcbad..efe547886 100644 --- a/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm +++ b/lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Conf/Parser.pm @@ -94,8 +94,7 @@ sub hdebug { # Main method #@return result sub check { - my $self = shift; - my $localConf = shift; + my ( $self, $localConf ) = @_; hdebug("# check()"); unless ( $self->newConf ) { @@ -107,7 +106,7 @@ sub check { } my $separator = $self->newConf->{multiValuesSeparator} || '; '; hdebug(" tests succeed"); - my %conf = %{ $self->newConf() }; + my %conf = %{ $self->newConf }; my %compactedConf = %{ $self->compactConf( $self->newConf ) }; my @removedKeys = (); unless ( $self->confChanged ) { @@ -115,11 +114,10 @@ sub check { $self->message('__confNotChanged__'); return 0; } - if ( $self->newConf->{compactConf} ) { - foreach ( sort keys %conf ) { - push @removedKeys, $_ unless exists $compactedConf{$_}; - } - } + + # Return removed keys if conf compacted + @removedKeys = map { exists $compactedConf{$_} ? () : $_ } sort keys %conf + if ( $self->newConf->{compactConf} ); push @{ $self->changes }, ( $self->{newConf}->{compactConf} @@ -129,6 +127,7 @@ sub check { } : { confCompacted => '0' } ); + return 1; } @@ -1093,8 +1092,7 @@ sub defaultValue { # #@return true if tests succeed sub testNewConf { - my $self = shift; - my $localConf = shift; + my ( $self, $localConf ) = @_; hdebug('# testNewConf()'); return $self->_unitTest( $self->newConf(), $localConf ) @@ -1121,7 +1119,7 @@ sub _unitTest { } hdebug("Testing $key"); my $attr = $attrs->{$key}; - my $type = $types->{ $attr->{type} }; + my $type = $types->{ $attr->{type} } if $attr; unless ( $type or $attr->{test} ) { $localConf->logger->debug("Unknown attribute $key, deleting it\n"); delete $conf->{$key}; diff --git a/lemonldap-ng-manager/t/12-save-changed-conf.t b/lemonldap-ng-manager/t/12-save-changed-conf.t index c3569f4e9..b18bae0a3 100644 --- a/lemonldap-ng-manager/t/12-save-changed-conf.t +++ b/lemonldap-ng-manager/t/12-save-changed-conf.t @@ -41,11 +41,20 @@ ok( @{ $resBody->{details}->{__changes__} } == 24, 'JSON response contains 24 changes' ) or print STDERR Dumper($resBody); +ok( + $resBody->{details}->{__changes__}->[23]->{confCompacted} == 1, + 'Conf. has been compacted' +) or print STDERR Dumper($resBody); -#print STDERR Dumper($resBody); +my @removedKeys = split /; /, $resBody->{details}->{__changes__}->[23]->{removedKeys}; +ok( + @removedKeys == 60, 'All removed keys found' +) or print STDERR Dumper(\@removedKeys); +#print STDERR Dumper($resBody); ok( -f $confFiles->[1], 'File is created' ); -count(4); +count(6); + my @changes = @{&changes}; my @cmsg = @{ $resBody->{details}->{__changes__} }; my $bug; diff --git a/lemonldap-ng-manager/t/16-cli.t b/lemonldap-ng-manager/t/16-cli.t index 629a8cbfa..a2cfef33b 100644 --- a/lemonldap-ng-manager/t/16-cli.t +++ b/lemonldap-ng-manager/t/16-cli.t @@ -3,7 +3,7 @@ use JSON; use strict; require 't/test-lib.pm'; -my $tests = 9; +my $tests = 10; use_ok('Lemonldap::NG::Common::Cli'); use_ok('Lemonldap::NG::Manager::Cli'); @@ -16,6 +16,8 @@ SKIP: { } my $client = Lemonldap::NG::Manager::Cli->new( iniFile => 't/lemonldap-ng.ini' ); + # my $client2 = + # Lemonldap::NG::Common::Cli->new( iniFile => 't/lemonldap-ng.ini' ); my @cmd; my $res; @@ -64,6 +66,22 @@ SKIP: { @cmd = ( 'restore', '-' ); Test::Output::combined_like( sub { $client->run(@cmd) }, qr/"cfgNum"\s*:\s*"3"/s, 'New config: 3' ); + + # Test 'set' command with force + @cmd = qw(-yes 1 -force 1 -cfgNum 2 set useSafeJail 0); + Test::Output::combined_like( + sub { $client->run(@cmd) }, + qr#cfgNum forced with 2#s, + '"Force cfgNum" OK' + ); + + # # Test 'set' command with force + # @cmd = qw(update-cache); + # Test::Output::combined_like( + # sub { $client2->run(@cmd) }, + # qr#Cache updated to configuration 1#s, + # '"update-cache" OK' + # ); } count($tests);