diff options
Diffstat (limited to 't/Nagios-Plugin-Functions-01.t')
-rw-r--r-- | t/Nagios-Plugin-Functions-01.t | 161 |
1 files changed, 0 insertions, 161 deletions
diff --git a/t/Nagios-Plugin-Functions-01.t b/t/Nagios-Plugin-Functions-01.t deleted file mode 100644 index 5268255..0000000 --- a/t/Nagios-Plugin-Functions-01.t +++ /dev/null | |||
@@ -1,161 +0,0 @@ | |||
1 | |||
2 | use strict; | ||
3 | use Test::More tests => 113; | ||
4 | |||
5 | BEGIN { use_ok("Nagios::Plugin::Functions", ":all"); } | ||
6 | Nagios::Plugin::Functions::_fake_exit(1); | ||
7 | |||
8 | my $this_version=$Nagios::Plugin::Functions::VERSION; | ||
9 | foreach my $m ("", qw(::Threshold ::Getopt ::Performance ::Range)) { | ||
10 | my $mod = "Nagios::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"); | ||
16 | } | ||
17 | |||
18 | # check get_shortname | ||
19 | is(get_shortname, "NAGIOS-PLUGIN-FUNCTIONS-01", "get_shortname ok"); | ||
20 | |||
21 | # Hardcoded checks of constants | ||
22 | ok(defined %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}"); | ||
28 | |||
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', ], | ||
37 | ); | ||
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])); | ||
46 | |||
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])); | ||
57 | } | ||
58 | |||
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') ], | ||
66 | ); | ||
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])); | ||
74 | } | ||
75 | |||
76 | # nagios_exit message corner cases | ||
77 | my @ugly2 = ( | ||
78 | [ '' ], | ||
79 | [ undef ], | ||
80 | [ UNKNOWN ], | ||
81 | ); | ||
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")); | ||
89 | } | ||
90 | |||
91 | # Test nagios_die( $msg ) | ||
92 | my @msg = ( | ||
93 | [ 'die you dog' ], | ||
94 | [ '' ], | ||
95 | [ undef ], | ||
96 | ); | ||
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")); | ||
106 | } | ||
107 | |||
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', ], | ||
116 | ); | ||
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])); | ||
125 | |||
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])); | ||
133 | |||
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])); | ||
144 | |||
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])); | ||
155 | } | ||
156 | |||
157 | # Check that _use_die set to 1 will catch exceptions correctly | ||
158 | Nagios::Plugin::Functions::_fake_exit(0); | ||
159 | Nagios::Plugin::Functions::_use_die(1); | ||
160 | eval { nagios_die("Using die") }; | ||
161 | is( $@, "NAGIOS-PLUGIN-FUNCTIONS-01 UNKNOWN - Using die\n", "Caught exception"); | ||