Code

1d6114f6b831f8cbfc9dc3b48f76f47dc39d0238
[nagiosplug.git] / plugins-scripts / t / check_ifoperstatus.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # SNMP Test via check_ifoperstatus
4 #
5 #
7 use strict;
8 use Test::More;
9 use NPTest;
11 my $tests = 15;
12 plan tests => $tests;
13 my $res;
15 my $plugin = "check_ifoperstatus";
16 SKIP: {
17         skip "$plugin is not created", $tests if ( ! -x $plugin );
19         my $host_snmp = getTestParameter( "NP_HOST_SNMP", "A host providing an SNMP Service", "localhost");
21         my $snmp_community = getTestParameter( "NP_SNMP_COMMUNITY",
22                                                "The SNMP Community string for SNMP Testing (pick default rather than 'none' when no snmp host is available)",
23                                                "public");
25         my ($snmp_interface, $snmp_ifxtable);
26         if ($host_snmp) {
27                 $snmp_interface   = getTestParameter( "NP_SNMP_INTERFACE", "Name of an active network interface on SNMP server", "lo" );
29                 $snmp_ifxtable   = getTestParameter( "NP_SNMP_IFXTABLE",   
30                                                      "Is IFXTABLE activated in SNMP server (1: yes, 0: no)? snmpwalk -v1 -c $snmp_community $host_snmp ifxtable",
31                                                      "1" );
32         }
34         my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", 
35                                                    "The hostname of system not responsive to network requests", "10.0.0.1" );
37         my $hostname_invalid   = getTestParameter( "NP_HOSTNAME_INVALID",
38                                                    "An invalid (not known to DNS) hostname",
39                                                    "nosuchhost" );
43         $res = NPTest->testCmd( "./$plugin" );
44         is( $res->return_code, 3, "No arguments" );
45         like( $res->output, '/usage/', "Output contains usage" );
46         
47         $res = NPTest->testCmd( "./$plugin -H fakehostname" );
48         is( $res->return_code, 3, "No key/descr specified" );
49         like( $res->output, '/Either a valid snmp key/', "Output contains 'Either a valid snmp key'" );
51         $res = NPTest->testCmd( "./$plugin -H fakehost -k 1 -v 3 --seclevel rubbish --secname foobar" );
52         is( $res->return_code, 3, "invalid seclevel" );
53         like( $res->output, "/Must define a valid security level/", "Output contains 'Must define a valid security level'" );
55         SKIP: {
56                 skip "no snmp host defined", 6 if ( ! $host_snmp );
58                 $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1");
59                 cmp_ok( $res->return_code, '==', 0, "Exit OK for ifindex 1" ); 
60                 like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up");
62                 SKIP: {
63                         skip "no snmp interface defined", 2 if ( ! $snmp_interface );
64                         $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -d $snmp_interface");
65                         cmp_ok( $res->return_code, '==', 0, "Exit OK for ifdescr $snmp_interface" );
66                         like($res->output, '/^OK.*Interface.*is up/', "String contains OK Interface is up");
67                 }
69                 SKIP: {
70                         skip "ifxtable not available", 2 if ( ! $snmp_ifxtable );
71                         $res = NPTest->testCmd( "./$plugin -H $host_snmp -C $snmp_community -k 1 -n rubbish");
72                         cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN if interface name doesn't match" ); 
73                         like($res->output, '/doesn\'t match snmp value/', "String contains 'doesn't match snmp value'");
74                 }
76         }
78         SKIP: {
79                 skip "no non responsive host defined", 1 if ( ! $host_nonresponsive );
80                 $res = NPTest->testCmd( "./$plugin -H $host_nonresponsive -C $snmp_community -k 1");
81                 cmp_ok( $res->return_code, '==', 1, "Exit WARNING with non responsive host" ); 
82         }
84         SKIP: {
85                 skip "no invalid host defined", 2 if ( ! $hostname_invalid );
86                 $res = NPTest->testCmd( "./$plugin -H $hostname_invalid -C $snmp_community -k 1");
87                 cmp_ok( $res->return_code, '==', 3, "Exit UNKNOWN with invalid host" ); 
88                 like($res->output, "/Unable to resolve.*$hostname_invalid/", "String matches unable to resolve.*$hostname_invalid");
89         }
91 }