]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Build a framework for testsuites
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 15:47:05 +0000 (16:47 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Nov 2017 15:49:17 +0000 (16:49 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
.gitignore
Makefile.am
tests/libpakfire/main.c [new file with mode: 0644]
tests/testsuite.c [new file with mode: 0644]
tests/testsuite.h [new file with mode: 0644]

index b73db560d8ed8176e526482ce400f50a6bf04c70..7db670d6237c03f2f8768c5b230b9593f8ae35f3 100644 (file)
@@ -9,6 +9,7 @@
 /src/scripts/quality-agent
 /src/scripts/extract-debuginfo
 /src/systemd/*.service
+/tests/libpakfire/main
 /tmp
 *.py[co]
 /*.tar.bz2
index f9f99711d46cd794b9bc73bc24688f9e00e32a6a..282e768612ea40d2f0bed0a34b50844cfa9ca117 100644 (file)
@@ -55,9 +55,10 @@ AM_CPPFLAGS = \
 AM_CFLAGS = $(OUR_CFLAGS)
 AM_LDFLAGS = $(OUR_LDFLAGS)
 
-PAKFIRE_CFLAGS = -I$(top_srcdir)/src/libpakfire/include
-PAKFIRE_LIBS   = libpakfire.la
+PAKFIRE_CPPFLAGS = -I$(top_srcdir)/src/libpakfire/include
+PAKFIRE_LIBS     = libpakfire.la
 
+check_PROGRAMS =
 lib_LTLIBRARIES =
 libexec_PROGRAMS =
 pkgpyexec_LTLIBRARIES =
@@ -210,10 +211,12 @@ _pakfire_la_SOURCES = \
        src/_pakfire/util.c \
        src/_pakfire/util.h
 
+_pakfire_la_CPPFLAGS = \
+       $(PAKFIRE_CPPFLAGS)
+
 _pakfire_la_CFLAGS = \
        $(AM_CFLAGS) \
        $(PYTHON_DEVEL_CFLAGS) \
-       $(PAKFIRE_CFLAGS) \
        $(CAP_CFLAGS) \
        $(SOLV_CFLAGS)
 
@@ -287,11 +290,11 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/util.h
 
 libpakfire_la_CFLAGS = \
-       $(AM_CFLAGS)
+       $(AM_CFLAGS) \
+       $(LIBGCRYPT_CFLAGS)
 
 libpakfire_la_CPPFLAGS = \
        $(AM_CPPFLAGS) \
-       $(LIBGCRYPT_CFLAGS) \
        -I$(top_srcdir)/src/libpakfire/include \
        -include $(top_builddir)/config.h \
        -DPAKFIRE_PRIVATE
@@ -314,6 +317,19 @@ libpakfire_la_DEPENDENCIES = \
 EXTRA_DIST += \
        src/libpakfire/libpakfire.sym
 
+check_PROGRAMS += \
+       tests/libpakfire/main
+
+dist_tests_libpakfire_main_SOURCES = \
+       tests/libpakfire/main.c
+
+tests_libpakfire_main_CPPFLAGS = \
+       $(TESTSUITE_CPPFLAGS)
+
+tests_libpakfire_main_LDADD = \
+       $(TESTSUITE_LDADD) \
+       $(PAKFIRE_LIBS)
+
 # ------------------------------------------------------------------------------
 
 lib_LTLIBRARIES += \
@@ -449,12 +465,27 @@ src/systemd/%: src/systemd/%.in Makefile
 
 # - testsuite ------------------------------------------------------------------
 
+check_LTLIBRARIES = \
+       tests/libtestsuite.la
+
+tests_libtestsuite_la_SOURCES = \
+       tests/testsuite.c \
+       tests/testsuite.h
+
+TESTSUITE_CPPFLAGS = \
+       $(AM_CPPFLAGS) \
+       $(PAKFIRE_CPPFLAGS)
+
+TESTSUITE_LDADD = \
+       tests/libtestsuite.la
+
 TESTS_ENVIRONMENT = \
        PYTHONPATH="$(top_srcdir)/.libs:$(top_srcdir)/src" \
        topdir="$(shell pwd)"
 
-dist_check_SCRIPTS = \
-       tests/module-load.py
+#dist_check_SCRIPTS = \
+#      tests/module-load.py
 
 TESTS = \
-       $(dist_check_SCRTIPS)
+       $(check_PROGRAMS) \
+       $(dist_check_SCRIPTS)
diff --git a/tests/libpakfire/main.c b/tests/libpakfire/main.c
new file mode 100644 (file)
index 0000000..8487bd4
--- /dev/null
@@ -0,0 +1,43 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2017 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pakfire/pakfire.h>
+
+#include "../testsuite.h"
+
+static int test_init(const test_t* t) {
+       Pakfire pakfire = pakfire_create("/", "x86_64");
+       if (!pakfire)
+               return EXIT_FAILURE;
+
+       LOG("Allocated at %p\n", pakfire);
+
+       pakfire_unref(pakfire);
+
+       return EXIT_SUCCESS;
+}
+
+int main(int argc, char** argv) {
+       testsuite_t* ts = testsuite_create(1);
+
+       testsuite_add_test(ts, "test_init", test_init);
+
+       return testsuite_run(ts);
+}
diff --git a/tests/testsuite.c b/tests/testsuite.c
new file mode 100644 (file)
index 0000000..902359c
--- /dev/null
@@ -0,0 +1,70 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2017 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include "testsuite.h"
+
+int test_run(const test_t* t) {
+       LOG("running %s\n", t->name);
+
+       int r = t->func(t);
+
+       return r;
+}
+
+testsuite_t* testsuite_create(size_t n) {
+       testsuite_t* ts = calloc(1, sizeof(*ts));
+       if (!ts)
+               exit(EXIT_FAILURE);
+
+       // Make space for n tests
+       ts->tests = calloc(n + 1, sizeof(*ts->tests));
+       ts->left = n;
+
+       return ts;
+};
+
+int testsuite_add_test(testsuite_t* ts, const char* name, test_function_t* func) {
+       if (ts->left == 0)
+               exit(EXIT_FAILURE);
+
+       test_t** last = ts->tests;
+       while (*last)
+               last++;
+
+       test_t* test = *last = calloc(1, sizeof(**last));
+       if (test) {
+               test->name = name;
+               test->func = func;
+       }
+
+       ts->left--;
+
+       return 0;
+}
+
+int testsuite_run(testsuite_t* ts) {
+       for (test_t** t = ts->tests; *t; t++) {
+               int r = test_run(*t);
+               if (r)
+                       exit(r);
+       }
+
+       return EXIT_SUCCESS;
+}
diff --git a/tests/testsuite.h b/tests/testsuite.h
new file mode 100644 (file)
index 0000000..0003a3d
--- /dev/null
@@ -0,0 +1,57 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2017 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdlib.h>
+#include <stdio.h>
+
+// Forward declaration
+struct test;
+
+typedef int (*test_function_t)(const struct test* t);
+
+typedef struct test {
+       const char* name;
+       test_function_t func;
+} test_t;
+
+typedef struct testsuite {
+       test_t** tests;
+       size_t left;
+} testsuite_t;
+
+testsuite_t* testsuite_create(size_t n);
+int testsuite_add_test(testsuite_t* ts, const char* name, test_function_t* func);
+int testsuite_run(testsuite_t* ts);
+
+int test_run(const test_t* t);
+
+#define _LOG(prefix, fmt, ...) printf("TESTS: " prefix fmt, ## __VA_ARGS__);
+#define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__);
+#define LOG_WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__);
+#define LOG_ERROR(fmt, ...) _LOG("ERROR: ", fmt, ## __VA_ARGS__);
+
+#define assert_return(expr, r) \
+       do { \
+               if ((!(expr))) { \
+                       ERR_("Failed assertion: " #expr " %s:%d %s\n", \
+                               __FILE__, __LINE__, __PRETTY_FUNCTION__); \
+                       return r; \
+               } \
+       } while (0)