Skip to content

Commit

Permalink
ncm-nss: replace LC::Check with FileWriter
Browse files Browse the repository at this point in the history
Use FileWriter instead of LC::Check and add a simple unit test of the
behaviour.
  • Loading branch information
ned21 committed Jul 13, 2016
1 parent b2d63d5 commit 1b66349
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
20 changes: 10 additions & 10 deletions ncm-nss/src/main/perl/nss.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ sub Configure {

my @new = ( "# Generated by ncm-nss\n\n" );
push(@new, map { "$_: $databases->{$_}\n" } sort keys %$databases);
my $contents = join("", @new);
# cannot use CAF::FileWriter because close() always returns false in
# noaction mode so cannot show when nscd would be restarted
my $result = LC::Check::file("/etc/nsswitch.conf",
backup => ".OLD",
contents => $contents,
owner => "root",
group => "root",
mode => "0444",
);
my $fh = CAF::FileWriter->open("/etc/nsswitch.conf",
backup => ".OLD",
owner => "root",
group => "root",
mode => "0444",
log => $self,
);
print $fh join("", @new);
my $result = $fh->close();

if ($result) {
$self->log("updated /etc/nsswitch.conf");

Expand Down
37 changes: 37 additions & 0 deletions ncm-nss/src/test/perl/configure.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/perl
# -*- mode: cperl -*-
use strict;
use warnings;

use Test::More;
use Test::Quattor qw(simple);
use NCM::Component::sysctl;
use Test::MockModule;
use CAF::Object;

$CAF::Object::NoAction = 1;

my $mock = Test::MockModule->new('NCM::Component::nss');

=pod
=head1 DESCRIPTION
Test the configure() method.
=cut

my $cmp = NCM::Component::nss->new('nss');
my $cfg = get_config_for_profile('simple');
my $testfile = "/etc/nsswitch.conf";
my $testcmd = "/usr/sbin/buildldap -d passwd";

is($cmp->Configure($cfg), 1, "Configure succeeds");
my $fh = get_file($testfile);
ok($fh, "$testfile was created");
isa_ok($fh, "CAF::FileWriter");
like("$fh", qr{^passwd: files ldap$}m, "passwd line present and correct");
ok(defined(get_command($testcmd)), "$testcmd was run");
ok(!defined(get_command('/usr/sbin/builddb')), "inactive db was not built");

done_testing();
15 changes: 15 additions & 0 deletions ncm-nss/src/test/resources/simple.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object template simple;

#function pkg_repl = { null; };
#include 'components/nss/config';
#"/software/components/nss/dependencies/pre" = null;

prefix "/software/components/nss";
"databases/passwd" = list("files", "ldap");
"build/ldap/script" = "/usr/sbin/buildldap -d <DB>";
"build/ldap/active" = true;
"build/db/script" = "/usr/sbin/builddb";
# active has a de-facto value of false in the code
#"build/db/active" = false;


0 comments on commit 1b66349

Please sign in to comment.