diff --git a/configure.ac b/configure.ac index ae7eb30b..b21be005 100644 --- a/configure.ac +++ b/configure.ac @@ -184,7 +184,7 @@ if test "$enable_libtap" = "yes" ; then EXTRA_TEST="test_utils test_tcp test_cmd test_base64" AC_SUBST(EXTRA_TEST) - EXTRA_PLUGIN_TESTS="tests/test_check_swap tests/test_check_disk" + EXTRA_PLUGIN_TESTS="tests/test_check_swap tests/test_check_disk tests/test_check_snmp" AC_SUBST(EXTRA_PLUGIN_TESTS) fi diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a35f273e..7c80283c 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -53,13 +53,12 @@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_hpjd \ SUBDIRS = picohttpparser -np_test_scripts = tests/test_check_swap.t \ - tests/test_check_snmp.t \ - tests/test_check_disk.t +np_test_scripts = tests/check_swap.t \ + tests/check_snmp.t \ + tests/check_disk.t EXTRA_DIST = t \ tests \ - $(np_test_scripts) \ negate.d \ check_swap.d \ check_ldap.d \ @@ -190,6 +189,8 @@ tests_test_check_swap_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap tests_test_check_swap_SOURCES = tests/test_check_swap.c check_swap.d/swap.c tests_test_check_snmp_LDADD = $(BASEOBJS) $(tap_ldflags) -ltap tests_test_check_snmp_SOURCES = tests/test_check_snmp.c check_snmp.d/check_snmp_helpers.c +tests_test_check_snmp_LDFLAGS = $(AM_LDFLAGS) -lm `net-snmp-config --libs` +tests_test_check_snmp_CFLAGS = $(AM_CFLAGS) `net-snmp-config --cflags` tests_test_check_disk_LDADD = $(BASEOBJS) $(tap_ldflags) check_disk.d/utils_disk.c -ltap tests_test_check_disk_SOURCES = tests/test_check_disk.c diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp_peer.t similarity index 98% rename from plugins/t/check_ntp.t rename to plugins/t/check_ntp_peer.t index 7703bc3b..a03be52b 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp_peer.t @@ -5,10 +5,11 @@ # use strict; +use warnings; use Test::More; use NPTest; -my @PLUGINS1 = ('check_ntp_peer', 'check_ntp_time'); +my @PLUGINS1 = ('check_ntp_peer'); my @PLUGINS2 = ('check_ntp_peer'); plan tests => (12 * scalar(@PLUGINS1)) + (6 * scalar(@PLUGINS2)); diff --git a/plugins/t/check_ntp_time.t b/plugins/t/check_ntp_time.t new file mode 100644 index 00000000..a814c3d6 --- /dev/null +++ b/plugins/t/check_ntp_time.t @@ -0,0 +1,87 @@ +#! /usr/bin/perl -w -I .. +# +# Testing NTP +# +# + +use strict; +use warnings; +use Test::More; +use NPTest; + +my @PLUGINS1 = ('check_ntp_time'); + +plan tests => (12 * scalar(@PLUGINS1)); + +my $res; + +my $ntp_service = getTestParameter( "NP_GOOD_NTP_SERVICE", + "A host providing NTP service", + "pool.ntp.org"); + +my $no_ntp_service = getTestParameter( "NP_NO_NTP_SERVICE", + "A host NOT providing the NTP service", + "localhost" ); + +my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", + "The hostname of system not responsive to network requests", + "10.0.0.1" ); + +my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", + "An invalid (not known to DNS) hostname", + "nosuchhost"); + +my $ntp_okmatch1 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_warnmatch1 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/'; +my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; +my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; +my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; +my $ntp_noresponse = '/(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)/'; +my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; + + +foreach my $plugin (@PLUGINS1) { + SKIP: { + skip "No NTP server defined", 6 unless $ntp_service; + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000" + ); + cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result (simple check)" ); + like( $res->output, $ntp_okmatch1, "$plugin: Output match OK (simple check)" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000: -c 2000" + ); + cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result (simple check)" ); + like( $res->output, $ntp_warnmatch1, "$plugin: Output match WARNING (simple check)" ); + + $res = NPTest->testCmd( + "./$plugin -H $ntp_service -w 1000 -c 2000:" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result (simple check)" ); + like( $res->output, $ntp_critmatch1, "$plugin: Output match CRITICAL (simple check)" ); + } + + SKIP: { + skip "No bad NTP server defined", 1 unless $no_ntp_service; + $res = NPTest->testCmd( + "./$plugin -H $no_ntp_service -t 3" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: No NTP service" ); + like( $res->output, $ntp_noresponse, "$plugin: Output match no NTP service" ); + } + + $res = NPTest->testCmd( + "./$plugin -H $host_nonresponsive -t 3" + ); + cmp_ok( $res->return_code, '==', 2, "$plugin: Server not responding" ); + like( $res->output, $ntp_noresponse, "$plugin: Output match non-responsive" ); + + $res = NPTest->testCmd( + "./$plugin -H $hostname_invalid" + ); + cmp_ok( $res->return_code, '==', 3, "$plugin: Invalid hostname/address" ); + like( $res->output, $ntp_nosuchhost, "$plugin: Output match invalid hostname/address" ); + +} diff --git a/plugins/tests/test_check_disk.t b/plugins/tests/check_disk.t similarity index 56% rename from plugins/tests/test_check_disk.t rename to plugins/tests/check_disk.t index 56354650..d6099bb3 100755 --- a/plugins/tests/test_check_disk.t +++ b/plugins/tests/check_disk.t @@ -1,6 +1,9 @@ #!/usr/bin/perl +use strict; +use warnings; + use Test::More; -if (! -e "./test_check_disk") { +if (! -e "./tests/test_check_disk") { plan skip_all => "./test_check_disk not compiled - please enable libtap library to test"; } -exec "./test_check_disk"; +exec "./tests/test_check_disk"; diff --git a/plugins/tests/check_snmp.t b/plugins/tests/check_snmp.t index 26d67898..2bdbfed1 100755 --- a/plugins/tests/check_snmp.t +++ b/plugins/tests/check_snmp.t @@ -275,3 +275,8 @@ like($res->output, '/.*4.20.*| iso.3.6.1.4.1.8072.3.2.67.19=4.20/', "Test multip $res = NPTest->testCmd( "./check_snmp -H 127.0.0.1 -C public -p $port_snmp -o .1.3.6.1.4.1.8072.3.2.67.19 --multiplier=.1 -w 1"); is($res->return_code, 1, "Test multiply RC + thresholds" ); like($res->output, '/.*4.20.* | iso.3.6.1.4.1.8072.3.2.67.19=4.20+;1/', "Test multiply .1 output + thresholds" ); + +if (! -e "./tests/test_check_snmp") { + plan skip_all => "./test_check_snmp not compiled - please enable libtap library to test"; +} +exec "./tests/test_check_snmp"; diff --git a/plugins/tests/test_check_swap.t b/plugins/tests/check_swap.t similarity index 56% rename from plugins/tests/test_check_swap.t rename to plugins/tests/check_swap.t index 826fae01..b3768d7f 100755 --- a/plugins/tests/test_check_swap.t +++ b/plugins/tests/check_swap.t @@ -1,6 +1,9 @@ #!/usr/bin/perl +use strict; +use warnings; + use Test::More; -if (! -e "./test_check_swap") { +if (! -e "./tests/test_check_swap") { plan skip_all => "./test_check_swap not compiled - please enable libtap library to test"; } -exec "./test_check_swap"; +exec "./tests/test_check_swap"; diff --git a/plugins/tests/test_check_snmp b/plugins/tests/test_check_snmp new file mode 100755 index 00000000..1650d6e5 Binary files /dev/null and b/plugins/tests/test_check_snmp differ diff --git a/plugins/tests/test_check_snmp.c b/plugins/tests/test_check_snmp.c index d71706d0..0fdcd9c2 100644 --- a/plugins/tests/test_check_snmp.c +++ b/plugins/tests/test_check_snmp.c @@ -16,7 +16,7 @@ * *****************************************************************************/ -#include "tap.h" +#include "../../tap/tap.h" #include "../../config.h" #include @@ -26,6 +26,10 @@ #include "utils_base.c" #include "../check_snmp.d/check_snmp_helpers.h" +int verbose = 0; +void print_usage() {} +const char *progname = "test_check_snmp"; + char *_np_state_generate_key(int argc, char **argv); char *_np_state_calculate_location_prefix(void); diff --git a/plugins/tests/test_check_snmp.t b/plugins/tests/test_check_snmp.t deleted file mode 100755 index 967633e9..00000000 --- a/plugins/tests/test_check_snmp.t +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/perl -use Test::More; -if (! -e "./test_check_snmp") { - plan skip_all => "./test_check_snmp not compiled - please enable libtap library to test"; -} -exec "./test_check_snmp";