Some POC of faultd test framework 86/134286/7
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Thu, 15 Jun 2017 22:25:10 +0000 (00:25 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 19 Jul 2017 07:28:49 +0000 (09:28 +0200)
Change-Id: Ib9b8f8bca49b0dc546287aec1dedecc9e149c4b2
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Makefile.am
configure.ac
packaging/faultd.spec
tests/unit/example.c [new file with mode: 0644]
tests/unit/fdtf.h [new file with mode: 0644]
tests/unit/runner.c [new file with mode: 0644]

index c4dd6fd206f407cc0de613d23c0ea9485e88fb81..d928a0dba18557ffe85a8032ee0aeaa64959b041 100644 (file)
@@ -112,3 +112,19 @@ nodist_unit_DATA += $(TEST_UNIT_FILES)
 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
index 318051fb3f54c2ad5d0d518a9b56dea3acf7ff5e..8de74b6097b465f56c16d73e9b227988be7f7edb 100644 (file)
@@ -18,6 +18,7 @@ AM_INIT_AUTOMAKE([
        no-dist-gzip
        dist-xz
        subdir-objects
+        serial-tests
 ])
 AC_PROG_CC_STDC
 AC_USE_SYSTEM_EXTENSIONS
@@ -38,9 +39,12 @@ AM_CONDITIONAL(BUILD_TEST_PROGRAMS, [test "x$enable_test_programs" = xyes])
 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,
index e050e6e74127b6b1b365bd2ac187425c8bd7e716..5f5abce1945b7d8ea30a02b35848bfff70ab77ab 100644 (file)
@@ -10,6 +10,8 @@ BuildRequires: pkgconfig(audit)
 BuildRequires: pkgconfig(libejdb)
 BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(json-c)
+BuildRequires: pkgconfig(cmocka)
+
 
 %description
 faultd monitors system services, detects their abnormal execution and
@@ -28,9 +30,11 @@ cp %{SOURCE1001} .
 
 %build
 %autogen
-%configure --enable-test-programs
+%configure --enable-test-programs --enable-tests
 make %{?_smp_mflags}
 
+make check
+
 %install
 %make_install
 
diff --git a/tests/unit/example.c b/tests/unit/example.c
new file mode 100644 (file)
index 0000000..a33438c
--- /dev/null
@@ -0,0 +1,22 @@
+#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)
+)
diff --git a/tests/unit/fdtf.h b/tests/unit/fdtf.h
new file mode 100644 (file)
index 0000000..7665c23
--- /dev/null
@@ -0,0 +1,60 @@
+#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 */
diff --git a/tests/unit/runner.c b/tests/unit/runner.c
new file mode 100644 (file)
index 0000000..f847957
--- /dev/null
@@ -0,0 +1,45 @@
+#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();
+}