Remove usage of defined %hash in test for perl 5.21.x and later
[Nagios-Plugin.git] / t / Nagios-Monitoring-Plugin-Functions-01.t
blobef4c3c8a6c8a99c2c602d1a8da9ca76091cd9e9c
2 use strict;
3 use Test::More tests => 113;
5 BEGIN { use_ok("Nagios::Monitoring::Plugin::Functions", ":all"); }
6 Nagios::Monitoring::Plugin::Functions::_fake_exit(1);
8 my $this_version=$Nagios::Monitoring::Plugin::Functions::VERSION;
9 foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) {
10         my $mod = "Nagios::Monitoring::Plugin$m";
11         use_ok($mod);
12         # Lots of hackery below. Easier to say $mod->VERSION, but this is probably a recent perl thing
13         my $v = "$mod"."::VERSION";
14         my $a = eval "\$$v";
15         is($a, $this_version, "Version number for $mod the same as Functions: $this_version");
18 # check get_shortname
19 is(get_shortname, "NAGIOS-MONITORING-PLUGIN-FUNCTIONS-01", "get_shortname ok");
21 # Hardcoded checks of constants
22 ok(%ERRORS, '%ERRORS defined');
23 is(OK,          $ERRORS{OK},            "OK        => $ERRORS{OK}");
24 is(WARNING,     $ERRORS{WARNING},       "WARNING   => $ERRORS{WARNING}");
25 is(CRITICAL,    $ERRORS{CRITICAL},      "CRITICAL  => $ERRORS{CRITICAL}");
26 is(UNKNOWN,     $ERRORS{UNKNOWN},       "UNKNOWN   => $ERRORS{UNKNOWN}");
27 is(DEPENDENT,   $ERRORS{DEPENDENT},     "DEPENDENT => $ERRORS{DEPENDENT}");
29 # Test nagios_exit( CONSTANT, $msg ), nagios_exit( $string, $msg )
30 my $r;
31 my @ok = (
32     [ OK,        "OK",           'test the first',  ],
33     [ WARNING,   "WARNING",      'test the second', ],
34     [ CRITICAL,  "CRITICAL",     'test the third',  ],
35     [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
36     [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
38 for (@ok) {
39     # CONSTANT
40     $r = nagios_exit($_->[0], $_->[2]);
41     is($r->return_code, $_->[0], 
42         sprintf('nagios_exit(%s, $msg) returned %s', $_->[1], $_->[0]));
43     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
44         sprintf('nagios_exit(%s, $msg) output matched "%s"', 
45             $_->[1], $_->[1] . '.*' . $_->[2]));
47     # $string
48     $r = nagios_exit($_->[1], $_->[2]);
49     is($r->return_code, $_->[0], 
50         sprintf('nagios_exit("%s", $msg) returned %s', $_->[1], $_->[0]));
51     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
52         sprintf('nagios_exit("%s", $msg) output matched "%s"', $_->[1], 
53             $_->[1] . '.*' . $_->[2]));
54     like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
55         sprintf('nagios_exit("%s", $msg) stringified matched "%s"', $_->[1], 
56             $_->[1] . '.*' . $_->[2]));
59 # nagios_exit code corner cases
60 my @ugly1 = (
61     [ -1, 'testing code -1' ],
62     [ 7, 'testing code 7' ],
63     [ undef, 'testing code undef' ],
64     [ '', qq(testing code '') ],
65     [ 'string', qq(testing code 'string') ],
67 for (@ugly1) {
68     $r = nagios_exit($_->[0], $_->[1]);
69     my $display = defined $_->[0] ? "'$_->[0]'" : 'undef';
70     is($r->return_code, UNKNOWN, "nagios_exit($display, \$msg) returned ". UNKNOWN);
71     like($r->message, qr/UNKNOWN\b.*\b$_->[1]$/, 
72         sprintf('nagios_exit(%s, $msg) output matched "%s"',
73             $display, 'UNKNOWN.*' . $_->[1]));
76 # nagios_exit message corner cases
77 my @ugly2 = (
78     [ '' ],
79     [ undef ],
80     [ UNKNOWN ],
82 for (@ugly2) {
83     $r = nagios_exit(CRITICAL, $_->[0]);
84     my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
85     my $display2 = defined $_->[0] ? $_->[0] : '';
86     like($r->message, qr/CRITICAL\b.*\b$display2$/, 
87         sprintf('nagios_exit(%s, $msg) output matched "%s"',
88             $display1, "CRITICAL.*$display2"));
91 # Test nagios_die( $msg )
92 my @msg = (
93     [ 'die you dog' ],
94     [ '' ],
95     [ undef ],
97 for (@msg) {
98     $r = nagios_die($_->[0]);
99     my $display1 = defined $_->[0] ? "'$_->[0]'" : "undef";
100     my $display2 = defined $_->[0] ? $_->[0] : '';
101     is($r->return_code, UNKNOWN,
102         sprintf('nagios_die(%s) returned UNKNOWN', $display1));
103     like($r->message, qr/UNKNOWN\b.*\b$display2$/, 
104         sprintf('nagios_die(%s) output matched "%s"', $display1,
105             "UNKNOWN.*$display2"));
108 # Test nagios_die( CONSTANT, $msg ), nagios_die( $msg, CONSTANT ), 
109 #   nagios_die( $string, $msg ), and nagios_die( $msg, $string )
110 @ok = (
111     [ OK,        "OK",           'test the first',  ],
112     [ WARNING,   "WARNING",      'test the second', ],
113     [ CRITICAL,  "CRITICAL",     'test the third',  ],
114     [ UNKNOWN,   "UNKNOWN",      'test the fourth', ],
115     [ DEPENDENT, "DEPENDENT",    'test the fifth',  ],
117 for (@ok) {
118     # CONSTANT, $msg
119     $r = nagios_die($_->[0], $_->[2]);
120     is($r->return_code, $_->[0], 
121         sprintf('nagios_die(%s, $msg) returned %s', $_->[1], $_->[0]));
122     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
123         sprintf('nagios_die(%s, $msg) output matched "%s"', 
124             $_->[1], $_->[1] . '.*' . $_->[2]));
126     # $msg, CONSTANT
127     $r = nagios_die($_->[2], $_->[0]);
128     is($r->return_code, $_->[0], 
129         sprintf('nagios_die($msg, %s) returned %s', $_->[1], $_->[0]));
130     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
131         sprintf('nagios_die($msg, %s) output matched "%s"', 
132             $_->[1], $_->[1] . '.*' . $_->[2]));
134     # $string, $msg
135     $r = nagios_die($_->[1], $_->[2]);
136     is($r->return_code, $_->[0], 
137         sprintf('nagios_die("%s", $msg) returned %s', $_->[1], $_->[0]));
138     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
139         sprintf('nagios_die("%s", $msg) output matched "%s"', $_->[1], 
140             $_->[1] . '.*' . $_->[2]));
141     like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
142         sprintf('nagios_die("%s", $msg) stringified matched "%s"', $_->[1], 
143             $_->[1] . '.*' . $_->[2]));
145     # $string, $msg
146     $r = nagios_die($_->[2], $_->[1]);
147     is($r->return_code, $_->[0], 
148         sprintf('nagios_die($msg, "%s") returned %s', $_->[1], $_->[0]));
149     like($r->message, qr/$_->[1]\b.*\b$_->[2]$/, 
150         sprintf('nagios_die($msg, "%s") output matched "%s"', $_->[1], 
151             $_->[1] . '.*' . $_->[2]));
152     like($r, qr/$_->[1]\b.*\b$_->[2]$/, 
153         sprintf('nagios_die($msg, "%s") stringified matched "%s"', $_->[1], 
154             $_->[1] . '.*' . $_->[2]));
157 # Check that _use_die set to 1 will catch exceptions correctly
158 Nagios::Monitoring::Plugin::Functions::_fake_exit(0);
159 Nagios::Monitoring::Plugin::Functions::_use_die(1);
160 eval { nagios_die("Using die") };
161 is( $@, "NAGIOS-MONITORING-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception");