update changelog
[monitoring-plugin-perl.git] / t / Monitoring-Plugin-Getopt-03.t
blobbcf324e2540ae0a0d94192b63dced659cde606fd
1 # Monitoring::Plugin::Getopt --extra-opts tests
3 use strict;
4 use File::Spec;
5 use File::Basename;
6 use IO::File;
8 use Test::More qw(no_plan);
9 BEGIN { use_ok('Monitoring::Plugin::Getopt') };
11 # Needed to get evals to work in testing
12 Monitoring::Plugin::Functions::_use_die(1);
14 my $tdir = 'npg03';
15 if (! -d $tdir) {
16   my $ttdir = File::Spec->catdir('t', $tdir);
17   die "missing '$tdir' directory\n" unless -d $ttdir;
18   $tdir = $ttdir;
21 # Load expected files
22 my %EXPECTED = ();
23 for 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   }
33 # Override MONITORING_CONFIG_PATH to use our test plugins.ini file
34 $ENV{MONITORING_CONFIG_PATH} = "/random/bogus/path:$tdir";
36 my %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>",
43 sub ng_setup
45   my $arg = shift;
47   # Instantiate object
48   my $ng = Monitoring::Plugin::Getopt->new(%PARAM);
50   if (ref $arg eq 'ARRAY' && @$arg) {
51     $ng->arg(%$_) foreach @$arg;
52   }
54   return $ng;
57 # Setup our Monitoring::Plugin::Getopt object
58 my $ng;
59 my $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' },
72 #my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3);
73 #my %SKIP = map { $_ => 1 } qw(06_singlechar2);
74 my %SKIP = ();
76 # Process all test cases in $tdir/input
77 my $glob = $ARGV[0] || '*';
78 for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) {
79   $ng = ng_setup($arg);
81   my $fh = IO::File->new($infile, 'r') or die "Cannot open input file '$infile': $!";
82   $infile = basename($infile);
84   if (my $cmd = $fh->getline()) {          # First line only!
85     $cmd =~ s/^\s+//;
86     my ($plugin, @args) = split /\s+/, $cmd;
88     # Fake out the plugin name
89     $ng->{_attr}->{plugin} = $plugin;
91     # Parse the options
92     SKIP: {
93       skip "Skipping ..." if $SKIP{$infile};
95       @ARGV = @args;
96       eval { $ng->getopts };
97       if ($@) {
98         chomp $@;
99         ok($infile =~ m/_(dies?|catch)$/, "$infile ($@)");
100         my $expect = $EXPECTED{$infile};
101         # windows expects backslashes fixes rt.cpan #100708
102         $expect =~ s#/#\\#gmx if $^O =~ m/^MSWin/;
103         is($@, $expect, $infile) if ($infile =~ m/_catch$/);
104       }
105       else {
106         is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile);
107       }
108     }
109   }