check_icmp.t 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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") {
  12. plan tests => 16;
  13. } else {
  14. plan skip_all => "Need sudo to test check_icmp";
  15. }
  16. my $successOutput = '/OK - .*?: rta (?:[\d\.]+ms)|(?:nan), lost \d+%/';
  17. my $failureOutput = '/(WARNING|CRITICAL) - .*?: rta [\d\.]+ms, lost \d%/';
  18. my $host_responsive = getTestParameter( "NP_HOST_RESPONSIVE",
  19. "The hostname of system responsive to network requests",
  20. "localhost" );
  21. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  22. "The hostname of system not responsive to network requests",
  23. "10.0.0.1" );
  24. my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
  25. "An invalid (not known to DNS) hostname",
  26. "nosuchhost" );
  27. my $res;
  28. $res = NPTest->testCmd(
  29. "sudo ./check_icmp -H $host_responsive -w 10000ms,100% -c 10000ms,100%"
  30. );
  31. is( $res->return_code, 0, "Syntax ok" );
  32. like( $res->output, $successOutput, "Output OK" );
  33. $res = NPTest->testCmd(
  34. "sudo ./check_icmp -H $host_responsive -w 0ms,0% -c 10000ms,100%"
  35. );
  36. is( $res->return_code, 1, "Syntax ok, with forced warning" );
  37. like( $res->output, $failureOutput, "Output OK" );
  38. $res = NPTest->testCmd(
  39. "sudo ./check_icmp -H $host_responsive -w 0,0% -c 0,0%"
  40. );
  41. is( $res->return_code, 2, "Syntax ok, with forced critical" );
  42. like( $res->output, $failureOutput, "Output OK" );
  43. $res = NPTest->testCmd(
  44. "sudo ./check_icmp -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100%"
  45. );
  46. is( $res->return_code, 2, "Timeout - host nonresponsive" );
  47. like( $res->output, '/100%/', "Error contains '100%' string (for 100% packet loss)" );
  48. $res = NPTest->testCmd(
  49. "sudo ./check_icmp -w 10000ms,100% -c 10000ms,100%"
  50. );
  51. is( $res->return_code, 3, "No hostname" );
  52. like( $res->output, '/No hosts to check/', "Output with appropriate error message");
  53. $res = NPTest->testCmd(
  54. "sudo ./check_icmp -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 0"
  55. );
  56. is( $res->return_code, 0, "One host nonresponsive - zero required" );
  57. like( $res->output, $successOutput, "Output OK" );
  58. $res = NPTest->testCmd(
  59. "sudo ./check_icmp -H $host_responsive -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 1"
  60. );
  61. is( $res->return_code, 0, "One of two host nonresponsive - one required" );
  62. like( $res->output, $successOutput, "Output OK" );
  63. $res = NPTest->testCmd(
  64. "sudo ./check_icmp -H $host_responsive -H $host_nonresponsive -w 10000ms,100% -c 10000ms,100% -n 1 -m 2"
  65. );
  66. is( $res->return_code, 2, "One of two host nonresponsive - two required" );
  67. like( $res->output, $failureOutput, "Output OK" );