endif
CLEANFILES += $(nodist_unit_DATA)
+
+if ENABLE_TESTS
+check_PROGRAMS = test
+
+test_LDFLAGS = -ldl
+
+test_LDADD = $(CMOCKA_LIBS)
+
+test_CFLAGS = -I${top_srcdir}/src/util
+
+test_SOURCES = \
+ tests/unit/runner.c \
+ tests/unit/example.c
+
+TESTS = $(check_PROGRAMS)
+endif
no-dist-gzip
dist-xz
subdir-objects
+ serial-tests
])
AC_PROG_CC_STDC
AC_USE_SYSTEM_EXTENSIONS
AC_ARG_ENABLE([tests],
AS_HELP_STRING([--enable-tests], [enable tests]),
[enable_test_programs=$enableval], [enable_tests=no])
-if test "x$enable_tests" = "xyes"; then
+
+AS_IF([test "x$enable_tests" = xyes], [
+ PKG_CHECK_MODULES([CMOCKA], [cmocka >= 1.0.0],
+ AC_DEFINE(HAS_CMOCKA, 1, [detected cmocka]))
AC_DEFINE(ENABLE_TESTS, 1, [Define if testing code is to be enabled])
-fi
+])
AM_CONDITIONAL(ENABLE_TESTS, [test "x$enable_tests" = xyes])
PKG_CHECK_MODULES(LIBSYSTEMD,
BuildRequires: pkgconfig(libejdb)
BuildRequires: pkgconfig(libsystemd)
BuildRequires: pkgconfig(json-c)
+BuildRequires: pkgconfig(cmocka)
+
%description
faultd monitors system services, detects their abnormal execution and
%build
%autogen
-%configure --enable-test-programs
+%configure --enable-test-programs --enable-tests
make %{?_smp_mflags}
+make check
+
%install
%make_install
--- /dev/null
+#include "fdtf.h"
+
+static void test1(void **state)
+{
+ return;
+}
+
+static void test2(void **state)
+{
+ return;
+}
+
+static void test3(void **state)
+{
+ return;
+}
+
+FAULTD_TEST_GROUP(
+ FAULTD_TEST_CASE(test1),
+ FAULTD_TEST_CASE(test2),
+ FAULTD_TEST_CASE(test3)
+)
--- /dev/null
+#ifndef FAULTD_UNIT_H
+#define FAULTD_UNIT_H
+
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <cmocka.h>
+
+#include "list.h"
+#include "common.h"
+#include "module.h"
+
+struct fdtf_test_group {
+ char *name;
+ CMFixtureFunction setup;
+ CMFixtureFunction teardown;
+ struct CMUnitTest *tests;
+ size_t number_of_tests;
+ struct GroupTest gt;
+ struct list_head node;
+};
+
+#ifndef __CONSTRUCTOR__
+#define __CONSTRUCTOR__ __attribute__((constructor))
+#endif
+
+void fdtf_register_test_group(struct fdtf_test_group *tg);
+
+#define FAULTD_TEST_GROUP(...) \
+ static struct CMUnitTest tests[] = { \
+ __VA_ARGS__ \
+ }; \
+ static struct fdtf_test_group test_group = { \
+ .name = FAULTD_MODNAME, \
+ .setup = NULL, \
+ .teardown = NULL, \
+ .tests = tests, \
+ .number_of_tests = ARRAY_SIZE(tests), \
+ .node = LIST_HEAD_INIT(test_group.node), \
+ }; \
+ static void __CONSTRUCTOR__ register_test_group(void) \
+ { \
+ fdtf_register_test_group(&test_group); \
+ }
+
+#define FAULTD_TEST_CASE_NST(NAME, FUNC, SETUP, TEARDOWN) \
+ { \
+ .name = NAME, \
+ .test_func = FUNC, \
+ .setup_func = SETUP, \
+ .teardown_func = TEARDOWN, \
+ }
+
+#define FAULTD_TEST_CASE_ST(func, setup, teardown) \
+ FAULTD_TEST_CASE_NST(#func, func, setup, teardown)
+
+#define FAULTD_TEST_CASE(func) \
+ FAULTD_TEST_CASE_ST(func, NULL, NULL)
+
+#endif /* FAULTD_UNIT_H */
--- /dev/null
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <cmocka.h>
+
+#include "fdtf.h"
+#include "module.h"
+
+static struct list_head test_groups = LIST_HEAD_INIT(test_groups);
+
+void fdtf_register_test_group(struct fdtf_test_group *tg)
+{
+ list_add_tail(&tg->node, &test_groups);
+}
+
+int fdtf_run_test_group(struct fdtf_test_group *tg)
+{
+ return _cmocka_run_group_tests(tg->name,
+ tg->tests,
+ tg->number_of_tests,
+ tg->setup,
+ tg->teardown);
+}
+
+int fdtf_run_all_test_groups()
+{
+ struct fdtf_test_group *tg;
+ int ret = 0;
+
+ list_for_each_entry(tg, &test_groups, node)
+ ret = fdtf_run_test_group(tg);
+
+ return ret;
+}
+
+int main(int argc, char **argv)
+{
+ /*
+ * TODO:
+ * Add here some more logic to allow to run
+ * only subset of tests
+ */
+ printf("dupa\n");
+ return fdtf_run_all_test_groups();
+}