diff options
| author | Gavin Carr <gonzai@users.sourceforge.net> | 2006-08-30 01:20:41 +0000 |
|---|---|---|
| committer | Gavin Carr <gonzai@users.sourceforge.net> | 2006-08-30 01:20:41 +0000 |
| commit | a70a6ff5acf2a2b252328427293801ce0ff42888 (patch) | |
| tree | 8d3b573f3abaaa68f4eec0cbae4afa739bdfb2a7 /t | |
| parent | 96933fd2e1f53aff9c9ef26639fafe9a84ec754e (diff) | |
| download | monitoring-plugin-perl-a70a6ff5acf2a2b252328427293801ce0ff42888.tar.gz | |
Add first-pass Nagios::Plugin::Getopt.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1470 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 't')
| -rw-r--r-- | t/Nagios-Plugin-Getopt-01.t | 134 | ||||
| -rw-r--r-- | t/Nagios-Plugin-Getopt-02.t | 61 |
2 files changed, 195 insertions, 0 deletions
diff --git a/t/Nagios-Plugin-Getopt-01.t b/t/Nagios-Plugin-Getopt-01.t new file mode 100644 index 0000000..fad68e3 --- /dev/null +++ b/t/Nagios-Plugin-Getopt-01.t | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | # Nagios::Plugin::Getopt basic tests | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Test::More tests => 72; | ||
| 6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | ||
| 7 | |||
| 8 | my %PARAM = ( | ||
| 9 | version => '0.01', | ||
| 10 | url => 'http://www.openfusion.com.au/labs/nagios/', | ||
| 11 | blurb => 'This plugin tests various stuff.', | ||
| 12 | usage => "Usage: %s -H <host> -w <warning_threshold> | ||
| 13 | -c <critical threshold>", | ||
| 14 | plugin => 'test_plugin', | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub setup | ||
| 18 | { | ||
| 19 | # Instantiate object | ||
| 20 | my $ng = Nagios::Plugin::Getopt->new(%PARAM); | ||
| 21 | ok($ng, 'constructor ok'); | ||
| 22 | |||
| 23 | # Add argument - short form - arg spec, help text, default, required? | ||
| 24 | $ng->arg('warning|w=s' => | ||
| 25 | qq(-w, --warning=INTEGER\n Exit with WARNING status if less than INTEGER foobars are free), | ||
| 26 | 5); | ||
| 27 | |||
| 28 | # Add argument - named version | ||
| 29 | $ng->arg( | ||
| 30 | spec => 'critical|c=s', | ||
| 31 | help => qq(-c, --critical=INTEGER\n Exit with CRITICAL status if less than INTEGER foobars are free), | ||
| 32 | required => 1, | ||
| 33 | ); | ||
| 34 | |||
| 35 | return $ng; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $ng; | ||
| 39 | |||
| 40 | # Simple usage (short and long args) | ||
| 41 | @ARGV = qw(-w 3 --critical 10 --timeout=12 --verbose); | ||
| 42 | $ng = setup; | ||
| 43 | $ng->getopts; | ||
| 44 | is($ng->warning, 3, 'warning set to 3'); | ||
| 45 | is($ng->critical, 10, 'critical set to 10'); | ||
| 46 | is($ng->timeout, 12, 'timeout set to 12'); | ||
| 47 | |||
| 48 | # Missing args | ||
| 49 | @ARGV = qw(); | ||
| 50 | $ng = setup; | ||
| 51 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
| 52 | like($@, qr/Usage:/, 'usage message'); | ||
| 53 | like($@, qr/Missing arg/, 'missing arguments'); | ||
| 54 | is($ng->verbose, 0, 'verbose set to 0'); | ||
| 55 | # Missing critical | ||
| 56 | @ARGV = qw(-w0 -v); | ||
| 57 | $ng = setup; | ||
| 58 | ok(! defined eval { $ng->getopts }, 'getopts died on missing args'); | ||
| 59 | like($@, qr/Usage:/, 'usage message'); | ||
| 60 | like($@, qr/Missing argument: critical/, 'missing argument: critical'); | ||
| 61 | unlike($@, qr/Missing argument: warning/, 'no missing argument: warning'); | ||
| 62 | is($ng->warning, 0, 'warning set to 0'); | ||
| 63 | is($ng->critical, undef, 'critical undef'); | ||
| 64 | is($ng->timeout, 15, 'timeout set to default'); | ||
| 65 | is($ng->verbose, 1, 'verbose set to true'); | ||
| 66 | # Missing warning | ||
| 67 | @ARGV = qw(--critical=27 --timeout 17 --verbose); | ||
| 68 | $ng = setup; | ||
| 69 | $ng->getopts; | ||
| 70 | is($ng->warning, 5, 'warning 5 (default)'); | ||
| 71 | is($ng->critical, 27, 'critical set to 27'); | ||
| 72 | is($ng->timeout, 17, 'timeout set to 17'); | ||
| 73 | is($ng->verbose, 1, 'verbose set to true'); | ||
| 74 | |||
| 75 | # -? --usage | ||
| 76 | @ARGV = ( '-?' ); | ||
| 77 | $ng = setup; | ||
| 78 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
| 79 | like($@, qr/Usage:/, 'usage message'); | ||
| 80 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 81 | @ARGV = ( '--usage' ); | ||
| 82 | $ng = setup; | ||
| 83 | ok(! defined eval { $ng->getopts }, 'getopts died on usage'); | ||
| 84 | like($@, qr/Usage:/, 'usage message'); | ||
| 85 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 86 | |||
| 87 | # -V --version | ||
| 88 | @ARGV = ( '-V' ); | ||
| 89 | $ng = setup; | ||
| 90 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
| 91 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
| 92 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
| 93 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
| 94 | unlike($@, qr/Usage:/, 'no usage message'); | ||
| 95 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 96 | @ARGV = ( '--version' ); | ||
| 97 | $ng = setup; | ||
| 98 | ok(! defined eval { $ng->getopts }, 'getopts died on version'); | ||
| 99 | like($@, qr/^$PARAM{plugin}/, 'version info includes plugin name'); | ||
| 100 | like($@, qr/$PARAM{version}/, 'version info includes version'); | ||
| 101 | like($@, qr/$PARAM{url}/, 'version info includes url'); | ||
| 102 | unlike($@, qr/Usage:/, 'no usage message'); | ||
| 103 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 104 | |||
| 105 | # -h --help | ||
| 106 | @ARGV = ( '-h' ); | ||
| 107 | $ng = setup; | ||
| 108 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
| 109 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
| 110 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
| 111 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
| 112 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
| 113 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
| 114 | like($@, qr/Usage:/, 'help includes usage message'); | ||
| 115 | like($@, qr/--version/, 'help includes default options 1'); | ||
| 116 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
| 117 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
| 118 | like($@, qr/--critical/, 'help includes custom option 2'); | ||
| 119 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 120 | @ARGV = ( '--help' ); | ||
| 121 | $ng = setup; | ||
| 122 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
| 123 | like($@, qr/^$PARAM{plugin}/, 'help includes plugin name'); | ||
| 124 | like($@, qr/$PARAM{version}/, 'help includes version'); | ||
| 125 | like($@, qr/$PARAM{url}/, 'help includes url'); | ||
| 126 | like($@, qr/General Public Licence/, 'help includes licence'); | ||
| 127 | like($@, qr/$PARAM{blurb}/, 'help includes blurb'); | ||
| 128 | like($@, qr/Usage:/, 'help includes usage message'); | ||
| 129 | like($@, qr/--version/, 'help includes default options 1'); | ||
| 130 | like($@, qr/--verbose/, 'help includes default options 2'); | ||
| 131 | like($@, qr/--warning/, 'help includes custom option 1'); | ||
| 132 | like($@, qr/--critical/, 'help includes custom option 2'); | ||
| 133 | unlike($@, qr/Missing arg/, 'no missing arguments'); | ||
| 134 | |||
diff --git a/t/Nagios-Plugin-Getopt-02.t b/t/Nagios-Plugin-Getopt-02.t new file mode 100644 index 0000000..26e0293 --- /dev/null +++ b/t/Nagios-Plugin-Getopt-02.t | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | # Nagios::Plugin::Getopt timeout tests | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Test::More tests => 14; | ||
| 6 | BEGIN { use_ok('Nagios::Plugin::Getopt') }; | ||
| 7 | |||
| 8 | my %PARAM = ( | ||
| 9 | version => '0.01', | ||
| 10 | url => 'http://www.openfusion.com.au/labs/nagios/', | ||
| 11 | blurb => 'This plugin tests various stuff.', | ||
| 12 | usage => "Usage: %s -H <host> -w <warning_threshold> | ||
| 13 | -c <critical threshold>", | ||
| 14 | plugin => 'test_plugin', | ||
| 15 | timeout => 18, | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub setup | ||
| 19 | { | ||
| 20 | # Instantiate object | ||
| 21 | my $ng = Nagios::Plugin::Getopt->new(%PARAM); | ||
| 22 | ok($ng, 'constructor ok'); | ||
| 23 | return $ng; | ||
| 24 | } | ||
| 25 | |||
| 26 | my $ng; | ||
| 27 | |||
| 28 | # No args | ||
| 29 | @ARGV = qw(); | ||
| 30 | $ng = setup(); | ||
| 31 | $ng->getopts; | ||
| 32 | is($ng->timeout, 18, 'default timeout set to 18'); | ||
| 33 | |||
| 34 | # Check help message | ||
| 35 | @ARGV = ( '-h' ); | ||
| 36 | $ng = setup; | ||
| 37 | ok(! defined eval { $ng->getopts }, 'getopts died on help'); | ||
| 38 | like($@, qr/times out.*default: 18\b/i, 'help timeout changed to 18'); | ||
| 39 | |||
| 40 | # Explicit timeout | ||
| 41 | @ARGV = qw(--timeout=25 --verbose); | ||
| 42 | $ng = setup(); | ||
| 43 | $ng->getopts; | ||
| 44 | is($ng->timeout, 25, 'timeout changed to 25'); | ||
| 45 | |||
| 46 | # Explicit timeout | ||
| 47 | @ARGV = qw(-t10 --verbose); | ||
| 48 | $ng = setup(); | ||
| 49 | $ng->getopts; | ||
| 50 | is($ng->timeout, 10, 'timeout changed to 10'); | ||
| 51 | |||
| 52 | # Short timeout, test default timeout handler | ||
| 53 | @ARGV = qw(-t2 --verbose); | ||
| 54 | $ng = setup(); | ||
| 55 | $ng->getopts; | ||
| 56 | is($ng->timeout, 2, 'timeout changed to 2'); | ||
| 57 | alarm($ng->timeout); | ||
| 58 | # Loop | ||
| 59 | ok(! defined eval { 1 while 1 }, 'loop timed out'); | ||
| 60 | like($@, qr/UNKNOWN\b.*\btimed out/, 'default timeout handler ok'); | ||
| 61 | |||
