Give the unit tests a big overhaul and move all the unit tests to /tests/<libname>/
The unit tests contain a lot of duplicated code and here's an attempt to reduce code duplication.
This commit does several things:
-
Remove #ifdef HAVE_CMOCKA - we already solve this with automake conditionals.
-
Create a set of ISC_TEST_* and ISC_*TEST macros to wrap the test implementations, test lists, and the main test routine, so we don't have to repeat this all over again. The macros were modeled after libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown functions, and per-test setup and teardown functions.
-
The macros prefix the test functions and cmocka entries, so the name of the test can now match the tested function name, and we don't have to append
_test
becauserun_test_
is automatically prepended to the main test function, andsetup_test_
andteardown_test_
is prepended to setup and teardown function. -
Update all the unit tests to use the new syntax and fix a few bits here and there.
-
In the future, we can separate the test declarations and test implementations which are going to greatly help with uncluttering the bigger unit tests like doh_test and netmgr_test, because the test implementations are not declared static (see
ISC_RUN_TEST_DECLARE
andISC_RUN_TEST_IMPL
for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly outweighs all the negatives of using the macros. There's less duplicated code, the tests are more uniform and the implementation can be more flexible.
--
The unit tests are now using a common base, which means that lib/dns/tests/ code now has to include lib/isc/include/isc/test.h and link with lib/isc/test.c and lib/ns/tests has to include both libisc and libdns parts.
Instead of cross-linking code between the directories, move the /lib//test.c to /tests/.c and /lib//include/test.h to /tests/include/tests/.h and create a single libtest.la convenience library in /tests/.
At the same time, move the /lib//tests/ to /tests// (but keep it symlinked to the old location) and adjust paths accordingly. In few places, we are now using absolute paths instead of relative paths, because the directory level has changed. By moving the directories under the /tests/ directory, the test-related code is kept in a single place and we can avoid referencing files between libns->libdns->libisc which is unhealthy because they live in a separate Makefile-space.
In the future, the /bin/tests/ should be merged to /tests/ and symlink kept, and the /fuzz/ directory moved to /tests/fuzz/.
--
This was conceived in the loopmgr branch where we need couple of extra macros when the body of the unit test needs to run on the loop and there was a lot of repeated code related to that