summaryrefslogtreecommitdiffstats
path: root/t/Nagios-Plugin-Getopt-01.t
blob: 7076d75de1b1b2544f542132aeb316c8e428d4c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Nagios::Plugin::Getopt basic tests

use strict;

use Test::More tests => 76;
BEGIN { use_ok('Nagios::Plugin::Getopt') };

# Needed to get evals to work in testing
Nagios::Plugin::Functions::_use_die(1);

my %PARAM = (
    version => '0.01',
    url => 'http://www.openfusion.com.au/labs/nagios/',
    blurb => 'This plugin tests various stuff.', 
    usage => "Usage: %s -H <host> -w <warning_threshold> 
  -c <critical threshold>",
    plugin => 'test_plugin',
);

sub setup 
{
  # Instantiate object
  my $ng = Nagios::Plugin::Getopt->new(%PARAM);
  ok($ng, 'constructor ok');

  # Add argument - short form - arg spec, help text, default, required?
  $ng->arg('warning|w=s' =>
    qq(-w, --warning=INTEGER\n   Exit with WARNING status if less than INTEGER foobars are free),
    5);
  
  # Add argument - named version
  $ng->arg(
    spec => 'critical|c=i',
    help => qq(Exit with CRITICAL status if less than INTEGER foobars are free),
    required => 1,
  );

  return $ng;
}

my $ng;

# Simple usage (short and long args)
@ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose);
$ng = setup;
$ng->getopts;
is($ng->warning, 3, 'warning set to 3');
is($ng->critical, 10, 'critical set to 10');
is($ng->timeout, 12, 'timeout set to 12');

# Check multiple verbose flags
@ARGV = qw(-w 3 --critical 10 -v -v -v);
$ng = setup;
$ng->getopts;
is ($ng->verbose, 3, "Verbose set to level 3");

@ARGV = qw(-w 3 --critical 10 --verbose --verbose --verbose);
$ng = setup;
$ng->getopts;
is ($ng->verbose, 3, "Verbose set to level 3 (longhand)");

# Missing args
@ARGV = qw();
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
like($@, qr/Usage:/, 'usage message');
like($@, qr/Missing arg/, 'missing arguments');
is($ng->verbose, 0, 'verbose set to 0');
# Missing critical
@ARGV = qw(-w0 -v);
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on missing args');
like($@, qr/Usage:/, 'usage message');
like($@, qr/Missing argument: critical/, 'missing argument: critical');
unlike($@, qr/Missing argument: warning/, 'no missing argument: warning');
is($ng->warning, 0, 'warning set to 0');
is($ng->critical, undef, 'critical undef');
is($ng->timeout, 15, 'timeout set to default');
is($ng->verbose, 1, 'verbose set to true');
# Missing warning
@ARGV = qw(--critical=27 --timeout 17 --verbose);
$ng = setup;
$ng->getopts;
is($ng->warning, 5, 'warning 5 (default)');
is($ng->critical, 27, 'critical set to 27');
is($ng->timeout, 17, 'timeout set to 17');
is($ng->verbose, 1, 'verbose set to true');

# -? --usage
@ARGV = ( '-?' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on usage');
like($@, qr/Usage:/, 'usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');
@ARGV = ( '--usage' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on usage');
like($@, qr/Usage:/, 'usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');

# -V --version
@ARGV = ( '-V' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on version');
like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
like($@, qr/$PARAM{version}/, 'version info includes version');
like($@, qr/$PARAM{url}/, 'version info includes url');
unlike($@, qr/Usage:/, 'no usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');

@ARGV = ( '--version' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on version');
like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name');
like($@, qr/$PARAM{version}/, 'version info includes version');
like($@, qr/$PARAM{url}/, 'version info includes url');
unlike($@, qr/Usage:/, 'no usage message');
unlike($@, qr/Missing arg/, 'no missing arguments');

# -h --help
@ARGV = ( '-h' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
like($@, qr/$PARAM{version}/, 'help includes version');
like($@, qr/$PARAM{url}/, 'help includes url');
like($@, qr/General Public Licence/, 'help includes licence');
like($@, qr/$PARAM{blurb}/, 'help includes blurb');
like($@, qr/Usage:/, 'help includes usage message');
like($@, qr/--version/, 'help includes default options 1');
like($@, qr/--verbose/, 'help includes default options 2');
like($@, qr/--warning/, 'help includes custom option 1');
like($@, qr/--critical/, 'help includes custom option 2');
unlike($@, qr/Missing arg/, 'no missing arguments');

@ARGV = ( '--help' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/^$PARAM{plugin}/, 'help includes plugin name');
like($@, qr/$PARAM{version}/, 'help includes version');
like($@, qr/$PARAM{url}/, 'help includes url');
like($@, qr/General Public Licence/, 'help includes licence');
like($@, qr/$PARAM{blurb}/, 'help includes blurb');
like($@, qr/Usage:/, 'help includes usage message');
like($@, qr/--version/, 'help includes default options 1');
like($@, qr/--verbose/, 'help includes default options 2');
like($@, qr/--warning/, 'help includes custom option 1');
like($@, qr/-c, --critical=INTEGER/, 'help includes custom option 2, with expanded args');
unlike($@, qr/Missing arg/, 'no missing arguments');