diff options
Diffstat (limited to 't/Monitoring-Plugin-Getopt-03.t')
| -rw-r--r-- | t/Monitoring-Plugin-Getopt-03.t | 107 |
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 | |||
| 3 | use strict; | ||
| 4 | use File::Spec; | ||
| 5 | use File::Basename; | ||
| 6 | use IO::File; | ||
| 7 | |||
| 8 | use Test::More qw(no_plan); | ||
| 9 | BEGIN { use_ok('Monitoring::Plugin::Getopt') }; | ||
| 10 | |||
| 11 | # Needed to get evals to work in testing | ||
| 12 | Monitoring::Plugin::Functions::_use_die(1); | ||
| 13 | |||
| 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; | ||
| 19 | } | ||
| 20 | |||
| 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 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | # Override MONITORING_CONFIG_PATH to use our test plugins.ini file | ||
| 34 | $ENV{MONITORING_CONFIG_PATH} = "/random/bogus/path:$tdir"; | ||
| 35 | |||
| 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>", | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub 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 | ||
| 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' }, | ||
| 70 | ]; | ||
| 71 | |||
| 72 | #my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); | ||
| 73 | #my %SKIP = map { $_ => 1 } qw(06_singlechar2); | ||
| 74 | my %SKIP = (); | ||
| 75 | |||
| 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); | ||
| 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 | } | ||
