Merge pull request #1739 from tokkee/sh/memcheck
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Sat, 4 Jun 2016 16:44:18 +0000 (18:44 +0200)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Sat, 4 Jun 2016 16:44:18 +0000 (18:44 +0200)
unit tests: Run all tests through Valgrind's memcheck, if available.

configure.ac
src/Makefile.am
src/ceph_test.c
src/daemon/Makefile.am
src/testing.h
src/utils_mount_test.c
src/utils_vl_lookup.c
testwrapper.sh [new file with mode: 0755]

index 99d665d..5ab5f0d 100644 (file)
@@ -57,6 +57,8 @@ AC_PROG_LIBTOOL
 AC_PROG_LEX
 AC_PROG_YACC
 
+AC_PATH_PROG([VALGRIND], [valgrind])
+
 # Warn when pkg.m4 is missing
 m4_pattern_forbid([^_?PKG_[A-Z_]+$],[*** pkg.m4 missing, please install pkg-config])
 
@@ -86,11 +88,9 @@ if test "x$PROTOC" != "x"; then
        fi
        AC_MSG_RESULT([$protoc3])
 fi
-AC_SUBST([PROTOC])
 AM_CONDITIONAL(HAVE_PROTOC3, test "x$have_protoc3" = "xyes")
 
 AC_PATH_PROG([GRPC_CPP_PLUGIN], [grpc_cpp_plugin])
-AC_SUBST([GRPC_CPP_PLUGIN])
 AM_CONDITIONAL(HAVE_GRPC_CPP, test "x$GRPC_CPP_PLUGIN" != "x")
 
 AC_CHECK_PROG([have_protoc_c], [protoc-c], [yes], [no])
index 6679296..67c2533 100644 (file)
@@ -17,6 +17,8 @@ endif
 AM_CPPFLAGS += -DPLUGINDIR='"${pkglibdir}"'
 AM_CPPFLAGS += -DPKGDATADIR='"${pkgdatadir}"'
 
+LOG_COMPILER = env VALGRIND="@VALGRIND@" $(abs_top_srcdir)/testwrapper.sh
+
 V_PROTOC = $(v_protoc_@AM_V@)
 v_protoc_ = $(v_protoc_@AM_DEFAULT_V@)
 v_protoc_0 = @echo "  PROTOC  " $@;
index 1141140..199d40e 100644 (file)
@@ -144,6 +144,7 @@ DEF_TEST(traverse_json)
   CHECK_ZERO (yajl_parse_complete (hndl));
 #endif
 
+  yajl_free (hndl);
   return 0;
 }
 
index d59e823..632872a 100644 (file)
@@ -87,6 +87,8 @@ else
 collectd_LDADD += -loconfig
 endif
 
+LOG_COMPILER = env VALGRIND="@VALGRIND@" $(abs_top_srcdir)/testwrapper.sh
+
 check_PROGRAMS = test_common test_meta_data test_utils_avltree test_utils_heap test_utils_time test_utils_subst
 TESTS          = test_common test_meta_data test_utils_avltree test_utils_heap test_utils_time test_utils_subst
 
index 2e4bf05..1bcc276 100644 (file)
@@ -56,12 +56,14 @@ static int check_count__ = 0;
 #define OK(cond) OK1(cond, #cond)
 
 #define EXPECT_EQ_STR(expect, actual) do { \
