summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Getopt-03.t
diff options
context:
space:
mode:
Diffstat (limited to 't/Monitoring-Plugin-Getopt-03.t')
-rw-r--r--t/Monitoring-Plugin-Getopt-03.t107
1 files changed, 107 insertions, 0 deletions
diff --git a/t/Monitoring-Plugin-Getopt-03.t b/t/Monitoring-Plugin-Getopt-03.t
new file mode 100644
index 0000000..6490145
--- /dev/null
+++ b/t/Monitoring-Plugin-Getopt-03.t
@@ -0,0 +1,107 @@
1# Monitoring::Plugin::Getopt --extra-opts tests
2
3use strict;
4use File::Spec;
5use File::Basename;
6use IO::File;
7
8use Test::More qw(no_plan);
9BEGIN { use_ok('Monitoring::Plugin::Getopt') };
10
11# Needed to get evals to work in testing
12Monitoring::Plugin::Functions::_use_die(1);
13
14my $tdir = 'npg03';
15if (! -d $tdir) {
16 my $ttdir = File::Spec->catdir('t', $tdir);
17 die "missing '$tdir' directory\n" unless -d $ttdir;
18 $tdir = $ttdir;
19}
20
21# Load expected files
22my %EXPECTED = ();
23for my $efile (glob File::Spec->catfile($tdir, 'expected', '*')) {
24 my $fh = IO::File->new($efile, 'r') or die "Cannot open input file '$efile': $!";
25 if (my $cmd = $fh->getline()) { # First line only!
26 chomp $cmd;
27 $cmd =~ s/^\s+//;
28 $cmd =~ s/\s+$//;
29 $EXPECTED{ basename($efile) } = $cmd;
30 }
31}
32
33# Override MONITORING_CONFIG_PATH to use our test plugins.ini file
34$ENV{MONITORING_CONFIG_PATH} = "/random/bogus/path:$tdir";
35
36my %PARAM = (
37 version => '0.01',
38 blurb => 'This plugin tests various stuff.',
39 usage => "Usage: %s -H <host> -w <warning_threshold>
40 -c <critical threshold>",
41);
42
43sub ng_setup
44{
45 my $arg = shift;
46
47 # Instantiate object
48 my $ng = Monitoring::Plugin::Getopt->new(%PARAM);
49
50 if (ref $arg eq 'ARRAY' && @$arg) {
51 $ng->arg(%$_) foreach @$arg;
52 }
53
54 return $ng;
55}
56
57# Setup our Monitoring::Plugin::Getopt object
58my $ng;
59my $arg = [
60 { spec => 'S', help => '-S' },
61 { spec => 'H=s', help => '-H' },
62 { spec => 'p=s@', help => '-p' },
63 { spec => 'path=s@', help => '--path' },
64 { spec => 'username|u=s', help => '--username' },
65 { spec => 'password=s', help => '--password' },
66 { spec => 'critical=s', help => '--critical' },
67 { spec => 'warning=s', help => '--warning' },
68 { spec => 'expect=s', help => '--expect' },
69 { spec => 'units=s', help => '--units' },
70];
71
72#my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3);
73#my %SKIP = map { $_ => 1 } qw(06_singlechar2);
74my %SKIP = ();
75
76# Process all test cases in $tdir/input
77my $glob = $ARGV[0] || '*';
78for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) {
79 $ng = ng_setup($arg);
80
81 my $fh = IO::File->new($infile, 'r') or die "Cannot open input file '$infile': $!";
82 $infile = basename($infile);
83
84 if (my $cmd = $fh->getline()) { # First line only!
85 $cmd =~ s/^\s+//;
86 my ($plugin, @args) = split /\s+/, $cmd;
87
88 # Fake out the plugin name
89 $ng->{_attr}->{plugin} = $plugin;
90
91 # Parse the options
92 SKIP: {
93 skip "Skipping ..." if $SKIP{$infile};
94
95 @ARGV = @args;
96 eval { $ng->getopts };
97 if ($@) {
98 chomp $@;
99 ok($infile =~ m/_(dies?|catch)$/, "$infile ($@)");
100 is($@, $EXPECTED{$infile}, $infile) if ($infile =~ m/_catch$/);
101 }
102 else {
103 is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile);
104 }
105 }
106 }
107}