check_dhcp.t 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # DHCP Tests via check_dhcp
  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 => 4;
  13. } else {
  14. plan skip_all => "Need sudo to test check_dhcp";
  15. }
  16. my $sudo = $> == 0 ? '' : 'sudo';
  17. my $successOutput = '/OK: Received \d+ DHCPOFFER\(s\), \d+ of 1 requested servers responded, max lease time = \d+ sec\./';
  18. my $failureOutput = '/CRITICAL: Received \d+ DHCPOFFER\(s\), 0 of \d+ requested servers responded/';
  19. my $host_responsive = getTestParameter( "NP_HOST_DHCP_RESPONSIVE",
  20. "The hostname of system responsive to dhcp requests",
  21. "localhost" );
  22. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  23. "The hostname of system not responsive to dhcp 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_dhcp -s $host_responsive"
  31. );
  32. is( $res->return_code, 0, "Syntax ok" );
  33. like( $res->output, $successOutput, "Output OK" );
  34. $res = NPTest->testCmd(
  35. "$sudo ./check_dhcp -s $host_nonresponsive"
  36. );
  37. is( $res->return_code, 2, "Timeout - host nonresponsive" );
  38. like( $res->output, $failureOutput, "Output OK" );