From 1b66349bc5a7bfe36e41918b5490e5d94dc8912c Mon Sep 17 00:00:00 2001 From: Nathan Dimmock Date: Thu, 2 Jun 2016 21:45:02 +0100 Subject: [PATCH] ncm-nss: replace LC::Check with FileWriter Use FileWriter instead of LC::Check and add a simple unit test of the behaviour. --- ncm-nss/src/main/perl/nss.pm | 20 +++++++-------- ncm-nss/src/test/perl/configure.t | 37 +++++++++++++++++++++++++++ ncm-nss/src/test/resources/simple.pan | 15 +++++++++++ 3 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 ncm-nss/src/test/perl/configure.t create mode 100644 ncm-nss/src/test/resources/simple.pan diff --git a/ncm-nss/src/main/perl/nss.pm b/ncm-nss/src/main/perl/nss.pm index 1191b88a22..04b2a4291d 100644 --- a/ncm-nss/src/main/perl/nss.pm +++ b/ncm-nss/src/main/perl/nss.pm @@ -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"); diff --git a/ncm-nss/src/test/perl/configure.t b/ncm-nss/src/test/perl/configure.t new file mode 100644 index 0000000000..e12e04867d --- /dev/null +++ b/ncm-nss/src/test/perl/configure.t @@ -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(); diff --git a/ncm-nss/src/test/resources/simple.pan b/ncm-nss/src/test/resources/simple.pan new file mode 100644 index 0000000000..4f60e2c718 --- /dev/null +++ b/ncm-nss/src/test/resources/simple.pan @@ -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 "; +"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; + +