Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Improve test isolation #298

Merged
merged 3 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ bench_log_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include
bench_log_LDADD = $(top_builddir)/lib/libqb.la

if HAVE_CHECK
EXTRA_DIST += resources.test
EXTRA_DIST += start.test resources.test
EXTRA_DIST += blackbox-segfault.sh

TESTS = array.test map.test rb.test log.test blackbox-segfault.sh loop.test ipc.test resources.test
TESTS = start.test array.test map.test rb.test log.test blackbox-segfault.sh loop.test ipc.test resources.test

resources.log: rb.log log.log ipc.log

check_LTLIBRARIES =
check_PROGRAMS = array.test map.test rb.test log.test loop.test ipc.test util.test crash_test_dummy file_change_bytes
dist_check_SCRIPTS = resources.test blackbox-segfault.sh
dist_check_SCRIPTS = start.test resources.test blackbox-segfault.sh

if HAVE_SLOW_TESTS
TESTS += util.test
Expand Down
30 changes: 12 additions & 18 deletions tests/check_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,19 @@ exit_handler(int32_t rsignal, void *data)
static void
set_ipc_name(const char *prefix)
{
/* We have to make the server name as unique as possible given
* the build- (seconds part of preprocessor's timestamp) and
* run-time (pid + lower 16 bits of the current timestamp)
* circumstances, because some build systems attempt to generate
* packages for libqb in parallel. These unit tests are run
* during the package build process. 2+ builds executing on
* the same machine (whether containerized or not because of
* abstract unix sockets namespace sharing) can stomp on each
* other's unit tests if the ipc server names aren't unique... */

/* single-shot grab of seconds part of preprocessor's timestamp */
static char t_sec[3] = "";
if (t_sec[0] == '\0') {
const char *const found = strrchr(__TIME__, ':');
strncpy(t_sec, found ? found + 1 : "-", sizeof(t_sec) - 1);
t_sec[sizeof(t_sec) - 1] = '\0';
FILE *f;
char process_name[256];

/* The process-unique part of the IPC name has already been decided
* and stored in the file ipc-test-name.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be nice to have some fallback when ipc-test-name file does not exists because right now, result is going to be random bytes from memory.

f= fopen("ipc-test-name", "r");
if (f) {
fgets(process_name, sizeof(process_name), f);
fclose(f);
}

snprintf(ipc_name, sizeof(ipc_name), "%s%s%lX%.4x", prefix, t_sec,
(unsigned long)getpid(), (unsigned) ((long) time(NULL) % (0x10000)));
snprintf(ipc_name, sizeof(ipc_name), "%s%s", prefix, process_name);
}

static int32_t
Expand Down Expand Up @@ -1444,6 +1437,7 @@ START_TEST(test_ipcc_truncate_when_unlink_fails_shm)
test_ipc_server_fail();
_fi_unlink_inject_failure = QB_FALSE;
qb_leave();
unlink(sock_file);
}
END_TEST
#endif
Expand Down
5 changes: 3 additions & 2 deletions tests/resources.test
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/bin/sh
RETURN=0

IPC_NAME=`cat ipc-test-name`
for d in /dev/shm /var/run; do
leftovers=$(find $d -name qb-test* -size +0c 2>/dev/null | wc -l)
leftovers=$(find $d -name qb-test*${IPC_NAME}* -size +0c 2>/dev/null | wc -l)
if [ "${leftovers}" -gt 0 ]; then
echo
echo "Error: shared memory segments not closed/unlinked"
echo
RETURN=1
fi
leftovers="$(find $d -name qb-test* -size 0c 2>/dev/null)"
leftovers="$(find $d -name qb-test*${IPC_NAME}* -size 0c 2>/dev/null)"
if [ "$(printf '%s\n' "${leftovers}" | wc -l)" -eq 6 ]; then
echo
echo "There were some empty leftovers (expected), removing them"
Expand Down
10 changes: 10 additions & 0 deletions tests/start.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

#
# Generate a unique(ish) name for the IPCs we will use in the tests and
# save it in a file for all of the tests to use. This way we know for sure
# which sockets are our and which we can ignore.
# The test programs all add "qb-test-<name>-" to the front of this.
#
NAME="$$-`date +%N`"
echo -n "$NAME" > ipc-test-name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line missing