From 2dec5182c508bd3cc286b4649836ead51aec50ef Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:18:51 +0100 Subject: check_swap: refactor to improve readability --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) (limited to '.gitignore') diff --git a/.gitignore b/.gitignore index 6f903d61..7e640a0e 100644 --- a/.gitignore +++ b/.gitignore @@ -223,6 +223,10 @@ NP-VERSION-FILE /plugins/tests/test_disk /plugins/tests/.deps +# /plugins/check_swap.d +/plugins/check_swap.d/.deps +/plugins/check_swap.d/.dirstamp + # /plugins-root/ /plugins-root/.deps /plugins-root/.libs -- cgit v1.2.3-74-g34f1 From 6fcbbaafc4bae79d4e674a2cf7f1d87d5a471603 Mon Sep 17 00:00:00 2001 From: RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:28:35 +0100 Subject: Implement first unit test for check_swap --- .gitignore | 2 ++ configure.ac | 3 +++ plugins/Makefile.am | 19 ++++++++++++++++--- plugins/tests/test_check_swap.c | 21 +++++++++++++++++++++ plugins/tests/test_check_swap.t | 6 ++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 plugins/tests/test_check_swap.c create mode 100644 plugins/tests/test_check_swap.t (limited to '.gitignore') diff --git a/.gitignore b/.gitignore index 7e640a0e..bbd7d844 100644 --- a/.gitignore +++ b/.gitignore @@ -221,7 +221,9 @@ NP-VERSION-FILE /plugins/tests/Makefile.in /plugins/tests/test_utils /plugins/tests/test_disk +/plugins/tests/test_check_swap /plugins/tests/.deps +/plugins/tests/.dirstamp # /plugins/check_swap.d /plugins/check_swap.d/.deps diff --git a/configure.ac b/configure.ac index 42fc0292..190b43ad 100644 --- a/configure.ac +++ b/configure.ac @@ -185,6 +185,9 @@ fi if test "$enable_libtap" = "yes" ; then EXTRA_TEST="test_utils test_disk test_tcp test_cmd test_base64" AC_SUBST(EXTRA_TEST) + + EXTRA_PLUGIN_TESTS="tests/test_check_swap" + AC_SUBST(EXTRA_PLUGIN_TESTS) fi dnl INI Parsing diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 1ca7cf3f..8ef2a246 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -38,19 +38,27 @@ check_tcp_programs = check_ftp check_imap check_nntp check_pop \ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ check_swap check_fping check_ldap check_game check_dig \ check_nagios check_by_ssh check_dns check_nt check_ide_smart \ - check_procs check_mysql_query check_apt check_dbi check_curl + check_procs check_mysql_query check_apt check_dbi check_curl \ + \ + tests/test_check_swap SUBDIRS = picohttpparser -EXTRA_DIST = t tests +np_test_scripts = tests/test_check_swap.t + +EXTRA_DIST = t tests $(np_test_scripts) PLUGINHDRS = common.h noinst_LIBRARIES = libnpcommon.a +noinst_PROGRAMS = @EXTRA_PLUGIN_TESTS@ +# These two lines support "make check", but we use "make test" +check_PROGRAMS = @EXTRA_PLUGIN_TESTS@ libnpcommon_a_SOURCES = utils.c netutils.c sslutils.c runcmd.c \ popen.c utils.h netutils.h popen.h common.h runcmd.c runcmd.h + BASEOBJS = libnpcommon.a ../lib/libmonitoringplug.a ../gl/libgnu.a $(LIB_CRYPTO) NETOBJS = $(BASEOBJS) $(EXTRA_NETOBLS) NETLIBS = $(NETOBJS) $(SOCKETLIBS) @@ -58,7 +66,10 @@ SSLOBJS = $(BASEOBJS) $(NETLIBS) $(SSLLIBS) $(LIB_CRYPTO) TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir) -TESTS = @PLUGIN_TEST@ +tap_ldflags = -L$(top_srcdir)/tap + +TESTS = @PLUGIN_TEST@ @EXTRA_PLUGIN_TESTS@ + test: perl -I $(top_builddir) -I $(top_srcdir) ../test.pl @@ -122,6 +133,8 @@ if !HAVE_UTMPX check_users_LDADD += popen.o endif +tests_test_check_swap_LDADD = $(BASEOBJS) check_swap.d/swap.o $(tap_ldflags) -ltap + ############################################################################## # secondary dependencies diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c new file mode 100644 index 00000000..42ac0086 --- /dev/null +++ b/plugins/tests/test_check_swap.c @@ -0,0 +1,21 @@ + +#include "../check_swap.d/check_swap.h" +#include "../../tap/tap.h" + +void print_usage() {}; +void print_help(swap_config config) { + (void) config; +}; + +const char *progname = "test_check_swap"; + +int main() { + + swap_config config = swap_config_init(); + + swap_result test_data = get_swap_data(config); + + plan_tests(1); + + ok(test_data.errorcode == 0, "Test whether we manage to retrieve swap data"); +} diff --git a/plugins/tests/test_check_swap.t b/plugins/tests/test_check_swap.t new file mode 100644 index 00000000..97c651a8 --- /dev/null +++ b/plugins/tests/test_check_swap.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use Test::More; +if (! -e "./test_check_swap") { + plan skip_all => "./test_swap not compiled - please enable libtap library to test"; +} +exec "./test_check_swap"; -- cgit v1.2.3-74-g34f1