unit-tests: Initialize tests with a callback

This commit is contained in:
Tobias Brunner
2013-11-06 10:31:07 +01:00
committed by Martin Willi
parent 8d2450d8b8
commit 334f44cd29
3 changed files with 58 additions and 56 deletions
+22 -44
View File
@@ -30,36 +30,11 @@
*/ */
#define TTY(color) tty_escape_get(2, TTY_FG_##color) #define TTY(color) tty_escape_get(2, TTY_FG_##color)
/**
* Load plugins from builddir
*/
static bool load_plugins(char *plugindirs[], char *plugins)
{
enumerator_t *enumerator;
char *name, path[PATH_MAX], dir[64];
int i;
enumerator = enumerator_create_token(plugins, " ", "");
while (enumerator->enumerate(enumerator, &name))
{
snprintf(dir, sizeof(dir), "%s", name);
translate(dir, "-", "_");
for (i = 0; plugindirs[i]; i++)
{
snprintf(path, sizeof(path), "%s/%s/.libs", plugindirs[i], dir);
lib->plugins->add_path(lib->plugins, path);
}
}
enumerator->destroy(enumerator);
return lib->plugins->load(lib->plugins, plugins);
}
/** /**
* Load all available test suites * Load all available test suites
*/ */
static array_t *load_suites(test_configuration_t configs[], static array_t *load_suites(test_configuration_t configs[],
char *plugindirs[], char *plugins) test_runner_init_t init)
{ {
array_t *suites; array_t *suites;
bool old = FALSE; bool old = FALSE;
@@ -69,7 +44,7 @@ static array_t *load_suites(test_configuration_t configs[],
test_setup_handler(); test_setup_handler();
if (!load_plugins(plugindirs, plugins)) if (init && !init(TRUE))
{ {
library_deinit(); library_deinit();
return NULL; return NULL;
@@ -97,6 +72,10 @@ static array_t *load_suites(test_configuration_t configs[],
lib->leak_detective->set_state(lib->leak_detective, old); lib->leak_detective->set_state(lib->leak_detective, old);
} }
if (init)
{
init(FALSE);
}
library_deinit(); library_deinit();
return suites; return suites;
@@ -172,7 +151,7 @@ static bool call_fixture(test_case_t *tcase, bool up)
/** /**
* Test initialization, initializes libstrongswan for the next run * Test initialization, initializes libstrongswan for the next run
*/ */
static bool pre_test(char *plugindirs[], char *plugins) static bool pre_test(test_runner_init_t init)
{ {
library_init(NULL); library_init(NULL);
@@ -188,12 +167,11 @@ static bool pre_test(char *plugindirs[], char *plugins)
lib->leak_detective->set_report_cb(lib->leak_detective, lib->leak_detective->set_report_cb(lib->leak_detective,
NULL, NULL, NULL); NULL, NULL, NULL);
} }
if (!load_plugins(plugindirs, plugins)) if (init && !init(TRUE))
{ {
library_deinit(); library_deinit();
return FALSE; return FALSE;
} }
dbg_default_set_level(LEVEL_SILENT); dbg_default_set_level(LEVEL_SILENT);
return TRUE; return TRUE;
} }
@@ -250,7 +228,8 @@ static void sum_leaks(report_data_t *data, int count, size_t bytes,
/** /**
* Do library cleanup and optionally check for memory leaks * Do library cleanup and optionally check for memory leaks
*/ */
static bool post_test(bool check_leaks, array_t *failures, char *name, int i) static bool post_test(test_runner_init_t init, bool check_leaks,
array_t *failures, char *name, int i)
{ {
report_data_t data = { report_data_t data = {
.failures = failures, .failures = failures,
@@ -258,10 +237,10 @@ static bool post_test(bool check_leaks, array_t *failures, char *name, int i)
.i = i, .i = i,
}; };
lib->processor->set_threads(lib->processor, 0); if (init)
lib->processor->cancel(lib->processor); {
lib->plugins->unload(lib->plugins); init(FALSE);
}
if (check_leaks && lib->leak_detective) if (check_leaks && lib->leak_detective)
{ {
lib->leak_detective->set_report_cb(lib->leak_detective, lib->leak_detective->set_report_cb(lib->leak_detective,
@@ -321,7 +300,7 @@ static void print_failures(array_t *failures)
/** /**
* Run a single test case with fixtures * Run a single test case with fixtures
*/ */
static bool run_case(test_case_t *tcase, char *plugindirs[], char *plugins) static bool run_case(test_case_t *tcase, test_runner_init_t init)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
test_function_t *tfun; test_function_t *tfun;
@@ -340,7 +319,7 @@ static bool run_case(test_case_t *tcase, char *plugindirs[], char *plugins)
for (i = tfun->start; i < tfun->end; i++) for (i = tfun->start; i < tfun->end; i++)
{ {
if (pre_test(plugindirs, plugins)) if (pre_test(init))
{ {
bool ok = FALSE, leaks = FALSE; bool ok = FALSE, leaks = FALSE;
@@ -361,7 +340,7 @@ static bool run_case(test_case_t *tcase, char *plugindirs[], char *plugins)
} }
} }
leaks = post_test(ok, failures, tfun->name, i); leaks = post_test(init, ok, failures, tfun->name, i);
test_setup_timeout(0); test_setup_timeout(0);
@@ -406,7 +385,7 @@ static bool run_case(test_case_t *tcase, char *plugindirs[], char *plugins)
/** /**
* Run a single test suite * Run a single test suite
*/ */
static bool run_suite(test_suite_t *suite, char *plugindirs[], char *plugins) static bool run_suite(test_suite_t *suite, test_runner_init_t init)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
test_case_t *tcase; test_case_t *tcase;
@@ -417,7 +396,7 @@ static bool run_suite(test_suite_t *suite, char *plugindirs[], char *plugins)
enumerator = array_create_enumerator(suite->tcases); enumerator = array_create_enumerator(suite->tcases);
while (enumerator->enumerate(enumerator, &tcase)) while (enumerator->enumerate(enumerator, &tcase))
{ {
if (run_case(tcase, plugindirs, plugins)) if (run_case(tcase, init))
{ {
passed++; passed++;
} }
@@ -438,8 +417,7 @@ static bool run_suite(test_suite_t *suite, char *plugindirs[], char *plugins)
/** /**
* See header. * See header.
*/ */
int test_runner_run(test_configuration_t configs[], int test_runner_run(test_configuration_t configs[], test_runner_init_t init)
char *plugindirs[], char *plugins)
{ {
array_t *suites; array_t *suites;
test_suite_t *suite; test_suite_t *suite;
@@ -449,7 +427,7 @@ int test_runner_run(test_configuration_t configs[],
/* redirect all output to stderr (to redirect make's stdout to /dev/null) */ /* redirect all output to stderr (to redirect make's stdout to /dev/null) */
dup2(2, 1); dup2(2, 1);
suites = load_suites(configs, plugindirs, plugins); suites = load_suites(configs, init);
if (!suites) if (!suites)
{ {
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -460,7 +438,7 @@ int test_runner_run(test_configuration_t configs[],
enumerator = array_create_enumerator(suites); enumerator = array_create_enumerator(suites);
while (enumerator->enumerate(enumerator, &suite)) while (enumerator->enumerate(enumerator, &suite))
{ {
if (run_suite(suite, plugindirs, plugins)) if (run_suite(suite, init))
{ {
passed++; passed++;
} }
+17 -7
View File
@@ -19,6 +19,19 @@
typedef struct test_configuration_t test_configuration_t; typedef struct test_configuration_t test_configuration_t;
/**
* Callback called before and after each test case to de-/initialize the
* environment (e.g. to load plugins). It is also called before and after the
* test suites are loaded.
*
* It is called after libstrongswan has been initialized and likewise before it
* gets deinitialized.
*
* @param init TRUE during initialization
* @return FALSE if de-/init failed
*/
typedef bool (*test_runner_init_t)(bool init);
/** /**
* Test configuration, suite constructor with plugin dependency * Test configuration, suite constructor with plugin dependency
*/ */
@@ -36,15 +49,12 @@ struct test_configuration_t {
}; };
/** /**
* Run test configuration, loading plugins from plugin base directory. * Run test configuration.
* *
* Both the configs and the plugindirs array must be terminated with a NULL * The configs array must be terminated with a NULL element.
* element.
* *
* @param configs test suite constructors with dependencies * @param configs test suite constructors with dependencies
* @param plugindirs base directories containing plugin directories to load * @param init_cb init/deinit callback
* @param plugins plugin names to load, space separated
* @return test result, EXIT_SUCCESS if all tests passed * @return test result, EXIT_SUCCESS if all tests passed
*/ */
int test_runner_run(test_configuration_t config[], int test_runner_run(test_configuration_t config[], test_runner_init_t init_cb);
char *plugindirs[], char *plugins);
+19 -5
View File
@@ -31,12 +31,26 @@ static test_configuration_t tests[] = {
{ .suite = NULL, } { .suite = NULL, }
}; };
static char *plugindirs[] = { static bool test_runner_init(bool init)
PLUGINDIR, {
NULL, if (init)
}; {
plugin_loader_add_plugindirs(PLUGINDIR, PLUGINS);
if (!lib->plugins->load(lib->plugins, PLUGINS))
{
return FALSE;
}
}
else
{
lib->processor->set_threads(lib->processor, 0);
lib->processor->cancel(lib->processor);
lib->plugins->unload(lib->plugins);
}
return TRUE;
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
return test_runner_run(tests, plugindirs, PLUGINS); return test_runner_run(tests, test_runner_init);
} }