Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -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
20 changes: 17 additions & 3 deletions libpromises/cmdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 0 additions & 8 deletions libpromises/cmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
5 changes: 4 additions & 1 deletion tests/unit/cmdb_test.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#include <test.h>

/* Includes cmdb.c itself, with its test hook (not in the production library) */
#define TEST_UNIT_TEST
#include <cmdb.c>

#include <eval_context.h>
#include <class.h> /* Class */
#include <set.h> /* StringSetContains */
#include <string_lib.h> /* StringEqual */
#include <var_expressions.h> /* VarRefParse */
#include <cmdb.h>
#include <known_dirs.h>
#include <misc_lib.h> /* xsnprintf */

Expand Down