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