From dc31f1cd3841d486e920e59ce42e888ca94e4289 Mon Sep 17 00:00:00 2001 From: Gavin Carr Date: Wed, 21 Mar 2007 00:52:56 +0000 Subject: Finished initial --extra-opts support; added Getopt spec-to-help and multiline help support. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1643 f882894a-f735-0410-b71e-b25c423dba1c --- t/Nagios-Plugin-04.t | 1 - t/Nagios-Plugin-Getopt-01.t | 8 ++- t/Nagios-Plugin-Getopt-03.t | 17 ++++-- t/Nagios-Plugin-Getopt-04.t | 95 ++++++++++++++++++++++++++++++++ t/npg03/README | 18 +++--- t/npg03/expected/00_noextra | 1 + t/npg03/expected/05_disk1 | 1 + t/npg03/expected/05_disk2 | 1 + t/npg03/expected/05_disk3 | 1 + t/npg03/expected/05_disk4 | 1 + t/npg03/expected/05_disk5 | 1 + t/npg03/expected/05_disk6 | 1 + t/npg03/expected/05_singlechar1 | 1 - t/npg03/expected/06_singlechar2 | 1 - t/npg03/expected/07_singlechar3 | 1 - t/npg03/input/00_basic | 2 +- t/npg03/input/00_noextra | 1 + t/npg03/input/01_override1 | 2 +- t/npg03/input/02_override2 | 2 +- t/npg03/input/05_disk1 | 1 + t/npg03/input/05_disk2 | 1 + t/npg03/input/05_disk3 | 1 + t/npg03/input/05_disk4 | 1 + t/npg03/input/05_disk5 | 1 + t/npg03/input/05_disk6 | 1 + t/npg03/input/05_singlechar1 | 1 - t/npg03/input/06_singlechar2 | 1 - t/npg03/input/07_singlechar3 | 1 - t/npg03/input/09_funnystuff | 2 +- t/npg03/input/13_nosection_explicit_dies | 2 +- t/npg03/input/14_badsection_dies | 2 +- t/npg03/plugins.cfg | 21 ------- t/npg03/plugins.ini | 30 ++++++++++ 33 files changed, 171 insertions(+), 51 deletions(-) create mode 100644 t/Nagios-Plugin-Getopt-04.t create mode 100644 t/npg03/expected/00_noextra create mode 100644 t/npg03/expected/05_disk1 create mode 100644 t/npg03/expected/05_disk2 create mode 100644 t/npg03/expected/05_disk3 create mode 100644 t/npg03/expected/05_disk4 create mode 100644 t/npg03/expected/05_disk5 create mode 100644 t/npg03/expected/05_disk6 delete mode 100644 t/npg03/expected/05_singlechar1 delete mode 100644 t/npg03/expected/06_singlechar2 delete mode 100644 t/npg03/expected/07_singlechar3 create mode 100644 t/npg03/input/00_noextra create mode 100644 t/npg03/input/05_disk1 create mode 100644 t/npg03/input/05_disk2 create mode 100644 t/npg03/input/05_disk3 create mode 100644 t/npg03/input/05_disk4 create mode 100644 t/npg03/input/05_disk5 create mode 100644 t/npg03/input/05_disk6 delete mode 100644 t/npg03/input/05_singlechar1 delete mode 100644 t/npg03/input/06_singlechar2 delete mode 100644 t/npg03/input/07_singlechar3 delete mode 100644 t/npg03/plugins.cfg create mode 100644 t/npg03/plugins.ini (limited to 't') diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t index 6f31b56..d88ad73 100644 --- a/t/Nagios-Plugin-04.t +++ b/t/Nagios-Plugin-04.t @@ -1,6 +1,5 @@ # tests for toplevel access to Threshold and GetOpts stuff -# $Id$ use strict; #use Test::More 'no_plan'; diff --git a/t/Nagios-Plugin-Getopt-01.t b/t/Nagios-Plugin-Getopt-01.t index b708a5f..bad1367 100644 --- a/t/Nagios-Plugin-Getopt-01.t +++ b/t/Nagios-Plugin-Getopt-01.t @@ -27,8 +27,8 @@ sub setup # 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), + spec => 'critical|c=i', + help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), required => 1, ); @@ -104,6 +104,7 @@ 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'); @@ -128,6 +129,7 @@ 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'); @@ -140,6 +142,6 @@ 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'); +like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); unlike($@, qr/Missing arg/, 'no missing arguments'); diff --git a/t/Nagios-Plugin-Getopt-03.t b/t/Nagios-Plugin-Getopt-03.t index 9dc39da..557a2c6 100644 --- a/t/Nagios-Plugin-Getopt-03.t +++ b/t/Nagios-Plugin-Getopt-03.t @@ -1,4 +1,4 @@ -# Nagios::Plugin::Getopt --default-opts tests +# Nagios::Plugin::Getopt --extra-opts tests use strict; use File::Spec; @@ -27,7 +27,8 @@ for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) { } } -$Nagios::Plugin::Getopt::DEFAULT_CONFIG_FILE = File::Spec->catfile($tdir, 'plugins.cfg'); +# Override NAGIOS_CONFIG_PATH to use our test plugins.ini file +$ENV{NAGIOS_CONFIG_PATH} = "/random/bogus/path:$tdir"; my %PARAM = ( version => '0.01', @@ -56,14 +57,18 @@ my $arg = [ { spec => 'S', help => '-S' }, { spec => 'H=s', help => '-H' }, { spec => 'p=s@', help => '-p' }, + { spec => 'path=s@', help => '--path' }, { spec => 'username|u=s', help => '--username' }, { spec => 'password=s', help => '--password' }, - { spec => 'critical=i', help => '--critical' }, - { spec => 'warning=i', help => '--warning' }, + { spec => 'critical=s', help => '--critical' }, + { spec => 'warning=s', help => '--warning' }, { spec => 'expect=s', help => '--expect' }, + { spec => 'units=s', help => '--units' }, ]; -my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); +#my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); +#my %SKIP = map { $_ => 1 } qw(06_singlechar2); +my %SKIP = (); # Process all test cases in $tdir/input my $glob = $ARGV[0] || '*'; @@ -82,7 +87,7 @@ for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) { # Parse the options SKIP: { - skip "Still discussing how overrides with multiple arguments should work ...", 1 if $SKIP{$infile}; + skip "Skipping ..." if $SKIP{$infile}; @ARGV = @args; eval { $ng->getopts }; diff --git a/t/Nagios-Plugin-Getopt-04.t b/t/Nagios-Plugin-Getopt-04.t new file mode 100644 index 0000000..9092636 --- /dev/null +++ b/t/Nagios-Plugin-Getopt-04.t @@ -0,0 +1,95 @@ +# Nagios::Plugin::Getopt spec-to-help generation tests + +use strict; + +use Test::More tests => 11; +BEGIN { use_ok('Nagios::Plugin::Getopt') }; + +my %PARAM = ( + version => '0.01', + usage => "Don't use this plugin!", +); + +sub setup +{ + # Instantiate object + my $ng = Nagios::Plugin::Getopt->new(%PARAM); + ok($ng, 'constructor ok'); + + # Positional args, no short arguments, INTEGER + $ng->arg('warning=i' => + qq(Exit with WARNING status if less than INTEGER foobars are free), + 5); + + # Named args, long + short arguments, INTEGER + $ng->arg( + spec => 'critical|c=i', + help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), + required => 1, + ); + + # Named args, multiple short arguments, STRING, default expansion + $ng->arg( + spec => 'x|y|z=s', + help => qq(Foobar. Default: %s), + default => "XYZ", + ); + + # Named args, multiple mixed, no label + $ng->arg( + spec => 'long|longer|longest|l', + help => qq(Long format), + ); + + # Named args, long + short, explicit label + $ng->arg( + spec => 'hostname|H=s', + label => 'ADDRESS', + help => qq(Hostname), + ); + + # Positional args, long only, explicit label + $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR'); + + # Multiline help test, named args + $ng->arg( + spec => 'disk=s', + label => [ qw(BYTES PERCENT%), undef ], + help => [ + qq(Disk limit in BYTES), + qq(Disk limit in PERCENT), + qq(Disk limit in FOOBARS (Default: %s)), + ], + default => 1024, + ); + + # Multiline help test, positional args + $ng->arg( + 'limit=s', + [ + qq(Limit in BYTES), + qq(Limit in PERCENT), + ], + undef, + undef, + [ undef, 'PERCENT%' ], + ); + + return $ng; +} + +my $ng; + +@ARGV = ( '--help' ); +$ng = setup; +ok(! defined eval { $ng->getopts }, 'getopts died on help'); +like($@, qr/\n --warning=INTEGER/, 'warning ok'); +like($@, qr/\n -c, --critical=INTEGER/, 'critical ok'); +like($@, qr/\n -x, -y, -z=STRING\n Foobar. Default: XYZ\n/, 'x|y|z ok'); +like($@, qr/\n -l, --long, --longer, --longest\n Long format\n/, 'long ok'); +like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); +like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); +like($@, qr/\n --disk=BYTES\n Disk limit in BYTES\n --disk=PERCENT%\n Disk limit in PERCENT\n --disk=STRING\n Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok'); +like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); +#print $@; + diff --git a/t/npg03/README b/t/npg03/README index a19f263..3dbdaf8 100644 --- a/t/npg03/README +++ b/t/npg03/README @@ -1,13 +1,15 @@ -Nagios-Plugin-Getopt-03.t automatically tests all cases defined in the 'input' directory -and expects the output to match the corresponding file in the 'expected' directory. To -define a new test case, just create a new file in the 'input' directory containing the -input command line, and a corresponding file in the 'expected' directory containing -what you think the expanded command line should be. Note that this expansion is normalised -as follows: +Nagios-Plugin-Getopt-03.t automatically tests all cases defined in +the 'input' directory and expects the output to match the +corresponding file in the 'expected' directory. To define a new test +case, just create a new file in the 'input' directory containing the +input command line, and a corresponding file in the 'expected' +directory containing what you think the expanded command line should +be. Note that this expansion is normalised as follows: - command line arguments are reported in alphabetical order - extraneous white space is removed -Also, if you use a completely new argument than those defined in Nagios-Plugin-Getopt-03.t -you will need to define it there as well. +Also, if you use a completely new argument than those currently +defined in Nagios-Plugin-Getopt-03.t you will need to define it +there as well. diff --git a/t/npg03/expected/00_noextra b/t/npg03/expected/00_noextra new file mode 100644 index 0000000..d649587 --- /dev/null +++ b/t/npg03/expected/00_noextra @@ -0,0 +1 @@ +check_mysql -H localhost -S diff --git a/t/npg03/expected/05_disk1 b/t/npg03/expected/05_disk1 new file mode 100644 index 0000000..5570904 --- /dev/null +++ b/t/npg03/expected/05_disk1 @@ -0,0 +1 @@ +check_disk -p /tmp -p /home diff --git a/t/npg03/expected/05_disk2 b/t/npg03/expected/05_disk2 new file mode 100644 index 0000000..692890b --- /dev/null +++ b/t/npg03/expected/05_disk2 @@ -0,0 +1 @@ +check_disk -p /tmp -p /home -p /users diff --git a/t/npg03/expected/05_disk3 b/t/npg03/expected/05_disk3 new file mode 100644 index 0000000..5252b4e --- /dev/null +++ b/t/npg03/expected/05_disk3 @@ -0,0 +1 @@ +check_disk -p /tmp -p /var diff --git a/t/npg03/expected/05_disk4 b/t/npg03/expected/05_disk4 new file mode 100644 index 0000000..34b382e --- /dev/null +++ b/t/npg03/expected/05_disk4 @@ -0,0 +1 @@ +check_disk -p /tmp -p /var -p /home diff --git a/t/npg03/expected/05_disk5 b/t/npg03/expected/05_disk5 new file mode 100644 index 0000000..ae61e62 --- /dev/null +++ b/t/npg03/expected/05_disk5 @@ -0,0 +1 @@ +check_disk -p /var -p /tmp -p /home diff --git a/t/npg03/expected/05_disk6 b/t/npg03/expected/05_disk6 new file mode 100644 index 0000000..dc4870a --- /dev/null +++ b/t/npg03/expected/05_disk6 @@ -0,0 +1 @@ +check_disk2 --critical=5% --path=/var --path=/home --path=/usr --units=GB --warning=10% diff --git a/t/npg03/expected/05_singlechar1 b/t/npg03/expected/05_singlechar1 deleted file mode 100644 index 13a3f9b..0000000 --- a/t/npg03/expected/05_singlechar1 +++ /dev/null @@ -1 +0,0 @@ -check_disk -p /home diff --git a/t/npg03/expected/06_singlechar2 b/t/npg03/expected/06_singlechar2 deleted file mode 100644 index 8f9df5e..0000000 --- a/t/npg03/expected/06_singlechar2 +++ /dev/null @@ -1 +0,0 @@ -check_disk -p /var diff --git a/t/npg03/expected/07_singlechar3 b/t/npg03/expected/07_singlechar3 deleted file mode 100644 index f4e6ed7..0000000 --- a/t/npg03/expected/07_singlechar3 +++ /dev/null @@ -1 +0,0 @@ -check_disk -p /home -p /users diff --git a/t/npg03/input/00_basic b/t/npg03/input/00_basic index 4c16788..f35f3c7 100644 --- a/t/npg03/input/00_basic +++ b/t/npg03/input/00_basic @@ -1 +1 @@ -check_mysql -S --default-opts= --default-opts=more_options -H localhost +check_mysql -S --extra-opts= --extra-opts=more_options -H localhost diff --git a/t/npg03/input/00_noextra b/t/npg03/input/00_noextra new file mode 100644 index 0000000..4d8a8fc --- /dev/null +++ b/t/npg03/input/00_noextra @@ -0,0 +1 @@ +check_mysql -S -H localhost diff --git a/t/npg03/input/01_override1 b/t/npg03/input/01_override1 index 9e051e9..f3cd232 100644 --- a/t/npg03/input/01_override1 +++ b/t/npg03/input/01_override1 @@ -1 +1 @@ -check_mysql --username=admin --default-opts=more_options --warning=5 +check_mysql --username=admin --extra-opts=more_options --warning=5 diff --git a/t/npg03/input/02_override2 b/t/npg03/input/02_override2 index ceabe55..fa96ff7 100644 --- a/t/npg03/input/02_override2 +++ b/t/npg03/input/02_override2 @@ -1 +1 @@ -check_mysql --default-opts= -u admin +check_mysql --extra-opts= -u admin diff --git a/t/npg03/input/05_disk1 b/t/npg03/input/05_disk1 new file mode 100644 index 0000000..5ccfe23 --- /dev/null +++ b/t/npg03/input/05_disk1 @@ -0,0 +1 @@ +check_disk --extra-opts= -p /home diff --git a/t/npg03/input/05_disk2 b/t/npg03/input/05_disk2 new file mode 100644 index 0000000..53e36d9 --- /dev/null +++ b/t/npg03/input/05_disk2 @@ -0,0 +1 @@ +check_disk --extra-opts= -p /home -p /users diff --git a/t/npg03/input/05_disk3 b/t/npg03/input/05_disk3 new file mode 100644 index 0000000..441accb --- /dev/null +++ b/t/npg03/input/05_disk3 @@ -0,0 +1 @@ +check_disk --extra-opts=check_2_disks diff --git a/t/npg03/input/05_disk4 b/t/npg03/input/05_disk4 new file mode 100644 index 0000000..da9d810 --- /dev/null +++ b/t/npg03/input/05_disk4 @@ -0,0 +1 @@ +check_disk -p /home --extra-opts=check_2_disks diff --git a/t/npg03/input/05_disk5 b/t/npg03/input/05_disk5 new file mode 100644 index 0000000..9ba2d40 --- /dev/null +++ b/t/npg03/input/05_disk5 @@ -0,0 +1 @@ +check_disk -p /home --extra-opts=check_2_disks_reprise diff --git a/t/npg03/input/05_disk6 b/t/npg03/input/05_disk6 new file mode 100644 index 0000000..c240d9c --- /dev/null +++ b/t/npg03/input/05_disk6 @@ -0,0 +1 @@ +check_disk2 --warning=10% --critical=5% --extra-opts= --path=/usr diff --git a/t/npg03/input/05_singlechar1 b/t/npg03/input/05_singlechar1 deleted file mode 100644 index 1edb8bf..0000000 --- a/t/npg03/input/05_singlechar1 +++ /dev/null @@ -1 +0,0 @@ -check_disk --default-opts= -p /home diff --git a/t/npg03/input/06_singlechar2 b/t/npg03/input/06_singlechar2 deleted file mode 100644 index 24965c7..0000000 --- a/t/npg03/input/06_singlechar2 +++ /dev/null @@ -1 +0,0 @@ -check_disk --default-opts=check_2_disks diff --git a/t/npg03/input/07_singlechar3 b/t/npg03/input/07_singlechar3 deleted file mode 100644 index 0abc70f..0000000 --- a/t/npg03/input/07_singlechar3 +++ /dev/null @@ -1 +0,0 @@ -check_disk --default-opts= -p /home -p /users diff --git a/t/npg03/input/09_funnystuff b/t/npg03/input/09_funnystuff index c2d6160..ab279d9 100644 --- a/t/npg03/input/09_funnystuff +++ b/t/npg03/input/09_funnystuff @@ -1 +1 @@ -check_disk --default-opts=funny_stuff +check_disk --extra-opts=funny_stuff diff --git a/t/npg03/input/13_nosection_explicit_dies b/t/npg03/input/13_nosection_explicit_dies index 90aab51..f18660e 100644 --- a/t/npg03/input/13_nosection_explicit_dies +++ b/t/npg03/input/13_nosection_explicit_dies @@ -1 +1 @@ -check_no_section --default-opts= -H localhost +check_no_section --extra-opts= -H localhost diff --git a/t/npg03/input/14_badsection_dies b/t/npg03/input/14_badsection_dies index 70815a9..6b1c20a 100644 --- a/t/npg03/input/14_badsection_dies +++ b/t/npg03/input/14_badsection_dies @@ -1 +1 @@ -check_no_section --default-opts=bad_section +check_no_section --extra-opts=bad_section diff --git a/t/npg03/plugins.cfg b/t/npg03/plugins.cfg deleted file mode 100644 index f893a21..0000000 --- a/t/npg03/plugins.cfg +++ /dev/null @@ -1,21 +0,0 @@ -[check_mysql] -username=tonvoon -password=secret - -[more_options] -username=altinity -warning=10 -critical=15 - -[check_disk] -p=/tmp - -[check_2_disks] -p=/tmp -p=/var - -[funny_stuff] -username="Ton Voon" -p= -expect=" space in front" - diff --git a/t/npg03/plugins.ini b/t/npg03/plugins.ini new file mode 100644 index 0000000..2d3c551 --- /dev/null +++ b/t/npg03/plugins.ini @@ -0,0 +1,30 @@ +[check_mysql] +username=tonvoon +password=secret + +[more_options] +username=altinity +warning=10 +critical=15 + +[check_disk] +p=/tmp + +[check_2_disks] +p=/tmp +p=/var + +[check_2_disks_reprise] +p=/var +p=/tmp + +[check_disk2] +path=/var +path=/home +units=GB + +[funny_stuff] +username="Ton Voon" +p= +expect=" space in front" + -- cgit v1.2.3-74-g34f1