From a9dd6b52bb3e946ad63ec55244dd2581813d2cd2 Mon Sep 17 00:00:00 2001 From: James Adams Date: Fri, 8 Jun 2018 17:08:57 +0100 Subject: [PATCH] ncm-sysconfig: Modernise and add unit tests * Switch to CAF for all file interaction and declare NoAction support. * Factor out filelist read/write to seperate functions. * Add a basic Configure unit test. --- ncm-sysconfig/src/main/perl/sysconfig.pm | 105 +++++++++--------- ncm-sysconfig/src/test/perl/configure.t | 23 ++++ .../src/test/resources/configure.pan | 6 + 3 files changed, 82 insertions(+), 52 deletions(-) create mode 100644 ncm-sysconfig/src/test/perl/configure.t create mode 100644 ncm-sysconfig/src/test/resources/configure.pan diff --git a/ncm-sysconfig/src/main/perl/sysconfig.pm b/ncm-sysconfig/src/main/perl/sysconfig.pm index 07df2dd516..322951a8cc 100755 --- a/ncm-sysconfig/src/main/perl/sysconfig.pm +++ b/ncm-sysconfig/src/main/perl/sysconfig.pm @@ -1,48 +1,62 @@ #${PMcomponent} -use parent qw(NCM::Component); +use parent qw(NCM::Component CAF::Path); our $EC = LC::Exception::Context->new->will_store_all; -use LC::Check; +use CAF::FileReader; +use CAF::FileWriter; -use File::Path; -use File::Basename; +use Readonly; +Readonly my $QUOTE => "\""; +Readonly my $SYSCONFIGDIR => "/etc/sysconfig"; # The base directory for sysconfig files. -sub Configure +sub filelist_read { - my ($self, $config) = @_; + my ($self) = @_; - # Define paths for convenience. - my $base = "/software/components/sysconfig"; + # Read first the list of sysconfig files which have been + # previously managed by this component. These will have to + # be deleted if no longer in the configuration. + my %filelist; + my $fh = CAF::FileReader->open ("$SYSCONFIGDIR/ncm-sysconfig"); + while (my $line = <$fh>) { + chomp($line); + $filelist{$line} = 1; + } + $fh->close(); - # The base directory for sysconfig files. - my $sysconfigdir = "/etc/sysconfig"; + return %filelist; +} + +sub filelist_write +{ + my ($self, %filelist) = @_; + + # Write the list of managed configuration files. + my $fh = CAF::FileWriter->open("$SYSCONFIGDIR/ncm-sysconfig"); + for my $file (keys %filelist) { + print $fh "$file\n"; + } + $fh->close(); + + return 1; +} + +sub Configure +{ + my ($self, $config) = @_; # Load configuration into a hash - my $sysconfig_config = $config->getElement($base)->getTree(); + my $sysconfig_config = $config->getTree($self->prefix()); # Ensure that sysconfig directory exists. - mkpath($sysconfigdir,0,0755) unless (-e $sysconfigdir); - if (! -d $sysconfigdir) { - $self->error("$sysconfigdir isn't a directory or can't be created"); - return 1; - } + $self->directory($SYSCONFIGDIR, owner=>0, group=>0, mode=>0755); # This will be a list of the configuration files managed by this component. - my %newcfg; + my %files_managed; - # Read first the list of sysconfig files which have been - # previously managed by this component. These will have to - # be deleted if no longer in the configuration. - my %oldcfg; - if (-f "$sysconfigdir/ncm-sysconfig") { - open CONF, "<", "$sysconfigdir/ncm-sysconfig"; - while () { - chomp; - $oldcfg{$_} = 1; - } - } + my %files_previous = $self->filelist_read(); # Loop over all of the defined files, writing each as necessary. if ( $sysconfig_config->{files} ) { @@ -71,39 +85,26 @@ sub Configure } # Now actually update the file, if needed. - - my $result = LC::Check::file("$sysconfigdir/$file", - backup => ".old", - contents => $contents, - ); - unless ( $result >= 0 ) { - $self->error("Error updating file $sysconfigdir/$file"); - } + my $fh = CAF::FileWriter->open("$SYSCONFIGDIR/$file", backup=>".old"); + print $fh $contents; + $fh->close(); + undef $fh; # Remove this file from the list of old configuration # files add to the new configuration files. - delete($oldcfg{"$sysconfigdir/$file"}); - $newcfg{"$sysconfigdir/$file"} = 1; + delete($files_previous{"$SYSCONFIGDIR/$file"}); + $files_managed{"$SYSCONFIGDIR/$file"} = 1; } } # Remove any old configuration files which haven't been updated. - for my $file (keys %oldcfg) { - unlink $file if (-e $file); + for my $file (keys %files_previous) { + $self->cleanup($file); } - # Write the list of managed configuration files. - if(open CONF, ">", "$sysconfigdir/ncm-sysconfig") { - for my $file (keys %newcfg) { - print CONF $file . "\n"; - } - close CONF; - } else { - $self->error("error writing file $sysconfigdir/ncm-sysconfig"); - return 1; - } + $self->filelist_write(%files_managed); - return 0; + return 1; } -1; # Required for PERL modules +1; # Required for PERL modules diff --git a/ncm-sysconfig/src/test/perl/configure.t b/ncm-sysconfig/src/test/perl/configure.t new file mode 100644 index 0000000000..c5657259ab --- /dev/null +++ b/ncm-sysconfig/src/test/perl/configure.t @@ -0,0 +1,23 @@ +use strict; +use warnings; +use Test::More; +use Test::Quattor qw(configure); +use NCM::Component::sysconfig; + +$CAF::Object::NoAction = 1; + +my $cmp = NCM::Component::sysconfig->new('sysconfig'); +my $cfg = get_config_for_profile('configure'); + +set_file_contents('/etc/sysconfig/ncm-sysconfig', "/etc/sysconfig/delete_me\n/etc/sysconfig/examplefile\n"); + +is($cmp->Configure($cfg), 1, "Component runs correctly with a test profile"); + +my $fh = get_file('/etc/sysconfig/ncm-sysconfig'); +isa_ok($fh, "CAF::FileWriter", "Correct class"); + +is(get_file_contents('/etc/sysconfig/ncm-sysconfig'), "/etc/sysconfig/examplefile\n", "Filelist updated correctly"); + +is(get_file_contents('/etc/sysconfig/examplefile'), "key1=testvalue\nkey2=valuetest\n", "Example file has correct contents"); + +done_testing(); diff --git a/ncm-sysconfig/src/test/resources/configure.pan b/ncm-sysconfig/src/test/resources/configure.pan new file mode 100644 index 0000000000..8ce521239a --- /dev/null +++ b/ncm-sysconfig/src/test/resources/configure.pan @@ -0,0 +1,6 @@ +object template configure; + +prefix '/software/components/sysconfig'; + +'files/examplefile/key1' = 'testvalue'; +'files/examplefile/key2' = 'valuetest';