Skip to content

Give the unit tests a big overhaul and move all the unit tests to /tests/<libname>/

Ondřej Surý requested to merge ondrej-unit-tests-refactoring into main

The unit tests contain a lot of duplicated code and here's an attempt to reduce code duplication.

This commit does several things:

  1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake conditionals.

  2. 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_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.

  1. 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 because run_test_ is automatically prepended to the main test function, and setup_test_ and teardown_test_ is prepended to setup and teardown function.

  2. Update all the unit tests to use the new syntax and fix a few bits here and there.

  3. 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 and ISC_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 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

Edited by Ondřej Surý

Merge request reports