check_icmp.t 2.7 KB

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