ASNMTAP

 view release on metacpan or  search on metacpan

lib/ASNMTAP/Asnmtap/Plugins/NPTest.pm  view on Meta::CPAN

package ASNMTAP::Asnmtap::Plugins::NPTest;

#
# Helper Functions for testing Nagios Plugins
#

require Exporter;
@ISA       = qw(Exporter);
@EXPORT    = qw(getTestParameter checkCmd skipMissingCmd);
@EXPORT_OK = qw(DetermineTestHarnessDirectory TestsFrom SetCacheFilename);

use strict;
use warnings;

use Cwd;
use File::Basename;

use IO::File;
use Data::Dumper;

use Test;

use vars qw($VERSION);
$VERSION = do { my @r = (q$Revision: 1.13 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker

=head1 NAME

NPTest - Simplify the testing of Nagios Plugins

=head1 DESCRIPTION

This modules provides convenience functions to assist in the testing
of Nagios Plugins, making the testing code easier to read and write;
hopefully encouraging the development of a more complete test suite for
the Nagios Plugins. It is based on the patterns of testing seen in the
1.4.0 release, and continues to use the L<Test> module as the basis of
testing.

=head1 FUNCTIONS

This module defines three public functions, C<getTestParameter(...)>,
C<checkCmd(...)> and C<skipMissingCmd(...)>.  These are exported by
default via the C<use NPTest;> statement.

=over

=item getTestParameter( "ENV_VARIABLE", $brief_description, $default )

$default is optional.

This function allows the test harness
developer to interactively request test parameter information from the
user. The user can accept the developer's default value or reply "none"
which will then be returned as "" for the test to skip if appropriate.

If a parameter needs to be entered and the test is run without a tty 
attached (such as a cronjob), the parameter will be assigned as if it 
was "none". Tests can check for the parameter and skip if not set.

Responses are stored in an external, file-based
cache so subsequent test runs will use these values. The user is able
to change the values by amending the values in the file /var/tmp/NPTest.pm,
or by setting the appropriate environment variable before running the test.

The option exists to store parameters in a scoped means, allowing a
test harness to a localise a parameter should the need arise. This
allows a parameter of the same name to exist in a test harness
specific scope, while not affecting the globally scoped parameter. The
scoping identifier is the name of the test harness sans the trailing
".t".  All cache searches first look to a scoped parameter before
looking for the parameter at global scope. Thus for a test harness
called "check_disk.t" requesting the parameter "mountpoint_valid", the
cache is first searched for "check_disk"/"mountpoint_valid", if this
fails, then a search is conducted for "mountpoint_valid".

To facilitate quick testing setup, it is possible to accept all the
developer provided defaults by setting the environment variable
"NPTEST_ACCEPTDEFAULT" to "1" (or any other perl truth value). Note
that, such defaults are not stored in the cache, as there is currently

lib/ASNMTAP/Asnmtap/Plugins/NPTest.pm  view on Meta::CPAN

    return $CACHE{$param};
  }
  return undef;	# Need this to say "nothing found"
}

sub SetCacheParameter
{
  my( $param, $scope, $value ) = @_;

  if ( defined( $scope ) )
  {
    $CACHE{$scope}{$param} = $value;
  }
  else
  {
    $CACHE{$param} = $value;
  }

  SaveCache();
}

sub LoadCache
{
  return if exists( $CACHE{'_cache_loaded_'} );

  if ( -f $CACHEFILENAME )
  {
    my( $fileHandle ) = new IO::File;

    if ( ! $fileHandle->open( "< ${CACHEFILENAME}" ) )
    {
      print STDERR "NPTest::LoadCache() : Problem opening ${CACHEFILENAME} : $!\n";
      return;
    }

    my( $fileContents ) = join( "\n", <$fileHandle> );

    $fileHandle->close();

    my( $contentsRef ) = eval $fileContents;
    %CACHE = %{$contentsRef};

  }

  $CACHE{'_cache_loaded_'} = 1;
}


sub SaveCache
{
  delete $CACHE{'_cache_loaded_'};

  my( $fileHandle ) = new IO::File;

  if ( ! $fileHandle->open( "> ${CACHEFILENAME}" ) )
  {
    print STDERR "NPTest::LoadCache() : Problem saving ${CACHEFILENAME} : $!\n";
    return;
  }

  my( $dataDumper ) = new Data::Dumper( [ \%CACHE ] );

  $dataDumper->Terse(1);

  print $fileHandle $dataDumper->Dump();

  $fileHandle->close();

  $CACHE{'_cache_loaded_'} = 1;
}

#
# (Questionable) Public Cache Management Functions
#

sub SetCacheFilename
{
  my( $filename ) = @_;

  # Unfortunately we can not validate the filename
  # in any meaningful way, as it may not yet exist
  $CACHEFILENAME = $filename;
}


#
# Test Harness Wrapper Functions
#

sub DetermineTestHarnessDirectory
{
  my( $userSupplied ) = @_;

  # User Supplied
  if ( defined( $userSupplied ) && $userSupplied )
  {
    if ( -d $userSupplied )
    {
      return $userSupplied;
    }
    else
    {
      return undef; # userSupplied is invalid -> FAIL
    }
  }

  # Simple Case : "t" is a subdirectory of the current directory
  if ( -d "./t" )
  {
    return "./t";
  }

  # To be honest I don't understand which case satisfies the
  # original code in test.pl : when $tstdir == `pwd` w.r.t.
  # $tstdir =~ s|^(.*)/([^/]+)/?$|$1/$2|; and if (-d "../../$2/t")
  # Assuming pwd is "/a/b/c/d/e" then we are testing for "/a/b/c/e/t"
  # if I understand the code correctly (a big assumption)

  # Simple Case : the current directory is "t"
  my $pwd = cwd();



( run in 0.335 second using v1.01-cache-2.11-cpan-f56aa216473 )