4
0

check_ifoperstatus.t 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #! /usr/bin/perl -w -I ..
  2. #
  3. # SNMP Test via check_ifoperstatus
  4. #
  5. #
  6. use strict;
  7. use Test::More;
  8. use NPTest;
  9. my $tests = 15;
  10. plan tests => $tests;
  11. my $res;
  12. my $plugin = "check_ifoperstatus";
  13. SKIP: {
  14. skip "$plugin is not created", $tests if ( ! -x $plugin );
  15. my $host_snmp = getTestParameter( "NP_HOST_SNMP", "A host providing an SNMP Service", "localhost");
  16. my $snmp_community = getTestParameter( "NP_SNMP_COMMUNITY",
  17. "The SNMP Community string for SNMP Testing",
  18. "public");
  19. my ($snmp_interface, $snmp_ifxtable);
  20. if ($host_snmp) {
  21. $snmp_interface = getTestParameter( "NP_SNMP_INTERFACE", "Name of an active network interface on SNMP server", "lo" );
  22. $snmp_ifxtable = getTestParameter( "NP_SNMP_IFXTABLE",
  23. "Is IFXTABLE activated in SNMP server (1: yes, 0: no)? snmpwalk -v1 -c $snmp_community $host_snmp ifxtable",
  24. "1" );
  25. }
  26. my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE",
  27. "The hostname of system not responsive to network requests", "10.0.0.1" );
  28. my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID",
  29. "An invalid (not known to DNS) hostname",
  30. "nosuchhost" );
  31. $res = NPTest->testCmd( "./$plugin" );
  32. is( $res->return_code, 3, "No arguments" );
  33. like( $res->output, '/usage/', "Output contains usage" );
  34. $res = NPTest->testCmd( "./$plugin -H fakehostname" );
  35. is( $res->return_code, 3, "No key/descr specified" );
  36. like( $res->output, '/Either a valid snmp key/', "Output contains 'Either a valid snmp key'" );
  37. $res = NPTest->testCmd( "./$plugin -H fakehost -k 1 -v 3 --seclevel rubbish --secname foobar" );
  38. is( $res->return_code, 3, "invalid seclevel" );
  39. like( $res->output, "/Must define a valid security level/", "Output contains 'Must define a valid security level'" );
  40. SKIP: {
  41. skip "no snmp host defined", 6 if ( ! $host_snmp );
  42. $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1");
  43. cmp_ok( $res->return_code, '==', 0, "Exit OK for ifindex 1" );
  44. like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up");
  45. SKIP: {
  46. skip "no snmp interface defined", 2 if ( ! $snmp_interface );
  47. $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -d $snmp_interface");
  48. cmp_ok( $res->return_code, '==', 0, "Exit OK for ifdescr $snmp_interface" );
  49. like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up");
  50. }
  51. SKIP: {
  52. skip "ifxtable not available", 2 if ( ! $snmp_ifxtable );
  53. $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1 -n rubbish");
  54. cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN if interface name doesn't match" );
  55. like($res->output, '/doesn\'t match snmp value/', "String contains 'doesn't match snmp value'");
  56. }
  57. }
  58. # These checks need a complete command line. An invalid community is used so
  59. # the tests can run on hosts w/o snmp host/community in NPTest.cache. Execution will fail anyway
  60. SKIP: {
  61. skip "no non responsive host defined", 1 if ( ! $host_nonresponsive );
  62. $res = NPTest->testCmd( "./$plugin -H $host_nonresponsive -C np_foobar -k 1");
  63. cmp_ok( $res->return_code, '==', 1, "Exit WARNING with non responsive host" );
  64. }
  65. SKIP: {
  66. skip "no invalid host defined", 2 if ( ! $hostname_invalid );
  67. $res = NPTest->testCmd( "./$plugin -H $hostname_invalid -C np_foobar -k 1");
  68. cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with invalid host" );
  69. like($res->output, "/Unable to resolve.*$hostname_invalid/", "String matches unable to resolve.*$hostname_invalid");
  70. }
  71. }