diff options
Diffstat (limited to 't/Monitoring-Plugin-Getopt-01.t')
-rw-r--r-- | t/Monitoring-Plugin-Getopt-01.t | 149 |
1 files changed, 149 insertions, 0 deletions
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 @@ | |||
1 | # Monitoring::Plugin::Getopt basic tests | ||
2 | |||
3 | use strict; | ||
4 | |||
5 | use Test::More tests => 76; | ||
6 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; | ||
7 | |||
8 | # Needed to get evals to work in testing | ||
9 | Monitoring::Plugin::Functions::_use_die(1); | ||
10 | |||
11 | my %PARAM = ( | ||
12 | version => '0.01', | ||
13 | url => 'http://www.openfusion.com.au/labs/nagios/', | ||
14 | blurb => 'This plugin tests various stuff.', | ||
15 | usage => "Usage: %s -H <host> -w <warning_threshold> | ||
16 | -c <critical threshold>", | ||
17 | plugin => 'test_plugin', | ||
18 | ); | ||
19 | |||
20 | sub setup | ||
21 | { | ||
22 | # Instantiate object | ||
23 | my $ng = Monitoring::Plugin::Getopt->new(%PARAM); | ||
24 | ok($ng, 'constructor ok'); | ||
25 | |||
26 | # Add argument - short form - arg spec, help text, default, required? | ||
27 | $ng->arg('warning|w=s' => | ||
28 | qq(-w, --warning=INTEGER\n Exit with WARNING status if less than INTEGER foobars are free), | ||
29 | 5); | ||
30 | |||
31 | # Add argument - named version | ||
32 | $ng->arg( | ||
33 | spec => 'critical|c=i', | ||
34 | help => qq(Exit with CRITICAL status if less than INTEGER foobars are free), | ||
35 | required => 1, | ||
36 | ); | ||
37 | |||
38 | return $ng; | ||
39 | } | ||
40 | |||
41 | my $ng; | ||
42 | |||
43 | # Simple usage (short and long args) | ||
44 | @ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose); | ||
45 | $ng = setup; | ||
46 | $ng->getopts; | ||
47 | is($ng->warning, 3, 'warning set to 3'); | ||
48 | is($ng->critical, 10, 'critical set to 10'); | ||
49 | is($ng->timeout, 12, 'timeout set to 12'); | ||
50 | |||
51 | # Check multiple verbose flags | ||
52 | @ARGV = qw(-w 3 --critical 10 -v -v -v); | ||
53 | $ng = setup; | ||
54 | $ng->getopts; | ||
55 | is ($ng->verbose, 3, "Verbose set to level 3"); | ||
56 | |||
57 | @ARGV = qw(-w 3 --critical 10 --verbose --verbose --verbose); | ||
58 | $ng = setup; | ||
59 | $ng->getopts; | ||
60 | is ($ng->verbose, 3, "Verbose set to level 3 (longhand)"); | ||
61 | |||
62 | # Missing args | ||
63 | @ARGV = qw(); | ||
64 | $ng = setup; | ||
65 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
66 | like($@, qr/Usage:/, 'usage message'); | ||
67 | like($@, qr/Missing arg/, 'missing arguments'); | ||
68 | is($ng->verbose, 0, 'verbose set to 0'); | ||
69 | # Missing critical | ||
70 | @ARGV = qw(-w0 -v); | ||
71 | $ng = setup; | ||
72 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
73 | like($@, qr/Usage:/, 'usage message'); | ||
74 | like($@, qr/Missing argument: critical/, 'missing argument: critical'); | ||
75 | unlike($@, qr/Missing argument: warning/, 'no missing argument: warning'); | ||
76 | is($ng->warning, 0, 'warning set to 0'); | ||
77 | is($ng->critical, undef, 'critical undef'); | ||
78 | is($ng->timeout, 15, 'timeout set to default'); | ||
79 | is($ng->verbose, 1, 'verbose set to true'); | ||
80 | # Missing warning | ||
81 | @ARGV = qw(--critical=27 --timeout 17 --verbose); | ||
82 | $ng = setup; | ||
83 | $ng->getopts; | ||
84 | is($ng->warning, 5, 'warning 5 (default)'); | ||
85 | is($ng->critical, 27, 'critical set to 27'); | ||
86 | is($ng->timeout, 17, 'timeout set to 17'); | ||
87 | is($ng->verbose, 1, 'verbose set to true'); | ||
88 | |||
89 | # -? --usage | ||
90 | @ARGV = ( '-?' ); | ||
91 | $ng = setup; | ||
92 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
93 | like($@, qr/Usage:/, 'usage message'); | ||
94 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
95 | @ARGV = ( '--usage' ); | ||
96 | $ng = setup; | ||
97 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
98 | like($@, qr/Usage:/, 'usage message'); | ||
99 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
100 | |||
101 | # -V --version | ||
102 | @ARGV = ( '-V' ); | ||
103 | $ng = setup; | ||
104 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
105 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
106 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
107 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
108 | unlike($@, qr/Usage:/, 'no usage message'); | ||
109 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
110 | |||
111 | @ARGV = ( '--version' ); | ||
112 | $ng = setup; | ||
113 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
114 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
115 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
116 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
117 | unlike($@, qr/Usage:/, 'no usage message'); | ||
118 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
119 | |||
120 | # -h --help | ||
121 | @ARGV = ( '-h' ); | ||
122 | $ng = setup; | ||
123 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
124 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
125 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
126 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
127 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
128 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
129 | like($@, qr/Usage:/, 'help includes usage message'); | ||
130 | like($@, qr/--version/, 'help includes default options 1'); | ||
131 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
132 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
133 | like($@, qr/--critical/, 'help includes custom option 2'); | ||
134 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
135 | |||
136 | @ARGV = ( '--help' ); | ||
137 | $ng = setup; | ||
138 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
139 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
140 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
141 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
142 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
143 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
144 | like($@, qr/Usage:/, 'help includes usage message'); | ||
145 | like($@, qr/--version/, 'help includes default options 1'); | ||
146 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
147 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
148 | like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args'); | ||
149 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||