summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Getopt-01.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Getopt-01.t')
-rw-r--r--t/Monitoring-Plugin-Getopt-01.t149
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
3use strict;
4
5use Test::More tests => 76;
6BEGIN { use_ok('Monitoring::Plugin::Getopt') };
7
8# Needed to get evals to work in testing
9Monitoring::Plugin::Functions::_use_die(1);
10
11my %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
20sub 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
41my $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;
47is($ng->warning, 3, 'warning set to 3');
48is($ng->critical, 10, 'critical set to 10');
49is($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;
55is ($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;
60is ($ng->verbose, 3, "Verbose set to level 3 (longhand)");
61
62# Missing args
63@ARGV = qw();
64$ng = setup;
65ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
66like($@, qr/Usage:/, 'usage message');
67like($@, qr/Missing arg/, 'missing arguments');
68is($ng->verbose, 0, 'verbose set to 0');
69# Missing critical
70@ARGV = qw(-w0 -v);
71$ng = setup;
72ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
73like($@, qr/Usage:/, 'usage message');
74like($@, qr/Missing argument: critical/, 'missing argument: critical');
75unlike($@, qr/Missing argument: warning/, 'no missing argument: warning');
76is($ng->warning, 0, 'warning set to 0');
77is($ng->critical, undef, 'critical undef');
78is($ng->timeout, 15, 'timeout set to default');
79is($ng->verbose, 1, 'verbose set to true');
80# Missing warning
81@ARGV = qw(--critical=27 --timeout 17 --verbose);
82$ng = setup;
83$ng->getopts;
84is($ng->warning, 5, 'warning 5 (default)');
85is($ng->critical, 27, 'critical set to 27');
86is($ng->timeout, 17, 'timeout set to 17');
87is($ng->verbose, 1, 'verbose set to true');
88
89# -? --usage
90@ARGV = ( '-?' );
91$ng = setup;
92ok(! defined eval { $ng->getopts }, 'getopts died on usage');
93like($@, qr/Usage:/, 'usage message');
94unlike($@, qr/Missing arg/, 'no missing arguments');
95@ARGV = ( '--usage' );
96$ng = setup;
97ok(! defined eval { $ng->getopts }, 'getopts died on usage');
98like($@, qr/Usage:/, 'usage message');
99unlike($@, qr/Missing arg/, 'no missing arguments');
100
101# -V --version
102@ARGV = ( '-V' );
103$ng = setup;
104ok(! defined eval { $ng->getopts }, 'getopts died on version');
105like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
106like($@, qr/$PARAM{version}/, 'version info includes version');
107like($@, qr/$PARAM{url}/, 'version info includes url');
108unlike($@, qr/Usage:/, 'no usage message');
109unlike($@, qr/Missing arg/, 'no missing arguments');
110
111@ARGV = ( '--version' );
112$ng = setup;
113ok(! defined eval { $ng->getopts }, 'getopts died on version');
114like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
115like($@, qr/$PARAM{version}/, 'version info includes version');
116like($@, qr/$PARAM{url}/, 'version info includes url');
117unlike($@, qr/Usage:/, 'no usage message');
118unlike($@, qr/Missing arg/, 'no missing arguments');
119
120# -h --help
121@ARGV = ( '-h' );
122$ng = setup;
123ok(! defined eval { $ng->getopts }, 'getopts died on help');
124like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
125like($@, qr/$PARAM{version}/, 'help includes version');
126like($@, qr/$PARAM{url}/, 'help includes url');
127like($@, qr/General Public Licence/, 'help includes licence');
128like($@, qr/$PARAM{blurb}/, 'help includes blurb');
129like($@, qr/Usage:/, 'help includes usage message');
130like($@, qr/--version/, 'help includes default options 1');
131like($@, qr/--verbose/, 'help includes default options 2');
132like($@, qr/--warning/, 'help includes custom option 1');
133like($@, qr/--critical/, 'help includes custom option 2');
134unlike($@, qr/Missing arg/, 'no missing arguments');
135
136@ARGV = ( '--help' );
137$ng = setup;
138ok(! defined eval { $ng->getopts }, 'getopts died on help');
139like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
140like($@, qr/$PARAM{version}/, 'help includes version');
141like($@, qr/$PARAM{url}/, 'help includes url');
142like($@, qr/General Public Licence/, 'help includes licence');
143like($@, qr/$PARAM{blurb}/, 'help includes blurb');
144like($@, qr/Usage:/, 'help includes usage message');
145like($@, qr/--version/, 'help includes default options 1');
146like($@, qr/--verbose/, 'help includes default options 2');
147like($@, qr/--warning/, 'help includes custom option 1');
148like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args');
149unlike($@, qr/Missing arg/, 'no missing arguments');