Code

Standardize the extra-opts notes
[nagiosplug.git] / plugins-root / t / check_icmp.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Ping Response Tests via check_icmp
4 #
6 use strict;
7 use Test::More;
8 use NPTest;
10 my $allow_sudo = getTestParameter( "NP_ALLOW_SUDO",
11         "If sudo is setup for this user to run any command as root ('yes' to allow)",
12         "no" );
14 if ($allow_sudo eq "yes") {
15         plan tests => 16;
16 } else {
17         plan skip_all => "Need sudo to test check_icmp";
18 }
20 my $successOutput = '/OK - .*?: rta (?:[\d\.]+ms)|(?:nan), lost \d+%/';
21 my $failureOutput = '/(WARNING|CRITICAL) - .*?: rta [\d\.]+ms, lost \d%/';
23 my $host_responsive    = getTestParameter( "NP_HOST_RESPONSIVE",
24                                 "The hostname of system responsive to network requests",
25                                 "localhost" );
27 my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
28                                 "The hostname of system not responsive to network requests",
29                                 "10.0.0.1" );
31 my $hostname_invalid   = getTestParameter( "NP_HOSTNAME_INVALID",
32                                 "An invalid (not known to DNS) hostname",
33                                 "nosuchhost" );
35 my $res;
37 $res = NPTest->testCmd(
38         "sudo ./check_icmp -H $host_responsive -w 10000ms,100% -c 10000ms,100%"
39         );
40 is( $res->return_code, 0, "Syntax ok" );
41 like( $res->output, $successOutput, "Output OK" );
43 $res = NPTest->testCmd(
44         "sudo ./check_icmp -H $host_responsive -w 0ms,0% -c 10000ms,100%"
45         );
46 is( $res->return_code, 1, "Syntax ok, with forced warning" );
47 like( $res->output, $failureOutput, "Output OK" );
49 $res = NPTest->testCmd(
50         "sudo ./check_icmp -H $host_responsive -w 0,0% -c 0,0%"
51         );
52 is( $res->return_code, 2, "Syntax ok, with forced critical" );
53 like( $res->output, $failureOutput, "Output OK" );
55 $res = NPTest->testCmd(
56         "sudo ./check_icmp -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100%"
57         );
58 is( $res->return_code, 2, "Timeout - host nonresponsive" );
59 like( $res->output, '/100%/', "Error contains '100%' string (for 100% packet loss)" );
61 $res = NPTest->testCmd(
62         "sudo ./check_icmp -w 10000ms,100% -c 10000ms,100%"
63         );
64 is( $res->return_code, 3, "No hostname" );
65 like( $res->output, '/No hosts to check/', "Output with appropriate error message");
67 $res = NPTest->testCmd(
68         "sudo ./check_icmp -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 0"
69         );
70 is( $res->return_code, 0, "One host nonresponsive - zero required" );
71 like( $res->output, $successOutput, "Output OK" );
73 $res = NPTest->testCmd(
74         "sudo ./check_icmp -H $host_responsive -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 1"
75         );
76 is( $res->return_code, 0, "One of two host nonresponsive - one required" );
77 like( $res->output, $successOutput, "Output OK" );
79 $res = NPTest->testCmd(
80         "sudo ./check_icmp -H $host_responsive -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 2"
81         );
82 is( $res->return_code, 2, "One of two host nonresponsive - two required" );
83 like( $res->output, $failureOutput, "Output OK" );