check_icmp.t 2.8 KB

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