diff options
author | Thomas Haller <thaller@redhat.com> | 2015-11-24 11:03:21 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-11-24 11:15:27 +0100 |
commit | 1e7d952731a39e71947ee0bfc3cd0dfcb9015caa (patch) | |
tree | c44e59cd6d9f1ee16c90599181fd7fa9fcc431f2 | |
parent | dd32d973b02c83b1315bb58b3f487aaf2364b47f (diff) | |
parent | f29aee87ec84f355864ba880358effcd32797c84 (diff) | |
download | NetworkManager-1e7d952731a39e71947ee0bfc3cd0dfcb9015caa.tar.gz |
tests: merge branch 'th/test-run-valgrind'
(cherry picked from commit 5276f6896b595b6ebc1a3a9233bc9eaa721a9e4e)
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | include/nm-test-utils.h | 110 | ||||
-rwxr-xr-x | tools/run-test-valgrind.sh | 93 |
3 files changed, 184 insertions, 21 deletions
diff --git a/configure.ac b/configure.ac index b44b3b80d4..7603ac455c 100644 --- a/configure.ac +++ b/configure.ac @@ -891,7 +891,7 @@ else fi fi AS_IF([test "$with_valgrind" != "no"], - AC_SUBST(VALGRIND_RULES, 'LOG_COMPILER = "$(top_srcdir)/tools/run-test-valgrind.sh" "$(LIBTOOL)" "$(with_valgrind)" '"$with_valgrind_suppressions"), + AC_SUBST(VALGRIND_RULES, 'LOG_COMPILER = "$(top_srcdir)/tools/run-test-valgrind.sh" --called-from-make "$(LIBTOOL)" "$(with_valgrind)" '"$with_valgrind_suppressions"), AC_SUBST(VALGRIND_RULES, [])) AM_CONDITIONAL(WITH_VALGRIND, test "${with_valgrind}" != "no") diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 258d39370b..0067fa7446 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -80,6 +80,10 @@ * - respect g_test_quick(), if the command line contains '-mslow', '-mquick', '-mthorough'. * - use compile time default * + * "p=PATH"|"s=PATH": passes the path to g_test_init() as "-p" and "-s", respectively. + * Unfortunately, these options conflict with "--tap" which our makefile passes to the + * tests, thus it's only useful outside of `make check`. + * *******************************************************************************/ #include <arpa/inet.h> @@ -142,6 +146,7 @@ struct __nmtst_internal gboolean assert_logging; gboolean no_expect_message; gboolean test_quick; + gboolean test_tap_log; char *sudo_cmd; char **orig_argv; }; @@ -256,6 +261,8 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ gboolean test_quick = FALSE; gboolean test_quick_set = FALSE; gboolean test_quick_argv = FALSE; + gs_unref_ptrarray GPtrArray *p_tests = NULL; + gs_unref_ptrarray GPtrArray *s_tests = NULL; if (!out_set_logging) out_set_logging = &_out_set_logging; @@ -275,17 +282,6 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ if (argc) __nmtst_internal.orig_argv = g_strdupv (*argv); - if (argc && !g_test_initialized ()) { - /* g_test_init() is a variadic function, so we cannot pass it - * (variadic) arguments. If you need to pass additional parameters, - * call nmtst_init() with argc==NULL and call g_test_init() yourself. */ - - /* g_test_init() sets g_log_set_always_fatal() for G_LOG_LEVEL_WARNING - * and G_LOG_LEVEL_CRITICAL. So, beware that the test will fail if you - * have any WARN or ERR log messages -- unless you g_test_expect_message(). */ - g_test_init (argc, argv, NULL); - } - __nmtst_internal.assert_logging = !!assert_logging; #if !GLIB_CHECK_VERSION (2, 35, 0) @@ -325,6 +321,14 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ sudo_cmd = g_strdup (&debug[strlen ("sudo-cmd=")]); } else if (!g_ascii_strcasecmp (debug, "no-expect-message")) { no_expect_message = TRUE; + } else if (!g_ascii_strncasecmp (debug, "p=", strlen ("p="))) { + if (!p_tests) + p_tests = g_ptr_array_new_with_free_func (g_free); + g_ptr_array_add (p_tests, g_strdup (&debug[strlen ("p=")])); + } else if (!g_ascii_strncasecmp (debug, "s=", strlen ("s="))) { + if (!s_tests) + s_tests = g_ptr_array_new_with_free_func (g_free); + g_ptr_array_add (s_tests, g_strdup (&debug[strlen ("s=")])); } else if (!g_ascii_strcasecmp (debug, "slow") || !g_ascii_strcasecmp (debug, "thorough")) { test_quick = FALSE; test_quick_set = TRUE; @@ -358,6 +362,90 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_ || !strcmp (*(a+1), "slow") || !strcmp (*(a+1), "thorough")))) test_quick_argv = TRUE; + else if (strcmp (*a, "--tap") == 0) + __nmtst_internal.test_tap_log = TRUE; + } + } + + if (!argc || g_test_initialized ()) { + if (p_tests || s_tests) { + char *msg = g_strdup_printf (">>> nmtst: ignore -p and -s options for test which calls g_test_init() itself"); + + g_array_append_val (debug_messages, msg); + } + } else { + /* g_test_init() is a variadic function, so we cannot pass it + * (variadic) arguments. If you need to pass additional parameters, + * call nmtst_init() with argc==NULL and call g_test_init() yourself. */ + + /* g_test_init() sets g_log_set_always_fatal() for G_LOG_LEVEL_WARNING + * and G_LOG_LEVEL_CRITICAL. So, beware that the test will fail if you + * have any WARN or ERR log messages -- unless you g_test_expect_message(). */ + GPtrArray *arg_array = g_ptr_array_new (); + gs_free char **arg_array_c = NULL; + int arg_array_n, j; + static char **s_tests_x, **p_tests_x; + + if (*argc) { + for (i = 0; i < *argc; i++) + g_ptr_array_add (arg_array, (*argv)[i]); + } else + g_ptr_array_add (arg_array, "./test"); + + if (test_quick_set && !test_quick_argv) + g_ptr_array_add (arg_array, "-m=quick"); + + if (!__nmtst_internal.test_tap_log) { + for (i = 0; p_tests && i < p_tests->len; i++) { + g_ptr_array_add (arg_array, "-p"); + g_ptr_array_add (arg_array, p_tests->pdata[i]); + } + for (i = 0; s_tests && i < s_tests->len; i++) { + g_ptr_array_add (arg_array, "-s"); + g_ptr_array_add (arg_array, s_tests->pdata[i]); + } + } else if (p_tests || s_tests) { + char *msg = g_strdup_printf (">>> nmtst: ignore -p and -s options for tap-tests"); + + g_array_append_val (debug_messages, msg); + } + + g_ptr_array_add (arg_array, NULL); + + arg_array_n = arg_array->len - 1; + arg_array_c = (char **) g_ptr_array_free (arg_array, FALSE); + + g_test_init (&arg_array_n, &arg_array_c, NULL); + + if (*argc > 1) { + /* collaps argc/argv by removing the arguments detected + * by g_test_init(). */ + for (i = 1, j = 1; i < *argc; i++) { + if ((*argv)[i] == arg_array_c[j]) + j++; + else + (*argv)[i] = NULL; + } + for (i = 1, j = 1; i < *argc; i++) { + if ((*argv)[i]) { + (*argv)[j++] = (*argv)[i]; + if (i >= j) + (*argv)[i] = NULL; + } + } + *argc = j; + } + + /* we must "leak" the test paths because they are not cloned by g_test_init(). */ + if (!__nmtst_internal.test_tap_log) { + if (p_tests) { + p_tests_x = (char **) g_ptr_array_free (p_tests, FALSE); + p_tests = NULL; + } + if (s_tests) { + s_tests_x = (char **) g_ptr_array_free (s_tests, FALSE); + s_tests = NULL; + } } } diff --git a/tools/run-test-valgrind.sh b/tools/run-test-valgrind.sh index ff5bc44ce3..424f633d06 100755 --- a/tools/run-test-valgrind.sh +++ b/tools/run-test-valgrind.sh @@ -1,19 +1,93 @@ -#!/bin/sh +#!/bin/bash + +die() { + echo "$@" + exit 5 +} + +SCRIPT_PATH="${SCRIPT_PATH:-$(readlink -f "$(dirname "$0")")}" -LIBTOOL="$1"; shift -VALGRIND="$1"; shift -SUPPRESSIONS="$1"; shift VALGRIND_ERROR=37 -if [ "$1" = "--launch-dbus" ]; then +if [ "$1" == "--called-from-make" ]; then + shift + NMTST_LIBTOOL=($1 --mode=execute); shift + NMTST_VALGRIND="$1"; shift + SUPPRESSIONS="$1"; shift + if [ "$1" = "--launch-dbus" ]; then + NMTST_LAUNCH_DBUS=yes + shift + else + NMTST_LAUNCH_DBUS=no + fi + TEST="$1"; shift +else + if [ -n "${NMTST_LIBTOOL-:x}" ]; then + NMTST_LIBTOOL=(sh "$SCRIPT_PATH/../libtool" --mode=execute) + elif [ -n "${NMTST_LIBTOOL-x}" ]; then + NMTST_LIBTOOL=() + else + NMTST_LIBTOOL=($NMTST_LIBTOOL --mode=execute) + fi + for a in "$@"; do + case "$a" in + "--launch-dbus") + NMTST_LAUNCH_DBUS=yes + shift + ;; + "--no-launch-dbus"|"-D") + NMTST_LAUNCH_DBUS=no + shift + ;; + "--no-libtool") + NMTST_LIBTOOL=() + shift + ;; + "--") + shift + break + ;; + *) + break + ;; + esac + done + # we support calling the script directly. In this case, + # only pass the path to the test to run. + TEST="$1"; shift + NMTST_VALGRIND="${NMTST_VALGRIND:-valgrind}" + if [ "$SUPPRESSIONS" == "" ]; then + SUPPRESSIONS="$SCRIPT_PATH/../valgrind.suppressions" + fi + + [ -x "$TEST" ] || die "Test \"$TEST\" does not exist" + + TEST_PATH="$(readlink -f "$(dirname "$TEST")")" + + if [ -n "${NMTST_LAUNCH_DBUS-x}" ]; then + # autodetect whether to launch D-Bus based on the test path. + if [[ $TEST_PATH == */libnm/tests || $TEST_PATH == */libnm-glib/tests ]]; then + NMTST_LAUNCH_DBUS=yes + else + NMTST_LAUNCH_DBUS=no + fi + fi + + # some tests require you to cd into the base directory. + # do that. + if [ "$NMTST_VALGRIND_NO_CD" == "" ]; then + cd "$TEST_PATH" + TEST="./$(basename "$TEST")" + fi +fi + +if [ "$NMTST_LAUNCH_DBUS" == "yes" ]; then # Spawn DBus eval `dbus-launch --sh-syntax` trap "kill $DBUS_SESSION_BUS_PID" EXIT - shift fi -TEST="$1" if [ "$NMTST_NO_VALGRIND" != "" ]; then - "$@" + "$TEST" "$@" exit $? fi @@ -21,7 +95,7 @@ LOGFILE="valgrind-`echo "$TEST" | tr -cd '[:alpha:]-'`.log" export G_SLICE=always-malloc export G_DEBUG=gc-friendly -$LIBTOOL --mode=execute "$VALGRIND" \ +"${NMTST_LIBTOOL[@]}" "$NMTST_VALGRIND" \ --quiet \ --error-exitcode=$VALGRIND_ERROR \ --leak-check=full \ @@ -29,6 +103,7 @@ $LIBTOOL --mode=execute "$VALGRIND" \ --suppressions="$SUPPRESSIONS" \ --num-callers=100 \ --log-file="$LOGFILE" \ + "$TEST" \ "$@" RESULT=$? |