diff options
-rw-r--r-- | configure.ac | 71 | ||||
-rw-r--r-- | doc/hacker-guide/en-US/markdown/unittest.md | 19 | ||||
-rw-r--r-- | glusterfs.spec.in | 17 | ||||
-rw-r--r-- | libglusterfs/src/Makefile.am | 6 | ||||
-rw-r--r-- | libglusterfs/src/mem-pool.c | 3 | ||||
-rw-r--r-- | libglusterfs/src/mem-pool.h | 4 | ||||
-rw-r--r-- | libglusterfs/src/unittest/global_mock.c | 2 | ||||
-rw-r--r-- | libglusterfs/src/unittest/log_mock.c | 2 | ||||
-rw-r--r-- | libglusterfs/src/unittest/mem_pool_unittest.c | 45 | ||||
-rw-r--r-- | libglusterfs/src/unittest/unittest.h | 45 | ||||
-rw-r--r-- | xlators/cluster/dht/src/Makefile.am | 3 | ||||
-rw-r--r-- | xlators/cluster/dht/src/dht-layout.c | 13 | ||||
-rw-r--r-- | xlators/cluster/dht/src/unittest/dht_layout_unittest.c | 10 |
13 files changed, 152 insertions, 88 deletions
diff --git a/configure.ac b/configure.ac index 2d93ce847b6..2c3b0e31c1c 100644 --- a/configure.ac +++ b/configure.ac @@ -344,21 +344,6 @@ AC_CHECK_LIB([crypto], [MD5], , AC_MSG_ERROR([OpenSSL crypto library is required AC_CHECK_LIB([pthread], [pthread_mutex_init], , AC_MSG_ERROR([Posix threads library is required to build glusterfs])) - -PKG_CHECK_MODULES([UNITTEST], [cmockery2], [ - UNITTEST_CPPFLAGS=`${PKG_CONFIG} --print-errors --cflags-only-I "cmockery2"` - UNITTEST_CFLAGS=`${PKG_CONFIG} --print-errors --cflags-only-other "cmockery2"` - UNITTEST_LDFLAGS=`${PKG_CONFIG} --print-errors --libs-only-L "cmockery2"` - UNITTEST_LIBS=`${PKG_CONFIG} --print-errors --libs-only-l "cmockery2"` -],[ - AC_CHECK_LIB([cmockery], [mock_assert], [ - UNITTEST_LDFLAGS="-lcmockery -lgcov" - UNITTEST_CFLAGS="-Wall -Werror -DUNIT_TESTING=1" - ], [ - AC_MSG_ERROR([cmockery2 library is required to build glusterfs]) - ]) -]) - AC_CHECK_FUNC([dlopen], [has_dlopen=yes], AC_CHECK_LIB([dl], [dlopen], , AC_MSG_ERROR([Dynamic linking library required to build glusterfs]))) AC_CHECK_LIB([readline], [rl_do_undo], [RL_UNDO="yes"], [RL_UNDO="no"]) @@ -740,22 +725,6 @@ dnl check for Monotonic clock AC_CHECK_LIB([rt], [clock_gettime], , AC_MSG_WARN([System doesn't have monotonic clock using contrib])) -dnl Add cmockery2 for unit tests -case $host_os in - freebsd*) - dnl remove --coverage on FreeBSD due to a known llvm packaging bug - UNITTEST_CFLAGS="${UNITTEST_CPPFLAGS} ${UNITTEST_CFLAGS} -g -DDEBUG -O0" - UNITTEST_LDFLAGS="${UNITTEST_LIBS} ${UNITTEST_LDFLAGS}" - ;; - *) - UNITTEST_CFLAGS="${UNITTEST_CPPFLAGS} ${UNITTEST_CFLAGS} -g -DDEBUG -O0 --coverage" - UNITTEST_LDFLAGS="${UNITTEST_LIBS} ${UNITTEST_LDFLAGS}" - ;; -esac - -AC_SUBST(UNITTEST_CFLAGS) -AC_SUBST(UNITTEST_LDFLAGS) - dnl Check for argp AC_CHECK_HEADER([argp.h], AC_DEFINE(HAVE_ARGP, 1, [have argp])) @@ -1105,12 +1074,45 @@ PKG_CHECK_MODULES([URCU_CDS], [liburcu-cds >= 0.8], [], [AC_DEFINE(URCU_0_7, 1, [Define if liburcu 0.7 is found])], [AC_MSG_ERROR([liburcu >= 0.7 required])])]) +BUILD_UNITTEST="no" +AC_ARG_ENABLE([cmocka], + AC_HELP_STRING([--enable-cmocka], + [Enable cmocka build options.])) +if test "x$enable_cmocka" = "xyes"; then + BUILD_UNITTEST="yes" + PKG_CHECK_MODULES([UNITTEST], [cmocka], [],[ + AC_CHECK_LIB([cmocka], [mock_assert], [ + UNITTEST_LDFLAGS="-lcmocka -lgcov" + UNITTEST_CFLAGS="-Wall -Werror" + ], [ + AC_MSG_ERROR([cmocka library is required to build glusterfs]) + ]) + ]) +fi +AM_CONDITIONAL([UNITTEST], [test x$BUILD_UNITTEST = xyes]) + +dnl Define UNIT_TESTING only for building cmocka binaries. +UNITTEST_CFLAGS="${UNITTEST_CFLAGS} -DUNIT_TESTING=1" + +dnl Add cmocka for unit tests +case $host_os in + freebsd*) + dnl remove --coverage on FreeBSD due to a known llvm packaging bug + UNITTEST_CFLAGS="${UNITTEST_CPPFLAGS} ${UNITTEST_CFLAGS} -g -DDEBUG -O0" + UNITTEST_LDFLAGS="${UNITTEST_LIBS} ${UNITTEST_LDFLAGS}" + ;; + *) + UNITTEST_CFLAGS="${UNITTEST_CPPFLAGS} ${UNITTEST_CFLAGS} -g -DDEBUG -O0 --coverage" + UNITTEST_LDFLAGS="${UNITTEST_LIBS} ${UNITTEST_LDFLAGS}" + ;; +esac + +AC_SUBST(UNITTEST_CFLAGS) +AC_SUBST(UNITTEST_LDFLAGS) + AC_SUBST(CFLAGS) # end enable debug section -dnl Required if cmockery2 headers are not in standar paths -GF_CFLAGS="${GF_CFLAGS} ${UNITTEST_CPPFLAGS}" - AC_SUBST(GF_HOST_OS) AC_SUBST(GF_CFLAGS) AC_SUBST(GF_LDFLAGS) @@ -1189,4 +1191,5 @@ echo "Use syslog : $USE_SYSLOG" echo "XML output : $BUILD_XML_OUTPUT" echo "QEMU Block formats : $BUILD_QEMU_BLOCK" echo "Encryption xlator : $BUILD_CRYPT_XLATOR" +echo "Unit Tests : $BUILD_UNITTEST" echo diff --git a/doc/hacker-guide/en-US/markdown/unittest.md b/doc/hacker-guide/en-US/markdown/unittest.md index 42dc210959e..5c6c0a8a039 100644 --- a/doc/hacker-guide/en-US/markdown/unittest.md +++ b/doc/hacker-guide/en-US/markdown/unittest.md @@ -14,10 +14,13 @@ * Maintainable * Trustworthy (when you see its result, you don’t need to debug the code just to be sure) -## Cmockery2 -GlusterFS unit test framework is based on [Cmockery2][]. Cmockery provides developers with methods to isolate and test modules written in C language. It also provides integration with Jenkins by providing JUnit XML compliant unit test results. +## cmocka +GlusterFS unit test framework is based on [cmocka][]. cmocka provides +developers with methods to isolate and test modules written in C language. It +also provides integration with Jenkins by providing JUnit XML compliant unit +test results. -Before continuing, you may want to familiarize yourself with Cmockery2 by reading the [usage guide][cmockery2usage]. +cmocka ## Running Unit Tests To execute the unit tests, all you need is to type `make check`. Here is a step-by-step example assuming you just cloned a GlusterFS tree: @@ -90,7 +93,7 @@ Expected assertion ((void *)0) != ptr occurred Add the following to your C file: ```c -#include <cmockery/pbc.h> +#include <cmocka_pbc.h> ``` ```c @@ -171,7 +174,7 @@ int divide (int n, int d) Cmockery2 provides its own memory allocation functions which check for buffer overrun and memory leaks. The following header file must be included **last** to be able to override any of the memory allocation functions: ```c -#include <cmockery/cmockery_override.h> +#include <cmocka.h> ``` This file will only take effect with the `UNIT_TESTING` CFLAG is set. @@ -204,7 +207,7 @@ TESTS += xxx_unittest Where `xxx` is the name of your C file. For example, look at `libglusterfs/src/Makefile.am`. -Copy the simple unit test from `cmockery2/src/example/run_tests.c` to `unittest/xxx_unittest.c`. If you would like to see an example of a unit test, please refer to `libglusterfs/src/unittest/mem_pool_unittest.c`. +Copy the simple unit test from the [cmocka API][cmockaapi] to `unittest/xxx_unittest.c`. If you would like to see an example of a unit test, please refer to `libglusterfs/src/unittest/mem_pool_unittest.c`. #### Mocking You may see that the linker will complain about missing functions needed by the C file you would like to test. Identify the required functions, then place their stubs in a file called `unittest/xxx_mock.c`, then include this file in `Makefile.am` in `xxx_unittest_SOURCES`. This will allow you to you Cmockery2's mocking functions. @@ -220,6 +223,6 @@ $ gdb libglusterfs/src/mem_pool_unittest ``` -[Cmockery2]: https://github.com/lpabon/cmockery2 +[cmocka]: https://cmocka.org [definitionofunittest]: http://artofunittesting.com/definition-of-a-unit-test/ -[cmockery2usage]: https://github.com/lpabon/cmockery2/blob/master/doc/usage.md +[cmockapi]: https://api.cmocka.org diff --git a/glusterfs.spec.in b/glusterfs.spec.in index 85695092755..dec67e2a637 100644 --- a/glusterfs.spec.in +++ b/glusterfs.spec.in @@ -9,6 +9,10 @@ ## All argument definitions should be placed here and keep them sorted ## +# if you wish to compile an rpm with cmocka unit testing... +# rpmbuild -ta @PACKAGE_NAME@-@PACKAGE_VERSION@.tar.gz --with cmocka +%{?_with_cmocka:%global _with_cmocka --enable-cmocka} + # if you wish to compile an rpm without rdma support, compile like this... # rpmbuild -ta @PACKAGE_NAME@-@PACKAGE_VERSION@.tar.gz --without rdma %{?_without_rdma:%global _without_rdma --disable-ibverbs} @@ -196,8 +200,10 @@ BuildRequires: libxml2-devel openssl-devel BuildRequires: libaio-devel BuildRequires: python-devel BuildRequires: python-ctypes -BuildRequires: cmockery2-devel BuildRequires: userspace-rcu-devel >= 0.7 +%if ( 0%{?_with_cmocka:0} ) +BuildRequires: libcmocka-devel >= 1.0.0 +%endif %if ( 0%{!?_without_systemtap:1} ) BuildRequires: systemtap-sdt-devel %endif @@ -501,7 +507,8 @@ This package provides the glusterfs server daemon. %{?_without_syslog} \ %{?_without_bd} \ %{?_without_qemu_block} \ - %{?_without_systemtap} + %{?_without_systemtap} \ + %{?_with_cmocka} # fix hardening and remove rpath in shlibs %if ( 0%{?fedora} && 0%{?fedora} > 17 ) || ( 0%{?rhel} && 0%{?rhel} > 6 ) @@ -1039,6 +1046,12 @@ fi %ghost %attr(0600,-,-) %{_sharedstatedir}/glusterd/nfs/run/nfs.pid %changelog +* Thu Feb 26 2015 Kaleb S. KEITHLEY <kkeithle@redhat.com> +- enable cmocka unittest support only when asked for (#1067059) + +* Wed Feb 18 2015 Andreas Schneider <asn@redhat.com> +- Change cmockery2 to cmocka. + * Wed Feb 18 2015 Kaushal M <kaushal@redhat.com> - add userspace-rcu as a requirement diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 181babe85b0..3d1de02e894 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -47,7 +47,8 @@ noinst_HEADERS = common-utils.h defaults.h dict.h glusterfs.h hashfn.h timespec. gidcache.h client_t.h glusterfs-acl.h glfs-message-id.h \ template-component-messages.h strfd.h syncop-utils.h parse-utils.h \ $(CONTRIBDIR)/mount/mntent_compat.h lvm-defaults.h \ - $(CONTRIBDIR)/libexecinfo/execinfo_compat.h + $(CONTRIBDIR)/libexecinfo/execinfo_compat.h \ + unittest/unittest.h EXTRA_DIST = graph.l graph.y @@ -61,7 +62,7 @@ y.tab.h: graph.y CLEANFILES = graph.lex.c y.tab.c y.tab.h CONFIG_CLEAN_FILES = $(CONTRIB_BUILDDIR)/uuid/uuid_types.h -#### UNIT TESTS ##### +if UNITTEST CLEANFILES += *.gcda *.gcno *_xunit.xml noinst_PROGRAMS = TESTS = @@ -76,3 +77,4 @@ mem_pool_unittest_CFLAGS = $(UNITTEST_CFLAGS) mem_pool_unittest_LDFLAGS = $(UNITTEST_LDFLAGS) noinst_PROGRAMS += mem_pool_unittest TESTS += mem_pool_unittest +endif diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c index 019be95e37e..3bba30fc72e 100644 --- a/libglusterfs/src/mem-pool.c +++ b/libglusterfs/src/mem-pool.c @@ -24,8 +24,7 @@ #define GLUSTERFS_ENV_MEM_ACCT_STR "GLUSTERFS_DISABLE_MEM_ACCT" -#include <cmockery/pbc.h> -#include <cmockery/cmockery_override.h> +#include "unittest/unittest.h" void gf_mem_acct_enable_set (void *data) diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h index a6adb6cc8c2..88ec9705604 100644 --- a/libglusterfs/src/mem-pool.h +++ b/libglusterfs/src/mem-pool.h @@ -26,7 +26,9 @@ * unit test versions */ #ifdef UNIT_TESTING -#include <cmockery/cmockery_override.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> #endif #define GF_MEM_HEADER_SIZE (4 + sizeof (size_t) + sizeof (xlator_t *) + 4 + 8) diff --git a/libglusterfs/src/unittest/global_mock.c b/libglusterfs/src/unittest/global_mock.c index b50638d1023..afdadc4e868 100644 --- a/libglusterfs/src/unittest/global_mock.c +++ b/libglusterfs/src/unittest/global_mock.c @@ -16,7 +16,7 @@ #include <setjmp.h> #include <inttypes.h> -#include <cmockery/cmockery.h> +#include <cmocka.h> xlator_t **__glusterfs_this_location () { diff --git a/libglusterfs/src/unittest/log_mock.c b/libglusterfs/src/unittest/log_mock.c index fec48bafc19..b35e03b3618 100644 --- a/libglusterfs/src/unittest/log_mock.c +++ b/libglusterfs/src/unittest/log_mock.c @@ -16,7 +16,7 @@ #include <setjmp.h> #include <inttypes.h> -#include <cmockery/cmockery.h> +#include <cmocka.h> int _gf_log (const char *domain, const char *file, const char *function, int32_t line, gf_loglevel_t level, diff --git a/libglusterfs/src/unittest/mem_pool_unittest.c b/libglusterfs/src/unittest/mem_pool_unittest.c index 0d7aa199df0..906ebb0a7f3 100644 --- a/libglusterfs/src/unittest/mem_pool_unittest.c +++ b/libglusterfs/src/unittest/mem_pool_unittest.c @@ -17,8 +17,15 @@ #include <setjmp.h> #include <inttypes.h> #include <string.h> -#include <cmockery/pbc.h> -#include <cmockery/cmockery.h> +#include <cmocka_pbc.h> +#include <cmocka.h> + +#ifndef assert_ptr_equal +#define assert_ptr_equal(a, b) \ + _assert_int_equal(cast_ptr_to_largest_integral_type(a), \ + cast_ptr_to_largest_integral_type(b), \ + __FILE__, __LINE__) +#endif /* * memory header for gf_mem_set_acct_info @@ -252,7 +259,7 @@ test_gf_calloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_calloc size = 1024; @@ -319,7 +326,7 @@ test_gf_malloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc size = 1024; @@ -352,7 +359,7 @@ test_gf_realloc_default_realloc(void **state) // Initialize xl xl = helper_xlator_init(10); assert_int_equal(xl->ctx->mem_acct_enable, 0); - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc then realloc size = 10; @@ -391,7 +398,7 @@ test_gf_realloc_mem_acct_enabled(void **state) xl->ctx->mem_acct_enable = 1; // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Call __gf_malloc then realloc size = 1024; @@ -436,7 +443,7 @@ test_gf_realloc_ptr(void **state) assert_int_equal(xl->ctx->mem_acct_enable, 0); // For line mem-pool.c:115 and mem-pool:118 - will_always_return(__glusterfs_this_location, &xl); + will_return_always(__glusterfs_this_location, &xl); // Tests according to the manpage for realloc @@ -458,18 +465,18 @@ test_gf_realloc_ptr(void **state) } int main(void) { - const UnitTest tests[] = { - unit_test(test_gf_mem_acct_enable_set), - unit_test(test_gf_mem_set_acct_info_asserts), - unit_test(test_gf_mem_set_acct_info_memory), - unit_test(test_gf_calloc_default_calloc), - unit_test(test_gf_calloc_mem_acct_enabled), - unit_test(test_gf_malloc_default_malloc), - unit_test(test_gf_malloc_mem_acct_enabled), - unit_test(test_gf_realloc_default_realloc), - unit_test(test_gf_realloc_mem_acct_enabled), - unit_test(test_gf_realloc_ptr), + const struct CMUnitTest libglusterfs_mem_pool_tests[] = { + cmocka_unit_test(test_gf_mem_acct_enable_set), + cmocka_unit_test(test_gf_mem_set_acct_info_asserts), + cmocka_unit_test(test_gf_mem_set_acct_info_memory), + cmocka_unit_test(test_gf_calloc_default_calloc), + cmocka_unit_test(test_gf_calloc_mem_acct_enabled), + cmocka_unit_test(test_gf_malloc_default_malloc), + cmocka_unit_test(test_gf_malloc_mem_acct_enabled), + cmocka_unit_test(test_gf_realloc_default_realloc), + cmocka_unit_test(test_gf_realloc_mem_acct_enabled), + cmocka_unit_test(test_gf_realloc_ptr), }; - return run_tests(tests, "libglusterfs_mem_pool"); + return cmocka_run_group_tests(libglusterfs_mem_pool_tests, NULL, NULL); } diff --git a/libglusterfs/src/unittest/unittest.h b/libglusterfs/src/unittest/unittest.h new file mode 100644 index 00000000000..6320217db0d --- /dev/null +++ b/libglusterfs/src/unittest/unittest.h @@ -0,0 +1,45 @@ +/* + Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _GF_UNITTEST_H_ +#define _GF_UNITTEST_H_ + +#ifdef UNIT_TESTING +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka_pbc.h> +#include <cmocka.h> + +extern void mock_assert(const int result, const char* const expression, + const char * const file, const int line); + +// Change GF_CALLOC and GF_FREE to use +// cmocka memory allocation versions +#ifdef UNIT_TESTING +#undef GF_CALLOC +#define GF_CALLOC(n, s, t) test_calloc(n, s) +#undef GF_FREE +#define GF_FREE test_free + +/* Catch intended assert()'s while unit-testing */ +extern void mock_assert(const int result, const char* const expression, + const char * const file, const int line); + +#undef assert +#define assert(expression) \ + mock_assert((int)(expression), #expression, __FILE__, __LINE__); +#endif +#else +#define REQUIRE(p) /**/ +#define ENSURE(p) /**/ +#endif + +#endif /* _GF_UNITTEST */ diff --git a/xlators/cluster/dht/src/Makefile.am b/xlators/cluster/dht/src/Makefile.am index ab58affe2b4..8d02749f4d9 100644 --- a/xlators/cluster/dht/src/Makefile.am +++ b/xlators/cluster/dht/src/Makefile.am @@ -36,7 +36,7 @@ uninstall-local: install-data-hook: ln -sf dht.so $(DESTDIR)$(xlatordir)/distribute.so -#### UNIT TESTS ##### +if UNITTEST CLEANFILES += *.gcda *.gcno *_xunit.xml noinst_PROGRAMS = TESTS = @@ -49,3 +49,4 @@ dht_layout_unittest_CFLAGS = $(AM_CFLAGS) $(UNITTEST_CFLAGS) dht_layout_unittest_LDFLAGS = $(UNITTEST_LDFLAGS) noinst_PROGRAMS += dht_layout_unittest TESTS += dht_layout_unittest +endif diff --git a/xlators/cluster/dht/src/dht-layout.c b/xlators/cluster/dht/src/dht-layout.c index fa29b6bfa28..757ec731d26 100644 --- a/xlators/cluster/dht/src/dht-layout.c +++ b/xlators/cluster/dht/src/dht-layout.c @@ -19,6 +19,7 @@ #include "dht-common.h" #include "byte-order.h" #include "dht-messages.h" +#include "unittest/unittest.h" #define layout_base_size (sizeof (dht_layout_t)) @@ -27,18 +28,6 @@ #define layout_size(cnt) (layout_base_size + (cnt * layout_entry_size)) -#include <cmockery/pbc.h> -#include <cmockery/cmockery_override.h> - -// Change GF_CALLOC and GF_FREE to use -// cmockery2 memory allocation versions -#ifdef UNIT_TESTING -#undef GF_CALLOC -#define GF_CALLOC(n, s, t) test_calloc(n, s) -#undef GF_FREE -#define GF_FREE test_free -#endif - dht_layout_t * dht_layout_new (xlator_t *this, int cnt) { diff --git a/xlators/cluster/dht/src/unittest/dht_layout_unittest.c b/xlators/cluster/dht/src/unittest/dht_layout_unittest.c index b5233d235d0..3702af366f9 100644 --- a/xlators/cluster/dht/src/unittest/dht_layout_unittest.c +++ b/xlators/cluster/dht/src/unittest/dht_layout_unittest.c @@ -12,12 +12,12 @@ #include "logging.h" #include "xlator.h" +#include <inttypes.h> #include <stdarg.h> #include <stddef.h> #include <setjmp.h> -#include <inttypes.h> -#include <cmockery/pbc.h> -#include <cmockery/cmockery.h> +#include <cmocka_pbc.h> +#include <cmocka.h> /* * Helper functions @@ -116,9 +116,9 @@ test_dht_layout_new(void **state) } int main(void) { - const UnitTest tests[] = { + const struct CMUnitTest xlator_dht_layout_tests[] = { unit_test(test_dht_layout_new), }; - return run_tests(tests, "xlator_dht_layout"); + return cmocka_run_group_tests(xlator_dht_layout_tests, NULL, NULL); } |