diff options
Diffstat (limited to 't/Nagios-Plugin-04.t')
-rw-r--r-- | t/Nagios-Plugin-04.t | 91 |
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 | |||
5 | use strict; | ||
6 | #use Test::More 'no_plan'; | ||
7 | use Test::More tests=>26; | ||
8 | use Test::Exception; | ||
9 | |||
10 | BEGIN { use_ok('Nagios::Plugin') }; | ||
11 | use Nagios::Plugin::Functions; | ||
12 | Nagios::Plugin::Functions::_fake_exit(1); | ||
13 | |||
14 | |||
15 | lives_ok sub { my $broke = Nagios::Plugin->new(); }, "constructor DOESN'T die without usage"; | ||
16 | |||
17 | my $p = Nagios::Plugin->new(); | ||
18 | dies_ok sub { $p->add_arg('warning', 'warning') }, "add_arg() dies if you haven't instantiated with usage"; | ||
19 | dies_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 | ||
24 | can_ok $p, 'opts'; | ||
25 | isa_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; | ||
32 | is $p->opts->warning, "5", "warning opt is accessible"; | ||
33 | is $p->opts->critical, "10", "critical opt is accessible"; | ||
34 | |||
35 | |||
36 | can_ok $p, 'perfdata'; | ||
37 | #isa_ok $p->perfdata, 'Nagios::Plugin::Performance', "perfdata object is defined"; | ||
38 | |||
39 | |||
40 | can_ok $p, 'threshold'; | ||
41 | #isa_ok $p->threshold, 'Nagios::Plugin::Threshold', "threshold object is defined"; | ||
42 | |||
43 | |||
44 | dies_ok sub { $p->check_threshold() }, "check_threshold dies if called with no args"; | ||
45 | |||
46 | |||
47 | # thresholds set implicitly | ||
48 | is $p->check_threshold(2), OK, "check_threshold OK when called implicitly"; | ||
49 | is $p->check_threshold(6), WARNING, "check_threshold WARNING"; | ||
50 | is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL"; | ||
51 | is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param"; | ||
52 | |||
53 | # thresholds set explicitly | ||
54 | is $p->check_threshold( | ||
55 | check => 2, | ||
56 | warning => 50, | ||
57 | critical => 100 | ||
58 | ), OK, "check_threshold explicit OK"; | ||
59 | |||
60 | is $p->check_threshold( | ||
61 | check => 66, | ||
62 | warning => 50, | ||
63 | critical => 100 | ||
64 | ), WARNING, "check_threshold explicit WARNING"; | ||
65 | |||
66 | |||
67 | is $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 | |||
79 | is $p->check_threshold(2), UNKNOWN, "everything is now UNKNOWN"; | ||
80 | is $p->check_threshold(-200), UNKNOWN, "everything is now UNKNOWN"; | ||
81 | is $p->check_threshold(134098.3124), UNKNOWN, "everything is now UNKNOWN"; | ||
82 | is $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"); | ||
88 | is $p->check_threshold(2), WARNING, "check_threshold works (WARNING) after explicit set_thresholds"; | ||
89 | is $p->check_threshold(-200), WARNING, "and again"; | ||
90 | is $p->check_threshold(25.5), WARNING, "and again"; | ||
91 | is $p->check_threshold(11), OK, "now OK"; | ||