diff --git a/.github/workflows/reactor_tests.yml b/.github/workflows/reactor_tests.yml index b182dbe6796..5c3e8779b03 100644 --- a/.github/workflows/reactor_tests.yml +++ b/.github/workflows/reactor_tests.yml @@ -22,4 +22,4 @@ jobs: ln -s "$PWD/cf-reactor/cf-reactor" "$PWD/cf-promises/cf-promises" "$RUNNER_TEMP/bin/" cd tests/reactor chmod -R go-w . - CF_BINDIR="$RUNNER_TEMP/bin" sh run.sh + CF_BINDIR="$RUNNER_TEMP/bin" sh run.sh --unsafe diff --git a/.gitignore b/.gitignore index ff74fda4594..cb1a01a8fe5 100644 --- a/.gitignore +++ b/.gitignore @@ -58,6 +58,7 @@ stamp-h1 /tests/acceptance/25_cf-execd/Makefile /tests/acceptance/Makefile /tests/load/Makefile +/tests/reactor/Makefile /tests/unit/Makefile /tests/static-check/Makefile /tests/valgrind-check/Makefile diff --git a/cf-agent/nfs.c b/cf-agent/nfs.c index e983d3af67a..2b8b9000458 100644 --- a/cf-agent/nfs.c +++ b/cf-agent/nfs.c @@ -400,11 +400,12 @@ bool LoadMountInfo(Seq *list) buf1[i] = '\0'; - SetTimeOut(RPCTIMEOUT); + SetChildTimeOut(RPCTIMEOUT); if ((pp = cf_popen(buf1, "r", true)) == NULL) { Log(LOG_LEVEL_ERR, "Can't open '%s'. (cf_popen: %s)", buf1, GetErrorStr()); + DisarmTimeOut(); return false; } @@ -423,6 +424,7 @@ bool LoadMountInfo(Seq *list) { Log(LOG_LEVEL_ERR, "Unable to read list of mounted filesystems. (fread: %s)", GetErrorStr()); cf_pclose(pp); + DisarmTimeOut(); free(vbuff); return false; } @@ -485,6 +487,7 @@ bool LoadMountInfo(Seq *list) Log(LOG_LEVEL_INFO, "Session failed while trying to talk to remote host"); Log(LOG_LEVEL_INFO, "%s", vbuff); cf_pclose(pp); + DisarmTimeOut(); free(vbuff); return false; } @@ -580,8 +583,7 @@ bool LoadMountInfo(Seq *list) } free(vbuff); - alarm(0); - signal(SIGALRM, SIG_DFL); + DisarmTimeOut(); cf_pclose(pp); return true; } @@ -1121,7 +1123,7 @@ void MountAll() } #endif - SetTimeOut(RPCTIMEOUT); + SetChildTimeOut(RPCTIMEOUT); const char *cmd = VMOUNTCOMM[VSYSTEMHARDCLASS]; @@ -1177,8 +1179,7 @@ void MountAll() } free(line); - alarm(0); - signal(SIGALRM, SIG_DFL); + DisarmTimeOut(); cf_pclose(pp); } @@ -1435,7 +1436,7 @@ PromiseResult ReconcileMountOptions(EvalContext *ctx, char *name, const Attribut free(ropts); Log(LOG_LEVEL_VERBOSE, "Reconciling '%s' via remount: %s", name, comm); - SetTimeOut(timeout); + SetChildTimeOut(timeout); FILE *pfp = cf_popen(comm, "r", true); if (pfp == NULL) diff --git a/cf-agent/package_module.c b/cf-agent/package_module.c index 840483f71b9..a07ae0e905d 100644 --- a/cf-agent/package_module.c +++ b/cf-agent/package_module.c @@ -47,123 +47,6 @@ static bool UpdateSinglePackageModuleCache(EvalContext *ctx, const PackageModuleWrapper *module_wrapper, UpdateType type, bool force_update); -static void GetPackageModuleExecInfo(const PackageModuleBody *package_module, char **exec_path, - char **script_path, char **script_path_quoted, char **script_exec_opts); -static int NegotiateSupportedAPIVersion(PackageModuleWrapper *wrapper); - - -void DeletePackageModuleWrapper(PackageModuleWrapper *wrapper) -{ - if (wrapper != NULL) - { - free(wrapper->path); - free(wrapper->script_path); - free(wrapper->script_path_quoted); - free(wrapper->script_exec_opts); - free(wrapper->name); - free(wrapper); - } -} - -PackageModuleWrapper *NewPackageModuleWrapper(PackageModuleBody *package_module) -{ - assert(package_module && package_module->name); - - PackageModuleWrapper *wrapper = xmalloc(sizeof(PackageModuleWrapper)); - - GetPackageModuleExecInfo(package_module, &(wrapper->path), - &(wrapper->script_path), &(wrapper->script_path_quoted), - &(wrapper->script_exec_opts)); - wrapper->name = SafeStringDuplicate(package_module->name); - wrapper->package_module = package_module; - - if (wrapper->path == NULL) - { - Log(LOG_LEVEL_ERR, "No executable for the package module '%s'", package_module->name); - DeletePackageModuleWrapper(wrapper); - return NULL; - } - - /* Check if the given files exist and have the required permissions. */ - if (wrapper->script_path != NULL) - { - /* we were given a path to a script to execute with the given - * interpreter */ - const char *interpreter_path = wrapper->path; - const char *script_path = wrapper->script_path; - - if (access(interpreter_path, X_OK) != 0) - { - Log(LOG_LEVEL_ERR, - "Cannot find package module interpreter at location '%s'" - " or access to the file is restricted: %s", - wrapper->path, GetErrorStr()); - DeletePackageModuleWrapper(wrapper); - return NULL; - } - if (access(script_path, R_OK) != 0) - { - Log(LOG_LEVEL_ERR, - "Cannot find package module script at location '%s'" - " or access to the file is restricted: %s", - wrapper->script_path, GetErrorStr()); - DeletePackageModuleWrapper(wrapper); - return NULL; - } - } - else if (access(wrapper->path, X_OK) != 0) - { - /* no script path specified --> the given path has to be executable */ - Log(LOG_LEVEL_ERR, - "Cannot find package module at location '%s' or access to file is restricted: %s", - wrapper->path, GetErrorStr()); - DeletePackageModuleWrapper(wrapper); - return NULL; - } - - /* Negotiate API version */ - wrapper->supported_api_version = NegotiateSupportedAPIVersion(wrapper); - if (wrapper->supported_api_version != 1) - { - Log(LOG_LEVEL_ERR, - "Unsupported package module wrapper API version: %d", - wrapper->supported_api_version); - DeletePackageModuleWrapper(wrapper); - return NULL; - } - - Log(LOG_LEVEL_DEBUG, - "Successfully created package module wrapper for '%s' package module.", - package_module->name); - - return wrapper; -} - -static int PackageWrapperCommunicate(const PackageModuleWrapper *wrapper, const char *args, - const char *request, Rlist **response) -{ - char *all_args = NULL; - if (wrapper->script_path != NULL) - { - if (wrapper->script_exec_opts == NULL) - { - all_args = StringConcatenate(3, wrapper->script_path_quoted, " ", args); - args = all_args; - } - else - { - all_args = StringConcatenate(5, wrapper->script_exec_opts, " ", - wrapper->script_path_quoted, " ", args); - args = all_args; - } - } - int ret = PipeReadWriteData(wrapper->path, args, request, response, - PACKAGE_PROMISE_SCRIPT_TIMEOUT_SEC, - PACKAGE_PROMISE_TERMINATION_CHECK_SEC); - free(all_args); - - return ret; -} void UpdatePackagesCache(EvalContext *ctx, bool force_update) { @@ -193,7 +76,8 @@ void UpdatePackagesCache(EvalContext *ctx, bool force_update) continue; } - PackageModuleWrapper *module_wrapper = NewPackageModuleWrapper(module); + PackageModuleWrapper *module_wrapper = + NewPackageModuleWrapper(module, PACKAGE_PROMISE_SCRIPT_TIMEOUT_SEC); if (!module_wrapper) { @@ -278,24 +162,6 @@ static void FreePackageInfo(PackageInfo *package_info) } } -static char *ParseOptions(Rlist *options) -{ - if (RlistIsNullList(options)) - { - return SafeStringDuplicate(""); - } - - Buffer *data = BufferNew(); - for (Rlist *rp = options; rp != NULL; rp = rp->next) - { - char *value = RlistScalarValue(rp); - BufferAppendString(data, "options="); - BufferAppendString(data, value); - BufferAppendString(data, "\n"); - } - return BufferClose(data); -} - static PackageInfo *ParseAndCheckPackageDataReply(const Rlist *data) { PackageInfo * package_data = xcalloc(1, sizeof(PackageInfo)); @@ -386,35 +252,6 @@ static PackageInfo *ParseAndCheckPackageDataReply(const Rlist *data) return package_data; } -static int NegotiateSupportedAPIVersion(PackageModuleWrapper *wrapper) -{ - assert(wrapper); - - Log(LOG_LEVEL_DEBUG, "Getting supported API version."); - - int api_version = -1; - - Rlist *response = NULL; - if (PackageWrapperCommunicate(wrapper, "supports-api-version", "", - &response) != 0) - { - Log(LOG_LEVEL_INFO, - "Error occurred while getting supported API version."); - return -1; - } - - if (response) - { - if (RlistLen(response) == 1) - { - api_version = atoi(RlistScalarValue(response)); - Log(LOG_LEVEL_DEBUG, "package wrapper API version: %d", api_version); - } - RlistDestroy(response); - } - return api_version; -} - /* IMPORTANT: this might not return all the data we need like version or architecture but package name MUST be known. */ static @@ -426,7 +263,7 @@ PackageInfo *GetPackageData(const char *name, const char *version, Log(LOG_LEVEL_DEBUG, "Getting package '%s' data.", name); - char *options_str = ParseOptions(options); + char *options_str = PackageModuleFormatOptions(options); char *ver = version ? StringFormat("Version=%s\n", version) : NULL; char *arch = architecture ? @@ -473,55 +310,6 @@ PackageInfo *GetPackageData(const char *name, const char *version, return package_data; } -static void GetPackageModuleExecInfo(const PackageModuleBody *package_module, char **exec_path, - char **script_path, char **script_path_quoted, - char **script_exec_opts) -{ - - assert(exec_path != NULL); - assert(script_path != NULL); - assert(script_path_quoted != NULL); - - char *package_module_path = NULL; - - if (package_module->module_path != NULL && !StringEqual(package_module->module_path, "")) - { - package_module_path = xstrdup(package_module->module_path); - } - else - { - package_module_path = StringFormat("%s%c%s%c%s%c%s", GetWorkDir(), FILE_SEPARATOR, - "modules", FILE_SEPARATOR, "packages", FILE_SEPARATOR, - package_module->name); - } - - if (package_module->interpreter && !StringEqual(package_module->interpreter, "")) - { - *script_path = package_module_path; - *script_path_quoted = Path_GetQuoted(*script_path); - - if (strchr(package_module->interpreter, ' ') == NULL) - { - /* No spaces in the 'interpreter' string, easy! Just interpreter path given, no opts. */ - *exec_path = xstrdup(package_module->interpreter); - *script_exec_opts = NULL; - } - else - { - /* we need to split the interpreter command from the (potential) - * interpreter args */ - ArgGetExecutableAndArgs(package_module->interpreter, exec_path, script_exec_opts); - } - } - else - { - *exec_path = package_module_path; - *script_path = NULL; - *script_path_quoted = NULL; - *script_exec_opts = NULL; - } -} - static int IsPackageInCache(EvalContext *ctx, const PackageModuleWrapper *module_wrapper, const char *name, const char *ver, const char *arch) @@ -780,7 +568,7 @@ bool UpdateCache(Rlist* options, const PackageModuleWrapper *wrapper, Log(LOG_LEVEL_DEBUG, "Updating cache: %d", type); - char *options_str = ParseOptions(options); + char *options_str = PackageModuleFormatOptions(options); Rlist *response = NULL; const char *req_type = NULL; @@ -880,7 +668,7 @@ PromiseResult RemovePackage(const char *name, Rlist* options, Log(LOG_LEVEL_DEBUG, "Removing package '%s'", name); - char *options_str = ParseOptions(options); + char *options_str = PackageModuleFormatOptions(options); char *ver = version ? StringFormat("Version=%s\n", version) : NULL; char *arch = architecture ? @@ -925,7 +713,7 @@ static PromiseResult InstallPackageGeneric(Rlist *options, type == PACKAGE_TYPE_FILE ? "file" : "repo", packages_list_formatted); - char *options_str = ParseOptions(options); + char *options_str = PackageModuleFormatOptions(options); char *request = StringFormat("%s%s", options_str, packages_list_formatted); PromiseResult res = PROMISE_RESULT_CHANGE; diff --git a/cf-agent/package_module.h b/cf-agent/package_module.h index b8372c96984..bbae854a92a 100644 --- a/cf-agent/package_module.h +++ b/cf-agent/package_module.h @@ -26,10 +26,10 @@ #define PACKAGE_MODULE_H #include +#include //TODO: use promise expireafter instead #define PACKAGE_PROMISE_SCRIPT_TIMEOUT_SEC 4 * 3600 /* 4 hours timeout */ -#define PACKAGE_PROMISE_TERMINATION_CHECK_SEC 1 #define GLOBAL_PACKAGE_PROMISE_LOCK_NAME "new_packages_promise_lock" @@ -54,17 +54,6 @@ typedef struct char *message; } PackageError; -typedef struct -{ - char *name; - char *path; - char *script_path; - char *script_path_quoted; - char *script_exec_opts; - PackageModuleBody *package_module; - int supported_api_version; -} PackageModuleWrapper; - typedef struct { CfLock g_lock; @@ -88,9 +77,6 @@ PromiseResult HandleAbsentPromiseAction(EvalContext *ctx, void UpdatePackagesCache(EvalContext *ctx, bool force_update); -PackageModuleWrapper *NewPackageModuleWrapper(PackageModuleBody *package_module); -void DeletePackageModuleWrapper(PackageModuleWrapper *wrapper); - PackagePromiseGlobalLock AcquireGlobalPackagePromiseLock(EvalContext *ctx); void YieldGlobalPackagePromiseLock(PackagePromiseGlobalLock lock); diff --git a/cf-agent/verify_exec.c b/cf-agent/verify_exec.c index 15c74d5fee9..bcd8b658fa9 100644 --- a/cf-agent/verify_exec.c +++ b/cf-agent/verify_exec.c @@ -302,7 +302,7 @@ static ActionResult RepairExec(EvalContext *ctx, const Attributes *a, { if (a->contain.timeout != CF_NOINT) { - SetTimeOut(a->contain.timeout); + SetChildTimeOut(a->contain.timeout); } #ifndef __MINGW32__ @@ -324,6 +324,10 @@ static ActionResult RepairExec(EvalContext *ctx, const Attributes *a, a->transaction.background); #else // !__MINGW32__ Log(LOG_LEVEL_ERR, "Powershell is only supported on Windows"); + if (a->contain.timeout != CF_NOINT) + { + DisarmTimeOut(); + } return ACTION_RESULT_FAILED; #endif // !__MINGW32__ } @@ -472,8 +476,7 @@ static ActionResult RepairExec(EvalContext *ctx, const Attributes *a, if (a->contain.timeout != CF_NOINT) { - alarm(0); - signal(SIGALRM, SIG_DFL); + DisarmTimeOut(); } Log(info_or_verbose, "Completed execution of '%s'", cmdline); diff --git a/cf-agent/verify_new_packages.c b/cf-agent/verify_new_packages.c index 82985a40f9b..3dcd9a010e3 100644 --- a/cf-agent/verify_new_packages.c +++ b/cf-agent/verify_new_packages.c @@ -100,7 +100,8 @@ PromiseResult HandleNewPackagePromiseType(EvalContext *ctx, const Promise *pp, c } PackageModuleWrapper *package_module = - NewPackageModuleWrapper(a->new_packages.module_body); + NewPackageModuleWrapper(a->new_packages.module_body, + PACKAGE_PROMISE_SCRIPT_TIMEOUT_SEC); if (package_module == NULL) { diff --git a/cf-reactor/Makefile.am b/cf-reactor/Makefile.am index 4036d272376..3c84df15546 100644 --- a/cf-reactor/Makefile.am +++ b/cf-reactor/Makefile.am @@ -45,6 +45,7 @@ libcf_reactor_la_SOURCES = \ reactor_context.c reactor_context.h \ reactor_transform.c reactor_transform.h \ stoppable_thread.c stoppable_thread.h \ + user_watcher.c user_watcher.h \ wakeup_channel.c wakeup_channel.h \ watcher.c watcher.h diff --git a/cf-reactor/file_watcher.c b/cf-reactor/file_watcher.c index b44730355ea..fa4f6d54809 100644 --- a/cf-reactor/file_watcher.c +++ b/cf-reactor/file_watcher.c @@ -82,7 +82,7 @@ typedef struct } FileWatcherState; -void *FileWatcherStateNew(const Rlist *args) +void *FileWatcherStateNew(ARG_UNUSED const EvalContext *ctx, const Rlist *args) { assert(args != NULL); const char *path = RlistScalarValue(args); @@ -93,7 +93,7 @@ void *FileWatcherStateNew(const Rlist *args) return fws; } -void *FileWatcherSizeStateNew(const Rlist *args) +void *FileWatcherSizeStateNew(ARG_UNUSED const EvalContext *ctx, const Rlist *args) { assert(args != NULL && args->next != NULL); const char *path = RlistScalarValue(args); diff --git a/cf-reactor/file_watcher.h b/cf-reactor/file_watcher.h index b184d3651f0..9c426883da7 100644 --- a/cf-reactor/file_watcher.h +++ b/cf-reactor/file_watcher.h @@ -29,8 +29,8 @@ #include /* WatcherCheckResult */ -void *FileWatcherStateNew(const Rlist *args); -void *FileWatcherSizeStateNew(const Rlist *args); +void *FileWatcherStateNew(const EvalContext *ctx, const Rlist *args); +void *FileWatcherSizeStateNew(const EvalContext *ctx, const Rlist *args); void FileWatcherStateDestroy(void *state); WatcherCheckResult FileWatcherCheckFileDeleted(void *state); diff --git a/cf-reactor/reactor_transform.c b/cf-reactor/reactor_transform.c index 565e5a1094f..451a68adf5c 100644 --- a/cf-reactor/reactor_transform.c +++ b/cf-reactor/reactor_transform.c @@ -42,6 +42,7 @@ #include // REACTOR_INTERVAL_RANGE, REACTOR_FILE_DELETED, REACTOR_*_TYPES, BundleIsReactorEventsBundle() #include // CheckConstraintTypeMatch(), PromiseTypeSyntaxGet(), BodySyntaxGetConstraintSyntax() #include +#include #include /* Resolves the agent, common or reactor bundle (looked up in that order) @@ -132,11 +133,13 @@ static const Rlist *GetThenBundles(const Promise *pp) /*****************************************************************************/ /* A constraint of the 'when' body passed to the watcher of an event, with the - * type of value it accepts */ + * type of value it accepts. An absent optional constraint isn't passed, so + * optional ones must come after the required ones. */ typedef struct { const char *name; RvalType type; + bool optional; } EventArg; typedef struct @@ -151,13 +154,13 @@ typedef struct static const EventArg NO_EXTRA_ARGS[] = { - { NULL, RVAL_TYPE_NOPROMISEE } + { NULL, RVAL_TYPE_NOPROMISEE, false } }; static const EventArg FILE_SIZE_EXCEEDS_ARGS[] = { - { REACTOR_SIZE_THRESHOLD, RVAL_TYPE_SCALAR }, - { NULL, RVAL_TYPE_NOPROMISEE } + { REACTOR_SIZE_THRESHOLD, RVAL_TYPE_SCALAR, false }, + { NULL, RVAL_TYPE_NOPROMISEE, false } }; #define EventTypeNew(name, type, args, new_state, destroy_state, check) \ @@ -168,6 +171,10 @@ static const EventArg FILE_SIZE_EXCEEDS_ARGS[] = #define FileEventTypeNew(name, check) \ EventTypeNew(name, RVAL_TYPE_SCALAR, NO_EXTRA_ARGS, FileWatcherStateNew, FileWatcherStateDestroy, check) +/* A user event, about the user with the name of its constraint */ +#define UserEventTypeNew(name, check) \ + EventTypeNew(name, RVAL_TYPE_SCALAR, NO_EXTRA_ARGS, UserWatcherStateNew, UserWatcherStateDestroy, check) + static const EventType EVENT_TABLE[] = { FileEventTypeNew(REACTOR_FILE_DELETED, FileWatcherCheckFileDeleted), @@ -178,6 +185,8 @@ static const EventType EVENT_TABLE[] = FileEventTypeNew(REACTOR_FILE_OWNER_CHANGED, FileWatcherCheckFileOwnerChanged), EventTypeNew(REACTOR_FILE_SIZE_EXCEEDS, RVAL_TYPE_SCALAR, FILE_SIZE_EXCEEDS_ARGS, FileWatcherSizeStateNew, FileWatcherStateDestroy, FileWatcherCheckFileSizeExceeds), + UserEventTypeNew(REACTOR_USER_ADDED, UserWatcherCheckUserAdded), + UserEventTypeNew(REACTOR_USER_REMOVED, UserWatcherCheckUserRemoved), EventTypeNewNull() }; @@ -264,8 +273,9 @@ static bool EventsPromiseGetInterval(const Promise *pp, time_t *interval) return true; } -static bool ResolveWhenBody(const Promise *pp, WatcherOptions *opt) +static bool ResolveWhenBody(const EvalContext *ctx, const Promise *pp, WatcherOptions *opt) { + assert(ctx != NULL); assert(pp != NULL); assert(opt != NULL); // Get the event and its arg @@ -279,9 +289,20 @@ static bool ResolveWhenBody(const Promise *pp, WatcherOptions *opt) } // If the event has additional args, add them to the list of args + NDEBUG_UNUSED bool seen_optional = false; for (const EventArg *extra_arg = event->extra_args; extra_arg->name != NULL; extra_arg++) { + /* The state constructors read args by position. So optional args + * must come last: an absent one then only shortens the list, instead + * of shifting the args after it. */ + assert(!seen_optional || extra_arg->optional); + seen_optional = seen_optional || extra_arg->optional; + const Constraint *con = PromiseGetConstraint(pp, extra_arg->name); + if (con == NULL && extra_arg->optional) + { + continue; + } if (con == NULL) { Log(LOG_LEVEL_ERR, @@ -336,10 +357,15 @@ static bool ResolveWhenBody(const Promise *pp, WatcherOptions *opt) return false; } - opt->state = event->new_state(args); + opt->state = event->new_state(ctx, args); + RlistDestroy(args); + if (opt->state == NULL) + { + /* The state constructor logs why */ + return false; + } opt->check_callback = event->check; opt->destroy_state = event->destroy_state; - RlistDestroy(args); return true; } @@ -364,7 +390,7 @@ static PromiseResult KeepEventsPromise(EvalContext *ctx, const Promise *pp) } WatcherOptions opt; - if (!ResolveWhenBody(pp, &opt)) + if (!ResolveWhenBody(ctx, pp, &opt)) { return PROMISE_RESULT_FAIL; } diff --git a/cf-reactor/user_watcher.c b/cf-reactor/user_watcher.c new file mode 100644 index 00000000000..083846a3be5 --- /dev/null +++ b/cf-reactor/user_watcher.c @@ -0,0 +1,286 @@ +/* + Copyright 2026 Northern.tech AS + + This file is part of CFEngine 3 - written and maintained by Northern.tech AS. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; version 3. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + + To the extent this program is licensed as part of the Enterprise + versions of CFEngine, the applicable Commercial Open Source License + (COSL) may apply to this file if you as a licensee so wish it. See + included file COSL.txt. +*/ + +#include +#include +#include +#include +#include +#include + +#ifdef __MINGW32__ +#include +#else +#include +#endif + +/* Minimum time between two logs of a persisting failure to look up the user, + * since the user is looked up on every poll */ +#define CHECK_ERROR_LOG_INTERVAL_SECS 60 + +typedef struct +{ + char *name; + time_t last_checked; /* Time of the last successful check, 0 if none yet */ + time_t + last_error_logged; /* Time the last error was logged, 0 if no errors */ + + /* What the last successful check observed */ + bool last_exists; +} UserWatcherState; + + +/** + * @brief Creates the state of a user_added or user_removed watcher. + * + * @param ctx Unused + * @param args The arguments of the event, the name of the user to watch + * @return The new state, to be destroyed with UserWatcherStateDestroy() + */ +void *UserWatcherStateNew(ARG_UNUSED const EvalContext *ctx, const Rlist *args) +{ + assert(args != NULL); + const char *name = RlistScalarValue(args); + + UserWatcherState *state = + (UserWatcherState *) xcalloc(1, sizeof(UserWatcherState)); + state->name = xstrdup(name); + + return state; +} + +/** + * @brief Destroys the state of a user watcher. + * + * @param state The watcher state, may be NULL + */ +void UserWatcherStateDestroy(void *_state) +{ + UserWatcherState *state = _state; + if (state != NULL) + { + free(state->name); + free(state); + } +} + +/** + * @brief Logs the error message of a failed operation performed by the + * watcher. + * + * A persisting failure is logged at most once every + * CHECK_ERROR_LOG_INTERVAL_SECS. + * + * @param state The watcher state + * @param operation The operation (e.g., LookupAccountName() or getpwnam_r()) + * @param err The error code (errno on Unix, Win32 error code on Windows) + */ +static void LogCheckError( + UserWatcherState *state, const char *operation, int err) +{ + assert(state != NULL); + + const time_t now = time(NULL); + if ((state->last_error_logged == 0) + || (now - state->last_error_logged >= CHECK_ERROR_LOG_INTERVAL_SECS)) + { + Log(LOG_LEVEL_ERR, + "Unable to look up user '%s' while checking for user events (%s: %s)", + state->name, + operation, + GetErrorStrFromCode(err)); + state->last_error_logged = now; + } +} + +/** + * @brief Checks whether the user of the watcher exists. + * + * Looks up the one name only, without listing all users, so it is safe to + * call while the main thread runs bundles. On Windows, only user accounts + * count, not groups or built-in accounts such as SYSTEM. + * + * @param state The watcher state + * @param[out] exists Whether user exists + * @return false on error + */ +static bool UserLookup(UserWatcherState *state, bool *exists) +{ + assert(state != NULL); + assert(exists != NULL); + +#ifdef __MINGW32__ + char sid[CF_MAXSIDSIZE]; + SID_NAME_USE type; + DWORD error = NovaWin_LookupAccountName( + state->name, (SID *) sid, sizeof(sid), &type); + if (error == ERROR_SUCCESS) + { + *exists = (type == SidTypeUser); + } + else if (NovaWin_IsAccountNotFound(error)) + { + *exists = false; + } + else + { + LogCheckError(state, "LookupAccountName", error); + return false; + } +#else + struct passwd pwd; + struct passwd *result = NULL; + char buf[GETPW_R_SIZE_MAX]; + int ret = getpwnam_r(state->name, &pwd, buf, sizeof(buf), &result); + if (result != NULL) + { + *exists = true; + } + else if (ret == 0) + { + /* POSIX: not found is a NULL result with a return value of 0. Any + * other return value is an error, so the user is not taken as + * removed on a failing lookup. */ + *exists = false; + } + else + { + LogCheckError(state, "getpwnam_r", ret); + return false; + } +#endif + + state->last_error_logged = 0; + return true; +} + +/** + * @brief Records the result of a successful check. + * + * The first successful check is only the reference for the next ones, so it + * never fires. + * + * @param state The watcher state + * @param exists Whether the user exists + * @param happened Whether the event happened since the last check + * @param event The name of the event for logging (e.g. "removal") + * @return WATCHER_CHECK_EVENT if the event fired, WATCHER_CHECK_NO_EVENT + * otherwise + */ +static WatcherCheckResult UserWatcherRecordObservation( + UserWatcherState *state, bool exists, bool happened, const char *event) +{ + assert(state != NULL); + assert(event != NULL); + + const time_t now = time(NULL); + happened = happened && (state->last_checked != 0); + + if (happened) + { + char last_checked_str[26]; + const char *last_checked_ts = + cf_strtimestamp_local(state->last_checked, last_checked_str); + + char now_str[26]; + const char *now_ts = cf_strtimestamp_local(now, now_str); + + Log(LOG_LEVEL_VERBOSE, + "Observed %s of user '%s' at %s (last checked at %s)", + event, + state->name, + (now_ts != NULL) ? now_ts : "unknown time", + (last_checked_ts != NULL) ? last_checked_ts : "unknown time"); + } + + state->last_checked = now; + state->last_exists = exists; + return happened ? WATCHER_CHECK_EVENT : WATCHER_CHECK_NO_EVENT; +} + +/** + * @brief Checks for the user_added event. + * + * Fires once when the user goes from absent to present. + * + * @param state The watcher state + * @return WATCHER_CHECK_EVENT if the user was added since the last check, + * WATCHER_CHECK_ERROR if the lookup failed, WATCHER_CHECK_NO_EVENT + * otherwise + */ +WatcherCheckResult UserWatcherCheckUserAdded(void *_state) +{ + assert(_state != NULL); + UserWatcherState *state = _state; + + bool exists; + if (!UserLookup(state, &exists)) + { + /* Unknown state, keep the last observation as is */ + return WATCHER_CHECK_ERROR; + } + + if (!exists && (state->last_exists || state->last_checked == 0)) + { + Log(LOG_LEVEL_VERBOSE, + "User '%s' is absent, watching for its addition", + state->name); + } + + bool added = !state->last_exists && exists; + return UserWatcherRecordObservation(state, exists, added, "addition"); +} + +/** + * @brief Checks for the user_removed event. + * + * Fires once when the user goes from present to absent. + * + * @param state The watcher state + * @return WATCHER_CHECK_EVENT if the user was removed since the last check, + * WATCHER_CHECK_ERROR if the lookup failed, WATCHER_CHECK_NO_EVENT + * otherwise + */ +WatcherCheckResult UserWatcherCheckUserRemoved(void *_state) +{ + assert(_state != NULL); + UserWatcherState *state = _state; + + bool exists; + if (!UserLookup(state, &exists)) + { + /* Unknown state, keep the last observation as is */ + return WATCHER_CHECK_ERROR; + } + + if (exists && !state->last_exists) + { + Log(LOG_LEVEL_VERBOSE, + "User '%s' is present, watching for its removal", + state->name); + } + + bool removed = state->last_exists && !exists; + return UserWatcherRecordObservation(state, exists, removed, "removal"); +} diff --git a/cf-reactor/user_watcher.h b/cf-reactor/user_watcher.h new file mode 100644 index 00000000000..5c0d92be277 --- /dev/null +++ b/cf-reactor/user_watcher.h @@ -0,0 +1,37 @@ +/* + Copyright 2026 Northern.tech AS + + This file is part of CFEngine 3 - written and maintained by Northern.tech AS. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; version 3. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + + To the extent this program is licensed as part of the Enterprise + versions of CFEngine, the applicable Commercial Open Source License + (COSL) may apply to this file if you as a licensee so wish it. See + included file COSL.txt. +*/ + +#ifndef CFENGINE_USER_WATCHER_H +#define CFENGINE_USER_WATCHER_H + +#include +#include + + +void *UserWatcherStateNew(const EvalContext *ctx, const Rlist *args); +WatcherCheckResult UserWatcherCheckUserAdded(void *state); +WatcherCheckResult UserWatcherCheckUserRemoved(void *state); +void UserWatcherStateDestroy(void *state); + +#endif diff --git a/cf-reactor/watcher.h b/cf-reactor/watcher.h index bf12a91f63c..46f1ef6c137 100644 --- a/cf-reactor/watcher.h +++ b/cf-reactor/watcher.h @@ -35,7 +35,11 @@ typedef enum WATCHER_CHECK_ERROR, /* unable to tell whether the event happened */ } WatcherCheckResult; -typedef void *(*WatcherStateNewFn)(const Rlist *args); +/* Called on the main thread when the policy is evaluated. ctx must not be + * kept in the state: the EvalContext is not thread safe, so the watcher + * thread can't use it. Returns NULL, after logging why, if the state can't + * be created. */ +typedef void *(*WatcherStateNewFn)(const EvalContext *ctx, const Rlist *args); typedef WatcherCheckResult (*WatcherCheckFn)(void *state); typedef void (*WatcherStateDestroyFn)(void *state); diff --git a/configure.ac b/configure.ac index 5ed3b61a351..414aa1d4c66 100644 --- a/configure.ac +++ b/configure.ac @@ -2032,6 +2032,7 @@ AC_CONFIG_FILES([Makefile tests/acceptance/25_cf-execd/Makefile tests/unit/Makefile tests/load/Makefile + tests/reactor/Makefile tests/static-check/Makefile tests/valgrind-check/Makefile tests/asan-check/Makefile]) diff --git a/libcfnet/tls_generic.c b/libcfnet/tls_generic.c index d3882abb5ba..e34fb6dbc8a 100644 --- a/libcfnet/tls_generic.c +++ b/libcfnet/tls_generic.c @@ -480,10 +480,12 @@ X509 *TLSGenerateCertFromPrivKey(RSA *privkey) goto err3; } - X509_NAME *name = X509_get_subject_name(x509); + /* OpenSSL 4 returns a const name from X509_get_subject_name(), so build + * a new one and set it. */ + X509_NAME *name = X509_NAME_new(); if (name == NULL) { - Log(LOG_LEVEL_ERR, "X509_get_subject_name: %s", + Log(LOG_LEVEL_ERR, "X509_NAME_new: %s", TLSErrorString(ERR_get_error())); goto err3; } @@ -492,9 +494,11 @@ X509 *TLSGenerateCertFromPrivKey(RSA *privkey) ret += X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const unsigned char *) "a", -1, -1, 0); + ret += X509_set_subject_name(x509, name); ret += X509_set_issuer_name(x509, name); + X509_NAME_free(name); ret += X509_set_pubkey(x509, pkey); - if (ret < 3) + if (ret < 4) { Log(LOG_LEVEL_ERR, "Failed to set certificate details: %s", TLSErrorString(ERR_get_error())); diff --git a/libpromises/Makefile.am b/libpromises/Makefile.am index dc3fcf9dd77..99368a35df4 100644 --- a/libpromises/Makefile.am +++ b/libpromises/Makefile.am @@ -145,6 +145,7 @@ libpromises_la_SOURCES = \ parser_state.h \ patches.c \ pipes.h pipes.c \ + package_module_wrapper.c package_module_wrapper.h \ processes_select.c processes_select.h \ process_lib.h process_unix_priv.h \ promises.c promises.h \ diff --git a/libpromises/eval_context.c b/libpromises/eval_context.c index 0acf39b326f..b2e2bb701e1 100644 --- a/libpromises/eval_context.c +++ b/libpromises/eval_context.c @@ -51,6 +51,7 @@ #include #include /* DataTypeIsIterable */ #include +#include /* If we need to put a scoped variable into a special scope, use the string * below to replace the original scope separator. @@ -1149,18 +1150,6 @@ static void StackFrameDestroy(StackFrame *frame) } } -static -void FreePackageManager(PackageModuleBody *manager) -{ - assert(manager != NULL); - - free(manager->name); - free(manager->interpreter); - free(manager->module_path); - RlistDestroy(manager->options); - free(manager); -} - static PackagePromiseContext *PackagePromiseConfigNew() { @@ -1169,7 +1158,7 @@ PackagePromiseContext *PackagePromiseConfigNew() package_promise_defaults->control_package_module = NULL; package_promise_defaults->control_package_inventory = NULL; package_promise_defaults->package_modules_bodies = - SeqNew(5, FreePackageManager); + SeqNew(5, PackageModuleBodyDestroy); return package_promise_defaults; } diff --git a/libpromises/files_operators.c b/libpromises/files_operators.c index d450b8a7e50..2c7432d12fd 100644 --- a/libpromises/files_operators.c +++ b/libpromises/files_operators.c @@ -209,18 +209,31 @@ bool SaveAsFile(EvalContext *ctx, SaveCallbackFn callback, void *param, const ch } } - strcpy(backup, BufferData(deref_file)); + const char *deref = BufferData(deref_file); + + strlcpy(backup, deref, sizeof(backup)); if (a->edits.backup == BACKUP_OPTION_TIMESTAMP) { snprintf(stamp, CF_BUFSIZE, "_%jd_%s", (intmax_t) CFSTARTTIME, CanonifyName(ctime(&stamp_now))); - strcat(backup, stamp); + strlcat(backup, stamp, sizeof(backup)); } - strcat(backup, ".cf-before-edit"); + if (strlcat(backup, ".cf-before-edit", sizeof(backup)) >= sizeof(backup)) + { + Log(LOG_LEVEL_ERR, + "Filename too long to create backup for editing: %s", + BufferData(pretty_file)); + goto end; + } - strcpy(new, BufferData(deref_file)); - strcat(new, ".cf-after-edit"); + if (strlcpy(new, deref, sizeof(new)) >= sizeof(new) || + strlcat(new, ".cf-after-edit", sizeof(new)) >= sizeof(new)) + { + Log(LOG_LEVEL_ERR, "Filename too long to edit: %s", + BufferData(pretty_file)); + goto end; + } unlink(new); /* Just in case of races */ if ((*callback)(new, param, new_line_mode) == false) diff --git a/libpromises/mod_reactor.c b/libpromises/mod_reactor.c index 4ad4b49f8a4..ea1f8c6b616 100644 --- a/libpromises/mod_reactor.c +++ b/libpromises/mod_reactor.c @@ -51,6 +51,8 @@ static const ConstraintSyntax when_constraints[] = ConstraintSyntaxNewString(REACTOR_FILE_OWNER_CHANGED, CF_ANYSTRING, "File to react for on change of its owning user or group", SYNTAX_STATUS_NORMAL), ConstraintSyntaxNewString(REACTOR_FILE_SIZE_EXCEEDS, CF_ANYSTRING, "File to react for when its size grows above '" REACTOR_SIZE_THRESHOLD "'", SYNTAX_STATUS_NORMAL), ConstraintSyntaxNewInt(REACTOR_SIZE_THRESHOLD, CF_VALRANGE, "Size in bytes above which '" REACTOR_FILE_SIZE_EXCEEDS "' fires", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewString(REACTOR_USER_ADDED, CF_ANYSTRING, "User to react for on addition", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewString(REACTOR_USER_REMOVED, CF_ANYSTRING, "User to react for on removal", SYNTAX_STATUS_NORMAL), ConstraintSyntaxNewInt("interval", REACTOR_INTERVAL_RANGE, "Seconds between two checks for the event", SYNTAX_STATUS_NORMAL), ConstraintSyntaxNewNull() }; diff --git a/libpromises/mod_reactor.h b/libpromises/mod_reactor.h index 839a72721bb..eeb5380a122 100644 --- a/libpromises/mod_reactor.h +++ b/libpromises/mod_reactor.h @@ -36,6 +36,8 @@ #define REACTOR_FILE_OWNER_CHANGED "file_owner_changed" #define REACTOR_FILE_SIZE_EXCEEDS "file_size_exceeds" #define REACTOR_SIZE_THRESHOLD "size_threshold" +#define REACTOR_USER_ADDED "user_added" +#define REACTOR_USER_REMOVED "user_removed" /* Range of the 'interval' attribute of the 'when' body of events promises */ #define REACTOR_INTERVAL_RANGE "1,99999999999" diff --git a/libpromises/package_module_wrapper.c b/libpromises/package_module_wrapper.c new file mode 100644 index 00000000000..cb94a54a221 --- /dev/null +++ b/libpromises/package_module_wrapper.c @@ -0,0 +1,278 @@ +/* + Copyright 2026 Northern.tech AS + + This file is part of CFEngine 3 - written and maintained by Northern.tech AS. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; version 3. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + + To the extent this program is licensed as part of the Enterprise + versions of CFEngine, the applicable Commercial Open Source License + (COSL) may apply to this file if you as a licensee so wish it. See + included file COSL.txt. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void GetPackageModuleExecInfo(const PackageModuleBody *package_module, char **exec_path, + char **script_path, char **script_path_quoted, char **script_exec_opts); +static int NegotiateSupportedAPIVersion(PackageModuleWrapper *wrapper); + +void DeletePackageModuleWrapper(PackageModuleWrapper *wrapper) +{ + if (wrapper != NULL) + { + free(wrapper->path); + free(wrapper->script_path); + free(wrapper->script_path_quoted); + free(wrapper->script_exec_opts); + free(wrapper->name); + free(wrapper); + } +} + +PackageModuleWrapper *NewPackageModuleWrapper(PackageModuleBody *package_module, + int timeout_secs) +{ + assert(package_module != NULL); + assert(package_module->name != NULL); + + PackageModuleWrapper *wrapper = xmalloc(sizeof(PackageModuleWrapper)); + + GetPackageModuleExecInfo(package_module, &(wrapper->path), + &(wrapper->script_path), &(wrapper->script_path_quoted), + &(wrapper->script_exec_opts)); + wrapper->name = SafeStringDuplicate(package_module->name); + wrapper->package_module = package_module; + wrapper->timeout_secs = timeout_secs; + + if (wrapper->path == NULL) + { + Log(LOG_LEVEL_ERR, "No executable for the package module '%s'", package_module->name); + DeletePackageModuleWrapper(wrapper); + return NULL; + } + + /* Check if the given files exist and have the required permissions. */ + if (wrapper->script_path != NULL) + { + /* we were given a path to a script to execute with the given + * interpreter */ + const char *interpreter_path = wrapper->path; + const char *script_path = wrapper->script_path; + + if (access(interpreter_path, X_OK) != 0) + { + Log(LOG_LEVEL_ERR, + "Cannot find package module interpreter at location '%s'" + " or access to the file is restricted: %s", + wrapper->path, GetErrorStr()); + DeletePackageModuleWrapper(wrapper); + return NULL; + } + if (access(script_path, R_OK) != 0) + { + Log(LOG_LEVEL_ERR, + "Cannot find package module script at location '%s'" + " or access to the file is restricted: %s", + wrapper->script_path, GetErrorStr()); + DeletePackageModuleWrapper(wrapper); + return NULL; + } + } + else if (access(wrapper->path, X_OK) != 0) + { + /* no script path specified --> the given path has to be executable */ + Log(LOG_LEVEL_ERR, + "Cannot find package module at location '%s' or access to file is restricted: %s", + wrapper->path, GetErrorStr()); + DeletePackageModuleWrapper(wrapper); + return NULL; + } + + /* Negotiate API version */ + wrapper->supported_api_version = NegotiateSupportedAPIVersion(wrapper); + if (wrapper->supported_api_version != 1) + { + Log(LOG_LEVEL_ERR, + "Unsupported package module wrapper API version: %d", + wrapper->supported_api_version); + DeletePackageModuleWrapper(wrapper); + return NULL; + } + + Log(LOG_LEVEL_DEBUG, + "Successfully created package module wrapper for '%s' package module.", + package_module->name); + + return wrapper; +} + +int PackageWrapperCommunicate(const PackageModuleWrapper *wrapper, const char *args, + const char *request, Rlist **response) +{ + assert(wrapper != NULL); + + char *all_args = NULL; + if (wrapper->script_path != NULL) + { + if (wrapper->script_exec_opts == NULL) + { + all_args = StringConcatenate(3, wrapper->script_path_quoted, " ", args); + args = all_args; + } + else + { + all_args = StringConcatenate(5, wrapper->script_exec_opts, " ", + wrapper->script_path_quoted, " ", args); + args = all_args; + } + } + int ret = PipeReadWriteData(wrapper->path, args, request, response, + wrapper->timeout_secs, + PACKAGE_PROMISE_TERMINATION_CHECK_SEC); + free(all_args); + + return ret; +} + +char *PackageModuleFormatOptions(Rlist *options) +{ + if (RlistIsNullList(options)) + { + return SafeStringDuplicate(""); + } + + Buffer *data = BufferNew(); + for (Rlist *rp = options; rp != NULL; rp = rp->next) + { + char *value = RlistScalarValue(rp); + BufferAppendString(data, "options="); + BufferAppendString(data, value); + BufferAppendString(data, "\n"); + } + return BufferClose(data); +} + +static int NegotiateSupportedAPIVersion(PackageModuleWrapper *wrapper) +{ + assert(wrapper); + + Log(LOG_LEVEL_DEBUG, "Getting supported API version."); + + int api_version = -1; + + Rlist *response = NULL; + if (PackageWrapperCommunicate(wrapper, "supports-api-version", "", + &response) != 0) + { + Log(LOG_LEVEL_INFO, + "Error occurred while getting supported API version."); + return -1; + } + + if (response) + { + if (RlistLen(response) == 1) + { + api_version = atoi(RlistScalarValue(response)); + Log(LOG_LEVEL_DEBUG, "package wrapper API version: %d", api_version); + } + RlistDestroy(response); + } + return api_version; +} + +static void GetPackageModuleExecInfo(const PackageModuleBody *package_module, char **exec_path, + char **script_path, char **script_path_quoted, + char **script_exec_opts) +{ + + assert(package_module != NULL); + assert(exec_path != NULL); + assert(script_path != NULL); + assert(script_path_quoted != NULL); + + char *package_module_path = NULL; + + if (package_module->module_path != NULL && !StringEqual(package_module->module_path, "")) + { + package_module_path = xstrdup(package_module->module_path); + } + else + { + package_module_path = StringFormat("%s%c%s%c%s%c%s", GetWorkDir(), FILE_SEPARATOR, + "modules", FILE_SEPARATOR, "packages", FILE_SEPARATOR, + package_module->name); + } + + if (package_module->interpreter && !StringEqual(package_module->interpreter, "")) + { + *script_path = package_module_path; + *script_path_quoted = Path_GetQuoted(*script_path); + + if (strchr(package_module->interpreter, ' ') == NULL) + { + /* No spaces in the 'interpreter' string, easy! Just interpreter path given, no opts. */ + *exec_path = xstrdup(package_module->interpreter); + *script_exec_opts = NULL; + } + else + { + /* we need to split the interpreter command from the (potential) + * interpreter args */ + ArgGetExecutableAndArgs(package_module->interpreter, exec_path, script_exec_opts); + } + } + else + { + *exec_path = package_module_path; + *script_path = NULL; + *script_path_quoted = NULL; + *script_exec_opts = NULL; + } +} + +PackageModuleBody *PackageModuleBodyCopy(const PackageModuleBody *manager) +{ + assert(manager != NULL); + + PackageModuleBody *copy = xmalloc(sizeof(PackageModuleBody)); + copy->name = SafeStringDuplicate(manager->name); + copy->updates_ifelapsed = manager->updates_ifelapsed; + copy->installed_ifelapsed = manager->installed_ifelapsed; + copy->options = RlistCopy(manager->options); + copy->interpreter = SafeStringDuplicate(manager->interpreter); + copy->module_path = SafeStringDuplicate(manager->module_path); + return copy; +} + +void PackageModuleBodyDestroy(PackageModuleBody *manager) +{ + if (manager != NULL) + { + free(manager->name); + free(manager->interpreter); + free(manager->module_path); + RlistDestroy(manager->options); + free(manager); + } +} diff --git a/libpromises/package_module_wrapper.h b/libpromises/package_module_wrapper.h new file mode 100644 index 00000000000..365acd650fa --- /dev/null +++ b/libpromises/package_module_wrapper.h @@ -0,0 +1,104 @@ +/* + Copyright 2026 Northern.tech AS + + This file is part of CFEngine 3 - written and maintained by Northern.tech AS. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; version 3. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + + To the extent this program is licensed as part of the Enterprise + versions of CFEngine, the applicable Commercial Open Source License + (COSL) may apply to this file if you as a licensee so wish it. See + included file COSL.txt. +*/ + +#ifndef CFENGINE_PACKAGE_MODULE_WRAPPER_H +#define CFENGINE_PACKAGE_MODULE_WRAPPER_H + +#include + +#define PACKAGE_PROMISE_TERMINATION_CHECK_SEC 1 + +typedef struct +{ + char *name; + char *path; + char *script_path; + char *script_path_quoted; + char *script_exec_opts; + PackageModuleBody *package_module; + int supported_api_version; + int timeout_secs; /**< Timeout of each run of the package module */ +} PackageModuleWrapper; + +/** + * @brief Creates a wrapper to run a package module. + * + * Runs the package module once, to negotiate the API version. + * + * @param package_module The package module body, not copied, so it must + * outlive the wrapper + * @param timeout_secs The timeout of each run of the package module + * @return The new wrapper, to be destroyed with DeletePackageModuleWrapper(), + * or NULL if the package module can't be run or doesn't support API + * version 1 + */ +PackageModuleWrapper *NewPackageModuleWrapper(PackageModuleBody *package_module, + int timeout_secs); + +/** + * @brief Destroys a package module wrapper. + * + * @param wrapper The wrapper, may be NULL + */ +void DeletePackageModuleWrapper(PackageModuleWrapper *wrapper); + +/** + * @brief Runs the package module with a request. + * + * @param wrapper The wrapper of the package module + * @param args The arguments of the package module, e.g. "list-installed" + * @param request The request, written to the stdin of the package module + * @param[out] response The lines of the stdout of the package module, only + * set on success + * @return 0 on success, -1 if the package module couldn't be run or exited + * with a non-zero code + */ +int PackageWrapperCommunicate(const PackageModuleWrapper *wrapper, const char *args, + const char *request, Rlist **response); + +/** + * @brief Formats the options of a package module as request lines. + * + * @param options The options, may be NULL + * @return One "options=VALUE" line per option, or an empty string if there + * are none. To be freed by the caller. + */ +char *PackageModuleFormatOptions(Rlist *options); + +/** + * @brief Copies a package module body. + * + * @param manager The package module body + * @return The copy, to be destroyed with PackageModuleBodyDestroy() + */ +PackageModuleBody *PackageModuleBodyCopy(const PackageModuleBody *manager); + +/** + * @brief Destroys a package module body. + * + * @param manager The package module body, may be NULL + */ +void PackageModuleBodyDestroy(PackageModuleBody *manager); + +#endif /* CFENGINE_PACKAGE_MODULE_WRAPPER_H */ diff --git a/libpromises/pipes_unix.c b/libpromises/pipes_unix.c index a9aece3778f..7aef0a95184 100644 --- a/libpromises/pipes_unix.c +++ b/libpromises/pipes_unix.c @@ -32,6 +32,7 @@ #include #include #include +#include #include static bool CfSetuid(uid_t uid, gid_t gid); @@ -168,6 +169,10 @@ typedef struct static pid_t GenericCreatePipeAndFork(IOPipe *pipes) { + /* The request made by SetChildTimeOut() is for this call only, so that + * one abandoned by a failure below cannot time out the next child. */ + const int timeout = TakeChildTimeOut(); + for (int i = 0; i < 2; i++) { if (pipes[i].type && !PipeTypeIsOk(pipes[i].type)) @@ -240,6 +245,11 @@ static pid_t GenericCreatePipeAndFork(IOPipe *pipes) ALARM_PID = (pid != 0 ? pid : -1); + if (pid > 0) + { + StartTimeOutClock(timeout); + } + return pid; } diff --git a/libpromises/timeout.c b/libpromises/timeout.c index 5ad63baf859..0bb16ab2de5 100644 --- a/libpromises/timeout.c +++ b/libpromises/timeout.c @@ -26,13 +26,61 @@ #include #include +/* Seconds requested by SetChildTimeOut(), waiting for cf_popen*() to publish + * the pid of the child they are meant for. */ +static int TIMEOUT_PENDING = 0; /* GLOBAL_X */ + void SetTimeOut(int timeout) { ALARM_PID = -1; + TIMEOUT_PENDING = 0; signal(SIGALRM, (void *) TimeOut); alarm(timeout); } +void SetChildTimeOut(int timeout) +{ +#if defined(__MINGW32__) || defined(__CYGWIN__) + /* pipes_unix.c is not built here, so nothing would start the clock. */ + SetTimeOut(timeout); +#else + /* A timer leaked by an earlier caller must not fire as ours. */ + SetTimeOut(0); + TIMEOUT_PENDING = timeout; +#endif +} + +void DisarmTimeOut(void) +{ + TIMEOUT_PENDING = 0; + alarm(0); + signal(SIGALRM, SIG_DFL); +} + +int TakeChildTimeOut(void) +{ + const int seconds = TIMEOUT_PENDING; + TIMEOUT_PENDING = 0; + return seconds; +} + +void StartTimeOutClock(int seconds) +{ +#if !defined(__MINGW32__) && !defined(__CYGWIN__) + /* Callers that disarm their timeout restore SIG_DFL; arming now would + * then have SIGALRM terminate us. */ + struct sigaction current; + if ((seconds > 0) + && (sigaction(SIGALRM, NULL, ¤t) == 0) + && ((void *) current.sa_handler == (void *) TimeOut)) + { + alarm(seconds); + } +#else + (void) seconds; +#endif +} + /*************************************************************************/ void TimeOut() diff --git a/libpromises/timeout.h b/libpromises/timeout.h index 33af0f7f056..bd116c33144 100644 --- a/libpromises/timeout.h +++ b/libpromises/timeout.h @@ -25,7 +25,19 @@ #ifndef CFENGINE_TIMEOUT_H #define CFENGINE_TIMEOUT_H +/* Arm a timeout now, for callers that do not fork. */ void SetTimeOut(int timeout); +/* Arm a timeout for the next child forked by cf_popen*(). The clock only + * starts once the child's pid is in ALARM_PID, so an alarm can never expire + * with no process to terminate. A request is good for one cf_popen*() call, + * whether or not it forks, and is dropped by DisarmTimeOut(). */ +void SetChildTimeOut(int timeout); +/* Cancel whatever SetTimeOut() or SetChildTimeOut() requested. */ +void DisarmTimeOut(void); +/* Used by cf_popen*(): take the request for this child, then start its clock + * in the parent once the child's pid is published. */ +int TakeChildTimeOut(void); +void StartTimeOutClock(int seconds); void TimeOut(void); time_t SetReferenceTime(void); diff --git a/tests/Makefile.am b/tests/Makefile.am index 0f80e566120..8603fc839e3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -21,4 +21,4 @@ # (COSL) may apply to this file if you as a licensee so wish it. See # included file COSL.txt. # -SUBDIRS = unit load acceptance static-check valgrind-check asan-check +SUBDIRS = unit load acceptance reactor static-check valgrind-check asan-check diff --git a/tests/acceptance/00_basics/01_compiler/string_contexts.cf b/tests/acceptance/00_basics/01_compiler/string_contexts.error.pass.cf similarity index 93% rename from tests/acceptance/00_basics/01_compiler/string_contexts.cf rename to tests/acceptance/00_basics/01_compiler/string_contexts.error.pass.cf index 0bf5503ac25..3d3f99d453b 100644 --- a/tests/acceptance/00_basics/01_compiler/string_contexts.cf +++ b/tests/acceptance/00_basics/01_compiler/string_contexts.error.pass.cf @@ -1,5 +1,9 @@ # fixes https://dev.cfengine.com/issues/2504 # using variables in string contexts +# +# This is an .error.pass.cf test, because the 'cf engine' class context +# (words separated by whitespace without an operator) is invalid on +# purpose and logs an error. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/00_basics/01_compiler/string_contexts.cf.expected.json b/tests/acceptance/00_basics/01_compiler/string_contexts.error.pass.cf.expected.json similarity index 100% rename from tests/acceptance/00_basics/01_compiler/string_contexts.cf.expected.json rename to tests/acceptance/00_basics/01_compiler/string_contexts.error.pass.cf.expected.json diff --git a/tests/acceptance/00_basics/04_bundles/wrong-bundle-type.cf b/tests/acceptance/00_basics/04_bundles/wrong-bundle-type.cf index db2641cbe6d..e1e1e57b980 100644 --- a/tests/acceptance/00_basics/04_bundles/wrong-bundle-type.cf +++ b/tests/acceptance/00_basics/04_bundles/wrong-bundle-type.cf @@ -20,6 +20,10 @@ bundle agent test "${sys.cf_agent} -Kf $(this.promise_dirname)/wrong-bundle-type.sub.x.cf", "noshell" ); + + # Indent the output in the DEBUG report, so that errors printed by the + # sub-agent are not mistaken for errors of this test by testall + "subout_indented" string => regex_replace($(subout), "^", " | ", "gm"); } bundle agent check @@ -29,7 +33,7 @@ bundle agent check reports: DEBUG:: - "$(test.subout)"; + "$(test.subout_indented)"; ok:: "$(this.promise_filename) Pass"; diff --git a/tests/acceptance/00_basics/def.json/augments.cf b/tests/acceptance/00_basics/def_json/augments.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/augments.cf rename to tests/acceptance/00_basics/def_json/augments.cf diff --git a/tests/acceptance/00_basics/def.json/augments.cf.augmenter.json b/tests/acceptance/00_basics/def_json/augments.cf.augmenter.json similarity index 100% rename from tests/acceptance/00_basics/def.json/augments.cf.augmenter.json rename to tests/acceptance/00_basics/def_json/augments.cf.augmenter.json diff --git a/tests/acceptance/00_basics/def.json/augments.cf.json b/tests/acceptance/00_basics/def_json/augments.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/augments.cf.json rename to tests/acceptance/00_basics/def_json/augments.cf.json diff --git a/tests/acceptance/00_basics/def.json/augments_classes/augments_classes.cf b/tests/acceptance/00_basics/def_json/augments_classes/augments_classes.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/augments_classes/augments_classes.cf rename to tests/acceptance/00_basics/def_json/augments_classes/augments_classes.cf diff --git a/tests/acceptance/00_basics/def.json/augments_classes/def.json b/tests/acceptance/00_basics/def_json/augments_classes/def.json similarity index 100% rename from tests/acceptance/00_basics/def.json/augments_classes/def.json rename to tests/acceptance/00_basics/def_json/augments_classes/def.json diff --git a/tests/acceptance/00_basics/def.json/augments_order.cf b/tests/acceptance/00_basics/def_json/augments_order.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/augments_order.cf rename to tests/acceptance/00_basics/def_json/augments_order.cf diff --git a/tests/acceptance/00_basics/def.json/augments_order.cf.augmenter.json b/tests/acceptance/00_basics/def_json/augments_order.cf.augmenter.json similarity index 100% rename from tests/acceptance/00_basics/def.json/augments_order.cf.augmenter.json rename to tests/acceptance/00_basics/def_json/augments_order.cf.augmenter.json diff --git a/tests/acceptance/00_basics/def.json/augments_order.cf.json b/tests/acceptance/00_basics/def_json/augments_order.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/augments_order.cf.json rename to tests/acceptance/00_basics/def_json/augments_order.cf.json diff --git a/tests/acceptance/00_basics/def.json/bad-json.cf b/tests/acceptance/00_basics/def_json/bad-json.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/bad-json.cf rename to tests/acceptance/00_basics/def_json/bad-json.cf diff --git a/tests/acceptance/00_basics/def.json/bad-json.cf.x.json b/tests/acceptance/00_basics/def_json/bad-json.cf.x.json similarity index 100% rename from tests/acceptance/00_basics/def.json/bad-json.cf.x.json rename to tests/acceptance/00_basics/def_json/bad-json.cf.x.json diff --git a/tests/acceptance/00_basics/def.json/class_from_classexpression.cf b/tests/acceptance/00_basics/def_json/class_from_classexpression.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/class_from_classexpression.cf rename to tests/acceptance/00_basics/def_json/class_from_classexpression.cf diff --git a/tests/acceptance/00_basics/def.json/class_from_classexpression.cf.json b/tests/acceptance/00_basics/def_json/class_from_classexpression.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/class_from_classexpression.cf.json rename to tests/acceptance/00_basics/def_json/class_from_classexpression.cf.json diff --git a/tests/acceptance/00_basics/def.json/classes.cf b/tests/acceptance/00_basics/def_json/classes.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/classes.cf rename to tests/acceptance/00_basics/def_json/classes.cf diff --git a/tests/acceptance/00_basics/def.json/classes.cf.json b/tests/acceptance/00_basics/def_json/classes.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/classes.cf.json rename to tests/acceptance/00_basics/def_json/classes.cf.json diff --git a/tests/acceptance/00_basics/def.json/classes_with_ns.cf b/tests/acceptance/00_basics/def_json/classes_with_ns.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/classes_with_ns.cf rename to tests/acceptance/00_basics/def_json/classes_with_ns.cf diff --git a/tests/acceptance/00_basics/def.json/classes_with_ns.cf.json b/tests/acceptance/00_basics/def_json/classes_with_ns.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/classes_with_ns.cf.json rename to tests/acceptance/00_basics/def_json/classes_with_ns.cf.json diff --git a/tests/acceptance/00_basics/def.json/classes_with_ns_tags.cf b/tests/acceptance/00_basics/def_json/classes_with_ns_tags.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/classes_with_ns_tags.cf rename to tests/acceptance/00_basics/def_json/classes_with_ns_tags.cf diff --git a/tests/acceptance/00_basics/def.json/classes_with_ns_tags.cf.json b/tests/acceptance/00_basics/def_json/classes_with_ns_tags.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/classes_with_ns_tags.cf.json rename to tests/acceptance/00_basics/def_json/classes_with_ns_tags.cf.json diff --git a/tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/both-can-define-vars.cf b/tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/both-can-define-vars.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/both-can-define-vars.cf rename to tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/both-can-define-vars.cf diff --git a/tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/def.json b/tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/def.json similarity index 100% rename from tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/def.json rename to tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/def.json diff --git a/tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/def.json b/tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/def.json similarity index 100% rename from tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/def.json rename to tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/def.json diff --git a/tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/variables-wins.cf b/tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/variables-wins.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/variables-wins.cf rename to tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins-variables-listed-first/variables-wins.cf diff --git a/tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins.cf b/tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/concurrent-vars-and-variables-keys/variables-wins.cf rename to tests/acceptance/00_basics/def_json/concurrent-vars-and-variables-keys/variables-wins.cf diff --git a/tests/acceptance/00_basics/def.json/def.json b/tests/acceptance/00_basics/def_json/def.json similarity index 100% rename from tests/acceptance/00_basics/def.json/def.json rename to tests/acceptance/00_basics/def_json/def.json diff --git a/tests/acceptance/00_basics/def.json/def_preferred.json/def.json b/tests/acceptance/00_basics/def_json/def_preferred.json/def.json similarity index 100% rename from tests/acceptance/00_basics/def.json/def_preferred.json/def.json rename to tests/acceptance/00_basics/def_json/def_preferred.json/def.json diff --git a/tests/acceptance/00_basics/def.json/def_preferred.json/def_preferred.cf b/tests/acceptance/00_basics/def_json/def_preferred.json/def_preferred.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/def_preferred.json/def_preferred.cf rename to tests/acceptance/00_basics/def_json/def_preferred.json/def_preferred.cf diff --git a/tests/acceptance/00_basics/def.json/def_preferred.json/def_preferred.json b/tests/acceptance/00_basics/def_json/def_preferred.json/def_preferred.json similarity index 100% rename from tests/acceptance/00_basics/def.json/def_preferred.json/def_preferred.json rename to tests/acceptance/00_basics/def_json/def_preferred.json/def_preferred.json diff --git a/tests/acceptance/00_basics/def.json/inputs.cf b/tests/acceptance/00_basics/def_json/inputs.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/inputs.cf rename to tests/acceptance/00_basics/def_json/inputs.cf diff --git a/tests/acceptance/00_basics/def.json/inputs.cf.json b/tests/acceptance/00_basics/def_json/inputs.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/inputs.cf.json rename to tests/acceptance/00_basics/def_json/inputs.cf.json diff --git a/tests/acceptance/00_basics/def.json/json-inputs-not-array.cf b/tests/acceptance/00_basics/def_json/json-inputs-not-array.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/json-inputs-not-array.cf rename to tests/acceptance/00_basics/def_json/json-inputs-not-array.cf diff --git a/tests/acceptance/00_basics/def.json/json-inputs-not-array.cf.json b/tests/acceptance/00_basics/def_json/json-inputs-not-array.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/json-inputs-not-array.cf.json rename to tests/acceptance/00_basics/def_json/json-inputs-not-array.cf.json diff --git a/tests/acceptance/00_basics/def.json/json-inputs-not-auto-applied.cf b/tests/acceptance/00_basics/def_json/json-inputs-not-auto-applied.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/json-inputs-not-auto-applied.cf rename to tests/acceptance/00_basics/def_json/json-inputs-not-auto-applied.cf diff --git a/tests/acceptance/00_basics/def.json/json-inputs-not-auto-applied.cf.json b/tests/acceptance/00_basics/def_json/json-inputs-not-auto-applied.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/json-inputs-not-auto-applied.cf.json rename to tests/acceptance/00_basics/def_json/json-inputs-not-auto-applied.cf.json diff --git a/tests/acceptance/00_basics/def.json/policy_server_classes.cf b/tests/acceptance/00_basics/def_json/policy_server_classes.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/policy_server_classes.cf rename to tests/acceptance/00_basics/def_json/policy_server_classes.cf diff --git a/tests/acceptance/00_basics/def.json/policy_server_classes.cf.json b/tests/acceptance/00_basics/def_json/policy_server_classes.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/policy_server_classes.cf.json rename to tests/acceptance/00_basics/def_json/policy_server_classes.cf.json diff --git a/tests/acceptance/00_basics/def.json/show_vars_JSON.cf b/tests/acceptance/00_basics/def_json/show_vars_JSON.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/show_vars_JSON.cf rename to tests/acceptance/00_basics/def_json/show_vars_JSON.cf diff --git a/tests/acceptance/00_basics/def.json/show_vars_JSON.cf.json b/tests/acceptance/00_basics/def_json/show_vars_JSON.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/show_vars_JSON.cf.json rename to tests/acceptance/00_basics/def_json/show_vars_JSON.cf.json diff --git a/tests/acceptance/00_basics/def.json/state.cf b/tests/acceptance/00_basics/def_json/state.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/state.cf rename to tests/acceptance/00_basics/def_json/state.cf diff --git a/tests/acceptance/00_basics/def.json/state.cf.expected.json b/tests/acceptance/00_basics/def_json/state.cf.expected.json similarity index 100% rename from tests/acceptance/00_basics/def.json/state.cf.expected.json rename to tests/acceptance/00_basics/def_json/state.cf.expected.json diff --git a/tests/acceptance/00_basics/def.json/variables.cf b/tests/acceptance/00_basics/def_json/variables.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/variables.cf rename to tests/acceptance/00_basics/def_json/variables.cf diff --git a/tests/acceptance/00_basics/def.json/variables.cf.json b/tests/acceptance/00_basics/def_json/variables.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/variables.cf.json rename to tests/acceptance/00_basics/def_json/variables.cf.json diff --git a/tests/acceptance/00_basics/def.json/variables_bare_array.cf b/tests/acceptance/00_basics/def_json/variables_bare_array.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_bare_array.cf rename to tests/acceptance/00_basics/def_json/variables_bare_array.cf diff --git a/tests/acceptance/00_basics/def.json/variables_bare_array.cf.json b/tests/acceptance/00_basics/def_json/variables_bare_array.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_bare_array.cf.json rename to tests/acceptance/00_basics/def_json/variables_bare_array.cf.json diff --git a/tests/acceptance/00_basics/def.json/variables_bare_string.cf b/tests/acceptance/00_basics/def_json/variables_bare_string.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_bare_string.cf rename to tests/acceptance/00_basics/def_json/variables_bare_string.cf diff --git a/tests/acceptance/00_basics/def.json/variables_bare_string.cf.json b/tests/acceptance/00_basics/def_json/variables_bare_string.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_bare_string.cf.json rename to tests/acceptance/00_basics/def_json/variables_bare_string.cf.json diff --git a/tests/acceptance/00_basics/def.json/variables_with_bundle.cf b/tests/acceptance/00_basics/def_json/variables_with_bundle.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_with_bundle.cf rename to tests/acceptance/00_basics/def_json/variables_with_bundle.cf diff --git a/tests/acceptance/00_basics/def.json/variables_with_bundle.cf.json b/tests/acceptance/00_basics/def_json/variables_with_bundle.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_with_bundle.cf.json rename to tests/acceptance/00_basics/def_json/variables_with_bundle.cf.json diff --git a/tests/acceptance/00_basics/def.json/variables_with_ns.cf b/tests/acceptance/00_basics/def_json/variables_with_ns.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_with_ns.cf rename to tests/acceptance/00_basics/def_json/variables_with_ns.cf diff --git a/tests/acceptance/00_basics/def.json/variables_with_ns.cf.json b/tests/acceptance/00_basics/def_json/variables_with_ns.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/variables_with_ns.cf.json rename to tests/acceptance/00_basics/def_json/variables_with_ns.cf.json diff --git a/tests/acceptance/00_basics/def.json/vars.cf b/tests/acceptance/00_basics/def_json/vars.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/vars.cf rename to tests/acceptance/00_basics/def_json/vars.cf diff --git a/tests/acceptance/00_basics/def.json/vars.cf.json b/tests/acceptance/00_basics/def_json/vars.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/vars.cf.json rename to tests/acceptance/00_basics/def_json/vars.cf.json diff --git a/tests/acceptance/00_basics/def.json/vars_with_bundle.cf b/tests/acceptance/00_basics/def_json/vars_with_bundle.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/vars_with_bundle.cf rename to tests/acceptance/00_basics/def_json/vars_with_bundle.cf diff --git a/tests/acceptance/00_basics/def.json/vars_with_bundle.cf.json b/tests/acceptance/00_basics/def_json/vars_with_bundle.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/vars_with_bundle.cf.json rename to tests/acceptance/00_basics/def_json/vars_with_bundle.cf.json diff --git a/tests/acceptance/00_basics/def.json/vars_with_ns.cf b/tests/acceptance/00_basics/def_json/vars_with_ns.cf similarity index 100% rename from tests/acceptance/00_basics/def.json/vars_with_ns.cf rename to tests/acceptance/00_basics/def_json/vars_with_ns.cf diff --git a/tests/acceptance/00_basics/def.json/vars_with_ns.cf.json b/tests/acceptance/00_basics/def_json/vars_with_ns.cf.json similarity index 100% rename from tests/acceptance/00_basics/def.json/vars_with_ns.cf.json rename to tests/acceptance/00_basics/def_json/vars_with_ns.cf.json diff --git a/tests/acceptance/01_vars/01_basic/013.cf b/tests/acceptance/01_vars/01_basic/013.error.pass.cf similarity index 88% rename from tests/acceptance/01_vars/01_basic/013.cf rename to tests/acceptance/01_vars/01_basic/013.error.pass.cf index 4bf5fdfc56c..87d40432ef9 100644 --- a/tests/acceptance/01_vars/01_basic/013.cf +++ b/tests/acceptance/01_vars/01_basic/013.error.pass.cf @@ -2,6 +2,10 @@ # # Test that 'classes' body works on 'vars' promises # +# This is an .error.pass.cf test, because execresult() of the +# non-existent '/nonexisting' fails on purpose and logs errors, to test +# that repair_failed classes are set. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.cf b/tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.error.pass.cf similarity index 93% rename from tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.cf rename to tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.error.pass.cf index ea9f92b962f..aba335f5e66 100644 --- a/tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.cf +++ b/tests/acceptance/01_vars/01_basic/can_not_define_variables_in_remote_bundles.error.pass.cf @@ -2,6 +2,10 @@ # # Test that it is invalid to define variables in remote bundles # +# This is an .error.pass.cf test, because defining a variable in a +# remote bundle is rejected with errors, which is what this test checks +# (CFE-1915). +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/01_basic/this_promiser_with_readjson.cf b/tests/acceptance/01_vars/01_basic/this_promiser_with_readjson.cf index 60cfff7563f..4d7d32a549b 100644 --- a/tests/acceptance/01_vars/01_basic/this_promiser_with_readjson.cf +++ b/tests/acceptance/01_vars/01_basic/this_promiser_with_readjson.cf @@ -26,7 +26,8 @@ bundle agent test { vars: "cf_promises_validated" - data => readjson("$(G.testfile).$(this.promiser)", 1K); + data => readjson("$(G.testfile).$(this.promiser)", 1K), + if => fileexists("$(G.testfile).$(this.promiser)"); "printable" string => format("%S", "cf_promises_validated"); diff --git a/tests/acceptance/01_vars/02_functions/006.cf b/tests/acceptance/01_vars/02_functions/006.error.pass.cf similarity index 91% rename from tests/acceptance/01_vars/02_functions/006.cf rename to tests/acceptance/01_vars/02_functions/006.error.pass.cf index 62914fdb892..63d85cbeb29 100644 --- a/tests/acceptance/01_vars/02_functions/006.cf +++ b/tests/acceptance/01_vars/02_functions/006.error.pass.cf @@ -2,6 +2,9 @@ # # Test diskfree() # +# This is an .error.pass.cf test, because diskfree() is called on the +# non-existent path '/lkjqeroiu' on purpose and logs an error. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/011.cf b/tests/acceptance/01_vars/02_functions/011.cf index 0405b37d51a..b9a7aebe1e6 100644 --- a/tests/acceptance/01_vars/02_functions/011.cf +++ b/tests/acceptance/01_vars/02_functions/011.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/012.cf b/tests/acceptance/01_vars/02_functions/012.cf index 6ace98654cb..7ce4aa0c051 100644 --- a/tests/acceptance/01_vars/02_functions/012.cf +++ b/tests/acceptance/01_vars/02_functions/012.cf @@ -38,7 +38,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/013.cf b/tests/acceptance/01_vars/02_functions/013.cf index 1708bbaddc0..37821656b07 100644 --- a/tests/acceptance/01_vars/02_functions/013.cf +++ b/tests/acceptance/01_vars/02_functions/013.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/014.cf b/tests/acceptance/01_vars/02_functions/014.cf index 716e8873390..ba82a13c2bc 100644 --- a/tests/acceptance/01_vars/02_functions/014.cf +++ b/tests/acceptance/01_vars/02_functions/014.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\.", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\.", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/015.cf b/tests/acceptance/01_vars/02_functions/015.cf index c515ba037d5..9071d657bdc 100644 --- a/tests/acceptance/01_vars/02_functions/015.cf +++ b/tests/acceptance/01_vars/02_functions/015.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/016.cf b/tests/acceptance/01_vars/02_functions/016.cf index 0688544a875..c646ecc710f 100644 --- a/tests/acceptance/01_vars/02_functions/016.cf +++ b/tests/acceptance/01_vars/02_functions/016.cf @@ -38,7 +38,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/017.cf b/tests/acceptance/01_vars/02_functions/017.cf index b9cfa15a1bc..3a61a27d7e5 100644 --- a/tests/acceptance/01_vars/02_functions/017.cf +++ b/tests/acceptance/01_vars/02_functions/017.cf @@ -38,7 +38,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/018.cf b/tests/acceptance/01_vars/02_functions/018.cf index d72de43df26..29a8a437490 100644 --- a/tests/acceptance/01_vars/02_functions/018.cf +++ b/tests/acceptance/01_vars/02_functions/018.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/019.cf b/tests/acceptance/01_vars/02_functions/019.cf index 6b1af5b6871..ce2cd9145e0 100644 --- a/tests/acceptance/01_vars/02_functions/019.cf +++ b/tests/acceptance/01_vars/02_functions/019.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "X", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "X", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/020.cf b/tests/acceptance/01_vars/02_functions/020.cf index cc59c326708..2429c9eb059 100644 --- a/tests/acceptance/01_vars/02_functions/020.cf +++ b/tests/acceptance/01_vars/02_functions/020.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", "\s", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", "\s", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/021.cf b/tests/acceptance/01_vars/02_functions/021.cf index 8032a987cc5..1e86c7e129e 100644 --- a/tests/acceptance/01_vars/02_functions/021.cf +++ b/tests/acceptance/01_vars/02_functions/021.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" slist => readstringlist("$(G.testfile)", "NoComment", ",", 5, 100); + "nums" + slist => readstringlist("$(G.testfile)", "NoComment", ",", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/024.cf b/tests/acceptance/01_vars/02_functions/024.cf index 74df030785c..fa2da9fc34b 100644 --- a/tests/acceptance/01_vars/02_functions/024.cf +++ b/tests/acceptance/01_vars/02_functions/024.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/025.cf b/tests/acceptance/01_vars/02_functions/025.cf index 3d6351e52c1..b138c0f236c 100644 --- a/tests/acceptance/01_vars/02_functions/025.cf +++ b/tests/acceptance/01_vars/02_functions/025.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/026.cf b/tests/acceptance/01_vars/02_functions/026.cf index 50b0cf1ec32..20a3158df7a 100644 --- a/tests/acceptance/01_vars/02_functions/026.cf +++ b/tests/acceptance/01_vars/02_functions/026.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/027.cf b/tests/acceptance/01_vars/02_functions/027.cf index 55a80306d4f..84930ea5a8a 100644 --- a/tests/acceptance/01_vars/02_functions/027.cf +++ b/tests/acceptance/01_vars/02_functions/027.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\.", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\.", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/028.cf b/tests/acceptance/01_vars/02_functions/028.cf index 322b4fa70a3..7ac8f774d9b 100644 --- a/tests/acceptance/01_vars/02_functions/028.cf +++ b/tests/acceptance/01_vars/02_functions/028.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/029.cf b/tests/acceptance/01_vars/02_functions/029.cf index 3a746744265..09ff4b92689 100644 --- a/tests/acceptance/01_vars/02_functions/029.cf +++ b/tests/acceptance/01_vars/02_functions/029.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/030.cf b/tests/acceptance/01_vars/02_functions/030.cf index c5e438f364f..83d89216bf1 100644 --- a/tests/acceptance/01_vars/02_functions/030.cf +++ b/tests/acceptance/01_vars/02_functions/030.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/031.cf b/tests/acceptance/01_vars/02_functions/031.cf index d40f59a1035..2fb7d1e63f9 100644 --- a/tests/acceptance/01_vars/02_functions/031.cf +++ b/tests/acceptance/01_vars/02_functions/031.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/032.cf b/tests/acceptance/01_vars/02_functions/032.cf index 742c4e4f0b2..2827c7dd1b5 100644 --- a/tests/acceptance/01_vars/02_functions/032.cf +++ b/tests/acceptance/01_vars/02_functions/032.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "X", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "X", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/033.cf b/tests/acceptance/01_vars/02_functions/033.cf index e90f323f897..59680fe18f8 100644 --- a/tests/acceptance/01_vars/02_functions/033.cf +++ b/tests/acceptance/01_vars/02_functions/033.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", "\s", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", "\s", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/034.cf b/tests/acceptance/01_vars/02_functions/034.cf index e9d1dbfafcf..7ac2dcfaef1 100644 --- a/tests/acceptance/01_vars/02_functions/034.cf +++ b/tests/acceptance/01_vars/02_functions/034.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" ilist => readintlist("$(G.testfile)", "NoComment", ",", 5, 100); + "nums" + ilist => readintlist("$(G.testfile)", "NoComment", ",", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/037.cf b/tests/acceptance/01_vars/02_functions/037.cf index 770c373c58c..860710e89d7 100644 --- a/tests/acceptance/01_vars/02_functions/037.cf +++ b/tests/acceptance/01_vars/02_functions/037.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/038.cf b/tests/acceptance/01_vars/02_functions/038.cf index 26eabcf4638..303a3915e56 100644 --- a/tests/acceptance/01_vars/02_functions/038.cf +++ b/tests/acceptance/01_vars/02_functions/038.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/039.cf b/tests/acceptance/01_vars/02_functions/039.cf index 53dd4046be4..0e67007ee63 100644 --- a/tests/acceptance/01_vars/02_functions/039.cf +++ b/tests/acceptance/01_vars/02_functions/039.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/040.cf b/tests/acceptance/01_vars/02_functions/040.cf index 3ba0f3c2dba..5b8cee5c36a 100644 --- a/tests/acceptance/01_vars/02_functions/040.cf +++ b/tests/acceptance/01_vars/02_functions/040.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\.", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\.", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/041.cf b/tests/acceptance/01_vars/02_functions/041.cf index 61608635fd5..99c50764f8e 100644 --- a/tests/acceptance/01_vars/02_functions/041.cf +++ b/tests/acceptance/01_vars/02_functions/041.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/042.cf b/tests/acceptance/01_vars/02_functions/042.cf index ee96964fae0..bbd55f03c90 100644 --- a/tests/acceptance/01_vars/02_functions/042.cf +++ b/tests/acceptance/01_vars/02_functions/042.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/043.cf b/tests/acceptance/01_vars/02_functions/043.cf index 7a9427aed85..27fcc3ddfb3 100644 --- a/tests/acceptance/01_vars/02_functions/043.cf +++ b/tests/acceptance/01_vars/02_functions/043.cf @@ -37,7 +37,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/044.cf b/tests/acceptance/01_vars/02_functions/044.cf index 6b0e85e0c7f..81690b5133d 100644 --- a/tests/acceptance/01_vars/02_functions/044.cf +++ b/tests/acceptance/01_vars/02_functions/044.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s+", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/045.cf b/tests/acceptance/01_vars/02_functions/045.cf index 0cddcd75623..5fe7c79764e 100644 --- a/tests/acceptance/01_vars/02_functions/045.cf +++ b/tests/acceptance/01_vars/02_functions/045.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "X", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "X", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/046.cf b/tests/acceptance/01_vars/02_functions/046.cf index 41a076f5609..895e665f796 100644 --- a/tests/acceptance/01_vars/02_functions/046.cf +++ b/tests/acceptance/01_vars/02_functions/046.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\s", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\s", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/047.cf b/tests/acceptance/01_vars/02_functions/047.cf index c8c73e774ed..456b7637def 100644 --- a/tests/acceptance/01_vars/02_functions/047.cf +++ b/tests/acceptance/01_vars/02_functions/047.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", ",", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", ",", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/050.cf b/tests/acceptance/01_vars/02_functions/050.cf index b0d9b0b64db..659225ef5d5 100644 --- a/tests/acceptance/01_vars/02_functions/050.cf +++ b/tests/acceptance/01_vars/02_functions/050.cf @@ -36,7 +36,10 @@ body delete init_delete bundle agent test { vars: - "nums" rlist => readreallist("$(G.testfile)", "NoComment", "\.", 5, 100); + "nums" + rlist => readreallist("$(G.testfile)", "NoComment", "\.", 5, 100), + if => fileexists("$(G.testfile)"); + "sum" real => sum("nums"); } diff --git a/tests/acceptance/01_vars/02_functions/400.cf b/tests/acceptance/01_vars/02_functions/400.cf index bc6edf6d783..da20512b37c 100644 --- a/tests/acceptance/01_vars/02_functions/400.cf +++ b/tests/acceptance/01_vars/02_functions/400.cf @@ -45,7 +45,8 @@ bundle agent test "cnt" int => readstringarray( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/401.cf b/tests/acceptance/01_vars/02_functions/401.cf index fd2f2da1be3..f06d4999a61 100644 --- a/tests/acceptance/01_vars/02_functions/401.cf +++ b/tests/acceptance/01_vars/02_functions/401.cf @@ -45,7 +45,8 @@ bundle agent test "cnt" int => readstringarray( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/410.cf b/tests/acceptance/01_vars/02_functions/410.cf index 2937dd75310..3a0fa32e4bf 100644 --- a/tests/acceptance/01_vars/02_functions/410.cf +++ b/tests/acceptance/01_vars/02_functions/410.cf @@ -48,7 +48,10 @@ body delete init_delete bundle agent test { vars: - "cnt" int => readstringarray("ary", "$(G.testfile)", "^#.*", ":+", 10, 14); + "cnt" + int => readstringarray("ary", "$(G.testfile)", "^#.*", ":+", 10, 14), + if => fileexists("$(G.testfile)"); + "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/420.cf b/tests/acceptance/01_vars/02_functions/420.cf index 634072f50f1..fb6947473ea 100644 --- a/tests/acceptance/01_vars/02_functions/420.cf +++ b/tests/acceptance/01_vars/02_functions/420.cf @@ -42,7 +42,9 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); "cnt" int => parsestringarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); diff --git a/tests/acceptance/01_vars/02_functions/421.cf b/tests/acceptance/01_vars/02_functions/421.cf index 9891f76758a..d571a65a372 100644 --- a/tests/acceptance/01_vars/02_functions/421.cf +++ b/tests/acceptance/01_vars/02_functions/421.cf @@ -42,7 +42,9 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); "cnt" int => parsestringarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); diff --git a/tests/acceptance/01_vars/02_functions/440.cf b/tests/acceptance/01_vars/02_functions/440.cf index 3ffe141d7b8..93c2839d1c8 100644 --- a/tests/acceptance/01_vars/02_functions/440.cf +++ b/tests/acceptance/01_vars/02_functions/440.cf @@ -45,7 +45,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/441.cf b/tests/acceptance/01_vars/02_functions/441.cf index 3a71cff5cd7..639d1e48cc5 100644 --- a/tests/acceptance/01_vars/02_functions/441.cf +++ b/tests/acceptance/01_vars/02_functions/441.cf @@ -45,7 +45,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/442.cf b/tests/acceptance/01_vars/02_functions/442.cf index 284a6f7357c..bd9d0ae93fb 100644 --- a/tests/acceptance/01_vars/02_functions/442.cf +++ b/tests/acceptance/01_vars/02_functions/442.cf @@ -46,7 +46,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "4"; } diff --git a/tests/acceptance/01_vars/02_functions/443.cf b/tests/acceptance/01_vars/02_functions/443.cf index 5663b905fb1..ef4bd364ef4 100644 --- a/tests/acceptance/01_vars/02_functions/443.cf +++ b/tests/acceptance/01_vars/02_functions/443.cf @@ -53,7 +53,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "4"; } diff --git a/tests/acceptance/01_vars/02_functions/444.cf b/tests/acceptance/01_vars/02_functions/444.cf index 5e384aabbd9..2a138207d9f 100644 --- a/tests/acceptance/01_vars/02_functions/444.cf +++ b/tests/acceptance/01_vars/02_functions/444.cf @@ -51,7 +51,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "9"; } diff --git a/tests/acceptance/01_vars/02_functions/445.cf b/tests/acceptance/01_vars/02_functions/445.cf index 82be099cc04..89b95d24738 100644 --- a/tests/acceptance/01_vars/02_functions/445.cf +++ b/tests/acceptance/01_vars/02_functions/445.cf @@ -54,7 +54,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 10, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "9"; } diff --git a/tests/acceptance/01_vars/02_functions/447.cf b/tests/acceptance/01_vars/02_functions/447.cf index 517919c7918..43379594bbf 100644 --- a/tests/acceptance/01_vars/02_functions/447.cf +++ b/tests/acceptance/01_vars/02_functions/447.cf @@ -48,7 +48,10 @@ body delete init_delete bundle agent test { vars: - "cnt" int => readstringarrayidx("ary", "$(G.testfile)", "", ":", 10, 1000); + "cnt" + int => readstringarrayidx("ary", "$(G.testfile)", "", ":", 10, 1000), + if => fileexists("$(G.testfile)"); + "num" int => "9"; } diff --git a/tests/acceptance/01_vars/02_functions/450.cf b/tests/acceptance/01_vars/02_functions/450.cf index c850c569dae..d686c41c1db 100644 --- a/tests/acceptance/01_vars/02_functions/450.cf +++ b/tests/acceptance/01_vars/02_functions/450.cf @@ -49,7 +49,8 @@ bundle agent test { vars: "cnt" - int => readstringarrayidx("ary", "$(G.testfile)", "^#.*", ":+", 10, 14); + int => readstringarrayidx("ary", "$(G.testfile)", "^#.*", ":+", 10, 14), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/500.cf b/tests/acceptance/01_vars/02_functions/500.cf index 205241c3089..01148598230 100644 --- a/tests/acceptance/01_vars/02_functions/500.cf +++ b/tests/acceptance/01_vars/02_functions/500.cf @@ -42,7 +42,8 @@ bundle agent test { vars: "cnt" - int => readintarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000); + int => readintarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/501.cf b/tests/acceptance/01_vars/02_functions/501.cf index 7c3c982d76e..ddaba1ac6e5 100644 --- a/tests/acceptance/01_vars/02_functions/501.cf +++ b/tests/acceptance/01_vars/02_functions/501.cf @@ -42,7 +42,8 @@ bundle agent test { vars: "cnt" - int => readintarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000); + int => readintarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/520.cf b/tests/acceptance/01_vars/02_functions/520.cf index b54fc402132..b592f86c158 100644 --- a/tests/acceptance/01_vars/02_functions/520.cf +++ b/tests/acceptance/01_vars/02_functions/520.cf @@ -41,7 +41,10 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); + "cnt" int => parseintarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/521.cf b/tests/acceptance/01_vars/02_functions/521.cf index 12424b904fc..79bc5877efe 100644 --- a/tests/acceptance/01_vars/02_functions/521.cf +++ b/tests/acceptance/01_vars/02_functions/521.cf @@ -41,7 +41,10 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); + "cnt" int => parseintarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/600.cf b/tests/acceptance/01_vars/02_functions/600.cf index fb48a433696..b402e6dd99f 100644 --- a/tests/acceptance/01_vars/02_functions/600.cf +++ b/tests/acceptance/01_vars/02_functions/600.cf @@ -42,7 +42,8 @@ bundle agent test { vars: "cnt" - int => readrealarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000); + int => readrealarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/601.cf b/tests/acceptance/01_vars/02_functions/601.cf index e19a254a1b0..843d72d1219 100644 --- a/tests/acceptance/01_vars/02_functions/601.cf +++ b/tests/acceptance/01_vars/02_functions/601.cf @@ -42,7 +42,8 @@ bundle agent test { vars: "cnt" - int => readrealarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000); + int => readrealarray("ary", "$(G.testfile)", "NoComment", ":", 10, 1000), + if => fileexists("$(G.testfile)"); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/620.cf b/tests/acceptance/01_vars/02_functions/620.cf index 8ce3531136f..cdcb767c6ea 100644 --- a/tests/acceptance/01_vars/02_functions/620.cf +++ b/tests/acceptance/01_vars/02_functions/620.cf @@ -41,7 +41,9 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); "cnt" int => parserealarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); diff --git a/tests/acceptance/01_vars/02_functions/621.cf b/tests/acceptance/01_vars/02_functions/621.cf index 59d8b7eb462..db38a789d46 100644 --- a/tests/acceptance/01_vars/02_functions/621.cf +++ b/tests/acceptance/01_vars/02_functions/621.cf @@ -41,7 +41,9 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); "cnt" int => parserealarray("ary", "$(teststr)", "NoComment", ":", 10, 1000); diff --git a/tests/acceptance/01_vars/02_functions/CFE-2704-0.cf b/tests/acceptance/01_vars/02_functions/CFE-2704-0.error.pass.cf similarity index 74% rename from tests/acceptance/01_vars/02_functions/CFE-2704-0.cf rename to tests/acceptance/01_vars/02_functions/CFE-2704-0.error.pass.cf index 27b521a02bc..8ea6f4a21ca 100644 --- a/tests/acceptance/01_vars/02_functions/CFE-2704-0.cf +++ b/tests/acceptance/01_vars/02_functions/CFE-2704-0.error.pass.cf @@ -1,3 +1,6 @@ +# This is an .error.pass.cf test, because mergedata() is given data +# which is not a container on purpose and logs an error, the test checks +# that the agent does not crash. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/01_vars/02_functions/CFE-2704-1.cf b/tests/acceptance/01_vars/02_functions/CFE-2704-1.error.pass.cf similarity index 74% rename from tests/acceptance/01_vars/02_functions/CFE-2704-1.cf rename to tests/acceptance/01_vars/02_functions/CFE-2704-1.error.pass.cf index a475b51d68f..7538a59b762 100644 --- a/tests/acceptance/01_vars/02_functions/CFE-2704-1.cf +++ b/tests/acceptance/01_vars/02_functions/CFE-2704-1.error.pass.cf @@ -1,3 +1,6 @@ +# This is an .error.pass.cf test, because mergedata() is given data +# which is not a container on purpose and logs an error, the test checks +# that the agent does not crash. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/01_vars/02_functions/classfilterdata_collecting.cf b/tests/acceptance/01_vars/02_functions/classfilterdata_collecting.cf index 88ea0d29e6d..8b2cbc66bc2 100644 --- a/tests/acceptance/01_vars/02_functions/classfilterdata_collecting.cf +++ b/tests/acceptance/01_vars/02_functions/classfilterdata_collecting.cf @@ -44,7 +44,8 @@ bundle agent test vars: "filtered" - data => classfilterdata(readjson("$(G.testfile).json"), "auto", "if"); + data => classfilterdata(readjson("$(G.testfile).json"), "auto", "if"), + if => fileexists("$(G.testfile).json"); } bundle agent check diff --git a/tests/acceptance/01_vars/02_functions/countlinesmatching_errors_on_absent_file.cf b/tests/acceptance/01_vars/02_functions/countlinesmatching_errors_on_absent_file.cf index 7df7778733f..3b2766f3932 100644 --- a/tests/acceptance/01_vars/02_functions/countlinesmatching_errors_on_absent_file.cf +++ b/tests/acceptance/01_vars/02_functions/countlinesmatching_errors_on_absent_file.cf @@ -25,7 +25,7 @@ bundle agent test vars: "subout" string => execresult( - "$(sys.cf_agent) -Kv --define AUTO -f $(this.promise_dirname)/countlinesmatching_returns_0_on_inaccessable_file.cf 2>&1 | $(G.grep) countlinesmatching", + "$(sys.cf_agent) -Kv --define AUTO -f $(this.promise_dirname)/countlinesmatching_returns_0_on_inaccessable_file.error.pass.cf 2>&1 | $(G.grep) countlinesmatching", "useshell" ); } diff --git a/tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.cf b/tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.error.pass.cf similarity index 89% rename from tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.cf rename to tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.error.pass.cf index 514d65a20aa..87f5a3769c7 100644 --- a/tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.cf +++ b/tests/acceptance/01_vars/02_functions/countlinesmatching_returns_0_on_inaccessable_file.error.pass.cf @@ -2,6 +2,9 @@ # # Test countlinesmatching() with some kind of failure expected # +# This is an .error.pass.cf test, because countlinesmatching() is called +# on an inaccessible file on purpose and logs an error. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/execresult_action_immediate.cf b/tests/acceptance/01_vars/02_functions/execresult_action_immediate.cf index 0181e9569e6..1e0634a6098 100644 --- a/tests/acceptance/01_vars/02_functions/execresult_action_immediate.cf +++ b/tests/acceptance/01_vars/02_functions/execresult_action_immediate.cf @@ -7,14 +7,23 @@ body common control version => "1.0"; } +# The 'immediate' body comes from plucked.sub.cf, which is not loaded yet when +# the policy is pre-evaluated, so promises using it are skipped until then +bundle common framework +{ + classes: + "framework_loaded" expression => isvariable("G.testdir"); +} + bundle agent check_date(x) { vars: "reports_file" string => "$(G.testdir)/execresult_action_immediate_report"; - "date" - string => execresult("date; sleep 1", "useshell"), - action => immediate; + framework_loaded:: + "date" + string => execresult("date; sleep 1", "useshell"), + action => immediate; reports: "Date for ${x}: ${date}" report_to_file => "$(reports_file)"; @@ -41,7 +50,8 @@ bundle agent check "content" slist => readstringlist( "$(check_date.reports_file)", "", '$(const.n)', inf, inf - ); + ), + if => fileexists("$(check_date.reports_file)"); "count" int => length(content); diff --git a/tests/acceptance/01_vars/02_functions/findfiles_up.cf b/tests/acceptance/01_vars/02_functions/findfiles_up.cf index f8ac71214c2..3cfd153bb36 100755 --- a/tests/acceptance/01_vars/02_functions/findfiles_up.cf +++ b/tests/acceptance/01_vars/02_functions/findfiles_up.cf @@ -46,27 +46,34 @@ bundle agent test "t1" data => findfiles_up( "$(G.testdir)/core/libntech/libutils/", ".gitignore", "inf" - ); + ), + if => isdir("$(G.testdir)/core/libntech/libutils/"); "t2" - data => findfiles_up("$(G.testdir)/core/libntech/libutils/", "string.?"); + data => findfiles_up("$(G.testdir)/core/libntech/libutils/", "string.?"), + if => isdir("$(G.testdir)/core/libntech/libutils/"); - "t3" data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".git/"); + "t3" + data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".git/"), + if => isdir("$(G.testdir)/core/libntech/libutils/"); "t4" data => findfiles_up( "$(G.testdir)/core/libntech/libutils/", ".git/", "1" - ); + ), + if => isdir("$(G.testdir)/core/libntech/libutils/"); "t5" data => findfiles_up( "$(G.testdir)/core/libntech/libutils/", ".git/config" - ); + ), + if => isdir("$(G.testdir)/core/libntech/libutils/"); "t6" data => findfiles_up( "$(G.testdir)/core/libntech/libutils/", "*/cf?{lex,parse}.[ly]" - ); + ), + if => isdir("$(G.testdir)/core/libntech/libutils/"); } bundle agent check diff --git a/tests/acceptance/01_vars/02_functions/getfields.cf b/tests/acceptance/01_vars/02_functions/getfields.cf index 7af6fdbd12d..037a0e86e59 100644 --- a/tests/acceptance/01_vars/02_functions/getfields.cf +++ b/tests/acceptance/01_vars/02_functions/getfields.cf @@ -41,7 +41,9 @@ body delete init_delete bundle agent test { vars: - "num_matching" int => getfields("t.*", "$(G.testfile)", ":", "fields"); + "num_matching" + int => getfields("t.*", "$(G.testfile)", ":", "fields"), + if => fileexists("$(G.testfile)"); } ####################################################### diff --git a/tests/acceptance/01_vars/02_functions/ifelse.cf b/tests/acceptance/01_vars/02_functions/ifelse.cf index bc3ffe9fadb..55a6d7b9910 100644 --- a/tests/acceptance/01_vars/02_functions/ifelse.cf +++ b/tests/acceptance/01_vars/02_functions/ifelse.cf @@ -35,9 +35,9 @@ bundle agent test "five" string => ifelse( - "this is not true", + "this_is_not_true", "5 parameters broken", - "this is also not true", + "this_is_also_not_true", "5 parameters broken 2", "5 parameters OK" ); diff --git a/tests/acceptance/01_vars/02_functions/long_string_from_text_file.cf b/tests/acceptance/01_vars/02_functions/long_string_from_text_file.cf index 80ecfd7f317..9c0ff0128b3 100644 --- a/tests/acceptance/01_vars/02_functions/long_string_from_text_file.cf +++ b/tests/acceptance/01_vars/02_functions/long_string_from_text_file.cf @@ -26,7 +26,10 @@ bundle agent test { vars: "keyfile" string => "$(G.testfile).data"; - "keys" slist => readstringlist("$(keyfile)", "#[^\n]*", "\n", 10, 3072); + + "keys" + slist => readstringlist("$(keyfile)", "#[^\n]*", "\n", 10, 3072), + if => fileexists("$(keyfile)"); } ####################################################### diff --git a/tests/acceptance/01_vars/02_functions/lsdir.cf b/tests/acceptance/01_vars/02_functions/lsdir.error.pass.cf similarity index 94% rename from tests/acceptance/01_vars/02_functions/lsdir.cf rename to tests/acceptance/01_vars/02_functions/lsdir.error.pass.cf index 50c103319b2..773dfd6caa2 100644 --- a/tests/acceptance/01_vars/02_functions/lsdir.cf +++ b/tests/acceptance/01_vars/02_functions/lsdir.error.pass.cf @@ -2,6 +2,9 @@ # # Test lsdir() # +# This is an .error.pass.cf test, because lsdir() is called on the +# non-existent directory '$(G.testdir)/b' on purpose and logs an error. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/mapdata_json_pipe.cf b/tests/acceptance/01_vars/02_functions/mapdata_json_pipe.cf index 1a3d32ece7d..3f354cc6b33 100644 --- a/tests/acceptance/01_vars/02_functions/mapdata_json_pipe.cf +++ b/tests/acceptance/01_vars/02_functions/mapdata_json_pipe.cf @@ -19,7 +19,12 @@ bundle agent test vars: "items" data => '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'; - "items_passthrough" data => mapdata("json_pipe", "$(G.cat)", items); + + # G comes from dynamically loaded dcs.sub.cf, so $(G.cat) is unresolved + # during pre-evaluation and json_pipe would execute it literally + "items_passthrough" + data => mapdata("json_pipe", "$(G.cat)", items), + if => isvariable("G.cat"); "items_jqinput" data => mapdata( diff --git a/tests/acceptance/01_vars/02_functions/nth_datacontainer.cf b/tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf similarity index 94% rename from tests/acceptance/01_vars/02_functions/nth_datacontainer.cf rename to tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf index e6090255eeb..6b5101c360d 100644 --- a/tests/acceptance/01_vars/02_functions/nth_datacontainer.cf +++ b/tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf @@ -2,6 +2,10 @@ # # Test nth() with data container input # +# This is an .error.pass.cf test, because nth() is called with keys +# which are not valid list indices (e.g. 'foo' or '') on purpose and +# logs errors. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/nth_datacontainer.cf.expected b/tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf.expected similarity index 100% rename from tests/acceptance/01_vars/02_functions/nth_datacontainer.cf.expected rename to tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf.expected diff --git a/tests/acceptance/01_vars/02_functions/nth_datacontainer.cf.json b/tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf.json similarity index 100% rename from tests/acceptance/01_vars/02_functions/nth_datacontainer.cf.json rename to tests/acceptance/01_vars/02_functions/nth_datacontainer.error.pass.cf.json diff --git a/tests/acceptance/01_vars/02_functions/parsestringarray_dontread_comment_duplicate_lastline2.cf b/tests/acceptance/01_vars/02_functions/parsestringarray_dontread_comment_duplicate_lastline2.cf index 643b67106ba..478a3eb0c63 100644 --- a/tests/acceptance/01_vars/02_functions/parsestringarray_dontread_comment_duplicate_lastline2.cf +++ b/tests/acceptance/01_vars/02_functions/parsestringarray_dontread_comment_duplicate_lastline2.cf @@ -51,7 +51,8 @@ bundle agent test "cnt" int => readstringarrayidx( "ary", "$(G.testfile)", "NoComment", ":", 6, 1000 - ); + ), + if => fileexists("$(G.testfile)"); "num" int => "6"; } diff --git a/tests/acceptance/01_vars/02_functions/parsestringarray_weird_indices_real_comments_noemptyfields2.cf b/tests/acceptance/01_vars/02_functions/parsestringarray_weird_indices_real_comments_noemptyfields2.cf index 801e3f09c2a..6443272ee0f 100644 --- a/tests/acceptance/01_vars/02_functions/parsestringarray_weird_indices_real_comments_noemptyfields2.cf +++ b/tests/acceptance/01_vars/02_functions/parsestringarray_weird_indices_real_comments_noemptyfields2.cf @@ -48,7 +48,10 @@ body delete init_delete bundle agent test { vars: - "teststr" string => readfile("$(G.testfile)", 1000); + "teststr" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); + "cnt" int => parsestringarray("ary", "$(teststr)", "^#.*", ":+", 10, 14); "num" int => "3"; } diff --git a/tests/acceptance/01_vars/02_functions/peers.cf b/tests/acceptance/01_vars/02_functions/peers.cf index 5b87f0b1cb4..e99d0373e91 100644 --- a/tests/acceptance/01_vars/02_functions/peers.cf +++ b/tests/acceptance/01_vars/02_functions/peers.cf @@ -56,10 +56,12 @@ bundle agent test "subtests" ilist => { "0", "1", "2", "3", "4", "5" }; "peers_$(tests)_$(subtests)" - slist => peers("$(G.testfile).$(tests)", "#.*", $(subtests)); + slist => peers("$(G.testfile).$(tests)", "#.*", $(subtests)), + if => fileexists("$(G.testfile).$(tests)"); "peerleaders_$(tests)_$(subtests)" - slist => peerleaders("$(G.testfile).$(tests)", "#.*", $(subtests)); + slist => peerleaders("$(G.testfile).$(tests)", "#.*", $(subtests)), + if => fileexists("$(G.testfile).$(tests)"); "actual_peers_$(tests)_$(subtests)" string => format("%S", "peers_$(tests)_$(subtests)"); @@ -68,7 +70,8 @@ bundle agent test string => format("%S", "peerleaders_$(tests)_$(subtests)"); "actual_peerleader_$(tests)_$(subtests)" - string => peerleader("$(G.testfile).$(tests)", "#.*", $(subtests)); + string => peerleader("$(G.testfile).$(tests)", "#.*", $(subtests)), + if => fileexists("$(G.testfile).$(tests)"); } ####################################################### diff --git a/tests/acceptance/01_vars/02_functions/read-file-endless-loop.cf b/tests/acceptance/01_vars/02_functions/read-file-endless-loop.error.pass.cf similarity index 77% rename from tests/acceptance/01_vars/02_functions/read-file-endless-loop.cf rename to tests/acceptance/01_vars/02_functions/read-file-endless-loop.error.pass.cf index 1cfaa98e160..46fe379f29d 100644 --- a/tests/acceptance/01_vars/02_functions/read-file-endless-loop.cf +++ b/tests/acceptance/01_vars/02_functions/read-file-endless-loop.error.pass.cf @@ -1,5 +1,9 @@ # Test that regline, getfields and countlinesmatching do not loop endlessly if # supplied with a directory (Redmine #2453) +# +# This is an .error.pass.cf test, because regline(), getfields() and +# countlinesmatching() are given the directory '/' on purpose and log +# errors. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/01_vars/02_functions/read_empty_file.cf b/tests/acceptance/01_vars/02_functions/read_empty_file.cf index 1ce7d0bb4ca..ca4281153a6 100644 --- a/tests/acceptance/01_vars/02_functions/read_empty_file.cf +++ b/tests/acceptance/01_vars/02_functions/read_empty_file.cf @@ -15,8 +15,13 @@ bundle agent init bundle agent test { vars: - "value1" string => readfile("$(G.testfile)", 10000); - "value2" string => readfile("$(G.testfile)", 0); + "value1" + string => readfile("$(G.testfile)", 10000), + if => fileexists("$(G.testfile)"); + + "value2" + string => readfile("$(G.testfile)", 0), + if => fileexists("$(G.testfile)"); } bundle agent check diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf similarity index 91% rename from tests/acceptance/01_vars/02_functions/readdata.cf rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf index 59767849337..09bba95c9ec 100644 --- a/tests/acceptance/01_vars/02_functions/readdata.cf +++ b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf @@ -2,6 +2,10 @@ # # Test readdata() with YAML # +# This is an .error.pass.cf test, because readdata() is given data in a +# format it cannot parse on purpose (the failed_* and guess_csv +# variables) and logs errors. +# ########################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.csv b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.csv similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata.cf.csv rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.csv diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.csv.guess b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.csv.guess similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata.cf.csv.guess rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.csv.guess diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.env b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.env similarity index 93% rename from tests/acceptance/01_vars/02_functions/readdata.cf.env rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.env index 71aaa7dd272..ac93f45f3d2 100644 --- a/tests/acceptance/01_vars/02_functions/readdata.cf.env +++ b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.env @@ -1,4 +1,4 @@ -# Example env file used for acceptance test readdata.cf +# Example env file used for acceptance test readdata.error.pass.cf # We can't test for all weird possibilites, # but this should serve most use cases diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.expected.json b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.expected.json similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata.cf.expected.json rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.expected.json diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.json b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.json similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata.cf.json rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.json diff --git a/tests/acceptance/01_vars/02_functions/readdata.cf.json.guess b/tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.json.guess similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata.cf.json.guess rename to tests/acceptance/01_vars/02_functions/readdata.error.pass.cf.json.guess diff --git a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf similarity index 87% rename from tests/acceptance/01_vars/02_functions/readdata_yaml.cf rename to tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf index 7e3fc3edd97..84922593d62 100644 --- a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf +++ b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf @@ -2,6 +2,10 @@ # # Test readdata() with YAML # +# This is an .error.pass.cf test, because readdata() is given data in a +# format it cannot parse on purpose (the failed_explicit_yaml and +# guess_yaml variables) and logs errors. +# ########################################################### body common control { diff --git a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf.expected.json b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.expected.json similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata_yaml.cf.expected.json rename to tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.expected.json diff --git a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yaml b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yaml similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yaml rename to tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yaml diff --git a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yaml.guess b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yaml.guess similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yaml.guess rename to tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yaml.guess diff --git a/tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yml b/tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yml similarity index 100% rename from tests/acceptance/01_vars/02_functions/readdata_yaml.cf.yml rename to tests/acceptance/01_vars/02_functions/readdata_yaml.error.pass.cf.yml diff --git a/tests/acceptance/01_vars/02_functions/readstringarray_dontread_comment_duplicatekey_lastline.cf b/tests/acceptance/01_vars/02_functions/readstringarray_dontread_comment_duplicatekey_lastline.cf index f9339fc69e5..2d9b30cc87b 100644 --- a/tests/acceptance/01_vars/02_functions/readstringarray_dontread_comment_duplicatekey_lastline.cf +++ b/tests/acceptance/01_vars/02_functions/readstringarray_dontread_comment_duplicatekey_lastline.cf @@ -49,7 +49,8 @@ bundle agent test { vars: "cnt" - int => readstringarray("ary", "$(G.testfile)", "NoComment", ":", 6, 1000); + int => readstringarray("ary", "$(G.testfile)", "NoComment", ":", 6, 1000), + if => fileexists("$(G.testfile)"); "num" int => "6"; } diff --git a/tests/acceptance/01_vars/02_functions/readstringarrayidx_order.cf b/tests/acceptance/01_vars/02_functions/readstringarrayidx_order.cf index 3d8236815ee..c8fd1b9a0b5 100644 --- a/tests/acceptance/01_vars/02_functions/readstringarrayidx_order.cf +++ b/tests/acceptance/01_vars/02_functions/readstringarrayidx_order.cf @@ -69,7 +69,8 @@ bundle agent test "num" int => readstringarrayidx( "mylines", "$(G.testfile).orig.txt", "\s*#[^\n]*", "\s*;;\s*", 50, 9999 - ); + ), + if => fileexists("$(G.testfile).orig.txt"); "file" slist => { diff --git a/tests/acceptance/01_vars/02_functions/regextract.cf b/tests/acceptance/01_vars/02_functions/regextract.cf index ee882bc0969..ff7c86c7c9f 100644 --- a/tests/acceptance/01_vars/02_functions/regextract.cf +++ b/tests/acceptance/01_vars/02_functions/regextract.cf @@ -32,7 +32,10 @@ bundle agent test "lefts" string => "$(init.pair[1])"; "lefti" int => "$(init.pair[1])"; "rights" string => "$(init.pair[2])"; - "rightr" real => "$(init.pair[2])"; + + "rightr" + real => "$(init.pair[2])", + if => isvariable("init.pair[2]"); } ####################################################### diff --git a/tests/acceptance/01_vars/04_containers/mergecontainer_conflict.cf b/tests/acceptance/01_vars/04_containers/mergecontainer_conflict.error.pass.cf similarity index 90% rename from tests/acceptance/01_vars/04_containers/mergecontainer_conflict.cf rename to tests/acceptance/01_vars/04_containers/mergecontainer_conflict.error.pass.cf index 64c72b206a4..cf578d5dbdb 100644 --- a/tests/acceptance/01_vars/04_containers/mergecontainer_conflict.cf +++ b/tests/acceptance/01_vars/04_containers/mergecontainer_conflict.error.pass.cf @@ -2,6 +2,9 @@ # # Test mergejson() with mismatched datas # +# This is an .error.pass.cf test, because parsejson() is given invalid +# JSON on purpose and logs an error, so that mergedata() fails. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/04_containers/parseyaml_abuse.cf b/tests/acceptance/01_vars/04_containers/parseyaml_abuse.error.pass.cf similarity index 87% rename from tests/acceptance/01_vars/04_containers/parseyaml_abuse.cf rename to tests/acceptance/01_vars/04_containers/parseyaml_abuse.error.pass.cf index 316b0ac7576..0878b53f51a 100644 --- a/tests/acceptance/01_vars/04_containers/parseyaml_abuse.cf +++ b/tests/acceptance/01_vars/04_containers/parseyaml_abuse.error.pass.cf @@ -2,6 +2,9 @@ # # Abuse parseyaml() # +# This is an .error.pass.cf test, because parseyaml() is given strings +# which are not valid YAML containers on purpose and logs errors. +# ####################################################### body common control { diff --git a/tests/acceptance/01_vars/04_containers/readjson.cf b/tests/acceptance/01_vars/04_containers/readjson.cf index a4a1998f314..4774201ddf4 100644 --- a/tests/acceptance/01_vars/04_containers/readjson.cf +++ b/tests/acceptance/01_vars/04_containers/readjson.cf @@ -40,7 +40,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_arrays.cf b/tests/acceptance/01_vars/04_containers/readjson_arrays.cf index 004676f31d4..6e96734d8ca 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_arrays.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_arrays.cf @@ -43,7 +43,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_new.cf b/tests/acceptance/01_vars/04_containers/readjson_new.cf index 6d453e4a67e..ebc1f19ff6b 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_new.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_new.cf @@ -40,7 +40,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json"); + "load" + data => readjson("$(G.testfile).json"), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_null.cf b/tests/acceptance/01_vars/04_containers/readjson_null.cf index fed055cb2e3..e9115b410cc 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_null.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_null.cf @@ -39,7 +39,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_objects.cf b/tests/acceptance/01_vars/04_containers/readjson_objects.cf index 137670afd03..8ba8cf47110 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_objects.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_objects.cf @@ -40,7 +40,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_scalar.cf b/tests/acceptance/01_vars/04_containers/readjson_scalar.cf index f1fe8bc9172..859aee42cc2 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_scalar.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_scalar.cf @@ -39,7 +39,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_subindex.cf b/tests/acceptance/01_vars/04_containers/readjson_subindex.cf index 7cbdf379d2d..51f0ae06df6 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_subindex.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_subindex.cf @@ -41,7 +41,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/01_vars/04_containers/readjson_subindex_quoted.cf b/tests/acceptance/01_vars/04_containers/readjson_subindex_quoted.cf index 39e203f37df..dce9a2b4bf7 100644 --- a/tests/acceptance/01_vars/04_containers/readjson_subindex_quoted.cf +++ b/tests/acceptance/01_vars/04_containers/readjson_subindex_quoted.cf @@ -41,7 +41,9 @@ bundle edit_line init_insert_lines bundle agent test { vars: - "load" data => readjson("$(G.testfile).json", 1024); + "load" + data => readjson("$(G.testfile).json", 1024), + if => fileexists("$(G.testfile).json"); files: "$(G.testfile).actual" diff --git a/tests/acceptance/02_classes/01_basic/cancel_attribute_persistent_lmdb.cf b/tests/acceptance/02_classes/01_basic/cancel_attribute_persistent_lmdb.cf index 27f78438256..e1f4ea95d87 100644 --- a/tests/acceptance/02_classes/01_basic/cancel_attribute_persistent_lmdb.cf +++ b/tests/acceptance/02_classes/01_basic/cancel_attribute_persistent_lmdb.cf @@ -58,8 +58,13 @@ bundle agent test bundle agent check { vars: - "before" string => readfile("$(G.testdir)/cancel_lmdb_before", 4096); - "after" string => readfile("$(G.testdir)/cancel_lmdb_after", 4096); + "before" + string => readfile("$(G.testdir)/cancel_lmdb_before", 4096), + if => fileexists("$(G.testdir)/cancel_lmdb_before"); + + "after" + string => readfile("$(G.testdir)/cancel_lmdb_after", 4096), + if => fileexists("$(G.testdir)/cancel_lmdb_after"); classes: # The class was stored in the LMDB by run 1 ... diff --git a/tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.cf b/tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.error.pass.cf similarity index 86% rename from tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.cf rename to tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.error.pass.cf index d8e0d02a920..deabdd53c7e 100644 --- a/tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.cf +++ b/tests/acceptance/02_classes/01_basic/common_bundle_select_class_empty_list.error.pass.cf @@ -1,4 +1,7 @@ # Test that select_class is skipped when no classes are given +# +# This is an .error.pass.cf test, because select_class is given an empty +# list on purpose and logs an error. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.cf b/tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.error.pass.cf similarity index 81% rename from tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.cf rename to tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.error.pass.cf index 833304f5120..cdf9cd73d8b 100644 --- a/tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.cf +++ b/tests/acceptance/02_classes/01_basic/common_bundle_select_class_variable_expands_empty_list.error.pass.cf @@ -1,4 +1,7 @@ # Test that select class is skipped when the list of classes is empty +# +# This is an .error.pass.cf test, because select_class is given a list +# variable which expands to an empty list on purpose and logs an error. body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/02_classes/01_basic/persistent_timer_policy_default_lmdb.cf b/tests/acceptance/02_classes/01_basic/persistent_timer_policy_default_lmdb.cf index 775c583ef04..d4e4841e2a5 100644 --- a/tests/acceptance/02_classes/01_basic/persistent_timer_policy_default_lmdb.cf +++ b/tests/acceptance/02_classes/01_basic/persistent_timer_policy_default_lmdb.cf @@ -78,9 +78,17 @@ bundle agent test bundle agent check { vars: - "policy" string => readfile("$(G.testdir)/lmdb_policy", 4096); - "e1" string => readfile("$(G.testdir)/lmdb_expires1", 4096); - "e2" string => readfile("$(G.testdir)/lmdb_expires2", 4096); + "policy" + string => readfile("$(G.testdir)/lmdb_policy", 4096), + if => fileexists("$(G.testdir)/lmdb_policy"); + + "e1" + string => readfile("$(G.testdir)/lmdb_expires1", 4096), + if => fileexists("$(G.testdir)/lmdb_expires1"); + + "e2" + string => readfile("$(G.testdir)/lmdb_expires2", 4096), + if => fileexists("$(G.testdir)/lmdb_expires2"); classes: # #1 The default classes: promise stored its persistent class with diff --git a/tests/acceptance/02_classes/02_functions/classesmatching_inherit_2.cf b/tests/acceptance/02_classes/02_functions/classesmatching_inherit_2.cf index f0b25fa0685..8f661fa559d 100644 --- a/tests/acceptance/02_classes/02_functions/classesmatching_inherit_2.cf +++ b/tests/acceptance/02_classes/02_functions/classesmatching_inherit_2.cf @@ -59,7 +59,9 @@ bundle agent check "defined_classes: defined_in_second$(const.n)" ); - "actual" string => readfile("$(common.logfile)", inf); + "actual" + string => readfile("$(common.logfile)", inf), + if => fileexists("$(common.logfile)"); reports: "$(this.promise_filename) Pass" if => strcmp($(expected), $(actual)); diff --git a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf similarity index 92% rename from tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf rename to tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf index c23da722aea..d1929a2a910 100644 --- a/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.cf +++ b/tests/acceptance/02_classes/02_functions/isipinsubnet-valid-ip.error.pass.cf @@ -2,6 +2,9 @@ # # Test isipinsubnet() # +# This is an .error.pass.cf test, because isipinsubnet() is given +# 'not-an-ip-address' on purpose and logs an error. +# ####################################################### body common control { diff --git a/tests/acceptance/04_examples/syntax/check_syntax.cf b/tests/acceptance/04_examples/syntax/check_syntax.cf index 347329a35a5..420f58e4f94 100644 --- a/tests/acceptance/04_examples/syntax/check_syntax.cf +++ b/tests/acceptance/04_examples/syntax/check_syntax.cf @@ -124,7 +124,7 @@ bundle agent test_example(file) classes: "failure_$(cfile)" - not => returnszero("$(checker) $(file)", "noshell"), + not => returnszero("$(checker) $(file) >/dev/null 2>&1", "useshell"), scope => "namespace"; reports: diff --git a/tests/acceptance/05_processes/01_matching/process_select_fncalls.cf b/tests/acceptance/05_processes/01_matching/process_select_fncalls.error.pass.cf similarity index 95% rename from tests/acceptance/05_processes/01_matching/process_select_fncalls.cf rename to tests/acceptance/05_processes/01_matching/process_select_fncalls.error.pass.cf index 027efcc2828..cccdc262618 100644 --- a/tests/acceptance/05_processes/01_matching/process_select_fncalls.cf +++ b/tests/acceptance/05_processes/01_matching/process_select_fncalls.error.pass.cf @@ -1,3 +1,5 @@ +# This is an .error.pass.cf test, because getuid() is called for the +# non-existent user 'nosuchuser' on purpose and logs errors (CFE-1968). body common control { inputs => { "../../default.sub.cf" }; diff --git a/tests/acceptance/08_commands/arglist.cf b/tests/acceptance/08_commands/arglist.cf index c86411e7173..bbf52686c0d 100644 --- a/tests/acceptance/08_commands/arglist.cf +++ b/tests/acceptance/08_commands/arglist.cf @@ -46,7 +46,10 @@ bundle agent test bundle agent check { vars: - "actual" string => readfile("$(G.testfile).actual"); + "actual" + string => readfile("$(G.testfile).actual"), + if => fileexists("$(G.testfile).actual"); + "expected" string => "one two three diff --git a/tests/acceptance/10_files/02_maintain/changes_silent.cf b/tests/acceptance/10_files/02_maintain/changes_silent.cf index db87d61d5c6..0a27de9e1cf 100644 --- a/tests/acceptance/10_files/02_maintain/changes_silent.cf +++ b/tests/acceptance/10_files/02_maintain/changes_silent.cf @@ -95,38 +95,47 @@ bundle agent check "n_quiet_content" int => countlinesmatching( ".*quiet_content\.txt,C,Content changed.*", "$(log)" - ); + ), + if => fileexists("$(log)"); "n_quiet_all_content" int => countlinesmatching( ".*quiet_all\.txt,C,Content changed.*", "$(log)" - ); + ), + if => fileexists("$(log)"); "n_quiet_all_stats" - int => countlinesmatching(".*quiet_all\.txt,S,.*", "$(log)"); + int => countlinesmatching(".*quiet_all\.txt,S,.*", "$(log)"), + if => fileexists("$(log)"); classes: - "leaked" - expression => isgreaterthan( - countlinesmatching(".*$(must_be_absent).*", "$(test.output)"), "0" - ); - - "missing" - expression => islessthan( - countlinesmatching(".*$(must_be_present).*", "$(test.output)"), "1" - ); - - "audit_ok" - and => { - isgreaterthan("$(n_quiet_content)", "0"), - isgreaterthan("$(n_quiet_all_content)", "0"), - isgreaterthan("$(n_quiet_all_stats)", "0"), - }; + # The output is read only once it exists, since the functions are also + # evaluated before the test bundle runs + "output_exists" expression => fileexists("$(test.output)"); + + output_exists:: + "leaked" + expression => isgreaterthan( + countlinesmatching(".*$(must_be_absent).*", "$(test.output)"), "0" + ); + + "missing" + expression => islessthan( + countlinesmatching(".*$(must_be_present).*", "$(test.output)"), "1" + ); + + any:: + "audit_ok" + and => { + isgreaterthan("$(n_quiet_content)", "0"), + isgreaterthan("$(n_quiet_all_content)", "0"), + isgreaterthan("$(n_quiet_all_stats)", "0"), + }; - "ok" and => { "!leaked", "!missing", "audit_ok" }; + "ok" and => { "output_exists", "!leaked", "!missing", "audit_ok" }; reports: - DEBUG:: + DEBUG.output_exists:: "silence leaked an alert matching '$(must_be_absent)'" if => isgreaterthan( countlinesmatching(".*$(must_be_absent).*", "$(test.output)"), "0" @@ -137,6 +146,7 @@ bundle agent check countlinesmatching(".*$(must_be_present).*", "$(test.output)"), "1" ); + DEBUG:: "Output of $(this.promise_filename).sub is in '$(test.output)'"; DEBUG.!audit_ok:: diff --git a/tests/acceptance/10_files/09_insert_lines/multi-files-dirs.cf b/tests/acceptance/10_files/09_insert_lines/multi-files-dirs.cf index 310c8a68553..9f0836b75d8 100644 --- a/tests/acceptance/10_files/09_insert_lines/multi-files-dirs.cf +++ b/tests/acceptance/10_files/09_insert_lines/multi-files-dirs.cf @@ -54,9 +54,18 @@ bundle agent check vars: "expect" string => "one two three test"; "results" slist => { "resultA", "resultB", "resultC" }; - "resultA" string => readfile("$(G.testdir)/dirA/testfa", "33"); - "resultB" string => readfile("$(G.testdir)/dirB/testfb", "33"); - "resultC" string => readfile("$(G.testdir)/dirC/testfc", "33"); + + "resultA" + string => readfile("$(G.testdir)/dirA/testfa", "33"), + if => fileexists("$(G.testdir)/dirA/testfa"); + + "resultB" + string => readfile("$(G.testdir)/dirB/testfb", "33"), + if => fileexists("$(G.testdir)/dirB/testfb"); + + "resultC" + string => readfile("$(G.testdir)/dirC/testfc", "33"), + if => fileexists("$(G.testdir)/dirC/testfc"); classes: "ok_A" expression => strcmp("$(expect)", "$(resultA)"); diff --git a/tests/acceptance/10_files/09_insert_lines/preserve_block_blank_lines.cf b/tests/acceptance/10_files/09_insert_lines/preserve_block_blank_lines.cf index 79810047971..c2c4e60f067 100644 --- a/tests/acceptance/10_files/09_insert_lines/preserve_block_blank_lines.cf +++ b/tests/acceptance/10_files/09_insert_lines/preserve_block_blank_lines.cf @@ -38,7 +38,10 @@ bundle agent check Bad 3 "; - "actual" string => readfile("$(init.test_file)", "inf"); + + "actual" + string => readfile("$(init.test_file)", "inf"), + if => fileexists("$(init.test_file)"); classes: "ok" expression => strcmp("$(expected)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/empty_template_empty_file.cf b/tests/acceptance/10_files/templating/empty_template_empty_file.cf index de9a28342b7..f41409d8de4 100644 --- a/tests/acceptance/10_files/templating/empty_template_empty_file.cf +++ b/tests/acceptance/10_files/templating/empty_template_empty_file.cf @@ -53,7 +53,10 @@ bundle agent check { vars: "expect" string => ""; - "actual" string => readfile("$(G.testfile)", 1000); + + "actual" + string => readfile("$(G.testfile)", 1000), + if => fileexists("$(G.testfile)"); classes: "ok" expression => regcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/large_template_blocks.cf b/tests/acceptance/10_files/templating/large_template_blocks.cf index 398a1f3028a..c6c77f527aa 100644 --- a/tests/acceptance/10_files/templating/large_template_blocks.cf +++ b/tests/acceptance/10_files/templating/large_template_blocks.cf @@ -50,7 +50,10 @@ bundle agent check { vars: "expected" int => "110"; - "actual" int => countlinesmatching(".*0123456789.*", "$(G.testfile)"); + + "actual" + int => countlinesmatching(".*0123456789.*", "$(G.testfile)"), + if => fileexists("$(G.testfile)"); classes: "ok" expression => strcmp("$(expected)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/mustache_abuse.cf b/tests/acceptance/10_files/templating/mustache_abuse.cf index a2b6eaa47d1..01ae5a2c531 100644 --- a/tests/acceptance/10_files/templating/mustache_abuse.cf +++ b/tests/acceptance/10_files/templating/mustache_abuse.cf @@ -99,7 +99,8 @@ bundle agent check ); "actual_$(init.tests)" - string => readfile("$(G.testfile).$(init.tests)", 10000); + string => readfile("$(G.testfile).$(init.tests)", 10000), + if => fileexists("$(G.testfile).$(init.tests)"); classes: "ok_$(init.tests)" diff --git a/tests/acceptance/10_files/templating/mustache_container_serialization.cf b/tests/acceptance/10_files/templating/mustache_container_serialization.cf index e6fdfb8cbe0..b626ce3cea3 100644 --- a/tests/acceptance/10_files/templating/mustache_container_serialization.cf +++ b/tests/acceptance/10_files/templating/mustache_container_serialization.cf @@ -64,7 +64,10 @@ bundle edit_line readthem { vars: "test" slist => { @(test.test) }; - "read_$(test)" string => readfile("$(G.testdir)/$(test)", 1k); + + "read_$(test)" + string => readfile("$(G.testdir)/$(test)", 1k), + if => fileexists("$(G.testdir)/$(test)"); insert_lines: "$(read_$(test))"; diff --git a/tests/acceptance/10_files/templating/mustache_datastate_demo.cf b/tests/acceptance/10_files/templating/mustache_datastate_demo.cf index 0725484f444..ee5724997ab 100644 --- a/tests/acceptance/10_files/templating/mustache_datastate_demo.cf +++ b/tests/acceptance/10_files/templating/mustache_datastate_demo.cf @@ -101,7 +101,9 @@ bundle agent check }" '; - "actual" string => readfile("$(G.testfile)", inf); + "actual" + string => readfile("$(G.testfile)", inf), + if => fileexists("$(G.testfile)"); classes: "ok" expression => strcmp($(expect), $(actual)); diff --git a/tests/acceptance/10_files/templating/mustache_demo.cf b/tests/acceptance/10_files/templating/mustache_demo.cf index 76bffa647e4..1e321f5bbb5 100644 --- a/tests/acceptance/10_files/templating/mustache_demo.cf +++ b/tests/acceptance/10_files/templating/mustache_demo.cf @@ -49,7 +49,10 @@ bundle agent check { vars: "expect" string => readfile("$(init.origtestdir)/demo.expected", 10000); - "actual" string => readfile("$(G.testfile)", 10000); + + "actual" + string => readfile("$(G.testfile)", 10000), + if => fileexists("$(G.testfile)"); classes: "ok" expression => regcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/mustache_expect_list_find_string.cf b/tests/acceptance/10_files/templating/mustache_expect_list_find_string.cf index 7c69a7d9619..3092114d1d9 100644 --- a/tests/acceptance/10_files/templating/mustache_expect_list_find_string.cf +++ b/tests/acceptance/10_files/templating/mustache_expect_list_find_string.cf @@ -85,8 +85,14 @@ bundle agent test vars: "template" string => "$(this.promise_filename).mustache"; - "actual_good" string => readfile("$(G.testfile).good", 4096); - "actual_bad" string => readfile("$(G.testfile).bad", 4096); + + "actual_good" + string => readfile("$(G.testfile).good", 4096), + if => fileexists("$(G.testfile).good"); + + "actual_bad" + string => readfile("$(G.testfile).bad", 4096), + if => fileexists("$(G.testfile).bad"); } ####################################################### diff --git a/tests/acceptance/10_files/templating/mustache_html_escape.cf b/tests/acceptance/10_files/templating/mustache_html_escape.cf index decc658f9ff..e3d85d91240 100644 --- a/tests/acceptance/10_files/templating/mustache_html_escape.cf +++ b/tests/acceptance/10_files/templating/mustache_html_escape.cf @@ -54,7 +54,10 @@ bundle agent check { vars: "expect" string => readfile("$(this.promise_filename).expected", 10000); - "actual" string => readfile("$(G.testfile)", 10000); + + "actual" + string => readfile("$(G.testfile)", 10000), + if => fileexists("$(G.testfile)"); classes: "ok" expression => strcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/mustache_inline_demo.cf b/tests/acceptance/10_files/templating/mustache_inline_demo.cf index d03e2d90681..97c841e004a 100644 --- a/tests/acceptance/10_files/templating/mustache_inline_demo.cf +++ b/tests/acceptance/10_files/templating/mustache_inline_demo.cf @@ -52,7 +52,9 @@ bundle agent check "expect" string => readfile("$(init.origtestdir)/inline_demo.expected", 10000); - "actual" string => readfile("$(G.testfile)", 10000); + "actual" + string => readfile("$(G.testfile)", 10000), + if => fileexists("$(G.testfile)"); classes: "ok" expression => regcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/mustache_render_quote.cf b/tests/acceptance/10_files/templating/mustache_render_quote.cf index 5ba4ba3ae9a..1d70e421b53 100644 --- a/tests/acceptance/10_files/templating/mustache_render_quote.cf +++ b/tests/acceptance/10_files/templating/mustache_render_quote.cf @@ -54,7 +54,10 @@ bundle agent check { vars: "expect" string => readfile("$(this.promise_filename).expected", 4000); - "actual" string => readfile("$(G.testfile)", 4000); + + "actual" + string => readfile("$(G.testfile)", 4000), + if => fileexists("$(G.testfile)"); classes: "ok" expression => regcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/templating/mustache_repair_existing.cf b/tests/acceptance/10_files/templating/mustache_repair_existing.cf index e2a1991d973..c7c78943fed 100644 --- a/tests/acceptance/10_files/templating/mustache_repair_existing.cf +++ b/tests/acceptance/10_files/templating/mustache_repair_existing.cf @@ -50,7 +50,10 @@ bundle agent check { vars: "expect" string => readfile("$(init.origtestdir)/demo.expected", 4096); - "actual" string => readfile("$(G.testfile)", 4096); + + "actual" + string => readfile("$(G.testfile)", 4096), + if => fileexists("$(G.testfile)"); classes: "content_ok" expression => regcmp("$(expect)", "$(actual)"); diff --git a/tests/acceptance/10_files/unsafe/02_immutable.cf b/tests/acceptance/10_files/unsafe/02_immutable.cf index e078b05411d..47a1dc2ea8a 100644 --- a/tests/acceptance/10_files/unsafe/02_immutable.cf +++ b/tests/acceptance/10_files/unsafe/02_immutable.cf @@ -49,7 +49,10 @@ bundle agent check { vars: "expected" string => "But agent can override"; - "actual" string => readfile("$(global.testfile)"); + + "actual" + string => readfile("$(global.testfile)"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => strcmp("$(actual)", "$(expected)"); diff --git a/tests/acceptance/10_files/unsafe/03_immutable.cf b/tests/acceptance/10_files/unsafe/03_immutable.cf index 97c0f81d88a..4e6ddaa683c 100644 --- a/tests/acceptance/10_files/unsafe/03_immutable.cf +++ b/tests/acceptance/10_files/unsafe/03_immutable.cf @@ -50,7 +50,10 @@ bundle agent check { vars: "expected" string => "But agent can override"; - "actual" string => readfile("$(global.testfile)"); + + "actual" + string => readfile("$(global.testfile)"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => strcmp("$(actual)", "$(expected)"); diff --git a/tests/acceptance/10_files/unsafe/05_immutable.cf b/tests/acceptance/10_files/unsafe/05_immutable.cf index e96b3b9dba1..87d2c85ddf5 100644 --- a/tests/acceptance/10_files/unsafe/05_immutable.cf +++ b/tests/acceptance/10_files/unsafe/05_immutable.cf @@ -57,7 +57,9 @@ bundle agent check "expected" string => "I'm immutable$(const.n)But agent can override$(const.n)"; - "actual" string => readfile("$(global.testfile)"); + "actual" + string => readfile("$(global.testfile)"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => strcmp("$(actual)", "$(expected)"); diff --git a/tests/acceptance/10_files/unsafe/06_immutable.cf b/tests/acceptance/10_files/unsafe/06_immutable.cf index f105c62fcb3..c04b5c5f510 100644 --- a/tests/acceptance/10_files/unsafe/06_immutable.cf +++ b/tests/acceptance/10_files/unsafe/06_immutable.cf @@ -56,7 +56,9 @@ bundle agent check "expected" string => '$(const.n)But agent can override$(const.n)'; - "actual" string => readfile("$(global.testfile)"); + "actual" + string => readfile("$(global.testfile)"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => strcmp("$(actual)", "$(expected)"); diff --git a/tests/acceptance/10_files/unsafe/09_immutable.cf b/tests/acceptance/10_files/unsafe/09_immutable.cf index 4b96b6acbe5..9b133625559 100644 --- a/tests/acceptance/10_files/unsafe/09_immutable.cf +++ b/tests/acceptance/10_files/unsafe/09_immutable.cf @@ -56,7 +56,10 @@ bundle agent check { vars: "expected" string => "Hello larsewi!"; - "actual" string => readfile("$(global.testfile)"); + + "actual" + string => readfile("$(global.testfile)"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => strcmp("$(actual)", "$(expected)"); diff --git a/tests/acceptance/10_files/unsafe/10_immutable.cf b/tests/acceptance/10_files/unsafe/10_immutable.cf index 855e245f777..9815ecc2621 100644 --- a/tests/acceptance/10_files/unsafe/10_immutable.cf +++ b/tests/acceptance/10_files/unsafe/10_immutable.cf @@ -62,7 +62,10 @@ bundle agent check { vars: "expected" string => ".*user:root:rwx.*"; - "acls" slist => getacls("$(global.testfile)", "access"); + + "acls" + slist => getacls("$(global.testfile)", "access"), + if => fileexists("$(global.testfile)"); classes: "ok" expression => some("$(expected)", "acls"); diff --git a/tests/acceptance/10_files/unsafe/14_immutable_content.cf b/tests/acceptance/10_files/unsafe/14_immutable_content.cf index ffaab2545df..0b01e255768 100644 --- a/tests/acceptance/10_files/unsafe/14_immutable_content.cf +++ b/tests/acceptance/10_files/unsafe/14_immutable_content.cf @@ -46,6 +46,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -61,7 +66,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/15_immutable_edit_line.cf b/tests/acceptance/10_files/unsafe/15_immutable_edit_line.cf index 45859a7fcdf..d3fbc9504bb 100644 --- a/tests/acceptance/10_files/unsafe/15_immutable_edit_line.cf +++ b/tests/acceptance/10_files/unsafe/15_immutable_edit_line.cf @@ -48,6 +48,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -63,7 +68,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/16_immutable_copy_from.cf b/tests/acceptance/10_files/unsafe/16_immutable_copy_from.cf index 0c8865bdecb..c718109c35a 100644 --- a/tests/acceptance/10_files/unsafe/16_immutable_copy_from.cf +++ b/tests/acceptance/10_files/unsafe/16_immutable_copy_from.cf @@ -49,6 +49,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -64,7 +69,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/17_immutable_template_mustache.cf b/tests/acceptance/10_files/unsafe/17_immutable_template_mustache.cf index b1f3963649a..5d2861bda61 100644 --- a/tests/acceptance/10_files/unsafe/17_immutable_template_mustache.cf +++ b/tests/acceptance/10_files/unsafe/17_immutable_template_mustache.cf @@ -49,6 +49,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -64,7 +69,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/18_immutable_template_cfengine.cf b/tests/acceptance/10_files/unsafe/18_immutable_template_cfengine.cf index 49beabe0694..a0a98401259 100644 --- a/tests/acceptance/10_files/unsafe/18_immutable_template_cfengine.cf +++ b/tests/acceptance/10_files/unsafe/18_immutable_template_cfengine.cf @@ -50,6 +50,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -65,7 +70,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/19_immutable_edit_xml.cf b/tests/acceptance/10_files/unsafe/19_immutable_edit_xml.cf index 4f08284cfac..a5ee0e8177c 100644 --- a/tests/acceptance/10_files/unsafe/19_immutable_edit_xml.cf +++ b/tests/acceptance/10_files/unsafe/19_immutable_edit_xml.cf @@ -48,6 +48,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -63,7 +68,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/20_immutable_template_inline_mustache.cf b/tests/acceptance/10_files/unsafe/20_immutable_template_inline_mustache.cf index 2adaad5c3e3..115f1c6650e 100644 --- a/tests/acceptance/10_files/unsafe/20_immutable_template_inline_mustache.cf +++ b/tests/acceptance/10_files/unsafe/20_immutable_template_inline_mustache.cf @@ -49,6 +49,11 @@ bundle agent check ), with => regex_replace("$(this.promise_filename)", "\.cf$", ".sub.cf", ""); + # Indent the sub-agent output in the DEBUG report, so that errors it + # prints are not mistaken for errors of the test by testall + "output_indented" + string => regex_replace("$(run[output])", "^", " | ", "gm"); + classes: "ok" expression => regcmp( @@ -64,7 +69,7 @@ bundle agent check DEBUG:: "$(this.bundle): agent exited '$(run[exit_code])', output was:"; - "$(run[output])"; + "$(output_indented)"; } bundle agent destroy diff --git a/tests/acceptance/10_files/unsafe/multiple-group-candidates-first-found-group-perms-if-no-listed-candidate-matches.cf b/tests/acceptance/10_files/unsafe/multiple-group-candidates-first-found-group-perms-if-no-listed-candidate-matches.cf index 00a8a5afc08..ab2172503ed 100644 --- a/tests/acceptance/10_files/unsafe/multiple-group-candidates-first-found-group-perms-if-no-listed-candidate-matches.cf +++ b/tests/acceptance/10_files/unsafe/multiple-group-candidates-first-found-group-perms-if-no-listed-candidate-matches.cf @@ -69,7 +69,10 @@ bundle agent check { vars: "observed_gid" string => filestat($(G.testfile), gid); - "desired_gid" int => getgid("pr_gr_1"); + + "desired_gid" + int => getgid("pr_gr_1"), + if => groupexists("pr_gr_1"); reports: "$(this.promise_filename) Pass" diff --git a/tests/acceptance/10_files/unsafe/multiple-owner-candidates-first-found-user-perms-if-no-listed-candidate-matches.cf b/tests/acceptance/10_files/unsafe/multiple-owner-candidates-first-found-user-perms-if-no-listed-candidate-matches.cf index 2d7325fc70a..2da83d66422 100644 --- a/tests/acceptance/10_files/unsafe/multiple-owner-candidates-first-found-user-perms-if-no-listed-candidate-matches.cf +++ b/tests/acceptance/10_files/unsafe/multiple-owner-candidates-first-found-user-perms-if-no-listed-candidate-matches.cf @@ -39,7 +39,10 @@ bundle agent check { vars: "observed_uid" string => filestat($(G.testfile), uid); - "desired_uid" int => getuid("pr_us_1"); + + "desired_uid" + int => getuid("pr_us_1"), + if => userexists("pr_us_1"); reports: "$(this.promise_filename) Pass" diff --git a/tests/acceptance/15_control/01_common/classic_evaluation_custom_promise_types.cf b/tests/acceptance/15_control/01_common/classic_evaluation_custom_promise_types.cf index 379663d0b9a..d5ea9e47962 100644 --- a/tests/acceptance/15_control/01_common/classic_evaluation_custom_promise_types.cf +++ b/tests/acceptance/15_control/01_common/classic_evaluation_custom_promise_types.cf @@ -1,13 +1,13 @@ body common control { inputs => { "../../default.sub.cf" }; - bundlesequence => { "test" }; + bundlesequence => { "init", "test", "cleanup" }; evaluation_order => "classic"; } promise agent dummy { - path => "$(this.promise_dirname)/top_down/dummy_promise_type.py"; + path => "$(this.promise_dirname)/dummy_promise_type.py"; interpreter => "/usr/bin/python3"; } @@ -23,13 +23,17 @@ bundle agent test "/tmp/dummyfile" smth => "hello"; vars: - "content" string => readfile("/tmp/dummyfile"); + "content" + string => readfile("/tmp/dummyfile"), + if => fileexists("/tmp/dummyfile"); files: "/tmp/dummyfile" content => "ok"; vars: - "content" string => readfile("/tmp/dummyfile"); + "content" + string => readfile("/tmp/dummyfile"), + if => fileexists("/tmp/dummyfile"); classes: "ok" expression => strcmp("$(content)", "ok"); diff --git a/tests/acceptance/15_control/01_common/classic_evaluation_long_policy.cf b/tests/acceptance/15_control/01_common/classic_evaluation_long_policy.cf index 077fffdbd95..1a06957f2e9 100644 --- a/tests/acceptance/15_control/01_common/classic_evaluation_long_policy.cf +++ b/tests/acceptance/15_control/01_common/classic_evaluation_long_policy.cf @@ -34,7 +34,10 @@ bundle agent test "/tmp/example" content => "fourth"; vars: - "content" string => readfile("/tmp/example"); + "content" + string => readfile("/tmp/example"), + if => fileexists("/tmp/example"); + "result" string => concat("$(result)", ", $(content)"); classes: @@ -48,7 +51,9 @@ bundle agent test "/tmp/otherfile" content => "something"; vars: - "othercontent" string => readfile("/tmp/otherfile"); + "othercontent" + string => readfile("/tmp/otherfile"), + if => fileexists("/tmp/otherfile"); files: "/tmp/otherfile" delete => tidy; diff --git a/tests/acceptance/15_control/01_common/dummy_promise_type.py b/tests/acceptance/15_control/01_common/dummy_promise_type.py new file mode 100644 index 00000000000..506733d056a --- /dev/null +++ b/tests/acceptance/15_control/01_common/dummy_promise_type.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 +# +# Minimal custom promise module which accepts any promise and does nothing, +# used to have a custom promise type in evaluation order tests. + +import sys + + +def read_request(): + request = {} + for line in sys.stdin: + line = line.rstrip("\n") + if not line: + return request + key, _, value = line.partition("=") + request[key] = value + sys.exit(0) + + +def write_response(operation, result): + sys.stdout.write("operation=%s\nresult=%s\n\n" % (operation, result)) + sys.stdout.flush() + + +# Skip the header from the agent and send ours +read_request() +sys.stdout.write("dummy_promise_type 0.0.1 v1 line_based\n\n") +sys.stdout.flush() + +while True: + operation = read_request().get("operation") + if operation == "validate_promise": + write_response(operation, "valid") + elif operation == "evaluate_promise": + write_response(operation, "kept") + elif operation == "terminate": + write_response(operation, "success") + sys.exit(0) + else: + write_response(operation, "error") diff --git a/tests/acceptance/15_control/01_common/top_down_evaluation_long_policy.cf b/tests/acceptance/15_control/01_common/top_down_evaluation_long_policy.cf index 5d85ecbb5a9..01151c1248f 100644 --- a/tests/acceptance/15_control/01_common/top_down_evaluation_long_policy.cf +++ b/tests/acceptance/15_control/01_common/top_down_evaluation_long_policy.cf @@ -34,7 +34,10 @@ bundle agent test "/tmp/example" content => "fourth"; vars: - "content" string => readfile("/tmp/example"); + "content" + string => readfile("/tmp/example"), + if => fileexists("/tmp/example"); + "result" string => concat("$(result)", ", $(content)"); classes: @@ -48,7 +51,9 @@ bundle agent test "/tmp/otherfile" content => "something"; vars: - "othercontent" string => readfile("/tmp/otherfile"); + "othercontent" + string => readfile("/tmp/otherfile"), + if => fileexists("/tmp/otherfile"); files: "/tmp/otherfile" delete => tidy; diff --git a/tests/acceptance/29_simulate_mode/diff_mode_ownership.cf b/tests/acceptance/29_simulate_mode/diff_mode_ownership.cf index 2fcde3b7ab2..80fb53de40c 100644 --- a/tests/acceptance/29_simulate_mode/diff_mode_ownership.cf +++ b/tests/acceptance/29_simulate_mode/diff_mode_ownership.cf @@ -36,7 +36,9 @@ bundle agent test bundle agent check { vars: - "actual" string => readfile("$(this.promise_filename).actual", inf); + "actual" + string => readfile("$(this.promise_filename).actual", inf), + if => fileexists("$(this.promise_filename).actual"); classes: "has_owner_change" diff --git a/tests/acceptance/29_simulate_mode/simulate_safe_functions.cf b/tests/acceptance/29_simulate_mode/simulate_safe_functions.cf index 289f5811d5a..484869b968b 100644 --- a/tests/acceptance/29_simulate_mode/simulate_safe_functions.cf +++ b/tests/acceptance/29_simulate_mode/simulate_safe_functions.cf @@ -56,6 +56,9 @@ bundle agent check "result" string => readfile( "$(sys.workdir)$(const.dirsep)state$(const.dirsep)agent.log" + ), + if => fileexists( + "$(sys.workdir)$(const.dirsep)state$(const.dirsep)agent.log" ); methods: diff --git a/tests/acceptance/31_tickets/CFE-3866/test.cf b/tests/acceptance/31_tickets/CFE-3866/test.cf index 5a8dd626451..dd8c0a1d98d 100644 --- a/tests/acceptance/31_tickets/CFE-3866/test.cf +++ b/tests/acceptance/31_tickets/CFE-3866/test.cf @@ -68,7 +68,10 @@ bundle agent check "expected" string => "[section] key=value "; - "actual" string => readfile("$(G.testfile)", "1000"); + + "actual" + string => readfile("$(G.testfile)", "1000"), + if => fileexists("$(G.testfile)"); classes: "ok" expression => strcmp("$(expected)", "$(actual)"); diff --git a/tests/acceptance/README b/tests/acceptance/README index 2435b57b224..b192cb5995c 100644 --- a/tests/acceptance/README +++ b/tests/acceptance/README @@ -113,6 +113,13 @@ case letters and underscores only. If the test is expected to generate an error additional '.x' suffix before '.cf' (e.g. 'string_vars.x.cf'). A crash will still cause a failure. +Regular tests (without any special suffix) fail if they log any errors, even +if they print the Pass message. If the test is expected to log errors (e.g. it +contains promises which are intentionally not kept), it should have an +additional '.error' suffix before '.cf' (e.g. 'bad_perms.error.cf'), in which +case it passes as long as it logs at least one error and exits with 0. If it +should also print the Pass message, use the '.error.pass.cf' suffix instead. + Tests which are not expected to pass yet (e.g. there is a bug in code which prevents tests from passing) should be placed in 'staging' subdirectory in the test directory where they belong. Such test cases will be only run if --staging diff --git a/tests/acceptance/dcs.sub.cf b/tests/acceptance/dcs.sub.cf index e752cf24c7d..33dd9422428 100644 --- a/tests/acceptance/dcs.sub.cf +++ b/tests/acceptance/dcs.sub.cf @@ -1097,6 +1097,10 @@ bundle agent dcs_passif_output(wanted, unwanted, command, test) string => execresult($(command), "useshell"), meta => { "simulate_safe" }; + # Indent the command output in the DEBUG report, so that errors printed + # by the command are not mistaken for errors of the test by testall + "subout_indented" string => regex_replace($(subout), "^", " | ", "gm"); + classes: "__passif_output_unwanted" expression => regcmp($(unwanted), $(subout)); "__passif_output_wanted" expression => regcmp($(wanted), $(subout)); @@ -1128,7 +1132,7 @@ bundle agent dcs_passif_output(wanted, unwanted, command, test) if => "!__passif_output_unwanted"; DEBUG:: - "$(this.bundle): the output of command '$(command)' was: '$(subout)'"; + "$(this.bundle): the output of command '$(command)' was: '$(subout_indented)'"; } bundle agent dcs_passif_output1(wanted, command, test) diff --git a/tests/acceptance/testall b/tests/acceptance/testall index b35bb1a962e..ed78891e731 100755 --- a/tests/acceptance/testall +++ b/tests/acceptance/testall @@ -217,6 +217,8 @@ usage() { echo "If no test is given, all standard tests are run:" echo " Tests with names of form .cf are expected to run successfully" echo " Tests with names of form .x.cf are expected to exit with error" + echo " Tests with names of form .error.cf are expected to log error(s) and exit with 0" + echo " Tests with names of form .error.pass.cf are expected to log error(s), pass and exit with 0" echo "Set ${COLOR_SUCCESS}CFENGINE_COLOR=1${COLOR_NORMAL} to get ANSI color markers where appropriate." echo echo "If arguments are given, those are executed as tests" @@ -574,26 +576,8 @@ export CFENGINE_TEST_OVERRIDE_WORKDIR TEMP CFENGINE_TEST_OVERRIDE_EXTENSION_LIBR RESULT=FAIL RESULT_MSG="${COLOR_FAILURE}FAIL (No errors printed)${COLOR_NORMAL}" fi - elif [ $TEST_TYPE = errorpass ] - then - ESCAPED_TEST="$(echo "($TEST|dcs.sub.cf)" | sed -e 's/\./\\./g')" - if [ $RETVAL != 0 ] - then - RESULT=FAIL - RESULT_MSG="${COLOR_FAILURE}FAIL (NON-ZERO EXIT CODE!)${COLOR_NORMAL}" - elif ! grep -F 'error: ' "$OUTFILE" > /dev/null - then - RESULT=FAIL - RESULT_MSG="${COLOR_FAILURE}FAIL (No error message!)${COLOR_NORMAL}" - elif ! grep -E "R: .*$ESCAPED_TEST Pass" $OUTFILE > /dev/null - then - RESULT=FAIL - RESULT_MSG="${COLOR_FAILURE}FAIL (Pass not printed!)${COLOR_NORMAL}" - else - RESULT=Pass - RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}" - fi - else # TEST_TYPE is not errorlog or errorexit or errorpass + else # TEST_TYPE is not errorlog or errorexit (errorpass is handled here + # too, so that soft fails and skips work for it) # We need to be careful when matching test outcomes. Because of convergence # passes, a test may output FAIL before it outputs Pass; in this case the # latter trumps the former. Also be careful when matching an [XS]FAIL; the @@ -646,8 +630,26 @@ export CFENGINE_TEST_OVERRIDE_WORKDIR TEMP CFENGINE_TEST_OVERRIDE_EXTENSION_LIBR fi elif grep -E "R: .*$ESCAPED_TEST Pass" $OUTFILE > /dev/null then - RESULT=Pass - RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}" + # Regular tests must not log errors, tests which are expected + # to log errors should be named *.error.cf or *.error.pass.cf + if [ $TEST_TYPE = errorpass ] + then + if grep -F 'error: ' "$OUTFILE" > /dev/null + then + RESULT=Pass + RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}" + else + RESULT=FAIL + RESULT_MSG="${COLOR_FAILURE}FAIL (No error message!)${COLOR_NORMAL}" + fi + elif grep -E '^ *error: ' $OUTFILE > /dev/null + then + RESULT=FAIL + RESULT_MSG="${COLOR_FAILURE}FAIL (Error printed)${COLOR_NORMAL}" + else + RESULT=Pass + RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}" + fi elif grep -E "R: .*$ESCAPED_TEST Skip/unsupported" $OUTFILE > /dev/null then RESULT=Skip diff --git a/tests/reactor/Makefile.am b/tests/reactor/Makefile.am new file mode 100644 index 00000000000..eb5325d0fbf --- /dev/null +++ b/tests/reactor/Makefile.am @@ -0,0 +1,44 @@ +# +# Copyright 2026 Northern.tech AS +# +# This file is part of CFEngine 3 - written and maintained by Northern.tech AS. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA +# +# To the extent this program is licensed as part of the Enterprise +# versions of CFEngine, the applicable Commercial Open Source License +# (COSL) may apply to this file if you as a licensee so wish it. See +# included file COSL.txt. +# + +# The test scripts, with their policy next to them +REACTOR_TEST_SH = $(sort $(wildcard \ + $(srcdir)/tests/*/*.sh \ + $(srcdir)/tests/*/unsafe/*.sh)) +REACTOR_TEST_CF = $(REACTOR_TEST_SH:.sh=.cf) + +# cf-reactor is not built with builtin extensions (e.g. on Windows) +if !BUILTIN_EXTENSIONS +TESTS = $(patsubst $(srcdir)/%,%,$(REACTOR_TEST_SH)) +endif + +# Each test is run by run_test.sh, with the binaries of this build. The ones +# in an unsafe/ directory are skipped unless UNSAFE_TESTS=1. +TESTS_ENVIRONMENT = env \ + CF_REACTOR=$(abs_top_builddir)/cf-reactor/cf-reactor \ + CF_PROMISES=$(abs_top_builddir)/cf-promises/cf-promises \ + $(SHELL) $(srcdir)/run_test.sh + +EXTRA_DIST = lib.cf lib.sh run.sh run_test.sh \ + $(patsubst $(srcdir)/%,%,$(REACTOR_TEST_SH) $(REACTOR_TEST_CF)) diff --git a/tests/reactor/lib.sh b/tests/reactor/lib.sh index f49a25905a2..d9754155079 100644 --- a/tests/reactor/lib.sh +++ b/tests/reactor/lib.sh @@ -1,21 +1,81 @@ # Helpers sourced by the tests. Expects CF_REACTOR and LOG to be exported by -# run.sh. +# run_test.sh. +# +# Only POSIX tools are used, as the tests are run on all the platforms (e.g. +# no timeout(1), grep -m or date +%s). + +# Seconds to wait for something before giving up +WAIT_TIMEOUT=60 # number of log lines already consumed by wait_for_log log_offset=0 +# Run command $@ until it succeeds, for at most WAIT_TIMEOUT seconds, polling +# every 0.1 second, or every second where sleep only takes whole seconds. +# Fails if it never succeeded. Runs in a subshell without tracing, so that +# set -x doesn't log every poll. +wait_until() { + ( + set +x + if sleep 0.1 2>/dev/null; then + interval=0.1 polls=$((WAIT_TIMEOUT * 10)) + else + interval=1 polls=$WAIT_TIMEOUT + fi + until "$@"; do + [ "$polls" -gt 0 ] || exit 1 + polls=$((polls - 1)) + sleep "$interval" + done + ) +} + +# Print the current time in seconds since the Epoch. date +%s is not POSIX; +# without it, srand() seeds with the time of day and returns the last seed. +now() { + t=$(date +%s 2>/dev/null) + case "$t" in + ''|*[!0-9]*) awk 'BEGIN { srand(); print srand() }' ;; + *) echo "$t" ;; + esac +} + +# Print the first line of the reactor log after line $1 that contains $2, +# prefixed with its number counted from line $1. Fails if there is none. +log_match() { + tail -n "+$(($1 + 1))" "$LOG" 2>/dev/null | grep -n -F -- "$2" | head -n 1 | grep . +} + +# Fail unless the reactor log contains $1. +log_contains() { + grep -q -F -- "$1" "$LOG" 2>/dev/null +} + +# Print the number of lines of file $1, 0 if it doesn't exist. +count_lines() { + if [ -f "$1" ]; then + # without the padding of some wc implementations + echo $(( $(wc -l < "$1") )) + else + echo 0 + fi +} + +# Fail unless file $1 has at least $2 lines. +has_lines() { + [ "$(count_lines "$1")" -ge "$2" ] +} + # Wait until the reactor log contains $1 in a line after the last match, then # skip past that line, so that the next wait only sees newer output. wait_for_log() { - if ! match=$(timeout 60 sh -c ' - until tail -n "+$(($2 + 1))" "$1" | grep -n -F -m 1 -- "$3"; do - sleep 0.1 - done' _ "$LOG" "$log_offset" "$1") + if ! wait_until log_match "$log_offset" "$1" >/dev/null then echo "Timed out waiting for '$1' in reactor log:" >&2 cat "$LOG" >&2 return 1 fi + match=$(log_match "$log_offset" "$1") log_offset=$((log_offset + ${match%%:*})) } @@ -23,10 +83,7 @@ wait_for_log() { # The watcher thread may log before this line, so search the whole log and # leave the offset of wait_for_log as is. wait_for_watchers() { - if ! timeout 60 sh -c ' - until grep -q -F -- "$2" "$1"; do - sleep 0.1 - done' _ "$LOG" "verbose: cf-reactor event watcher has $1 watcher(s)" + if ! wait_until log_contains "verbose: cf-reactor event watcher has $1 watcher(s)" then echo "Timed out waiting for $1 watcher(s) in reactor log:" >&2 cat "$LOG" >&2 @@ -36,17 +93,14 @@ wait_for_watchers() { # Wait until file $1 has at least $2 lines, then fail if it has more than $2. wait_for_lines() { - if ! timeout 60 sh -c ' - until [ "$(cat "$1" 2>/dev/null | wc -l)" -ge "$2" ]; do - sleep 0.1 - done' _ "$1" "$2" + if ! wait_until has_lines "$1" "$2" then echo "Timed out waiting for $2 line(s) in '$1', reactor log:" >&2 cat "$LOG" >&2 return 1 fi - lines=$(wc -l < "$1") + lines=$(count_lines "$1") if [ "$lines" -ne "$2" ] then echo "Expected $2 line(s) in '$1', got $lines" >&2 diff --git a/tests/reactor/run.sh b/tests/reactor/run.sh index 17e21f198c5..8394314c5d6 100644 --- a/tests/reactor/run.sh +++ b/tests/reactor/run.sh @@ -2,96 +2,79 @@ # Usage: run.sh [--unsafe] # -# Must be run from the tests/reactor directory. +# Runs all the reactor tests, see run_test.sh, and prints a summary of their +# results. The output of a test is only printed if it failed. They are also +# run by "make check". # # Tests in an unsafe/ directory modify the system they are running on, so they -# are only run with --unsafe. +# are skipped unless run with --unsafe (or UNSAFE_TESTS=1). # # The binaries are taken from $CF_BINDIR if it is set, otherwise from the # CFEngine build tree this directory is part of if they are built, otherwise # from /var/cfengine/bin. -fail=0 - -unsafe=0 if [ "$1" = "--unsafe" ]; then - unsafe=1 + UNSAFE_TESTS=1 fi - -banner() { - name=" $(basename "$(dirname "$1")")/$(basename "$1") " - width=72 - left=$(( (width - ${#name}) / 2 )) - right=$(( width - ${#name} - left )) - [ "$left" -lt 0 ] && left=0 - [ "$right" -lt 0 ] && right=0 - printf '%s%s%s\n' \ - "$(printf "%${left}s" '' | tr ' ' '=')" \ - "$name" \ - "$(printf "%${right}s" '' | tr ' ' '=')" -} - -error() { - echo "test '$1' FAILED" >&2 - fail=1 -} - -# Print the path of binary $1, see the usage above. -find_binary() { - if [ -n "$CF_BINDIR" ]; then - echo "$CF_BINDIR/$1" - elif [ -x "$PWD/../../$1/$1" ]; then - # in-tree build, may be a libtool wrapper script - echo "$PWD/../../$1/$1" - else - echo "/var/cfengine/bin/$1" - fi +export UNSAFE_TESTS + +dir=$(cd "$(dirname "$0")" && pwd) + +# for now() +. "$dir/lib.sh" + +passed=0 +failed=0 +skipped=0 +total=0 +failures="" +suite_start=$(now) + +# Run test $1 and print its result, with its output if it failed +run_test() { + name=${1#"$dir/tests/"} + name=${name%.sh} + total=$((total + 1)) + + start=$(now) + output=$(sh "$dir/run_test.sh" "$1" 2>&1) + result=$? + elapsed=$(($(now) - start)) + + case $result in + 0) + passed=$((passed + 1)) + echo "--- PASS: $name (${elapsed}s)" + ;; + 77) + skipped=$((skipped + 1)) + echo "--- SKIP: $name (${elapsed}s)" + ;; + *) + failed=$((failed + 1)) + failures="$failures $name +" + echo "--- FAIL: $name (${elapsed}s)" + echo "$output" + echo "---" + ;; + esac } -CF_REACTOR=$(find_binary cf-reactor) -CF_PROMISES=$(find_binary cf-promises) -export CF_REACTOR - -for b in "$CF_REACTOR" "$CF_PROMISES"; do - if [ ! -x "$b" ]; then - echo "'$b' not found, build CFEngine or set CF_BINDIR" >&2 - exit 2 - fi -done -echo "Using $CF_REACTOR and $CF_PROMISES" - -for t in "$PWD"/tests/*/*.sh "$PWD"/tests/*/unsafe/*.sh; do +for t in "$dir"/tests/*/*.sh "$dir"/tests/*/unsafe/*.sh; do # skip the pattern itself if there are no unsafe tests [ -f "$t" ] || continue - case "$t" in - */unsafe/*) [ "$unsafe" = 1 ] || continue ;; - esac - - banner "$t" - - # create throwaway work dir - WORKDIR=$(mktemp -d) - mkdir -p "$WORKDIR/bin" "$WORKDIR/tmp" - ln -s "$CF_PROMISES" "$WORKDIR/bin/cf-promises" - - # Run a copy of the policy, keeping its path relative to lib.cf, as - # CFEngine refuses to read policy files writable by group or others, which - # those of a checkout made with a umask of 002 are - rel=${t#"$PWD"/} - policy="$WORKDIR/policy/${rel%.sh}.cf" - mkdir -p "$(dirname "$policy")" - cp lib.cf "$WORKDIR/policy/" - cp "${t%.sh}.cf" "$policy" - chmod 600 "$WORKDIR/policy/lib.cf" "$policy" - - CFENGINE_TEST_OVERRIDE_WORKDIR="$WORKDIR" - LOG="$WORKDIR/reactor.log" - PROMISE_NAME=$(basename "$t" .sh) - export WORKDIR CFENGINE_TEST_OVERRIDE_WORKDIR LOG PROMISE_NAME - - sh "$t" "$policy" || error "$t" - - rm -rf "$WORKDIR" + run_test "$t" done -exit $fail +echo "" +echo "==============================" +echo "Test Results: $passed passed, $failed failed, $skipped skipped (total: $total, $(($(now) - suite_start))s)" +if [ "$failed" -gt 0 ]; then + echo "" + echo "Failed tests:" + printf '%s' "$failures" + exit 1 +fi +echo "==============================" +echo "All reactor tests passed" diff --git a/tests/reactor/run_test.sh b/tests/reactor/run_test.sh new file mode 100644 index 00000000000..dc4b66fb79b --- /dev/null +++ b/tests/reactor/run_test.sh @@ -0,0 +1,100 @@ +#!/bin/sh + +# Usage: run_test.sh TEST +# +# Runs the reactor test TEST (a tests/*/*.sh or tests/*/unsafe/*.sh file) in a +# throwaway work dir, and exits with its result: 0 if it passed, 77 if it was +# skipped, anything else if it failed. Used as the test driver by "make check" +# and by run.sh. +# +# The output of the test, with its set -x trace, is only printed if it didn't +# pass, so that the output of the tests that pass doesn't bury it. +# +# Tests in an unsafe/ directory modify the system they are running on, so they +# are skipped unless UNSAFE_TESTS=1. +# +# The binaries are taken from $CF_REACTOR and $CF_PROMISES if they are set, +# otherwise from $CF_BINDIR if it is set, otherwise from the CFEngine build +# tree this directory is part of if they are built, otherwise from +# /var/cfengine/bin. + +if [ $# -ne 1 ]; then + echo "Usage: $0 TEST" >&2 + exit 2 +fi + +t=$1 + +# Explicitly use POSIX tools if needed (Solaris) +if [ -f /usr/xpg4/bin/grep ]; then + PATH=/usr/xpg4/bin:$PATH + export PATH +fi + +case "$t" in + */unsafe/*) + if [ "$UNSAFE_TESTS" != 1 ]; then + echo "Skipping unsafe test '$t', set UNSAFE_TESTS=1 to run it" + exit 77 + fi + ;; +esac + +# directory of lib.cf and lib.sh +REACTOR_TESTS_DIR=$(cd "$(dirname "$0")" && pwd) + +# Print the path of binary $1, see the usage above. +find_binary() { + if [ -n "$CF_BINDIR" ]; then + echo "$CF_BINDIR/$1" + elif [ -x "$REACTOR_TESTS_DIR/../../$1/$1" ]; then + # in-tree build, may be a libtool wrapper script + echo "$REACTOR_TESTS_DIR/../../$1/$1" + else + echo "/var/cfengine/bin/$1" + fi +} + +CF_REACTOR=${CF_REACTOR:-$(find_binary cf-reactor)} +CF_PROMISES=${CF_PROMISES:-$(find_binary cf-promises)} +export CF_REACTOR + +for b in "$CF_REACTOR" "$CF_PROMISES"; do + if [ ! -x "$b" ]; then + echo "'$b' not found, build CFEngine or set CF_BINDIR" >&2 + exit 2 + fi +done + +# Create the throwaway work dir, with mktemp -d where available +WORKDIR=$(mktemp -d "${TMPDIR:-/tmp}/cf-reactor-test.XXXXXX" 2>/dev/null) || { + WORKDIR="${TMPDIR:-/tmp}/cf-reactor-test.$$" + (umask 077 && mkdir "$WORKDIR") || exit 2 +} +WORKDIR=$(cd "$WORKDIR" && pwd) +trap 'rm -rf "$WORKDIR"' EXIT +mkdir -p "$WORKDIR/bin" "$WORKDIR/tmp" +ln -s "$CF_PROMISES" "$WORKDIR/bin/cf-promises" + +# Run a copy of the policy, keeping its path relative to lib.cf, as CFEngine +# refuses to read policy files writable by group or others, which those of a +# checkout made with a umask of 002 are +p="/$t" +rel="tests/${p##*/tests/}" +policy="$WORKDIR/policy/${rel%.sh}.cf" +mkdir -p "$(dirname "$policy")" +cp "$REACTOR_TESTS_DIR/lib.cf" "$WORKDIR/policy/" +cp "${t%.sh}.cf" "$policy" +chmod 600 "$WORKDIR/policy/lib.cf" "$policy" + +CFENGINE_TEST_OVERRIDE_WORKDIR="$WORKDIR" +LOG="$WORKDIR/reactor.log" +PROMISE_NAME=$(basename "$t" .sh) +export WORKDIR CFENGINE_TEST_OVERRIDE_WORKDIR LOG PROMISE_NAME + +sh "$t" "$policy" > "$WORKDIR/test.log" 2>&1 +result=$? +if [ "$result" -ne 0 ]; then + cat "$WORKDIR/test.log" +fi +exit "$result" diff --git a/tests/reactor/tests/00_basics/009_interval.sh b/tests/reactor/tests/00_basics/009_interval.sh index 4a749849370..df73a4cf09d 100644 --- a/tests/reactor/tests/00_basics/009_interval.sh +++ b/tests/reactor/tests/00_basics/009_interval.sh @@ -19,7 +19,7 @@ wait_for_log "File '$TESTFILE' is present, watching for its deletion" rm "$TESTFILE" wait_for_lines "$EVENTS_FILE" 1 check_event "$EVENTS_FILE" 1 "$TESTFILE was deleted" -t1=$(date +%s) +t1=$(now) # recreate the file and wait for the watcher to see it before deleting it again touch "$TESTFILE" @@ -27,7 +27,7 @@ wait_for_log "File '$TESTFILE' is present, watching for its deletion" rm "$TESTFILE" wait_for_lines "$EVENTS_FILE" 2 check_event "$EVENTS_FILE" 2 "$TESTFILE was deleted" -t2=$(date +%s) +t2=$(now) # checking that the interval is at minimum 10 seconds delta=$((t2 - t1)) diff --git a/tests/reactor/tests/00_basics/020_policy_reload.sh b/tests/reactor/tests/00_basics/020_policy_reload.sh index 48348e72338..d6476072c9d 100644 --- a/tests/reactor/tests/00_basics/020_policy_reload.sh +++ b/tests/reactor/tests/00_basics/020_policy_reload.sh @@ -30,7 +30,7 @@ reload_and_wait_for_paused_watchers() { kill -HUP "$reactor_pid" wait_for_log "Rereading policy file" - if ! timeout 60 sh -c 'until [ -f "$1" ]; do sleep 0.1; done' _ "$RELOAD_MARKER" + if ! wait_until [ -f "$RELOAD_MARKER" ] then echo "Timed out waiting for the policy to be reloaded, reactor log:" >&2 cat "$LOG" >&2 diff --git a/tests/reactor/tests/01_event_types/unsafe/008_user_events.cf b/tests/reactor/tests/01_event_types/unsafe/008_user_events.cf new file mode 100644 index 00000000000..3e2352168fa --- /dev/null +++ b/tests/reactor/tests/01_event_types/unsafe/008_user_events.cf @@ -0,0 +1,38 @@ +####################################################### +# +# Test the user_added and user_removed events +# +####################################################### +body common control +{ + inputs => { "../../../lib.cf" }; + bundlesequence => { "test" }; + version => "1.0"; +} + +body when added(user) +{ + user_added => "$(user)"; +} + +body when removed(user) +{ + user_removed => "$(user)"; +} + +bundle reactor test +{ + vars: + "test_user" string => getenv("TEST_USER", 999); + "events_file" string => getenv("EVENTS_FILE", 999); + "promise_name" string => basename("$(this.promise_filename)", ".cf"); + + events: + "$(promise_name)_added" + when => added("$(test_user)"), + then => record_event("$(events_file)", "added"); + + "$(promise_name)_removed" + when => removed("$(test_user)"), + then => record_event("$(events_file)", "removed"); +} diff --git a/tests/reactor/tests/01_event_types/unsafe/008_user_events.sh b/tests/reactor/tests/01_event_types/unsafe/008_user_events.sh new file mode 100644 index 00000000000..dd271c696e6 --- /dev/null +++ b/tests/reactor/tests/01_event_types/unsafe/008_user_events.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +# check that user_added and user_removed fire once when a user is added and +# removed. Adds and removes a user with sudo. + +set -ex + +. "$(dirname "$0")/../../../lib.sh" + +POLICY_FILE="$1" + +TEST_USER="cfreactortest" +EVENTS_FILE="$WORKDIR/tmp/events" +export TEST_USER EVENTS_FILE + +# never touch an existing user +if id "$TEST_USER" >/dev/null 2>&1 +then + echo "User '$TEST_USER' already exists" >&2 + exit 1 +fi + +start_reactor "$POLICY_FILE" + +# replaces the trap of start_reactor +cleanup() { + stop_reactor + sudo userdel "$TEST_USER" 2>/dev/null || true +} +trap cleanup EXIT + +wait_for_watchers 2 + +# checked on registration +wait_for_log "User '$TEST_USER' is absent, watching for its addition" + +sudo useradd -M "$TEST_USER" +wait_for_lines "$EVENTS_FILE" 1 +check_event "$EVENTS_FILE" 1 "added" + +wait_for_log "User '$TEST_USER' is present, watching for its removal" + +sudo userdel "$TEST_USER" +wait_for_lines "$EVENTS_FILE" 2 +check_event "$EVENTS_FILE" 2 "removed" diff --git a/tests/reactor/tests/02_then_bundles/005_users.cf b/tests/reactor/tests/02_then_bundles/005_users.cf index 29bbb8063fd..d696e8ecefe 100644 --- a/tests/reactor/tests/02_then_bundles/005_users.cf +++ b/tests/reactor/tests/02_then_bundles/005_users.cf @@ -15,12 +15,15 @@ body when deleted(file) file_deleted => "$(file)"; } -bundle reactor run_users(user) +bundle reactor run_users { users: - # the user running the test, so that it is kept without being root - "$(user)" - policy => "present", + # a user that doesn't exist, so that the promise is kept without changing + # anything, whether or not the test runs as root. An existing user with + # policy "present" is unlocked if its password is locked, which happens + # to root on some systems. + "cf-reactor-test-no-such-user" + policy => "absent", classes => then_outcome("users"); } @@ -29,13 +32,10 @@ bundle reactor test vars: "test" string => getenv("TESTFILE", 999); "events_file" string => getenv("EVENTS_FILE", 999); - "user" string => getenv("TEST_USER", 999); "promise_name" string => basename("$(this.promise_filename)", ".cf"); events: "$(promise_name)" when => deleted("$(test)"), - then => { - run_users("$(user)"), record_then_outcome("$(events_file)", "users") - }; + then => { run_users, record_then_outcome("$(events_file)", "users") }; } diff --git a/tests/reactor/tests/02_then_bundles/005_users.sh b/tests/reactor/tests/02_then_bundles/005_users.sh index 84c8a509110..9601df9f8dd 100644 --- a/tests/reactor/tests/02_then_bundles/005_users.sh +++ b/tests/reactor/tests/02_then_bundles/005_users.sh @@ -13,9 +13,6 @@ EVENTS_FILE="$WORKDIR/tmp/events" export TESTFILE EVENTS_FILE -TEST_USER=$(id -un) -export TEST_USER - touch "$TESTFILE" start_reactor "$POLICY_FILE" diff --git a/tests/reactor/tests/02_then_bundles/008_guest_environments.cf b/tests/reactor/tests/02_then_bundles/008_guest_environments.cf index 9cbb424c202..eb03bec6746 100644 --- a/tests/reactor/tests/02_then_bundles/008_guest_environments.cf +++ b/tests/reactor/tests/02_then_bundles/008_guest_environments.cf @@ -24,7 +24,7 @@ bundle reactor run_guest_environments # no classes body here: the test looks at the log instead. "test" environment_type => "test", - environment_host => "$(sys.host)", + environment_host => canonify("$(sys.host)"), environment_state => "running"; } diff --git a/tests/reactor/tests/02_then_bundles/010_processes.sh b/tests/reactor/tests/02_then_bundles/010_processes.sh index b653eb059e9..ea0aa104fef 100644 --- a/tests/reactor/tests/02_then_bundles/010_processes.sh +++ b/tests/reactor/tests/02_then_bundles/010_processes.sh @@ -21,8 +21,13 @@ start_process() { ( sleep 978654 & echo $! > "$PID_FILE" ) } +# Fail if process $1 still exists +process_gone() { + ! kill -0 "$1" 2>/dev/null +} + wait_for_process_killed() { - if ! timeout 60 sh -c 'while kill -0 "$1" 2>/dev/null; do sleep 0.1; done' _ "$(cat "$PID_FILE")" + if ! wait_until process_gone "$(cat "$PID_FILE")" then echo "Process $(cat "$PID_FILE") not killed, reactor log:" >&2 cat "$LOG" >&2 diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 42e9128a522..d46e5af1ce5 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -159,6 +159,7 @@ check_PROGRAMS = \ client_code_test \ watcher_test \ file_watcher_test \ + user_watcher_test \ stoppable_thread_test \ wakeup_channel_test @@ -171,6 +172,10 @@ endif endif if !NT +check_PROGRAMS += timeout_test +timeout_test_SOURCES = timeout_test.c +timeout_test_LDADD = ../../libpromises/libpromises.la libtest.la + check_PROGRAMS += redirection_test noinst_PROGRAMS = redirection_test_stub @@ -241,6 +246,8 @@ watcher_test_SOURCES = watcher_test.c \ file_watcher_test_SOURCES = file_watcher_test.c \ ../../cf-reactor/file_watcher.c +user_watcher_test_SOURCES = user_watcher_test.c + stoppable_thread_test_SOURCES = stoppable_thread_test.c \ ../../cf-reactor/stoppable_thread.c diff --git a/tests/unit/file_watcher_test.c b/tests/unit/file_watcher_test.c index 51bdb0ccf4f..9ff463f88bb 100644 --- a/tests/unit/file_watcher_test.c +++ b/tests/unit/file_watcher_test.c @@ -28,7 +28,7 @@ static void *NewFileDeletedState(const char *path) { Rlist *args = NULL; RlistAppendScalar(&args, path); - void *state = FileWatcherStateNew(args); + void *state = FileWatcherStateNew(NULL, args); RlistDestroy(args); return state; } diff --git a/tests/unit/nfs_test.c b/tests/unit/nfs_test.c index 2e0c403e812..078b8a7756a 100644 --- a/tests/unit/nfs_test.c +++ b/tests/unit/nfs_test.c @@ -174,6 +174,97 @@ static void test_GetFstabEntryOptions(void) assert_true(GetFstabEntryOptions("/mnt/opts/nosuchmount") == NULL); } +/* Only here to interrupt a read: SA_RESTART is deliberately not set. */ +static void Interrupt(int signum) +{ + (void) signum; +} + +/* Runs LoadMountInfo() with a shell script from stdin in place of the mount + * command, and says whether it returned true and whether a timeout was + * still armed when it returned (which it must not be: the alarm would expire + * later, and terminate whatever child is running by then). + * + * This relies on the mount command of the Windows NT class being "/bin/sh + * /etc/fstab", which LoadMountInfo() cuts at the first space. If that row of + * the table in nfs.c changes, so does the meaning of these tests. */ +static void RunLoadMountInfo(const char *script, bool *loaded, + unsigned int *still_armed) +{ + int script_pipe[2]; + assert_int_equal(pipe(script_pipe), 0); + assert_int_equal(write(script_pipe[1], script, strlen(script)), + strlen(script)); + close(script_pipe[1]); + int saved_stdin = dup(STDIN_FILENO); + assert_true(dup2(script_pipe[0], STDIN_FILENO) == STDIN_FILENO); + close(script_pipe[0]); + + const PlatformContext saved_class = VSYSTEMHARDCLASS; + VSYSTEMHARDCLASS = PLATFORM_CONTEXT_WINDOWS_NT; + + Seq *list = SeqNew(1, NULL); + *loaded = LoadMountInfo(list); + *still_armed = alarm(0); + + SeqDestroy(list); + VSYSTEMHARDCLASS = saved_class; + signal(SIGALRM, SIG_DFL); + if (saved_stdin >= 0) + { + dup2(saved_stdin, STDIN_FILENO); + close(saved_stdin); + } +} + +/* The early return made by a line of output that mentions an RPC failure. */ +static void test_LoadMountInfo_disarms_timeout_on_rpc_error(void) +{ + bool loaded = true; + unsigned int still_armed = 1; + RunLoadMountInfo("echo 'RPC: Timed out'\n", &loaded, &still_armed); + + assert_false(loaded); + assert_int_equal(still_armed, 0); +} + +/* The early return made by a read error, which is made here by interrupting + * the read of the command's output with a signal. The script signals us + * once we are blocked in that read, which stays empty: it waits until ps + * shows us asleep (we have nothing else to sleep on between the fork and the + * read), then signals three times in case that sleep was not the read. All + * of this is over before the script exits, so the handler is still in place + * for every signal. */ +static void test_LoadMountInfo_disarms_timeout_on_read_error(void) +{ + const char script[] = + "n=0\n" + "while [ $n -lt 600 ]\n" + "do\n" + " case $(ps -o stat= -p $PPID) in *S*) break;; esac\n" + " sleep 0.1\n" + " n=$((n + 1))\n" + "done\n" + "for i in 1 2 3\n" + "do\n" + " kill -USR1 $PPID\n" + " sleep 0.1\n" + "done\n"; + + struct sigaction interrupt = { .sa_handler = Interrupt }; + sigemptyset(&interrupt.sa_mask); + struct sigaction previous; + assert_int_equal(sigaction(SIGUSR1, &interrupt, &previous), 0); + + bool loaded = true; + unsigned int still_armed = 1; + RunLoadMountInfo(script, &loaded, &still_armed); + sigaction(SIGUSR1, &previous, NULL); + + assert_false(loaded); + assert_int_equal(still_armed, 0); +} + int main() { PRINT_TEST_BANNER(); @@ -183,6 +274,8 @@ int main() unit_test(test_OptionStringExpandDefaults), unit_test(test_MountOptionsFromLine), unit_test(test_GetFstabEntryOptions), + unit_test(test_LoadMountInfo_disarms_timeout_on_rpc_error), + unit_test(test_LoadMountInfo_disarms_timeout_on_read_error), }; return run_tests(tests); diff --git a/tests/unit/timeout_test.c b/tests/unit/timeout_test.c new file mode 100644 index 00000000000..f971847af37 --- /dev/null +++ b/tests/unit/timeout_test.c @@ -0,0 +1,95 @@ +#include + +#include +#include +#include +#include + +/* Runs "/bin/sleep " under the way the agent reads a command's + * output, which ends when the child does, and returns how long it took. No + * shell here, as a shell terminated on its own would leave its child holding + * the pipe open. */ +static time_t RunSleep(const char *command) +{ + time_t start = time(NULL); + FILE *pfp = cf_popen(command, "r", true); + assert_true(pfp != NULL); + + char line[16]; + assert_true(fgets(line, sizeof(line), pfp) == NULL); + cf_pclose(pfp); + + return time(NULL) - start; +} + +/* A timeout must still bound a child forked after it would already have + * expired: the child is terminated one timeout after it started. */ +static void test_timeout_survives_a_stall_before_fork(void) +{ + SetChildTimeOut(1); + + /* Stands in for the agent stalling between arming and forking. */ + sleep(2); + + time_t elapsed = RunSleep("/bin/sleep 20"); + + DisarmTimeOut(); + + assert_true(elapsed < 15); +} + +/* SetTimeOut() is for callers that never fork, so it arms at once; only + * SetChildTimeOut() waits for a child. DisarmTimeOut() undoes both. */ +static void test_SetTimeOut_arms_at_once_SetChildTimeOut_waits(void) +{ + SetTimeOut(60); + assert_true(alarm(0) > 0); + DisarmTimeOut(); + assert_int_equal(alarm(0), 0); + + SetChildTimeOut(60); + assert_int_equal(alarm(0), 0); + DisarmTimeOut(); +} + +/* A request dropped by DisarmTimeOut() must not start later, whoever installs + * the handler again in the meantime. */ +static void test_disarmed_timeout_does_not_start_later(void) +{ + SetChildTimeOut(1); + DisarmTimeOut(); + signal(SIGALRM, (void *) TimeOut); + + time_t elapsed = RunSleep("/bin/sleep 3"); + + DisarmTimeOut(); + + assert_true(elapsed >= 3); +} + +/* A request is for one cf_popen*() call only: one that fails before forking + * must not leave the request for the next, unrelated, child. */ +static void test_failed_popen_does_not_leave_timeout_for_next_child(void) +{ + SetChildTimeOut(1); + assert_true(cf_popen("/bin/true", "x", true) == NULL); + + time_t elapsed = RunSleep("/bin/sleep 3"); + + DisarmTimeOut(); + + assert_true(elapsed >= 3); +} + +int main() +{ + PRINT_TEST_BANNER(); + const UnitTest tests[] = { + unit_test(test_timeout_survives_a_stall_before_fork), + unit_test(test_SetTimeOut_arms_at_once_SetChildTimeOut_waits), + unit_test(test_disarmed_timeout_does_not_start_later), + unit_test(test_failed_popen_does_not_leave_timeout_for_next_child), + }; + + return run_tests(tests); +} diff --git a/tests/unit/user_watcher_test.c b/tests/unit/user_watcher_test.c new file mode 100644 index 00000000000..e57124e443f --- /dev/null +++ b/tests/unit/user_watcher_test.c @@ -0,0 +1,252 @@ +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +/* Unix only, like file_watcher_test. Tells what the lookup found from the + * verbose log of the first check, since the first check never fires. The + * .c file is included so that tests can fake an earlier check. */ + +#define ABSENT_USER "cfengine_user_watcher_test_absent" + +typedef struct +{ + LoggingPrivContext ctx; + LogLevel old_level; + int count; /* Number of VERBOSE messages captured */ + char message[256]; /* Last VERBOSE message captured */ +} LogCapture; + +static char *CaptureVerboseLogHook( + LoggingPrivContext *pctx, LogLevel level, const char *message) +{ + if (level == LOG_LEVEL_VERBOSE) + { + LogCapture *capture = pctx->param; + capture->count++; + strlcpy(capture->message, message, sizeof(capture->message)); + } + return (char *) message; +} + +/* Captures VERBOSE messages with the log hook. The global level is set to + * CRIT so they aren't printed. force_hook_level still runs the hook for + * them. Stop with StopCapture(), which frees the returned capture. */ +static LogCapture *StartCapture(void) +{ + LogCapture *capture = xcalloc(1, sizeof(LogCapture)); + capture->ctx.log_hook = CaptureVerboseLogHook; + capture->ctx.param = capture; + capture->ctx.force_hook_level = LOG_LEVEL_VERBOSE; + LoggingPrivSetContext(&capture->ctx); + capture->old_level = LogGetGlobalLevel(); + LogSetGlobalLevel(LOG_LEVEL_CRIT); + return capture; +} + +static void StopCapture(LogCapture *capture) +{ + LogSetGlobalLevel(capture->old_level); + LoggingPrivSetContext(NULL); + free(capture); +} + +/* Copies the name of the user running the test, which always exists, to + * name */ +static void GetPresentUser(char *name, size_t name_size) +{ + const struct passwd *pw = getpwuid(getuid()); + assert_true(pw != NULL); + strlcpy(name, pw->pw_name, name_size); +} + +/* Returns a new state of a user watcher of the user with the given name */ +static void *NewUserState(const char *name) +{ + Rlist *args = NULL; + RlistAppendScalar(&args, name); + void *state = UserWatcherStateNew(NULL, args); + RlistDestroy(args); + return state; +} + +static void test_check_user_removed_present(void) +{ + char name[256]; + GetPresentUser(name, sizeof(name)); + char expected[512]; + snprintf( + expected, + sizeof(expected), + "User '%s' is present, watching for its removal", + name); + + LogCapture *capture = StartCapture(); + void *state = NewUserState(name); + + /* The first check is the reference, so it doesn't fire */ + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + assert_string_equal(capture->message, expected); + + /* Still present: not removed, and not logged again */ + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_user_removed_absent(void) +{ + LogCapture *capture = StartCapture(); + void *state = NewUserState(ABSENT_USER); + + /* Absent, so nothing to watch for yet and nothing logged */ + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 0); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_user_added_absent(void) +{ + LogCapture *capture = StartCapture(); + void *state = NewUserState(ABSENT_USER); + + /* The first check is the reference, so it doesn't fire */ + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + assert_string_equal( + capture->message, + "User '" ABSENT_USER "' is absent, watching for its addition"); + + /* Still absent: not added, and not logged again */ + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_user_added_present(void) +{ + char name[256]; + GetPresentUser(name, sizeof(name)); + + LogCapture *capture = StartCapture(); + void *state = NewUserState(name); + + /* Present, so nothing to watch for yet and nothing logged */ + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 0); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_user_added_fires(void) +{ + char name[256]; + GetPresentUser(name, sizeof(name)); + char expected[512]; + snprintf( + expected, + sizeof(expected), + "Observed addition of user '%s' at ", + name); + + LogCapture *capture = StartCapture(); + UserWatcherState *state = NewUserState(name); + + /* Fake an earlier check that saw the user absent */ + state->last_checked = time(NULL); + state->last_exists = false; + + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_EVENT); + assert_int_equal(capture->count, 1); + assert_true(StringStartsWith(capture->message, expected)); + + /* Fires only once */ + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_user_removed_fires(void) +{ + LogCapture *capture = StartCapture(); + UserWatcherState *state = NewUserState(ABSENT_USER); + + /* Fake an earlier check that saw the user present */ + state->last_checked = time(NULL); + state->last_exists = true; + + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_EVENT); + assert_int_equal(capture->count, 1); + assert_true(StringStartsWith( + capture->message, + "Observed removal of user '" ABSENT_USER "' at ")); + + /* Fires only once */ + assert_int_equal( + UserWatcherCheckUserRemoved(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + + UserWatcherStateDestroy(state); + StopCapture(capture); +} + +static void test_check_empty_name(void) +{ + LogCapture *capture = StartCapture(); + + /* An empty name is a user that doesn't exist, not a lookup error */ + void *state = NewUserState(""); + assert_int_equal(UserWatcherCheckUserAdded(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(capture->count, 1); + assert_string_equal( + capture->message, "User '' is absent, watching for its addition"); + UserWatcherStateDestroy(state); + + StopCapture(capture); +} + +static void test_state_destroy_null(void) +{ + UserWatcherStateDestroy(NULL); +} + +int main() +{ + PRINT_TEST_BANNER(); + const UnitTest tests[] = { + unit_test(test_check_user_removed_present), + unit_test(test_check_user_removed_absent), + unit_test(test_check_user_added_absent), + unit_test(test_check_user_added_present), + unit_test(test_check_user_added_fires), + unit_test(test_check_user_removed_fires), + unit_test(test_check_empty_name), + unit_test(test_state_destroy_null), + }; + + return run_tests(tests); +} diff --git a/tests/unit/watcher_test.c b/tests/unit/watcher_test.c index c297793698f..b74308b8901 100644 --- a/tests/unit/watcher_test.c +++ b/tests/unit/watcher_test.c @@ -30,7 +30,7 @@ static WatcherOptions *FileDeletedOptions(const char *path, time_t interval) Rlist *args = NULL; RlistAppendScalar(&args, path); opt = (WatcherOptions) { - .state = FileWatcherStateNew(args), + .state = FileWatcherStateNew(NULL, args), .check_callback = FileWatcherCheckFileDeleted, .destroy_state = FileWatcherStateDestroy, .interval = interval,