From: Michael Tremer Date: Tue, 28 Nov 2017 15:47:05 +0000 (+0100) Subject: libpakfire: Build a framework for testsuites X-Git-Tag: 0.9.28~1285^2~1275 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7b463f341822df2eb23eb405aea0b089b3088706;p=pakfire.git libpakfire: Build a framework for testsuites Signed-off-by: Michael Tremer --- diff --git a/.gitignore b/.gitignore index b73db560d..7db670d62 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /src/scripts/quality-agent /src/scripts/extract-debuginfo /src/systemd/*.service +/tests/libpakfire/main /tmp *.py[co] /*.tar.bz2 diff --git a/Makefile.am b/Makefile.am index f9f99711d..282e76861 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..8487bd407 --- /dev/null +++ b/tests/libpakfire/main.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 000000000..902359ca4 --- /dev/null +++ b/tests/testsuite.c @@ -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 . # +# # +#############################################################################*/ + +#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 index 000000000..0003a3d15 --- /dev/null +++ b/tests/testsuite.h @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +// 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)