Skip to contentSkip to conversation

add infrastructure for unit tests

PATCH In review Thomas Haller 2 participants 10 messages Nov 5, 2023 – Dec 6, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover add infrastructure for unit tests

Changes to v1:

- rename some make targets
- add make check-unit to alias make check-TESTS. These targets run the tests hooked up as "TESTS=" (which for now are the tests in tests/unit).
- improve commit messages and various minor code changes.

Thomas Haller (5):
build: add basic "check-{local,more,all}" and "build-all" make targets
build: add make check-build to run ./tests/build/run-tests.sh
build: add make check-tree to check consistency of source tree
build: cleanup if-blocks for conditional compilation in "Makefile.am" tests/unit: add unit tests for libnftables

 .gitignore                           |  15 +-
 Makefile.am                          | 134 ++++++++++++---
 src/.gitignore                       |   5 -
 tests/unit/nft-test.h                |  14 ++
 tests/unit/test-libnftables-static.c |  16 ++
 tests/unit/test-libnftables.c        |  21 +++
 tools/test-runner.sh                 | 235 +++++++++++++++++++++++++++
 7 files changed, 412 insertions(+), 28 deletions(-)
 create mode 100644 tests/unit/nft-test.h
 create mode 100644 tests/unit/test-libnftables-static.c
 create mode 100644 tests/unit/test-libnftables.c
 create mode 100755 tools/test-runner.sh
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/5 build: add basic "check-{local,more,all}" and "build-all" make targets

Add targets "check-local" and "check-more", which later will hook up additional tests. For now, they are empty targets.

- with autotools, make distcheck implies make check.
- with autotools, make check implies make check-local and make check-TESTS.

Most tests should of course hook via check-local or via TESTS= (check-TESTS). There is a small place for additional tests, in particular "tests/build/run-tests.sh", which itself runs make distcheck. So make check-more contains additional tests not run by make check.

And make check-all just means make check check-more.

---
 Makefile.am | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
Makefile.am +23 -0
index 0ed831a19e95..93bd47970077 100644
@@ -31,8 +31,11 @@ lib_LTLIBRARIES =
 noinst_LTLIBRARIES =
 sbin_PROGRAMS =
 check_PROGRAMS =
+check_LTLIBRARIES =
 dist_man_MANS =
 CLEANFILES =
+check_local =
+check_more =
 
 ###############################################################################
 
@@ -409,3 +412,23 @@ EXTRA_DIST += \
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libnftables.pc
+
+###############################################################################
+
+build-all: all $(check_PROGRAMS) $(check_LTLIBRARIES)
+
+.PHONY: build-all
+
+###############################################################################
+
+check-local: build-all $(check_local)
+
+.PHONY: check-local $(check_local)
+
+check-more: build-all $(check_more)
+
+.PHONY: check-more $(check_more)
+
+check-all: check check-more
+
+.PHONY: check-all
Signature · 1 line
2.41.0
#2 lore raw

PATCH v2 2/5 build: add `make check-build` to run `./tests/build/run-tests.sh`

In reply to Thomas Haller

./tests/build/run-tests.sh performs a build check. Integrate it in make as make check-build target.

make check-build is hooked into make check-more, which in turn is hooked as make check-all.

Note that this is intentionally not part of make check, because make check is run by make distcheck, and the build test itself calls make distcheck. Even if that cycle would be resolved, doing a build check several times during make check seems wrong and would add another minute to the test runtime.

---
 Makefile.am | 8 ++++++++
 1 file changed, 8 insertions(+)
Makefile.am +8 -0
index 93bd47970077..6a0b04641afc 100644
@@ -421,6 +421,14 @@ build-all: all $(check_PROGRAMS) $(check_LTLIBRARIES)
 
 ###############################################################################
 
+check-build: build-all
+	cd "$(srcdir)/tests/build/" ; \
+	CC="$(CC)" CFLAGS='-Werror' ./run-tests.sh
+
+check_more += check-build
+
+###############################################################################
+
 check-local: build-all $(check_local)
 
 .PHONY: check-local $(check_local)
Signature · 1 line
2.41.0
#3 lore raw

PATCH v2 3/5 build: add `make check-tree` to check consistency of source tree

In reply to Thomas Haller

The script "tools/check-tree.sh" performs some consistency checks of the source tree. Call it from a make target "check-tree".

Note that make check-tree is hooked into make check-local, which in turn is hooked into make check and make distcheck.

---
 Makefile.am | 8 ++++++++
 1 file changed, 8 insertions(+)
Makefile.am +8 -0
index 6a0b04641afc..48d138636d2f 100644
@@ -408,6 +408,7 @@ EXTRA_DIST += \
 EXTRA_DIST += \
 	files \
 	tests \
+	tools \
 	$(NULL)
 
 pkgconfigdir = $(libdir)/pkgconfig
@@ -421,6 +422,13 @@ build-all: all $(check_PROGRAMS) $(check_LTLIBRARIES)
 
 ###############################################################################
 
+check-tree:
+	"$(srcdir)/tools/check-tree.sh"
+
+check_local += check-tree
+
+###############################################################################
+
 check-build: build-all
 	cd "$(srcdir)/tests/build/" ; \
 	CC="$(CC)" CFLAGS='-Werror' ./run-tests.sh
Signature · 1 line
2.41.0
#4 lore raw

PATCH v2 4/5 build: cleanup if-blocks for conditional compilation in "Makefile.am"

In reply to Thomas Haller

configure sets those $(*_LIBS) variables to something empty, when the dependency is not available. It's cumbersome and unnecessary to explicitly check.

Also, the order in which libraries are specified on the command line matters. Commonly we want our own libraries first ("src/libminigmp.la" should come pretty early). That is cumbersome to get right otherwise.

Thus, remove those conditional checks and just refer to those $(*_LIBS) variables unconditionally. Rely on them being empty, when the dependency is disabled.

Also, as "src/xt.c" is always built, just directly add it to the list of source files.

---
 Makefile.am | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)
Makefile.am +11 -15
index 48d138636d2f..396bf3fa2c22 100644
@@ -195,6 +195,12 @@ src_libminigmp_la_CFLAGS = \
 	-Wno-sign-compare \
 	$(NULL)
 
+LIBMINIGMP_LIBS = src/libminigmp.la
+
+else
+
+LIBMINIGMP_LIBS =
+
 endif
 
 ###############################################################################
@@ -247,10 +253,9 @@ src_libnftables_la_SOURCES = \
 	src/tcpopt.c \
 	src/utils.c \
 	src/xfrm.c \
+	src/xt.c \
 	$(NULL)
 
-src_libnftables_la_SOURCES += src/xt.c
-
 if BUILD_JSON
 src_libnftables_la_SOURCES += \
 	src/json.c \
@@ -264,23 +269,14 @@ src_libnftables_la_LDFLAGS = \
 	$(NULL)
 
 src_libnftables_la_LIBADD = \
+	src/libparser.la \
+	$(LIBMINIGMP_LIBS) \
 	$(LIBMNL_LIBS) \
 	$(LIBNFTNL_LIBS) \
-	src/libparser.la \
+	$(XTABLES_LIBS) \
+	$(JANSSON_LIBS) \
 	$(NULL)
 
-if BUILD_MINIGMP
-src_libnftables_la_LIBADD += src/libminigmp.la
-endif
-
-if BUILD_XTABLES
-src_libnftables_la_LIBADD += $(XTABLES_LIBS)
-endif
-
-if BUILD_JSON
-src_libnftables_la_LIBADD += $(JANSSON_LIBS)
-endif
-
 ###############################################################################
 
 sbin_PROGRAMS += src/nft
Signature · 1 line
2.41.0
#5 lore raw

PATCH v2 5/5 tests/unit: add unit tests for libnftables

In reply to Thomas Haller

We have a lot of tests/shell tests, that use the "nft" binary and we have python tests that can use the public API of "libnftables.so". However, it's useful to also write unit tests, that can test the internal C code more immediately.

Since no such tests infrastructure exist yet, it would be cumbersome to write such a test. Add two new test binaries, that can be used as a place for such tests. Currently there are no real tests there, it's only to show how it works, and that those dummy tests pass (including that linking and execution passes).

To access the internals, build an intermediate static library "src/libnftables-static.la", which then makes up the public, dynamic "src/libnftables.la" library. Add two tests:

- tests/unit/test-libnftables-static
- tests/unit/test-libnftables

The former statically links with "src/libnftables-static.la" and can test internal API from headers under "include/*.h". The latter dynamically links with "src/libnftables.la", and can only test the public API from "includes/nftables/libnftables.h".

You can run the unit tests alone with make check-TESTS. There is also make check-unit, which aliases check-TESTS target. Calling VALGRIND=y make check works as expected.

Also add a LOG_COMPILER script "tools/test-runner.sh". This wraps the execution of the tests. Even for manual testing, you likely don't want to run "tests/unit/test-*" executables directly, but rather

  $ ./tools/test-runner.sh tests/unit/test-libnftables

This sets up an unshared namespace, honors VALGRIND=y to run the test under valgrind, handles libtool, and supports options --make and --gdb. It also set up some environment variables that will be useful for some tests (e.g. "$SRCDIR").

Also, ignore some build artifacts from top level gitignore file. The build artifacts like "*.o" will not only be found under "src/". Move those patterns.

---
 .gitignore                           |  15 +-
 Makefile.am                          |  75 ++++++++-
 src/.gitignore                       |   5 -
 tests/unit/nft-test.h                |  14 ++
 tests/unit/test-libnftables-static.c |  16 ++
 tests/unit/test-libnftables.c        |  21 +++
 tools/test-runner.sh                 | 235 +++++++++++++++++++++++++++
 7 files changed, 365 insertions(+), 16 deletions(-)
 create mode 100644 tests/unit/nft-test.h
 create mode 100644 tests/unit/test-libnftables-static.c
 create mode 100644 tests/unit/test-libnftables.c
 create mode 100755 tools/test-runner.sh
.gitignore +13 -2
index a62e31f31c6b..369678a13987 100644
@@ -1,6 +1,11 @@
-# Generated by autoconf/configure/automake
+# Generated by autoconf/configure/automake/make
 *.m4
+*.la
+*.lo
+*.o
+.deps/
 .dirstamp
+.libs/
 Makefile
 Makefile.in
 stamp-h1
@@ -20,7 +25,13 @@ libtool
 
 # Generated by tests
 *.payload.got
-tests/build/tests.log
+test-suite.log
+tests/**/*.log
+tests/**/*.trs
+tests/**/*.valgrind-log
+
+tests/unit/test-libnftables
+tests/unit/test-libnftables-static
 
 # Debian package build temporary files
 build-stamp
Makefile.am +66 -9

140 lines not on this page · raw message

src/.gitignore +0 -5
index 2d907425cbb0..f34105c6cda4 100644
@@ -1,8 +1,3 @@
-*.la
-*.lo
-*.o
-.deps/
-.libs/
 nft
 parser_bison.c
 parser_bison.h
tests/unit/nft-test.h new file +14 -0
new file mode 100644
index 000000000000..cab97b42c669
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef __NFT_TEST__H__
+#define __NFT_TEST__H__
+
+#undef NDEBUG
+
+#include <nft.h>
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#endif /* __NFT_TEST__H__ */
tests/unit/test-libnftables-static.c new file +16 -0
new file mode 100644
index 000000000000..e34fcfd77f39
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "nft-test.h"
+
+#include <datatype.h>
+
+static void test_datatype(void)
+{
+	assert(!datatype_lookup(-1));
+}
+
+int main(int argc, char **argv)
+{
+	test_datatype();
+	return 0;
+}
tests/unit/test-libnftables.c new file +21 -0
new file mode 100644
index 000000000000..100558cd5e0f
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "nft-test.h"
+
+#include <nftables/libnftables.h>
+
+static void test_nft_ctx(void)
+{
+	struct nft_ctx *ctx;
+
+	ctx = nft_ctx_new(0);
+	assert(ctx);
+
+	nft_ctx_free(ctx);
+}
+
+int main(int argc, char **argv)
+{
+	test_nft_ctx();
+	return 0;
+}
tools/test-runner.sh new file +235 -0

235 lines not on this page · raw message

Signature · 1 line
2.41.0
#6 lore raw
In reply to Thomas Haller
Quoted 25 lines · Thomas Haller

Changes to v1:

- rename some make targets
- add make check-unit to alias make check-TESTS. These targets run the tests hooked up as "TESTS=" (which for now are the tests in tests/unit).
- improve commit messages and various minor code changes.

Thomas Haller (5):
  build: add basic "check-{local,more,all}" and "build-all" make targets
  build: add make check-build to run ./tests/build/run-tests.sh
  build: add make check-tree to check consistency of source tree
  build: cleanup if-blocks for conditional compilation in "Makefile.am"
  tests/unit: add unit tests for libnftables

 .gitignore                           |  15 +- Makefile.am                          | 134 ++++++++++++--- src/.gitignore                       |   5 - tests/unit/nft-test.h                |  14 ++ tests/unit/test-libnftables-static.c |  16 ++ tests/unit/test-libnftables.c        |  21 +++ tools/test-runner.sh                 | 235
+++++++++++++++++++++++++++

Quoted 5 lines
 7 files changed, 412 insertions(+), 28 deletions(-)

 create mode 100644 tests/unit/nft-test.h create mode 100644 tests/unit/test-libnftables-static.c create mode 100644 tests/unit/test-libnftables.c create mode 100755 tools/test-runner.sh

Hi Pablo,

any concerns about this? Could it be merged?

Thank you,
Thomas

#7 lore raw
Quoted 3 lines · Thomas Haller

Hi Pablo,

any concerns about this? Could it be merged?

Sorry. JSON support is not working, I had to locally revert those patches to run tests on -stable 5.4 here.

Let's agree on some basic rule from now on: One series at a time only, anything else coming after will be marked as deferred in patchwork.

Thanks.

#8 lore raw
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller

Hi Pablo,

any concerns about this? Could it be merged?

Quoted 9 lines

Sorry. JSON support is not working, I had to locally revert those patches to run tests on -stable 5.4 here.

Let's agree on some basic rule from now on: One series at a time only,
anything else coming after will be marked as deferred in patchwork.

Thanks.

Hi,

Could this be considered?

This provides the basis for unit tests (and the possibility to even add any such tests).

It also hooks up tests to make check. Which would be desirable to build upon. make check currently does nothing. For example, Florian's afl++ patches could hook into make check (or make check-more), if this basis was there.

Thomas

#9 lore raw
Quoted 17 lines · Thomas Haller
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Thomas Haller

Hi Pablo,

any concerns about this? Could it be merged?

Quoted 9 lines

Sorry. JSON support is not working, I had to locally revert those patches to run tests on -stable 5.4 here.

Let's agree on some basic rule from now on: One series at a time only,
anything else coming after will be marked as deferred in patchwork.

Thanks.

Quoted 6 lines

Hi,

Could this be considered?

This provides the basis for unit tests (and the possibility to even add any such tests).

We are still discussing the json integration into tests/shell. I suggest, let dust settle on each front before making more changes.

Quoted 4 lines

It also hooks up tests to make check. Which would be desirable to build upon. make check currently does nothing. For example, Florian's afl++ patches could hook into make check (or make check-more), if this basis was there.

I still doubt make check provides any benefit to the release process, which will exercise this path because of make distcheck' which I might have to relax it to make dist' to skip this to ensure release process is reliable.

I think all these tests should continously and provide reports to us, but not necessarily integrate them into `make check'.