summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorGavin Carr <gonzai@users.sourceforge.net>2006-08-30 01:20:41 (GMT)
committerGavin Carr <gonzai@users.sourceforge.net>2006-08-30 01:20:41 (GMT)
commita70a6ff5acf2a2b252328427293801ce0ff42888 (patch)
tree8d3b573f3abaaa68f4eec0cbae4afa739bdfb2a7 /t
parent96933fd2e1f53aff9c9ef26639fafe9a84ec754e (diff)
downloadmonitoring-plugin-perl-a70a6ff5acf2a2b252328427293801ce0ff42888.tar.gz
Add first-pass Nagios::Plugin::Getopt.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1470 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
-rw-r--r--t/Nagios-Plugin-Getopt-01.t134
-rw-r--r--t/Nagios-Plugin-Getopt-02.t61
2 files changed, 195 insertions, 0 deletions
diff --git a/t/Nagios-Plugin-Getopt-01.t b/t/Nagios-Plugin-Getopt-01.t
new file mode 100644
index 0000000..fad68e3
--- /dev/null
+++ b/t/Nagios-Plugin-Getopt-01.t
@@ -0,0 +1,134 @@
1# Nagios::Plugin::Getopt basic tests
2
3use strict;
4
5use Test::More tests => 72;
6BEGIN { use_ok('Nagios::Plugin::Getopt') };
7
8my %PARAM = (
9 version => '0.01',
10 url => 'http://www.openfusion.com.au/labs/nagios/',
11 blurb => 'This plugin tests various stuff.',
12 usage => "Usage: %s -H <host> -w <warning_threshold>
13 -c <critical threshold>",
14 plugin => 'test_plugin',
15);
16
17sub setup
18{
19 # Instantiate object
20 my $ng = Nagios::Plugin::Getopt->new(%PARAM);
21 ok($ng, 'constructor ok');
22
23 # Add argument - short form - arg spec, help text, default, required?
24 $ng->arg('warning|w=s' =>
25 qq(-w, --warning=INTEGER\n Exit with WARNING status if less than INTEGER foobars are free),
26 5);
27
28 # Add argument - named version
29 $ng->arg(
30 spec => 'critical|c=s',
31 help => qq(-c, --critical=INTEGER\n Exit with CRITICAL status if less than INTEGER foobars are free),
32 required => 1,
33 );
34
35 return $ng;
36}
37
38my $ng;
39
40# Simple usage (short and long args)
41@ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose);
42$ng = setup;
43$ng->getopts;
44is($ng->warning, 3, 'warning set to 3');
45is($ng->critical, 10, 'critical set to 10');
46is($ng->timeout, 12, 'timeout set to 12');
47
48# Missing args
49@ARGV = qw();
50$ng = setup;
51ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
52like($@, qr/Usage:/, 'usage message');
53like($@, qr/Missing arg/, 'missing arguments');
54is($ng->verbose, 0, 'verbose set to 0');
55# Missing critical
56@ARGV = qw(-w0 -v);
57$ng = setup;
58ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
59like($@, qr/Usage:/, 'usage message');
60like($@, qr/Missing argument: critical/, 'missing argument: critical');
61unlike($@, qr/Missing argument: warning/, 'no missing argument: warning');
62is($ng->warning, 0, 'warning set to 0');
63is($ng->critical, undef, 'critical undef');
64is($ng->timeout, 15, 'timeout set to default');
65is($ng->verbose, 1, 'verbose set to true');
66# Missing warning
67@ARGV = qw(--critical=27 --timeout 17 --verbose);
68$ng = setup;
69$ng->getopts;
70is($ng->warning, 5, 'warning 5 (default)');
71is($ng->critical, 27, 'critical set to 27');
72is($ng->timeout, 17, 'timeout set to 17');
73is($ng->verbose, 1, 'verbose set to true');
74
75# -? --usage
76@ARGV = ( '-?' );
77$ng = setup;
78ok(! defined eval { $ng->getopts }, 'getopts died on usage');
79like($@, qr/Usage:/, 'usage message');
80unlike($@, qr/Missing arg/, 'no missing arguments');
81@ARGV = ( '--usage' );
82$ng = setup;
83ok(! defined eval { $ng->getopts }, 'getopts died on usage');
84like($@, qr/Usage:/, 'usage message');
85unlike($@, qr/Missing arg/, 'no missing arguments');
86
87# -V --version
88@ARGV = ( '-V' );
89$ng = setup;
90ok(! defined eval { $ng->getopts }, 'getopts died on version');
91like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
92like($@, qr/$PARAM{version}/, 'version info includes version');
93like($@, qr/$PARAM{url}/, 'version info includes url');
94unlike($@, qr/Usage:/, 'no usage message');
95unlike($@, qr/Missing arg/, 'no missing arguments');
96@ARGV = ( '--version' );
97$ng = setup;
98ok(! defined eval { $ng->getopts }, 'getopts died on version');
99like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
100like($@, qr/$PARAM{version}/, 'version info includes version');
101like($@, qr/$PARAM{url}/, 'version info includes url');
102unlike($@, qr/Usage:/, 'no usage message');
103unlike($@, qr/Missing arg/, 'no missing arguments');
104
105# -h --help
106@ARGV = ( '-h' );
107$ng = setup;
108ok(! defined eval { $ng->getopts }, 'getopts died on help');
109like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
110like($@, qr/$PARAM{version}/, 'help includes version');
111like($@, qr/$PARAM{url}/, 'help includes url');
112like($@, qr/General Public Licence/, 'help includes licence');
113like($@, qr/$PARAM{blurb}/, 'help includes blurb');
114like($@, qr/Usage:/, 'help includes usage message');
115like($@, qr/--version/, 'help includes default options 1');
116like($@, qr/--verbose/, 'help includes default options 2');
117like($@, qr/--warning/, 'help includes custom option 1');
118like($@, qr/--critical/, 'help includes custom option 2');
119unlike($@, qr/Missing arg/, 'no missing arguments');
120@ARGV = ( '--help' );
121$ng = setup;
122ok(! defined eval { $ng->getopts }, 'getopts died on help');
123like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
124like($@, qr/$PARAM{version}/, 'help includes version');
125like($@, qr/$PARAM{url}/, 'help includes url');
126like($@, qr/General Public Licence/, 'help includes licence');
127like($@, qr/$PARAM{blurb}/, 'help includes blurb');
128like($@, qr/Usage:/, 'help includes usage message');
129like($@, qr/--version/, 'help includes default options 1');
130like($@, qr/--verbose/, 'help includes default options 2');
131like($@, qr/--warning/, 'help includes custom option 1');
132like($@, qr/--critical/, 'help includes custom option 2');
133unlike($@, qr/Missing arg/, 'no missing arguments');
134
diff --git a/t/Nagios-Plugin-Getopt-02.t b/t/Nagios-Plugin-Getopt-02.t
new file mode 100644
index 0000000..26e0293
--- /dev/null
+++ b/t/Nagios-Plugin-Getopt-02.t
@@ -0,0 +1,61 @@
1# Nagios::Plugin::Getopt timeout tests
2
3use strict;
4
5use Test::More tests => 14;
6BEGIN { use_ok('Nagios::Plugin::Getopt') };
7
8my %PARAM = (
9 version => '0.01',
10 url => 'http://www.openfusion.com.au/labs/nagios/',
11 blurb => 'This plugin tests various stuff.',
12 usage => "Usage: %s -H <host> -w <warning_threshold>
13 -c <critical threshold>",
14 plugin => 'test_plugin',
15 timeout => 18,
16);
17
18sub setup
19{
20 # Instantiate object
21 my $ng = Nagios::Plugin::Getopt->new(%PARAM);
22 ok($ng, 'constructor ok');
23 return $ng;
24}
25
26my $ng;
27
28# No args
29@ARGV = qw();
30$ng = setup();
31$ng->getopts;
32is($ng->timeout, 18, 'default timeout set to 18');
33
34# Check help message
35@ARGV = ( '-h' );
36$ng = setup;
37ok(! defined eval { $ng->getopts }, 'getopts died on help');
38like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18');
39
40# Explicit timeout
41@ARGV = qw(--timeout=25 --verbose);
42$ng = setup();
43$ng->getopts;
44is($ng->timeout, 25, 'timeout changed to 25');
45
46# Explicit timeout
47@ARGV = qw(-t10 --verbose);
48$ng = setup();
49$ng->getopts;
50is($ng->timeout, 10, 'timeout changed to 10');
51
52# Short timeout, test default timeout handler
53@ARGV = qw(-t2 --verbose);
54$ng = setup();
55$ng->getopts;
56is($ng->timeout, 2, 'timeout changed to 2');
57alarm($ng->timeout);
58# Loop
59ok(! defined eval { 1 while 1 }, 'loop timed out');
60like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok');
61