[Nagiosplug-checkins] Nagios-Plugin/t Nagios-Plugin-Getopt-01.t, NONE, 1.1 Nagios-Plugin-Getopt-02.t, NONE, 1.1

Gavin Carr gonzai at users.sourceforge.net
Wed Aug 30 03:20:43 CEST 2006


Update of /cvsroot/nagiosplug/Nagios-Plugin/t
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17966/t

Added Files:
	Nagios-Plugin-Getopt-01.t Nagios-Plugin-Getopt-02.t 
Log Message:
Add first-pass Nagios::Plugin::Getopt.

--- NEW FILE: Nagios-Plugin-Getopt-01.t ---
# Nagios::Plugin::Getopt basic tests

use strict;

use Test::More tests => 72;
BEGIN { use_ok('Nagios::Plugin::Getopt') };

my %PARAM = (
    version => '0.01',
    url => 'http://www.openfusion.com.au/labs/nagios/',
    blurb => 'This plugin tests various stuff.', 
    usage => "Usage: %s -H <host> -w <warning_threshold> 
  -c <critical threshold>",
    plugin => 'test_plugin',
);

sub setup 
{
  # Instantiate object
  my $ng = Nagios::Plugin::Getopt->new(%PARAM);
  ok($ng, 'constructor ok');

  # Add argument - short form - arg spec, help text, default, required?
  $ng->arg('warning|w=s' =>
    qq(-w, --warning=INTEGER\n   Exit with WARNING status if less than INTEGER foobars are free),
    5);
  
  # Add argument - named version
  $ng->arg(
    spec => 'critical|c=s',
    help => qq(-c, --critical=INTEGER\n   Exit with CRITICAL status if less than INTEGER foobars are free),
    required => 1,
  );

  return $ng;
}

my $ng;

# Simple usage (short and long args)
@ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose);
$ng = setup;
$ng->getopts;
is($ng->warning, 3, 'warning set to 3');
is($ng->critical, 10, 'critical set to 10');
is($ng->timeout, 12, 'timeout set to 12');

# Missing args
@ARGV = qw();
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
like($@, qr/Usage:/, 'usage message');
like($@, qr/Missing arg/, 'missing arguments');
is($ng->verbose, 0, 'verbose set to 0');
# Missing critical
@ARGV = qw(-w0 -v);
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
like($@, qr/Usage:/, 'usage message');
like($@, qr/Missing argument: critical/, 'missing argument: critical');
unlike($@, qr/Missing argument: warning/, 'no missing argument: warning');
is($ng->warning, 0, 'warning set to 0');
is($ng->critical, undef, 'critical undef');
is($ng->timeout, 15, 'timeout set to default');
is($ng->verbose, 1, 'verbose set to true');
# Missing warning
@ARGV = qw(--critical=27 --timeout 17 --verbose);
$ng = setup;
$ng->getopts;
is($ng->warning, 5, 'warning 5 (default)');
is($ng->critical, 27, 'critical set to 27');
is($ng->timeout, 17, 'timeout set to 17');
is($ng->verbose, 1, 'verbose set to true');

# -? --usage
@ARGV = ( '-?' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on usage');
like($@, qr/Usage:/, 'usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');
@ARGV = ( '--usage' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on usage');
like($@, qr/Usage:/, 'usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');

# -V --version
@ARGV = ( '-V' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on version');
like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
like($@, qr/$PARAM{version}/, 'version info includes version');
like($@, qr/$PARAM{url}/, 'version info includes url');
unlike($@, qr/Usage:/, 'no usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');
@ARGV = ( '--version' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on version');
like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
like($@, qr/$PARAM{version}/, 'version info includes version');
like($@, qr/$PARAM{url}/, 'version info includes url');
unlike($@, qr/Usage:/, 'no usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');

# -h --help
@ARGV = ( '-h' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
like($@, qr/$PARAM{version}/, 'help includes version');
like($@, qr/$PARAM{url}/, 'help includes url');
like($@, qr/General Public Licence/, 'help includes licence');
like($@, qr/$PARAM{blurb}/, 'help includes blurb');
like($@, qr/Usage:/, 'help includes usage message');
like($@, qr/--version/, 'help includes default options 1');
like($@, qr/--verbose/, 'help includes default options 2');
like($@, qr/--warning/, 'help includes custom option 1');
like($@, qr/--critical/, 'help includes custom option 2');
unlike($@, qr/Missing arg/, 'no missing arguments');
@ARGV = ( '--help' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
like($@, qr/$PARAM{version}/, 'help includes version');
like($@, qr/$PARAM{url}/, 'help includes url');
like($@, qr/General Public Licence/, 'help includes licence');
like($@, qr/$PARAM{blurb}/, 'help includes blurb');
like($@, qr/Usage:/, 'help includes usage message');
like($@, qr/--version/, 'help includes default options 1');
like($@, qr/--verbose/, 'help includes default options 2');
like($@, qr/--warning/, 'help includes custom option 1');
like($@, qr/--critical/, 'help includes custom option 2');
unlike($@, qr/Missing arg/, 'no missing arguments');


--- NEW FILE: Nagios-Plugin-Getopt-02.t ---
# Nagios::Plugin::Getopt timeout tests

use strict;

use Test::More tests => 14;
BEGIN { use_ok('Nagios::Plugin::Getopt') };

my %PARAM = (
    version => '0.01',
    url => 'http://www.openfusion.com.au/labs/nagios/',
    blurb => 'This plugin tests various stuff.', 
    usage => "Usage: %s -H <host> -w <warning_threshold> 
  -c <critical threshold>",
    plugin => 'test_plugin',
    timeout => 18,
);

sub setup 
{
  # Instantiate object
  my $ng = Nagios::Plugin::Getopt->new(%PARAM);
  ok($ng, 'constructor ok');
  return $ng;
}

my $ng;

# No args
@ARGV = qw();
$ng = setup();
$ng->getopts;
is($ng->timeout, 18, 'default timeout set to 18');

# Check help message
@ARGV = ( '-h' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18');

# Explicit timeout
@ARGV = qw(--timeout=25 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 25, 'timeout changed to 25');

# Explicit timeout
@ARGV = qw(-t10 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 10, 'timeout changed to 10');

# Short timeout, test default timeout handler
@ARGV = qw(-t2 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 2, 'timeout changed to 2');
alarm($ng->timeout);
# Loop
ok(! defined eval { 1 while 1 }, 'loop timed out');
like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok');






More information about the Commits mailing list