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 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 */