From b418181dfe80dd75169b6e8a619ac1932155dea2 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 20 Jan 2014 00:54:34 +0100 Subject: renamed module into Monitoring::Plugin since the complete monitoring team has been renamed, we also rename this module. Signed-off-by: Sven Nierlein --- t/Monitoring-Plugin-Getopt-01.t | 149 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 t/Monitoring-Plugin-Getopt-01.t (limited to 't/Monitoring-Plugin-Getopt-01.t') diff --git a/t/Monitoring-Plugin-Getopt-01.t b/t/Monitoring-Plugin-Getopt-01.t new file mode 100644 index 0000000..36f1f55 --- /dev/null +++ b/t/Monitoring-Plugin-Getopt-01.t @@ -0,0 +1,149 @@ +# Monitoring::Plugin::Getopt basic tests + +use strict; + +use Test::More tests => 76; +BEGIN { use_ok('Monitoring::Plugin::Getopt') }; + +# Needed to get evals to work in testing +Monitoring::Plugin::Functions::_use_die(1); + +my %PARAM = ( + version => '0.01', + url => 'http://www.openfusion.com.au/labs/nagios/', + blurb => 'This plugin tests various stuff.', + usage => "Usage: %s -H -w + -c ", + plugin => 'test_plugin', +); + +sub setup +{ + # Instantiate object + my $ng = Monitoring::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=i', + help => qq(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'); + +# Check multiple verbose flags +@ARGV = qw(-w 3 --critical 10 -v -v -v); +$ng = setup; +$ng->getopts; +is ($ng->verbose, 3, "Verbose set to level 3"); + +@ARGV = qw(-w 3 --critical 10 --verbose --verbose --verbose); +$ng = setup; +$ng->getopts; +is ($ng->verbose, 3, "Verbose set to level 3 (longhand)"); + +# 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/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); +unlike($@, qr/Missing arg/, 'no missing arguments'); -- cgit v1.2.3-74-g34f1