summaryrefslogtreecommitdiffstats
path: root/t/Monitoring-Plugin-Getopt-04.t
blob: 2c91e23d0631b1b6c69ce731b81f3dcaed806c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Monitoring::Plugin::Getopt spec-to-help generation tests

use strict;

use Test::More tests => 15;
BEGIN { use_ok('Monitoring::Plugin::Getopt') };

# Needed to get evals to work in testing
Monitoring::Plugin::Functions::_use_die(1);

my %PARAM = (
    version => '0.01',
    usage => "Don't use this plugin!",
);

sub setup
{
  # Instantiate object
  my $ng = Monitoring::Plugin::Getopt->new(%PARAM);
  ok($ng, 'constructor ok');

  # Positional args, no short arguments, INTEGER
  $ng->arg('warning=i' =>
    qq(Exit with WARNING status if less than INTEGER foobars are free),
    5);

  # Named args, long + short arguments, INTEGER
  $ng->arg(
    spec => 'critical|c=i',
    help => qq(Exit with CRITICAL status if less than INTEGER foobars are free),
    required => 1,
  );

  # Named args, multiple short arguments, STRING, default expansion
  $ng->arg(
    spec => 'x|y|z=s',
    help => qq(Foobar. Default: %s),
    default => "XYZ",
  );

  # Named args, multiple mixed, no label
  $ng->arg(
    spec => 'long|longer|longest|l',
    help => qq(Long format),
  );

  # Named args, long + short, explicit label
  $ng->arg(
    spec => 'hostname|H=s',
    label => 'ADDRESS',
    help => qq(Hostname),
  );

  # Positional args, long only, explicit label
  $ng->arg('avatar=s', 'Avatar', undef, undef, 'AVATAR');

  # Multiline help test, named args
  $ng->arg(
    spec => 'disk=s',
    label => [ qw(BYTES PERCENT%), undef ],
    help => [
      qq(Disk limit in BYTES),
      qq(Disk limit in PERCENT),
      qq(Disk limit in FOOBARS (Default: %s)),
    ],
    default => 1024,
  );

  # Multiline help test, positional args
  $ng->arg(
    'limit=s',
    [
      qq(Limit in BYTES),
      qq(Limit in PERCENT),
    ],
    undef,
    undef,
    [ undef, 'PERCENT%' ],
  );

  # Named args with *optional* but pre-set value
  $ng->arg(
    spec => 'dirport|d:9030',
    help => 'dirport',
  );

  # Named args with *optional* string value
  $ng->arg(
    spec => 'enablesomething|s:s',
    help => 'something',
  );

  # Named args with *optional* integer value (same as ":0")
  $ng->arg(
    spec => 'testtimeout|T:i',
    help => 'testtimeout',
  );

  # Named args with *optional* but increasing integer value
  $ng->arg(
    spec => 'verbosity|v:+',
    help => 'verbosity',
  );

  return $ng;
}

my $ng;

@ARGV = ( '--help' );
$ng = setup;
ok(! defined eval { $ng->getopts }, 'getopts died on help');
like($@, qr/\n --warning=INTEGER/, 'warning ok');
like($@, qr/\n -c, --critical=INTEGER/, 'critical ok');
like($@, qr/\n -x, -y, -z=STRING\n   Foobar. Default: XYZ\n/, 'x|y|z ok');
like($@, qr/\n -l, --long, --longer, --longest\n   Long format\n/, 'long ok');
like($@, qr/\n -H, --hostname=ADDRESS\n   Hostname\n/, 'hostname ok');
like($@, qr/\n --avatar=AVATAR\n   Avatar\n/, 'avatar ok');
like($@, qr/\n --disk=BYTES\n   Disk limit in BYTES\n --disk=PERCENT%\n   Disk limit in PERCENT\n --disk=STRING\n   Disk limit in FOOBARS \(Default: 1024\)\n/, 'disk multiline ok');
like($@, qr/\n --limit=STRING\n   Limit in BYTES\n --limit=PERCENT%\n   Limit in PERCENT\n/, 'limit multiline ok');
like($@, qr/\n -d, --dirport\[=INTEGER\]/, 'dirport ok');
like($@, qr/\n -s, --enablesomething\[=STRING\]/, 'enablesomething ok');
like($@, qr/\n -T, --testtimeout\[=INTEGER\]/, 'testtimeout ok');
like($@, qr/\n -v, --verbosity\[=INTEGER\]/, 'verbosity ok');
#print $@;