update changelog
[monitoring-plugin-perl.git] / t / Monitoring-Plugin-04.t
blob41027f71acd8303354010cd4301c9ba809a46baa
2 # tests for toplevel access to Threshold and GetOpts stuff
4 use strict;
5 #use Test::More 'no_plan';
6 use Test::More tests=>30;
8 BEGIN { use_ok('Monitoring::Plugin') };
9 use Monitoring::Plugin::Functions;
10 Monitoring::Plugin::Functions::_fake_exit(1);
13 eval { Monitoring::Plugin->new(); };
14 ok(! $@, "constructor DOESN'T die without usage");
16 my $p = Monitoring::Plugin->new();
17 eval { $p->add_arg('warning', 'warning') };
18 ok($@, "add_arg() dies if you haven't instantiated with usage");
19 eval { $p->getopts };
20 ok($@, "getopts() dies if you haven't instantiated with usage");
22 $p = Monitoring::Plugin->new( usage => "dummy usage statement" );
24 # option accessors work
25 can_ok $p, 'opts';
26 isa_ok $p->opts, 'Monitoring::Plugin::Getopt', "Getopt object is defined";
28 $p->add_arg('warning|w=s', "warning");
29 $p->add_arg('critical|c=s', "critical");
31 @ARGV = qw(-w 5 -c 10);
32 $p->getopts;
33 is $p->opts->warning, "5", "warning opt is accessible";
34 is $p->opts->critical, "10", "critical opt is accessible";
37 can_ok $p, 'perfdata';
38 #isa_ok $p->perfdata, 'Monitoring::Plugin::Performance', "perfdata object is defined";
41 can_ok $p, 'threshold';
42 #isa_ok $p->threshold, 'Monitoring::Plugin::Threshold', "threshold object is defined";
45 eval { $p->check_threshold() };
46 ok($@,  "check_threshold dies if called with no args");
49 # thresholds set implicitly
50 is $p->check_threshold(2), OK, "check_threshold OK when called implicitly";
51 is $p->check_threshold(6), WARNING, "check_threshold WARNING";
52 is $p->check_threshold(11), CRITICAL, "check_threshold CRITICAL";
53 is $p->check_threshold(check=>11), CRITICAL, "check_threshold CRITICAL with hash param";
55 # Check that arrays allowed
56 is $p->check_threshold([2,1]), OK, "check_threshold OK when called implicitly";
57 is $p->check_threshold([6,2]), WARNING, "check_threshold WARNING";
58 is $p->check_threshold([1,2,6,11]), CRITICAL, "check_threshold CRITICAL";
59 is $p->check_threshold(check=>[1,2,6,11]), CRITICAL, "check_threshold CRITICAL with hash param";
61 # thresholds set explicitly
62 is $p->check_threshold(
63                                            check    => 2,
64                                            warning  => 50,
65                                            critical => 100
66 ), OK, "check_threshold explicit OK";
68 is $p->check_threshold(
69                                            check    => 66,
70                                            warning  => 50,
71                                            critical => 100
72 ), WARNING, "check_threshold explicit WARNING";
75 is $p->check_threshold(
76                                            check    => -1,
77                                            warning  => 5,
78                                            critical => '0:5',
79 ), CRITICAL, "check_threshold explicit CRITICAL";
83 # what happens if you forget to define warning or critical thresholds?
84 $p = undef;
85 $p = Monitoring::Plugin->new();
87 is $p->check_threshold(2), UNKNOWN, "everything is now UNKNOWN";
88 is $p->check_threshold(-200), UNKNOWN, "everything is now UNKNOWN";
89 is $p->check_threshold(134098.3124), UNKNOWN, "everything is now UNKNOWN";
90 is $p->check_threshold("foo bar baz"), UNKNOWN, "everything is now UNKNOWN";
93 # how about when you define just one?
95 $p->set_thresholds(warning => "10:25");
96 is $p->check_threshold(2), WARNING, "check_threshold works (WARNING) after explicit set_thresholds";
97 is $p->check_threshold(-200), WARNING, "and again";
98 is $p->check_threshold(25.5), WARNING, "and again";
99 is $p->check_threshold(11), OK, "now OK";