1 # Monitoring::Plugin::Getopt --extra-opts tests
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);
16 my $ttdir = File::Spec->catdir('t', $tdir);
17 die "missing '$tdir' directory\n" unless -d $ttdir;
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!
29 $EXPECTED{ basename($efile) } = $cmd;
33 # Override MONITORING_CONFIG_PATH to use our test plugins.ini file
34 $ENV{MONITORING_CONFIG_PATH} = "/random/bogus/path:$tdir";
38 blurb => 'This plugin tests various stuff.',
39 usage => "Usage: %s -H <host> -w <warning_threshold>
40 -c <critical threshold>",
48 my $ng = Monitoring::Plugin::Getopt->new(%PARAM);
50 if (ref $arg eq 'ARRAY' && @$arg) {
51 $ng->arg(%$_) foreach @$arg;
57 # Setup our Monitoring::Plugin::Getopt object
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);
76 # Process all test cases in $tdir/input
77 my $glob = $ARGV[0] || '*';
78 for my $infile (glob File::Spec->catfile($tdir, 'input', $glob)) {
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!
86 my ($plugin, @args) = split /\s+/, $cmd;
88 # Fake out the plugin name
89 $ng->{_attr}->{plugin} = $plugin;
93 skip "Skipping ..." if $SKIP{$infile};
96 eval { $ng->getopts };
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$/);
106 is($plugin . ' ' . $ng->_cmdline, $EXPECTED{$infile}, $infile);