summaryrefslogtreecommitdiffstats
path: root/t/Nagios-Plugin-04.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Nagios-Plugin-04.t')
-rw-r--r--t/Nagios-Plugin-04.t91
1 files changed, 91 insertions, 0 deletions
diff --git a/t/Nagios-Plugin-04.t b/t/Nagios-Plugin-04.t
new file mode 100644
index 0000000..a110b4c
--- /dev/null
+++ b/t/Nagios-Plugin-04.t
@@ -0,0 +1,91 @@
1
2# tests for toplevel access to Threshold and GetOpts stuff
3# $Id$
4
5use strict;
6#use Test::More 'no_plan';
7use Test::More tests=>26;
8use Test::Exception;
9
10BEGIN { use_ok('Nagios::Plugin') };
11use Nagios::Plugin::Functions;
12Nagios::Plugin::Functions::_fake_exit(1);
13
14
15lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage";
16
17my $p = Nagios::Plugin->new();
18dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage";
19dies_ok sub { $p->getopts }, "getopts() dies if you haven't instantiated with usage";
20
21$p = Nagios::Plugin->new( usage => "dummy usage statement" );
22
23# option accessors work
24can_ok $p, 'opts';
25isa_ok $p->opts, 'Nagios::Plugin::Getopt', "Getopt object is defined";
26
27$p->add_arg('warning|w=s', "warning");
28$p->add_arg('critical|c=s', "critical");
29
30@ARGV = qw(-w 5 -c 10);
31$p->getopts;
32is $p->opts->warning, "5", "warning opt is accessible";
33is $p->opts->critical, "10", "critical opt is accessible";
34
35
36can_ok $p, 'perfdata';
37#isa_ok $p->perfdata, 'Nagios::Plugin::Performance', "perfdata object is defined";
38
39
40can_ok $p, 'threshold';
41#isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined";
42
43
44dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args";
45
46
47# thresholds set implicitly
48is $p->check_threshold(2), OK, "check_threshold OK when called implicitly";
49is $p->check_threshold(6), WARNING, "check_threshold WARNING";
50is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL";
51is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param";
52
53# thresholds set explicitly
54is $p->check_threshold(
55 check => 2,
56 warning => 50,
57 critical => 100
58), OK, "check_threshold explicit OK";
59
60is $p->check_threshold(
61 check => 66,
62 warning => 50,
63 critical => 100
64), WARNING, "check_threshold explicit WARNING";
65
66
67is $p->check_threshold(
68 check => -1,
69 warning => 5,
70 critical => '0:5',
71), CRITICAL, "check_threshold explicit CRITICAL";
72
73
74
75# what happens if you forget to define warning or critical thresholds?
76$p = undef;
77$p = Nagios::Plugin->new();
78
79is $p->check_threshold(2), UNKNOWN, "everything is now UNKNOWN";
80is $p->check_threshold(-200), UNKNOWN, "everything is now UNKNOWN";
81is $p->check_threshold(134098.3124), UNKNOWN, "everything is now UNKNOWN";
82is $p->check_threshold("foo bar baz"), UNKNOWN, "everything is now UNKNOWN";
83
84
85# how about when you define just one?
86
87$p->set_thresholds(warning => "10:25");
88is $p->check_threshold(2), WARNING, "check_threshold works (WARNING) after explicit set_thresholds";
89is $p->check_threshold(-200), WARNING, "and again";
90is $p->check_threshold(25.5), WARNING, "and again";
91is $p->check_threshold(11), OK, "now OK";