-  if (strcmp (expect, actual) != 0) { \
+  /* Evaluate 'actual' only once. */ \
+  const char *got__ = actual; \
+  if (strcmp (expect, got__) != 0) { \
     printf ("not ok %i - %s = \"%s\", want \"%s\"\n", \
-        ++check_count__, #actual, actual, expect); \
+        ++check_count__, #actual, got__, expect); \
     return (-1); \
   } \
-  printf ("ok %i - %s = \"%s\"\n", ++check_count__, #actual, actual); \
+  printf ("ok %i - %s = \"%s\"\n", ++check_count__, #actual, got__); \
 } while (0)
 
 #define EXPECT_EQ_INT(expect, actual) do { \
index d9d685a..dc6f79f 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "collectd.h"
+#include "common.h"
 #include "testing.h"
 #include "utils_mount.h"
 
@@ -80,16 +81,25 @@ DEF_TEST(cu_mount_getoptionvalue)
 {
   char line_opts[] = "foo=one,bar=two,qux=three";
   char line_bool[] = "one,two,three";
-
-  EXPECT_EQ_STR ("one", cu_mount_getoptionvalue (line_opts, "foo="));
-  EXPECT_EQ_STR ("two", cu_mount_getoptionvalue (line_opts, "bar="));
-  EXPECT_EQ_STR ("three", cu_mount_getoptionvalue (line_opts, "qux="));
-  OK (NULL == cu_mount_getoptionvalue (line_opts, "unknown="));
-
-  EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "one"));
-  EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "two"));
-  EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "three"));
-  OK (NULL == cu_mount_getoptionvalue (line_bool, "four"));
+  char *v;
+
+  EXPECT_EQ_STR ("one", v = cu_mount_getoptionvalue (line_opts, "foo="));
+  sfree (v);
+  EXPECT_EQ_STR ("two", v = cu_mount_getoptionvalue (line_opts, "bar="));
+  sfree (v);
+  EXPECT_EQ_STR ("three", v = cu_mount_getoptionvalue (line_opts, "qux="));
+  sfree (v);
+  OK (NULL == (v = cu_mount_getoptionvalue (line_opts, "unknown=")));
+  sfree (v);
+
+  EXPECT_EQ_STR ("", v = cu_mount_getoptionvalue (line_bool, "one"));
+  sfree (v);
+  EXPECT_EQ_STR ("", v = cu_mount_getoptionvalue (line_bool, "two"));
+  sfree (v);
+  EXPECT_EQ_STR ("", v = cu_mount_getoptionvalue (line_bool, "three"));
+  sfree (v);
+  OK (NULL == (v = cu_mount_getoptionvalue (line_bool, "four")));
+  sfree (v);
 
   return (0);
 }
index a023784..8a2e567 100644 (file)
@@ -495,6 +495,21 @@ static void lu_destroy_user_class_list (lookup_t *obj, /* {{{ */
       obj->cb_free_class (user_class_list->entry.user_class);
     user_class_list->entry.user_class = NULL;
 
+#define CLEAR_FIELD(field) do { \
+    if (user_class_list->entry.match.field.is_regex) { \
+      regfree (&user_class_list->entry.match.field.regex); \
+      user_class_list->entry.match.field.is_regex = 0; \
+    } \
+} while (0)
+
+    CLEAR_FIELD (host);
+    CLEAR_FIELD (plugin);
+    CLEAR_FIELD (plugin_instance);
+    CLEAR_FIELD (type);
+    CLEAR_FIELD (type_instance);
+
+#undef CLEAR_FIELD
+
     lu_destroy_user_obj (obj, user_class_list->entry.user_obj_list);
     user_class_list->entry.user_obj_list = NULL;
     pthread_mutex_destroy (&user_class_list->entry.lock);
diff --git a/testwrapper.sh b/testwrapper.sh
new file mode 100755 (executable)
index 0000000..bec33c0
--- /dev/null
@@ -0,0 +1,22 @@
+#! /bin/bash
+#
+# collectd -- testwrapper.sh
+#
+# A wrapper script for running tests. If valgrind is available, memory
+# checking will be enabled for all tests.
+
+set -e
+
+MEMCHECK=""
+
+if test -n "$VALGRIND"; then
+       MEMCHECK="$VALGRIND --quiet --tool=memcheck --error-exitcode=1"
+       MEMCHECK="$MEMCHECK --trace-children=yes"
+       MEMCHECK="$MEMCHECK --leak-check=full"
+       MEMCHECK="$MEMCHECK --gen-suppressions=all"
+fi
+
+exec $MEMCHECK "$@"
+
+# vim: set tw=78 sw=4 ts=4 noexpandtab :
+