From 78df90b180740712d0c90b6d982b78241cc99d72 Mon Sep 17 00:00:00 2001 From: Chrissie Caulfield Date: Mon, 21 Mar 2022 09:10:35 +0000 Subject: [PATCH] tests: Run IPC with use-filesystem-sockets active (#455) * tests: Run IPC with use-filesystem-sockets active Provide an LD_PRELOAD library that simulates the presence of /etc/libqb/use-filesystem-sockets so that we can test that functionality without actually having the file on the system and affecting everything else running on the box. * use $() rather than `` * Cope with spaces in directory names * Docs: quote DOXYGEN2MAN in Makefile.am rather than configure.ac --- docs/Makefile.am | 26 +++++++++--------- tests/Makefile.am | 11 +++++--- tests/ipc_sock.test | 19 ++++++++++++++ tests/libstat_wrapper.c | 58 +++++++++++++++++++++++++++++++++++++++++ tests/resources.test | 20 ++++++++++---- tests/start.test | 2 +- 6 files changed, 114 insertions(+), 22 deletions(-) create mode 100755 tests/ipc_sock.test create mode 100644 tests/libstat_wrapper.c diff --git a/docs/Makefile.am b/docs/Makefile.am index fa1bac6b4..ab58a463d 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -236,19 +236,19 @@ txt-man: man.dox xml-man: txt-man mkdir -p man3 - $(DOXYGEN2MAN) $(doxygen2man_flags) qbarray_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbatomic_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbdefs_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbhdb_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbipcc_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbipc__common_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbipcs_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qblist_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qblog_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbloop_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbmap_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbrb_8h.xml - $(DOXYGEN2MAN) $(doxygen2man_flags) qbutil_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbarray_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbatomic_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbdefs_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbhdb_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbipcc_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbipc__common_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbipcs_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qblist_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qblog_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbloop_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbmap_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbrb_8h.xml + "$(DOXYGEN2MAN)" $(doxygen2man_flags) qbutil_8h.xml all: $(man3_MANS) xml-man diff --git a/tests/Makefile.am b/tests/Makefile.am index 7fc40b3df..093bd1e50 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -114,11 +114,16 @@ endif bench_log_SOURCES = bench-log.c bench_log_LDADD = $(top_builddir)/lib/libqb.la +lib_LTLIBRARIES = libstat_wrapper.la +libstat_wrapper_la_SOURCES = libstat_wrapper.c +libstat_wrapper_la_LIBADD = -ldl +libdir= $(TESTDIR) + if HAVE_CHECK -EXTRA_DIST += start.test resources.test +EXTRA_DIST += start.test resources.test ipc_sock.test EXTRA_DIST += blackbox-segfault.sh -TESTS = start.test array.test map.test rb.test list.test log.test blackbox-segfault.sh loop.test ipc.test resources.test +TESTS = start.test array.test map.test rb.test list.test log.test blackbox-segfault.sh loop.test ipc.test ipc_sock.test resources.test TESTS_ENVIRONMENT = export PATH=.:../tools:$$PATH; resources.log: rb.log log.log ipc.log @@ -127,7 +132,7 @@ check_LTLIBRARIES = check_PROGRAMS = array.test ipc.test list.test log.test loop.test \ map.test rb.test util.test tlist.test \ crash_test_dummy file_change_bytes -dist_check_SCRIPTS = start.test resources.test blackbox-segfault.sh +dist_check_SCRIPTS = start.test resources.test blackbox-segfault.sh ipc_sock.test if HAVE_SLOW_TESTS TESTS += util.test diff --git a/tests/ipc_sock.test b/tests/ipc_sock.test new file mode 100755 index 000000000..0bcebe251 --- /dev/null +++ b/tests/ipc_sock.test @@ -0,0 +1,19 @@ +#!/bin/sh +# +# Run the IPC tests under the stat wrapper, +# this simulates /etc/libqb/use-filesystem-sockets existing +# so we can test both options without breaking other things +# that might be running on this system +# +if [ "$(uname -s)" = "Linux" ] +then + if [ -f "$(pwd)/.libs/libstat_wrapper.so" ] + then + export "LD_PRELOAD=$(pwd)/.libs/libstat_wrapper.so" + else + export "LD_PRELOAD=$(pwd)/libstat_wrapper.so" + fi + ./ipc.test +else + exit 0 +fi diff --git a/tests/libstat_wrapper.c b/tests/libstat_wrapper.c new file mode 100644 index 000000000..37f51e6e5 --- /dev/null +++ b/tests/libstat_wrapper.c @@ -0,0 +1,58 @@ +/* + * Simulate FORCESOCKETSFILE existing for the IPC tests + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include "../include/config.h" +#if defined(QB_LINUX) || defined(QB_CYGWIN) +#include +#endif + +// __xstat for earlier libc +int __xstat(int __ver, const char *__filename, struct stat *__stat_buf) +{ +#if defined(QB_LINUX) || defined(QB_CYGWIN) + static int opened = 0; + static int (*real_xstat)(int __ver, const char *__filename, void *__stat_buf); + + if (!opened) { + real_xstat = dlsym(RTLD_NEXT, "__xstat"); + opened = 1; + } + + if (strcmp(__filename, FORCESOCKETSFILE) == 0) { + fprintf(stderr, "__xstat called for %s\n", __filename); + return 0; /* it exists! */ + } + + return real_xstat(__ver, __filename, __stat_buf); +#else + return -1; /* Error in the unlikely event we get called on *BSD* */ +#endif +} + +// stat for F35 and later +int stat(const char *__filename, struct stat *__stat_buf) +{ +#if defined(QB_LINUX) || defined(QB_CYGWIN) + static int opened = 0; + static int (*real_stat)(const char *__filename, void *__stat_buf); + + if (!opened) { + real_stat = dlsym(RTLD_NEXT, "stat"); + opened = 1; + } + + if (strcmp(__filename, FORCESOCKETSFILE) == 0) { + fprintf(stderr, "stat called for %s\n", __filename); + return 0; /* it exists! */ + } + + return real_stat(__filename, __stat_buf); +#else + return -1; /* Error in the unlikely event we get called on *BSD* */ +#endif +} diff --git a/tests/resources.test b/tests/resources.test index 252c71a53..d3f8e0f1e 100755 --- a/tests/resources.test +++ b/tests/resources.test @@ -1,23 +1,33 @@ #!/bin/sh RETURN=0 SOCKS_PER_PROCESS=3 +EXPECTED_DLOCK=6 +EXPECTED_LEFTOVER=2 + +# Linux also runs filesystem socket tests +if [ "$(uname -s)" = "Linux" ] +then + EXPECTED_DLOCK=12 + EXPECTED_LEFTOVER=4 +fi tidy_qb_dirs() { for dd in "$@"; do rm $dd - rmdir `dirname $dd` 2> /dev/null + rmdir $(dirname $dd) 2> /dev/null done } -IPC_NAME=`cat ipc-test-name 2>/dev/null` +IPC_NAME=$(cat ipc-test-name 2>/dev/null) for d in /dev/shm /var/run $SOCKETDIR; do # Tidy up the deadlock checker sockets first dlocks=$(find $d -name "qb-*-test_*dlock*${IPC_NAME}*" -size +0c 2>/dev/null) - if [ "`echo $dlocks|wc -w`" -eq $(($SOCKS_PER_PROCESS * 6)) ]; then - tidy_qb_dirs $dlocks + if [ "$(echo $dlocks|wc -w)" -eq $(($SOCKS_PER_PROCESS * $EXPECTED_DLOCK)) ]; then + tidy_qb_dirs $dlocks + rm $dlocks elif [ -n "${dlocks}" ]; then echo echo "Error: dlock shared memory segments not closed/unlinked" @@ -34,7 +44,7 @@ for d in /dev/shm /var/run $SOCKETDIR; do RETURN=1 fi leftovers="$(find $d -name "qb-*-test_*${IPC_NAME}*" -size 0c 2>/dev/null)" - if [ "$(printf '%s\n' "${leftovers}" | wc -l)" -eq $(($SOCKS_PER_PROCESS * 2)) ]; then + if [ "$(printf '%s\n' "${leftovers}" | wc -l)" -eq $(($SOCKS_PER_PROCESS * $EXPECTED_LEFTOVER)) ]; then echo echo "There were some empty leftovers (expected), removing them" echo "${leftovers}" diff --git a/tests/start.test b/tests/start.test index 91d1734f0..7f6354037 100755 --- a/tests/start.test +++ b/tests/start.test @@ -7,7 +7,7 @@ # The test programs all add "qb-test--" to the front of this. # -NAME="$$-`date +%s`" +NAME="$$-$(date +%s)" printf "$NAME" > ipc-test-name mkdir -p $SOCKETDIR