diff options
Diffstat (limited to 't')
32 files changed, 150 insertions, 30 deletions
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 @@ | |||
1 | 1 | ||
2 | # tests for toplevel access to Threshold and GetOpts stuff | 2 | # tests for toplevel access to Threshold and GetOpts stuff |
3 | # $Id$ | ||
4 | 3 | ||
5 | use strict; | 4 | use strict; |
6 | #use Test::More 'no_plan'; | 5 | #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 | |||
27 | 27 | ||
28 | # Add argument - named version | 28 | # Add argument - named version |
29 | $ng->arg( | 29 | $ng->arg( |
30 | spec => 'critical|c=s', | 30 | spec => 'critical|c=i', |
31 | help => qq(-c, --critical=INTEGER\n Exit with CRITICAL status if less than INTEGER foobars are free), | 31 | help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), |
32 | required => 1, | 32 | required => 1, |
33 | ); | 33 | ); |
34 | 34 | ||
@@ -104,6 +104,7 @@ like($@, qr/$PARAM{version}/, 'version info includes version'); | |||
104 | like($@, qr/$PARAM{url}/, 'version info includes url'); | 104 | like($@, qr/$PARAM{url}/, 'version info includes url'); |
105 | unlike($@, qr/Usage:/, 'no usage message'); | 105 | unlike($@, qr/Usage:/, 'no usage message'); |
106 | unlike($@, qr/Missing arg/, 'no missing arguments'); | 106 | unlike($@, qr/Missing arg/, 'no missing arguments'); |
107 | |||
107 | @ARGV = ( '--version' ); | 108 | @ARGV = ( '--version' ); |
108 | $ng = setup; | 109 | $ng = setup; |
109 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | 110 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); |
@@ -128,6 +129,7 @@ like($@, qr/--verbose/, 'help includes default options 2'); | |||
128 | like($@, qr/--warning/, 'help includes custom option 1'); | 129 | like($@, qr/--warning/, 'help includes custom option 1'); |
129 | like($@, qr/--critical/, 'help includes custom option 2'); | 130 | like($@, qr/--critical/, 'help includes custom option 2'); |
130 | unlike($@, qr/Missing arg/, 'no missing arguments'); | 131 | unlike($@, qr/Missing arg/, 'no missing arguments'); |
132 | |||
131 | @ARGV = ( '--help' ); | 133 | @ARGV = ( '--help' ); |
132 | $ng = setup; | 134 | $ng = setup; |
133 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | 135 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); |
@@ -140,6 +142,6 @@ like($@, qr/Usage:/, 'help includes usage message'); | |||
140 | like($@, qr/--version/, 'help includes default options 1'); | 142 | like($@, qr/--version/, 'help includes default options 1'); |
141 | like($@, qr/--verbose/, 'help includes default options 2'); | 143 | like($@, qr/--verbose/, 'help includes default options 2'); |
142 | like($@, qr/--warning/, 'help includes custom option 1'); | 144 | like($@, qr/--warning/, 'help includes custom option 1'); |
143 | like($@, qr/--critical/, 'help includes custom option 2'); | 145 | like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); |
144 | unlike($@, qr/Missing arg/, 'no missing arguments'); | 146 | unlike($@, qr/Missing arg/, 'no missing arguments'); |
145 | 147 | ||
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 @@ | |||
1 | # Nagios::Plugin::Getopt --default-opts tests | 1 | # Nagios::Plugin::Getopt --extra-opts tests |
2 | 2 | ||
3 | use strict; | 3 | use strict; |
4 | use File::Spec; | 4 | use File::Spec; |
@@ -27,7 +27,8 @@ for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) { | |||
27 | } | 27 | } |
28 | } | 28 | } |
29 | 29 | ||
30 | $Nagios::Plugin::Getopt::DEFAULT_CONFIG_FILE = File::Spec->catfile($tdir, 'plugins.cfg'); | 30 | # Override NAGIOS_CONFIG_PATH to use our test plugins.ini file |
31 | $ENV{NAGIOS_CONFIG_PATH} = "/random/bogus/path:$tdir"; | ||
31 | 32 | ||
32 | my %PARAM = ( | 33 | my %PARAM = ( |
33 | version => '0.01', | 34 | version => '0.01', |
@@ -56,14 +57,18 @@ my $arg = [ | |||
56 | { spec => 'S', help => '-S' }, | 57 | { spec => 'S', help => '-S' }, |
57 | { spec => 'H=s', help => '-H' }, | 58 | { spec => 'H=s', help => '-H' }, |
58 | { spec => 'p=s@', help => '-p' }, | 59 | { spec => 'p=s@', help => '-p' }, |
60 | { spec => 'path=s@', help => '--path' }, | ||
59 | { spec => 'username|u=s', help => '--username' }, | 61 | { spec => 'username|u=s', help => '--username' }, |
60 | { spec => 'password=s', help => '--password' }, | 62 | { spec => 'password=s', help => '--password' }, |
61 | { spec => 'critical=i', help => '--critical' }, | 63 | { spec => 'critical=s', help => '--critical' }, |
62 | { spec => 'warning=i', help => '--warning' }, | 64 | { spec => 'warning=s', help => '--warning' }, |
63 | { spec => 'expect=s', help => '--expect' }, | 65 | { spec => 'expect=s', help => '--expect' }, |
66 | { spec => 'units=s', help => '--units' }, | ||
64 | ]; | 67 | ]; |
65 | 68 | ||
66 | my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); | 69 | #my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); |
70 | #my %SKIP = map { $_ => 1 } qw(06_singlechar2); | ||
71 | my %SKIP = (); | ||
67 | 72 | ||
68 | # Process all test cases in $tdir/input | 73 | # Process all test cases in $tdir/input |
69 | my $glob = $ARGV[0] || '*'; | 74 | my $glob = $ARGV[0] || '*'; |
@@ -82,7 +87,7 @@ for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) { | |||
82 | 87 | ||
83 | # Parse the options | 88 | # Parse the options |
84 | SKIP: { | 89 | SKIP: { |
85 | skip "Still discussing how overrides with multiple arguments should work ...", 1 if $SKIP{$infile}; | 90 | skip "Skipping ..." if $SKIP{$infile}; |
86 | 91 | ||
87 | @ARGV = @args; | 92 | @ARGV = @args; |
88 | eval { $ng->getopts }; | 93 | 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 @@ | |||
1 | # Nagios::Plugin::Getopt spec-to-help generation tests | ||
2 | |||
3 | use strict; | ||
4 | |||
5 | use Test::More tests => 11; | ||
6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | ||
7 | |||
8 | my %PARAM = ( | ||
9 | version => '0.01', | ||
10 | usage => "Don't use this plugin!", | ||
11 | ); | ||
12 | |||
13 | sub setup | ||
14 | { | ||
15 | # Instantiate object | ||
16 | my $ng = Nagios::Plugin::Getopt->new(%PARAM); | ||
17 | ok($ng, 'constructor ok'); | ||
18 | |||
19 | # Positional args, no short arguments, INTEGER | ||
20 | $ng->arg('warning=i' => | ||
21 | qq(Exit with WARNING status if less than INTEGER foobars are free), | ||
22 | 5); | ||
23 | |||
24 | # Named args, long + short arguments, INTEGER | ||
25 | $ng->arg( | ||
26 | spec => 'critical|c=i', | ||
27 | help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), | ||
28 | required => 1, | ||
29 | ); | ||
30 | |||
31 | # Named args, multiple short arguments, STRING, default expansion | ||
32 | $ng->arg( | ||
33 | spec => 'x|y|z=s', | ||
34 | help => qq(Foobar. Default: %s), | ||
35 | default => "XYZ", | ||
36 | ); | ||
37 | |||
38 | # Named args, multiple mixed, no label | ||
39 | $ng->arg( | ||
40 | spec => 'long|longer|longest|l', | ||
41 | help => qq(Long format), | ||
42 | ); | ||
43 | |||
44 | # Named args, long + short, explicit label | ||
45 | $ng->arg( | ||
46 | spec => 'hostname|H=s', | ||
47 | label => 'ADDRESS', | ||
48 | help => qq(Hostname), | ||
49 | ); | ||
50 | |||
51 | # Positional args, long only, explicit label | ||
52 | $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR'); | ||
53 | |||
54 | # Multiline help test, named args | ||
55 | $ng->arg( | ||
56 | spec => 'disk=s', | ||
57 | label => [ qw(BYTES PERCENT%), undef ], | ||
58 | help => [ | ||
59 | qq(Disk limit in BYTES), | ||
60 | qq(Disk limit in PERCENT), | ||
61 | qq(Disk limit in FOOBARS (Default: %s)), | ||
62 | ], | ||
63 | default => 1024, | ||
64 | ); | ||
65 | |||
66 | # Multiline help test, positional args | ||
67 | $ng->arg( | ||
68 | 'limit=s', | ||
69 | [ | ||
70 | qq(Limit in BYTES), | ||
71 | qq(Limit in PERCENT), | ||
72 | ], | ||
73 | undef, | ||
74 | undef, | ||
75 | [ undef, 'PERCENT%' ], | ||
76 | ); | ||
77 | |||
78 | return $ng; | ||
79 | } | ||
80 | |||
81 | my $ng; | ||
82 | |||
83 | @ARGV = ( '--help' ); | ||
84 | $ng = setup; | ||
85 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
86 | like($@, qr/\n --warning=INTEGER/, 'warning ok'); | ||
87 | like($@, qr/\n -c, --critical=INTEGER/, 'critical ok'); | ||
88 | like($@, qr/\n -x, -y, -z=STRING\n Foobar. Default: XYZ\n/, 'x|y|z ok'); | ||
89 | like($@, qr/\n -l, --long, --longer, --longest\n Long format\n/, 'long ok'); | ||
90 | like($@, qr/\n -H, --hostname=ADDRESS\n Hostname\n/, 'hostname ok'); | ||
91 | like($@, qr/\n --avatar=AVATAR\n Avatar\n/, 'avatar ok'); | ||
92 | 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'); | ||
93 | like($@, qr/\n --limit=STRING\n Limit in BYTES\n --limit=PERCENT%\n Limit in PERCENT\n/, 'limit multiline ok'); | ||
94 | #print $@; | ||
95 | |||
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 @@ | |||
1 | Nagios-Plugin-Getopt-03.t automatically tests all cases defined in the 'input' directory | 1 | Nagios-Plugin-Getopt-03.t automatically tests all cases defined in |
2 | and expects the output to match the corresponding file in the 'expected' directory. To | 2 | the 'input' directory and expects the output to match the |
3 | define a new test case, just create a new file in the 'input' directory containing the | 3 | corresponding file in the 'expected' directory. To define a new test |
4 | input command line, and a corresponding file in the 'expected' directory containing | 4 | case, just create a new file in the 'input' directory containing the |
5 | what you think the expanded command line should be. Note that this expansion is normalised | 5 | input command line, and a corresponding file in the 'expected' |
6 | as follows: | 6 | directory containing what you think the expanded command line should |
7 | be. Note that this expansion is normalised as follows: | ||
7 | 8 | ||
8 | - command line arguments are reported in alphabetical order | 9 | - command line arguments are reported in alphabetical order |
9 | - extraneous white space is removed | 10 | - extraneous white space is removed |
10 | 11 | ||
11 | Also, if you use a completely new argument than those defined in Nagios-Plugin-Getopt-03.t | 12 | Also, if you use a completely new argument than those currently |
12 | you will need to define it there as well. | 13 | defined in Nagios-Plugin-Getopt-03.t you will need to define it |
14 | there as well. | ||
13 | 15 | ||
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 @@ | |||
1 | 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 @@ | |||
1 | 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 @@ | |||
1 | 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 @@ | |||
1 | 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 @@ | |||
1 | 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 @@ | |||
1 | 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.ini index f893a21..2d3c551 100644 --- a/t/npg03/plugins.cfg +++ b/t/npg03/plugins.ini | |||
@@ -14,6 +14,15 @@ p=/tmp | |||
14 | p=/tmp | 14 | p=/tmp |
15 | p=/var | 15 | p=/var |
16 | 16 | ||
17 | [check_2_disks_reprise] | ||
18 | p=/var | ||
19 | p=/tmp | ||
20 | |||
21 | [check_disk2] | ||
22 | path=/var | ||
23 | path=/home | ||
24 | units=GB | ||
25 | |||
17 | [funny_stuff] | 26 | [funny_stuff] |
18 | username="Ton Voon" | 27 | username="Ton Voon" |
19 | p= | 28 | p= |