diff options
author | Gavin Carr <gonzai@users.sourceforge.net> | 2007-02-08 05:18:06 +0000 |
---|---|---|
committer | Gavin Carr <gonzai@users.sourceforge.net> | 2007-02-08 05:18:06 +0000 |
commit | 0e41613e9f547ab97365329572555936e84f77b2 (patch) | |
tree | 4d657ad1a7bb96ba4f4ff40037198edc58449aed | |
parent | d56d5a0d2d4b29ac2a296bc39a7b80862a7f7e8f (diff) | |
download | monitoring-plugin-perl-0e41613e9f547ab97365329572555936e84f77b2.tar.gz |
Add initial --default-opts implementation to Nagios::Plugin::Getopt (II).
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1617 f882894a-f735-0410-b71e-b25c423dba1c
-rw-r--r-- | t/Nagios-Plugin-Getopt-03.t | 99 | ||||
-rw-r--r-- | t/npg03/README | 13 | ||||
-rw-r--r-- | t/npg03/expected/00_basic | 1 | ||||
-rw-r--r-- | t/npg03/expected/01_override1 | 1 | ||||
-rw-r--r-- | t/npg03/expected/02_override2 | 1 | ||||
-rw-r--r-- | t/npg03/expected/05_singlechar1 | 1 | ||||
-rw-r--r-- | t/npg03/expected/06_singlechar2 | 1 | ||||
-rw-r--r-- | t/npg03/expected/07_singlechar3 | 1 | ||||
-rw-r--r-- | t/npg03/expected/09_funnystuff | 1 | ||||
-rw-r--r-- | t/npg03/expected/12_nosection_implicit | 1 | ||||
-rw-r--r-- | t/npg03/input/00_basic | 1 | ||||
-rw-r--r-- | t/npg03/input/01_override1 | 1 | ||||
-rw-r--r-- | t/npg03/input/02_override2 | 1 | ||||
-rw-r--r-- | t/npg03/input/05_singlechar1 | 1 | ||||
-rw-r--r-- | t/npg03/input/06_singlechar2 | 1 | ||||
-rw-r--r-- | t/npg03/input/07_singlechar3 | 1 | ||||
-rw-r--r-- | t/npg03/input/09_funnystuff | 1 | ||||
-rw-r--r-- | t/npg03/input/12_nosection_implicit | 1 | ||||
-rw-r--r-- | t/npg03/input/13_nosection_explicit_dies | 1 | ||||
-rw-r--r-- | t/npg03/input/14_badsection_dies | 1 | ||||
-rw-r--r-- | t/npg03/plugins.cfg | 21 |
21 files changed, 151 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 | |||
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('Nagios::Plugin::Getopt') }; | ||
10 | |||
11 | my $tdir = 'npg03'; | ||
12 | if (! -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 | ||
19 | my %EXPECTED = (); | ||
20 | for 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 | |||
32 | my %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 | |||
39 | sub 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 | ||
54 | my $ng; | ||
55 | my $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 | |||
66 | my %SKIP = map { $_ => 1 } qw(05_singlechar1 07_singlechar3); | ||
67 | |||
68 | # Process all test cases in $tdir/input | ||
69 | my $glob = $ARGV[0] || '*'; | ||
70 | for 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 | |||
diff --git a/t/npg03/README b/t/npg03/README new file mode 100644 index 0000000..a19f263 --- /dev/null +++ b/t/npg03/README | |||
@@ -0,0 +1,13 @@ | |||
1 | Nagios-Plugin-Getopt-03.t automatically tests all cases defined in the 'input' directory | ||
2 | and expects the output to match the corresponding file in the 'expected' directory. To | ||
3 | define a new test case, just create a new file in the 'input' directory containing the | ||
4 | input command line, and a corresponding file in the 'expected' directory containing | ||
5 | what you think the expanded command line should be. Note that this expansion is normalised | ||
6 | as follows: | ||
7 | |||
8 | - command line arguments are reported in alphabetical order | ||
9 | - extraneous white space is removed | ||
10 | |||
11 | Also, if you use a completely new argument than those defined in Nagios-Plugin-Getopt-03.t | ||
12 | you will need to define it there as well. | ||
13 | |||
diff --git a/t/npg03/expected/00_basic b/t/npg03/expected/00_basic new file mode 100644 index 0000000..9707dbd --- /dev/null +++ b/t/npg03/expected/00_basic | |||
@@ -0,0 +1 @@ | |||
check_mysql -H localhost -S --critical=15 --password=secret --username=altinity --warning=10 | |||
diff --git a/t/npg03/expected/01_override1 b/t/npg03/expected/01_override1 new file mode 100644 index 0000000..8288914 --- /dev/null +++ b/t/npg03/expected/01_override1 | |||
@@ -0,0 +1 @@ | |||
check_mysql --critical=15 --username=admin --warning=5 | |||
diff --git a/t/npg03/expected/02_override2 b/t/npg03/expected/02_override2 new file mode 100644 index 0000000..7946308 --- /dev/null +++ b/t/npg03/expected/02_override2 | |||
@@ -0,0 +1 @@ | |||
check_mysql --password=secret --username=admin | |||
diff --git a/t/npg03/expected/05_singlechar1 b/t/npg03/expected/05_singlechar1 new file mode 100644 index 0000000..13a3f9b --- /dev/null +++ b/t/npg03/expected/05_singlechar1 | |||
@@ -0,0 +1 @@ | |||
check_disk -p /home | |||
diff --git a/t/npg03/expected/06_singlechar2 b/t/npg03/expected/06_singlechar2 new file mode 100644 index 0000000..8f9df5e --- /dev/null +++ b/t/npg03/expected/06_singlechar2 | |||
@@ -0,0 +1 @@ | |||
check_disk -p /var | |||
diff --git a/t/npg03/expected/07_singlechar3 b/t/npg03/expected/07_singlechar3 new file mode 100644 index 0000000..f4e6ed7 --- /dev/null +++ b/t/npg03/expected/07_singlechar3 | |||
@@ -0,0 +1 @@ | |||
check_disk -p /home -p /users | |||
diff --git a/t/npg03/expected/09_funnystuff b/t/npg03/expected/09_funnystuff new file mode 100644 index 0000000..6d510b8 --- /dev/null +++ b/t/npg03/expected/09_funnystuff | |||
@@ -0,0 +1 @@ | |||
check_disk --expect=" space in front" -p "" --username="Ton Voon" | |||
diff --git a/t/npg03/expected/12_nosection_implicit b/t/npg03/expected/12_nosection_implicit new file mode 100644 index 0000000..7bb6136 --- /dev/null +++ b/t/npg03/expected/12_nosection_implicit | |||
@@ -0,0 +1 @@ | |||
check_no_section -H localhost | |||
diff --git a/t/npg03/input/00_basic b/t/npg03/input/00_basic new file mode 100644 index 0000000..4c16788 --- /dev/null +++ b/t/npg03/input/00_basic | |||
@@ -0,0 +1 @@ | |||
check_mysql -S --default-opts= --default-opts=more_options -H localhost | |||
diff --git a/t/npg03/input/01_override1 b/t/npg03/input/01_override1 new file mode 100644 index 0000000..9e051e9 --- /dev/null +++ b/t/npg03/input/01_override1 | |||
@@ -0,0 +1 @@ | |||
check_mysql --username=admin --default-opts=more_options --warning=5 | |||
diff --git a/t/npg03/input/02_override2 b/t/npg03/input/02_override2 new file mode 100644 index 0000000..ceabe55 --- /dev/null +++ b/t/npg03/input/02_override2 | |||
@@ -0,0 +1 @@ | |||
check_mysql --default-opts= -u admin | |||
diff --git a/t/npg03/input/05_singlechar1 b/t/npg03/input/05_singlechar1 new file mode 100644 index 0000000..1edb8bf --- /dev/null +++ b/t/npg03/input/05_singlechar1 | |||
@@ -0,0 +1 @@ | |||
check_disk --default-opts= -p /home | |||
diff --git a/t/npg03/input/06_singlechar2 b/t/npg03/input/06_singlechar2 new file mode 100644 index 0000000..24965c7 --- /dev/null +++ b/t/npg03/input/06_singlechar2 | |||
@@ -0,0 +1 @@ | |||
check_disk --default-opts=check_2_disks | |||
diff --git a/t/npg03/input/07_singlechar3 b/t/npg03/input/07_singlechar3 new file mode 100644 index 0000000..0abc70f --- /dev/null +++ b/t/npg03/input/07_singlechar3 | |||
@@ -0,0 +1 @@ | |||
check_disk --default-opts= -p /home -p /users | |||
diff --git a/t/npg03/input/09_funnystuff b/t/npg03/input/09_funnystuff new file mode 100644 index 0000000..c2d6160 --- /dev/null +++ b/t/npg03/input/09_funnystuff | |||
@@ -0,0 +1 @@ | |||
check_disk --default-opts=funny_stuff | |||
diff --git a/t/npg03/input/12_nosection_implicit b/t/npg03/input/12_nosection_implicit new file mode 100644 index 0000000..7bb6136 --- /dev/null +++ b/t/npg03/input/12_nosection_implicit | |||
@@ -0,0 +1 @@ | |||
check_no_section -H localhost | |||
diff --git a/t/npg03/input/13_nosection_explicit_dies b/t/npg03/input/13_nosection_explicit_dies new file mode 100644 index 0000000..90aab51 --- /dev/null +++ b/t/npg03/input/13_nosection_explicit_dies | |||
@@ -0,0 +1 @@ | |||
check_no_section --default-opts= -H localhost | |||
diff --git a/t/npg03/input/14_badsection_dies b/t/npg03/input/14_badsection_dies new file mode 100644 index 0000000..70815a9 --- /dev/null +++ b/t/npg03/input/14_badsection_dies | |||
@@ -0,0 +1 @@ | |||
check_no_section --default-opts=bad_section | |||
diff --git a/t/npg03/plugins.cfg b/t/npg03/plugins.cfg new file mode 100644 index 0000000..f893a21 --- /dev/null +++ b/t/npg03/plugins.cfg | |||
@@ -0,0 +1,21 @@ | |||
1 | [check_mysql] | ||
2 | username=tonvoon | ||
3 | password=secret | ||
4 | |||
5 | [more_options] | ||
6 | username=altinity | ||
7 | warning=10 | ||
8 | critical=15 | ||
9 | |||
10 | [check_disk] | ||
11 | p=/tmp | ||
12 | |||
13 | [check_2_disks] | ||
14 | p=/tmp | ||
15 | p=/var | ||
16 | |||
17 | [funny_stuff] | ||
18 | username="Ton Voon" | ||
19 | p= | ||
20 | expect=" space in front" | ||
21 | |||