diff options
Diffstat (limited to 't/Nagios-Plugin-Getopt-01.t')
-rw-r--r-- | t/Nagios-Plugin-Getopt-01.t | 134 |
1 files changed, 134 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 | |||
3 | use strict; | ||
4 | |||
5 | use Test::More tests => 72; | ||
6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | ||
7 | |||
8 | my %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 | |||
17 | sub 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 | |||
38 | my $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; | ||
44 | is($ng->warning, 3, 'warning set to 3'); | ||
45 | is($ng->critical, 10, 'critical set to 10'); | ||
46 | is($ng->timeout, 12, 'timeout set to 12'); | ||
47 | |||
48 | # Missing args | ||
49 | @ARGV = qw(); | ||
50 | $ng = setup; | ||
51 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
52 | like($@, qr/Usage:/, 'usage message'); | ||
53 | like($@, qr/Missing arg/, 'missing arguments'); | ||
54 | is($ng->verbose, 0, 'verbose set to 0'); | ||
55 | # Missing critical | ||
56 | @ARGV = qw(-w0 -v); | ||
57 | $ng = setup; | ||
58 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
59 | like($@, qr/Usage:/, 'usage message'); | ||
60 | like($@, qr/Missing argument: critical/, 'missing argument: critical'); | ||
61 | unlike($@, qr/Missing argument: warning/, 'no missing argument: warning'); | ||
62 | is($ng->warning, 0, 'warning set to 0'); | ||
63 | is($ng->critical, undef, 'critical undef'); | ||
64 | is($ng->timeout, 15, 'timeout set to default'); | ||
65 | is($ng->verbose, 1, 'verbose set to true'); | ||
66 | # Missing warning | ||
67 | @ARGV = qw(--critical=27 --timeout 17 --verbose); | ||
68 | $ng = setup; | ||
69 | $ng->getopts; | ||
70 | is($ng->warning, 5, 'warning 5 (default)'); | ||
71 | is($ng->critical, 27, 'critical set to 27'); | ||
72 | is($ng->timeout, 17, 'timeout set to 17'); | ||
73 | is($ng->verbose, 1, 'verbose set to true'); | ||
74 | |||
75 | # -? --usage | ||
76 | @ARGV = ( '-?' ); | ||
77 | $ng = setup; | ||
78 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
79 | like($@, qr/Usage:/, 'usage message'); | ||
80 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
81 | @ARGV = ( '--usage' ); | ||
82 | $ng = setup; | ||
83 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
84 | like($@, qr/Usage:/, 'usage message'); | ||
85 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
86 | |||
87 | # -V --version | ||
88 | @ARGV = ( '-V' ); | ||
89 | $ng = setup; | ||
90 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
91 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
92 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
93 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
94 | unlike($@, qr/Usage:/, 'no usage message'); | ||
95 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
96 | @ARGV = ( '--version' ); | ||
97 | $ng = setup; | ||
98 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
99 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
100 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
101 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
102 | unlike($@, qr/Usage:/, 'no usage message'); | ||
103 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
104 | |||
105 | # -h --help | ||
106 | @ARGV = ( '-h' ); | ||
107 | $ng = setup; | ||
108 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
109 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
110 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
111 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
112 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
113 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
114 | like($@, qr/Usage:/, 'help includes usage message'); | ||
115 | like($@, qr/--version/, 'help includes default options 1'); | ||
116 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
117 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
118 | like($@, qr/--critical/, 'help includes custom option 2'); | ||
119 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
120 | @ARGV = ( '--help' ); | ||
121 | $ng = setup; | ||
122 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
123 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
124 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
125 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
126 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
127 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
128 | like($@, qr/Usage:/, 'help includes usage message'); | ||
129 | like($@, qr/--version/, 'help includes default options 1'); | ||
130 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
131 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
132 | like($@, qr/--critical/, 'help includes custom option 2'); | ||
133 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
134 | |||