summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Getopt-02.t
blob: c6c563edf812506fd296624d3bfbe192d5c19453 (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
# Monitoring::Plugin::Getopt timeout tests

use strict;

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

# Needed to get evals to work in testing
Monitoring::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',
    timeout => 18,
);

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

my $ng;

# No args
@ARGV = qw();
$ng = setup();
$ng->getopts;
is($ng->timeout, 18, 'default timeout set to 18');

# Check help message
@ARGV = ( '-h' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18');

# Explicit timeout
@ARGV = qw(--timeout=25 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 25, 'timeout changed to 25');

# Explicit timeout
@ARGV = qw(-t10 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 10, 'timeout changed to 10');

# Short timeout, test default timeout handler
@ARGV = qw(-t2 --verbose);
$ng = setup();
$ng->getopts;
is($ng->timeout, 2, 'timeout changed to 2');
alarm($ng->timeout);
# Loop
ok(! defined eval { 1 while 1 }, 'loop timed out');
like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok');