From a04da508c0d18dad7453cf73478882803e2e447c Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 16:56:16 -0500 Subject: [PATCH 1/2] Kept the CMDB insertion-failure test hook out of the production library CMDBTestFailInsertion was a writable, exported function pointer in libpromises, consulted on every CMDB load. It now exists only when TEST_UNIT_TEST is defined, and cmdb_test.c includes cmdb.c with that defined, the way split_process_line_test.c does for processes_select.c. In production the check is a constant false and the symbol and its declaration in cmdb.h are gone. The test cases are unchanged. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- libpromises/cmdb.c | 20 +++++++++++++++++--- libpromises/cmdb.h | 8 -------- tests/unit/cmdb_test.c | 5 ++++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/libpromises/cmdb.c b/libpromises/cmdb.c index f7d9ca45665..b0c42a50e38 100644 --- a/libpromises/cmdb.c +++ b/libpromises/cmdb.c @@ -49,7 +49,21 @@ #define CMDB_CLASSES_REGULAR_EXPRESSIONS "regular_expressions" #define CMDB_COMMENT_KEY "comment" -bool (*CMDBTestFailInsertion)(const char *key) = NULL; +#ifdef TEST_UNIT_TEST +/** + * Only in the unit test build (cmdb_test.c includes this file with + * TEST_UNIT_TEST defined, as split_process_line_test.c does for + * processes_select.c): a variable or class whose CMDB key makes the hook + * return true is treated as one that cannot be inserted into the EvalContext, + * which validation cannot otherwise produce. It lets a test exercise the + * rollback of LoadCMDBData(). Production has no such pointer. + */ +static bool (*CMDBTestFailInsertion)(const char *key) = NULL; +#define CMDBInsertionForcedToFail(key) \ + (CMDBTestFailInsertion != NULL && CMDBTestFailInsertion(key)) +#else +#define CMDBInsertionForcedToFail(key) false +#endif /** * @param strict refuse what JsonParseFile() would repair or ignore: a repeated @@ -369,7 +383,7 @@ static bool AddCMDBVariable(EvalContext *ctx, const char *key, const VarRef *ref assert(data != NULL); assert(tags != NULL); - if (CMDBTestFailInsertion != NULL && CMDBTestFailInsertion(key)) + if (CMDBInsertionForcedToFail(key)) { Log(LOG_LEVEL_ERR, "Variable '%s' cannot be added (insertion failure forced by a test)", key); StringSetDestroy(tags); @@ -805,7 +819,7 @@ static bool AddCMDBClass(EvalContext *ctx, const char *key, StringSet *tags, con assert(key != NULL); assert(tags != NULL); - if (CMDBTestFailInsertion != NULL && CMDBTestFailInsertion(key)) + if (CMDBInsertionForcedToFail(key)) { Log(LOG_LEVEL_ERR, "Class '%s' cannot be added (insertion failure forced by a test)", key); StringSetDestroy(tags); diff --git a/libpromises/cmdb.h b/libpromises/cmdb.h index 9510982704e..e9e34be78ff 100644 --- a/libpromises/cmdb.h +++ b/libpromises/cmdb.h @@ -33,12 +33,4 @@ JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_max); bool LoadCMDBData(EvalContext *ctx); -/** - * Test seam, NULL in production: when set, a variable or class whose CMDB key - * makes it return true is treated as one that cannot be inserted into the - * EvalContext, which validation cannot otherwise produce. It lets a unit test exercise the - * rollback of LoadCMDBData(). - */ -extern bool (*CMDBTestFailInsertion)(const char *key); - #endif /* CFENGINE_CMDB_H */ diff --git a/tests/unit/cmdb_test.c b/tests/unit/cmdb_test.c index 708d6e7b96c..8d2d02853b1 100644 --- a/tests/unit/cmdb_test.c +++ b/tests/unit/cmdb_test.c @@ -1,11 +1,14 @@ #include +/* Includes cmdb.c itself, with its test hook (not in the production library) */ +#define TEST_UNIT_TEST +#include + #include #include /* Class */ #include /* StringSetContains */ #include /* StringEqual */ #include /* VarRefParse */ -#include #include #include /* xsnprintf */ From 8d8dc94edefb6630e44cb636c13e59c965d43fb7 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Sat, 14 Nov 2026 16:56:18 -0500 Subject: [PATCH 2/2] Pointed the libntech submodule at the djbclark/libntech fork The libntech gitlink is a commit of the fork's tendcf-integration branch, which NorthernTechHQ/libntech does not have, so a plain clone could not fetch it. Fork-only: PRs offered upstream keep upstream's URL. Changelog: None Ticket: none Co-Authored-By: Claude Sonnet 5.5 --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 3b5d0268688..3c49a74ca23 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "libntech"] path = libntech - url = https://github.com/NorthernTechHQ/libntech + url = https://github.com/djbclark/libntech [submodule "contrib/emacs-code-style"] path = contrib/emacs-code-style url = https://github.com/cfengine/cfengine-code-style