diff --git a/.CFVERSION b/.CFVERSION index c7c9773262c..1cfe511a3ed 100644 --- a/.CFVERSION +++ b/.CFVERSION @@ -1 +1 @@ -3.29.0 +3.30.0 diff --git a/.github/workflows/backport.yml b/.github/workflows/backport.yml new file mode 100644 index 00000000000..46cf4aafc94 --- /dev/null +++ b/.github/workflows/backport.yml @@ -0,0 +1,14 @@ +name: Backport + +on: + push: + branches: [master] + +permissions: + contents: write + pull-requests: write + +jobs: + backport: + if: github.repository == 'cfengine/core' + uses: ./.github/workflows/job-backport.yml diff --git a/.github/workflows/job-backport.yml b/.github/workflows/job-backport.yml new file mode 100644 index 00000000000..e93108a2bc5 --- /dev/null +++ b/.github/workflows/job-backport.yml @@ -0,0 +1,118 @@ +name: Backport + +# Opens backport pull requests to the LTS branches when a pull request with the +# `cherry-pick?` label is merged to master. Commits are cherry-picked with `-x`. +# Each branch comments on the original pull request with the backport link, or +# says that a manual backport is needed. If all branches succeed, the label is +# removed. +# +# The label must be set before merging. +# +# Call it from a workflow triggered on push to master. The caller must grant +# `contents: write` and `pull-requests: write`. + +on: workflow_call + +jobs: + backport: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + branch: [3.27.x, 3.24.x] + steps: + - name: Check for the label + id: find_pr + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + MERGE_SHA: ${{ github.sha }} + run: | + # For a commit on master, this returns the merged PR that added it. + pr=$(gh api "repos/$GH_REPO/commits/$MERGE_SHA/pulls" \ + --jq 'first(.[] | select(any(.labels[]; .name == "cherry-pick?")))') + if [ -z "$pr" ]; then + echo "No merged PR with the cherry-pick? label, nothing to do" + exit 0 + fi + echo "number=$(jq -r .number <<< "$pr")" >> "$GITHUB_OUTPUT" + echo "title=$(jq -r .title <<< "$pr")" >> "$GITHUB_OUTPUT" + echo "author=$(jq -r .user.login <<< "$pr")" >> "$GITHUB_OUTPUT" + + - uses: actions/checkout@v4 + if: steps.find_pr.outputs.number != '' + with: + ref: ${{ matrix.branch }} + # 0 means full history. It is needed to reach the merged commits. + fetch-depth: 0 + + - name: Cherry-pick + id: cherry_pick + if: steps.find_pr.outputs.number != '' + continue-on-error: true + env: + MERGE_SHA: ${{ github.sha }} + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + + # A merge commit has two parents: ^1 is master before the merge, ^2 is + # the tip of the PR branch. ^1..^2 is then the commits of the PR. + # shellcheck disable=SC2046 # word splitting is intended + if ! git cherry-pick -x $(git rev-list --reverse --no-merges "$MERGE_SHA^1..$MERGE_SHA^2"); then + git cherry-pick --abort || true + exit 1 + fi + + - name: Open a pull request + id: create_pr + if: steps.cherry_pick.outcome == 'success' + uses: cfengine/create-pull-request@v7.0.7a + with: + base: ${{ matrix.branch }} + branch: backport-${{ steps.find_pr.outputs.number }}-${{ matrix.branch }} + delete-branch: true + sign-commits: true + title: ${{ steps.find_pr.outputs.title }} (${{ matrix.branch }}) + body: | + Backport of #${{ steps.find_pr.outputs.number }} by @${{ steps.find_pr.outputs.author }} to ${{ matrix.branch }}. + + Opened automatically by [.github/workflows/backport.yml](${{ github.server_url }}/${{ github.repository }}/blob/master/.github/workflows/backport.yml). + + Pull requests opened with `GITHUB_TOKEN` do not trigger other workflows, so CI has not started on its own. + + - name: Comment on the original pull request + if: always() && steps.find_pr.outputs.number != '' + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + PR: ${{ steps.find_pr.outputs.number }} + BRANCH: ${{ matrix.branch }} + URL: ${{ steps.create_pr.outputs.pull-request-url }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: | + if [ -n "$URL" ]; then + gh pr comment "$PR" --body "Backport to $BRANCH: $URL" + else + gh pr comment "$PR" --body "Backport to $BRANCH failed, manual backport needed: $RUN_URL" + # Fail the job so the label is kept + exit 1 + fi + + remove_label: + needs: backport + runs-on: ubuntu-latest + steps: + - name: Remove the label + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + MERGE_SHA: ${{ github.sha }} + run: | + pr=$(gh api "repos/$GH_REPO/commits/$MERGE_SHA/pulls" \ + --jq 'first(.[] | select(any(.labels[]; .name == "cherry-pick?")) | .number)') + if [ -z "$pr" ]; then + echo "No merged PR with the cherry-pick? label, nothing to do" + exit 0 + fi + gh pr edit "$pr" --remove-label "cherry-pick?" 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/CHANGELOG.md b/CHANGELOG.md index 1db4eedd974..f96f5f057b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,86 @@ +## 3.29.0 + +- Added `--no-lock` long option to `cf-runagent`, matching the existing `-K` short option +- Added SELinux state and Federated Reporting transport metadata to `cf-support` (ENT-14405) +- Added Termux for `sys.os_name_human` when running in Termux Linux Environment (Android) (CFE-4427) +- Added `package_installed` and `package_uninstalled` watchers to `cf-reactor` + (ENT-14766) +- Added `file_older_than()` policy function (ENT-6065) +- Added inventory tag `Short hostname` to sys.uqhost (ENT-11265) +- Added opt-in remount reconciliation for storage mount options + (CFE-1864, CFE-3366, CFE-90) +- Added sshd logs to the `cf-support` Federated Reporting collection + (ENT-14405) +- Added the `silence` attribute to `body changes`, which suppresses the alert output of selected file-change categories while still setting the change classes and writing the audit log. + (CFE-159) +- Added the parameters of `string_mustache()` to the syntax description (CFE-4534) +- Added the promisee to verbose output for reports promises (CFE-4228) +- Added `validfiledata()` policy function (ENT-8193) +- Fixed policy function `IsReadable()` on Termux platform + (CFE-4401) +- Allowed modules to define empty lists (CFE-2139) +- Allowed modules to define lists with a trailing comma (CFE-2140) +- Allowed mount promises to target a specific server (CFE-2350) +- Community `files` promises using `hash => "best"` now use SHA-512 (previously MD5+SHA1) and no longer report a content change on two consecutive agent runs. Affected files are re-baselined on the first run after upgrade. + (CFE-3725) +- Extended string_replace with list arguments (CFE-4694) +- IPv6 masks in `iprange()` no longer need to be a multiple of 8 bits (CFE-2034) +- Fixed `--simulate` symlink errors to show the real target, not a chroot path + (ENT-14714) +- Fixed `--simulate=diff` to report permission and ownership changes + (ENT-14694) +- Fixed `--simulate=diff` to show the new target of a replaced symlink + (ENT-14713) +- Fixed Federated Reporting transport failing on RHEL 10 hubs (ENT-14405) +- Fixed a bug regarding the resolution of namespaces inside function-calls + (ENT-10199) +- Fixed `cf-agent` assertion failure when reading `/proc//stat` (CFE-4285) +- Fixed `cf-check -V` (`--version`) short option +- Fixed `cf-net -t` (`--tls-version`) and `-c` (`--ciphers`) short options +- Fixed `cf-secret -v` (`--verbose`), `-g` (`--log-level`) and `-I` (`--inform`) short options +- Fixed `cf-serverd -L` (`--ld-library-path`) to accept its argument +- Fixed `insert_lines` ignoring `include_end_delimiter` when locating the region (CFE-3988) +- Fixed `insert_lines` prepending to the file instead of to the selected region (CFE-3987) +- Fixed lmdb maxkeysize assertion for macos +- Fixed macOS process start-time/state detection (`process_macos.c`) and the macOS ps syntax bug in `mon_processes_test.c` + (ENT-14471) +- Fixed out-of-bounds read when parsing a remote directory listing +- Fixed out-of-bounds read of extended attribute values in policy function `filestat()` +- Fixed `process_select` and `file_select` time ranges on Windows +- Fixed issue where reports promise was kept even though the report could not be written + (CFE-4699) +- Fixed simulate diff mode not reporting a removal with a version +- Fixed simulate mode reporting both removal and install of the same package (CFE-4742) +- Fixed simulate mode swapping package version and architecture +- Fixed stack buffer overflow in `cf-serverd` when serving `remotescalar()` and `hubknowledge()` requests +- Fixed `usemodule()` reporting success when the module exited non-zero (CFE-942) +- Increased the maximum number of `cf-monitord` measurement slots from 100 to 300 + (ENT-6511) +- Made an unrecognized `action_policy` fatal instead of defaulting to fix + (CFE-1982) +- Made format function collect data container (CFE-3105) +- Maintained fstab entries for already-mounted filesystems (CFE-1539) +- Now mounts a single filesystem surgically instead of running `mount -a` + (CFE-1863) +- `cf-reactor` is now a part of CFEngine Community (ENT-14275) +- Raised `cf-apache.service` start timeout to avoid PID-file race (ENT-11189) +- The source tarball is now reproducible +- `cf-reactor` can now react to a user being added or removed, with the `user_added` and `user_removed` attributes of `when` bodies + (ENT-14765) +- `and()`, `or()`, `concat()`, `ifelse()`, `findfiles()`, `mergedata()` and the `classmatch()` family reported no parameters at all. + Only the leading arguments are described, as `bundlesmatching()` and `format()` already do. (CFE-4714) +- `cf-check` dump now reports measurement names instead of labeling them spare + (ENT-14329) +- `cf-check` now checks LMDB value size before reading lastseen and lock structs +- `cf-monitord` no longer silently truncates the `ts_key` measurement name file + (ENT-14329) +- `cf-monitord` now stores only in-use measurement slots (ENT-14329) +- Added missing option description in `--help` output of `cf-reactor` +- Fixed `cf-serverd` leaking uninitialized memory to clients when refusing a file copy request +- Fixed out-of-bounds read in `iprange()` and `isipinsubnet()` when the address is shorter than the range +- Fixed stack buffer overflow in `cf-agent` and `cf-monitord` on overly long lines in `mount` or `netstat` output +- Policy function `format()` now rejects format specifiers `*` and `$` + ## 3.28.0 - Adapted date constraints to allow dates after Y2038 (CFE-4620) diff --git a/SECURITY.md b/SECURITY.md index e516691923d..bc20f55af4b 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -2,7 +2,7 @@ ## Supported versions -For information about versions which are supported and recieving security patches see https://docs.cfengine.com +For information about versions which are supported and receiving security patches see https://docs.cfengine.com ## Reporting a vulnerability 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/simulate_mode.c b/cf-agent/simulate_mode.c index 35264e62cc2..2b1d2995ab7 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -793,6 +793,31 @@ static inline char *GetPkgOperationMsg(ChrootPkgOperationCode op, const char *pk return msg; } +static bool PkgRecordShouldBeReplaced(const PkgOperationRecord *prev_record, + const char *new_ver) +{ + if (prev_record == NULL) + { + /* Nothing recorded yet */ + return true; + } + + if (NULL_OR_EMPTY(new_ver)) + { + /* An unversioned record never replaces an existing one */ + return false; + } + + if (NULL_OR_EMPTY(prev_record->pkg_ver)) + { + /* A versioned record is more specific than an unversioned one */ + return true; + } + + /* Both are versioned, keep the higher version */ + return PkgVersionIsGreater(new_ver, prev_record->pkg_ver); +} + bool DiffPkgOperations() { const char *pkgs_ops_csv_file = ToChangesChroot(CHROOT_PKGS_OPS_FILE); @@ -893,7 +918,7 @@ bool DiffPkgOperations() MapRemove(removed, name_arch); PkgOperationRecord *prev_record = MapGet(installed, name_arch); - if ((prev_record == NULL) || PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver)) + if (PkgRecordShouldBeReplaced(prev_record, pkg_ver)) { char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, pkg_name, pkg_arch, pkg_ver); diff --git a/cf-agent/verify_files_utils.c b/cf-agent/verify_files_utils.c index 7931ae87544..cf7ad2a0be1 100644 --- a/cf-agent/verify_files_utils.c +++ b/cf-agent/verify_files_utils.c @@ -1338,16 +1338,26 @@ static PromiseResult LinkCopy(EvalContext *ctx, char *sourcefile, char *destfile strlcpy(vbuff, changes_sourcefile, CF_BUFSIZE); ChopLastNode(vbuff); AddSlash(vbuff); - strncat(vbuff, chrooted_linkbuf, CF_BUFSIZE - 1); - strlcpy(chrooted_linkbuf, vbuff, CF_BUFSIZE - 1); + if (strlcat(vbuff, chrooted_linkbuf, sizeof(vbuff)) >= sizeof(vbuff) || + strlcpy(chrooted_linkbuf, vbuff, sizeof(chrooted_linkbuf)) >= sizeof(chrooted_linkbuf)) + { + RecordFailure(ctx, pp, attr, + "Absolute target of link '%s' is too long", sourcefile); + return PROMISE_RESULT_FAIL; + } } else { strlcpy(vbuff, sourcefile, CF_BUFSIZE); ChopLastNode(vbuff); AddSlash(vbuff); - strncat(vbuff, linkbuf, CF_BUFSIZE - 1); - strlcpy(linkbuf, vbuff, CF_BUFSIZE - 1); + if (strlcat(vbuff, linkbuf, sizeof(vbuff)) >= sizeof(vbuff) || + strlcpy(linkbuf, vbuff, sizeof(linkbuf)) >= sizeof(linkbuf)) + { + RecordFailure(ctx, pp, attr, + "Absolute target of link '%s' is too long", sourcefile); + return PROMISE_RESULT_FAIL; + } } } } 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-check/dump.c b/cf-check/dump.c index e1f8e2cd427..e6f5d3b7b49 100644 --- a/cf-check/dump.c +++ b/cf-check/dump.c @@ -556,6 +556,12 @@ int dump_main(int argc, const char *const *const argv) } Seq *files = argv_to_lmdb_files(argc, argv, offset); + if (files == NULL || SeqLength(files) == 0) + { + printf("No database files to dump.\n"); + return 1; + } + const int ret = dump_dbs(files, mode, tskey_filename); SeqDestroy(files); return ret; diff --git a/cf-reactor/Makefile.am b/cf-reactor/Makefile.am index 4036d272376..21bbd49a28e 100644 --- a/cf-reactor/Makefile.am +++ b/cf-reactor/Makefile.am @@ -42,9 +42,11 @@ libcf_reactor_la_LIBADD = ../libpromises/libpromises.la libcf_reactor_la_SOURCES = \ cf-reactor.c \ file_watcher.c file_watcher.h \ + package_watcher.c package_watcher.h \ 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..2ac95b7824d 100644 --- a/cf-reactor/file_watcher.c +++ b/cf-reactor/file_watcher.c @@ -34,6 +34,10 @@ #include #include +#ifdef __MINGW32__ +#include /* NovaWin_GetFileOwnerSid() */ +#endif + /* Minimum time between two logs of a persisting failure to check the file, * since the file is checked on every poll */ #define CHECK_ERROR_LOG_INTERVAL_SECS 60 @@ -68,8 +72,15 @@ typedef struct /* FileWatcherCheckFileOwnerChanged() */ struct { +#ifdef __MINGW32__ + /* st_uid/st_gid are always 0 on Windows, so the owner SID is + * compared instead. Zero-padded, so it can be compared with + * memcmp(). */ + unsigned char sid[SECURITY_MAX_SID_SIZE]; +#else uid_t uid; gid_t gid; +#endif } owner; /* FileWatcherCheckFileSizeExceeds() */ @@ -82,7 +93,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 +104,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); @@ -118,19 +129,23 @@ void FileWatcherStateDestroy(void *state) // =========== code shared by the file event types =========== -/* Logs the failure of operation op (with errno err) on the file of the - * watcher. A persisting failure is logged at most once every - * CHECK_ERROR_LOG_INTERVAL_SECS. */ -static void LogCheckError(FileWatcherState *fws, const char *op, int err) +/* Logs the failure of operation op (with error message error) on the file of + * the watcher. A persisting failure is logged at most once every + * CHECK_ERROR_LOG_INTERVAL_SECS. + * + * Takes the message rather than an error code, since on Windows + * GetErrorStrFromCode() expects a Windows error code, not an errno value. */ +static void LogCheckError(FileWatcherState *fws, const char *op, const char *error) { assert(fws != NULL); + assert(error != NULL); const time_t now = time(NULL); if (fws->last_error_logged == 0 || now - fws->last_error_logged >= CHECK_ERROR_LOG_INTERVAL_SECS) { Log(LOG_LEVEL_ERR, "Unable to %s '%s' while checking for file events: %s", - op, fws->path, GetErrorStrFromCode(err)); + op, fws->path, error); fws->last_error_logged = now; } } @@ -162,7 +177,7 @@ static bool FileLstat(FileWatcherState *fws, bool *exists, struct stat *sb) return true; } - LogCheckError(fws, "lstat", errno); + LogCheckError(fws, "lstat", strerror(errno)); return false; } @@ -343,7 +358,7 @@ WatcherCheckResult FileWatcherCheckFileContentModified(void *state) } else if (errno != ENOENT && errno != ENOTDIR) /* Not deleted since lstat() */ { - LogCheckError(fws, "hash", errno); + LogCheckError(fws, "hash", strerror(errno)); return WATCHER_CHECK_ERROR; } } @@ -395,6 +410,30 @@ WatcherCheckResult FileWatcherCheckFileOwnerChanged(void *state) return WATCHER_CHECK_ERROR; } +#ifdef __MINGW32__ + unsigned char sid[SECURITY_MAX_SID_SIZE] = { 0 }; + if (exists) + { + const DWORD status = NovaWin_GetFileOwnerSid(fws->path, (SID *) sid, + sizeof(sid)); + if (status == ERROR_FILE_NOT_FOUND || status == ERROR_PATH_NOT_FOUND) + { + exists = false; /* Deleted since lstat() */ + memset(sid, 0, sizeof(sid)); + } + else if (status != ERROR_SUCCESS) + { + LogCheckError(fws, "get the owner of", GetErrorStrFromCode(status)); + return WATCHER_CHECK_ERROR; + } + } + + /* Only changes of a file present at both checks */ + const bool changed = fws->last_exists && exists && + (memcmp(fws->last.owner.sid, sid, sizeof(sid)) != 0); + + memcpy(fws->last.owner.sid, sid, sizeof(sid)); +#else const uid_t uid = exists ? sb.st_uid : 0; const gid_t gid = exists ? sb.st_gid : 0; @@ -404,6 +443,8 @@ WatcherCheckResult FileWatcherCheckFileOwnerChanged(void *state) fws->last.owner.uid = uid; fws->last.owner.gid = gid; +#endif + return FileWatcherRecordObservation(fws, exists, changed, "owner change"); } 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/package_watcher.c b/cf-reactor/package_watcher.c new file mode 100644 index 00000000000..b6074eba5f7 --- /dev/null +++ b/cf-reactor/package_watcher.c @@ -0,0 +1,675 @@ +/* + 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 +#include +#include +#include + +/* Timeout of a run of the package module. Only the listing thread of the + * module waits for it. Tests can define it before including this file. */ +#ifndef LISTING_TIMEOUT_SECS +#define LISTING_TIMEOUT_SECS 60 +#endif + +/* Minimum time between the starts of two listings of a package module, since + * a listing can take a while. Tests can define it before including this file. + */ +#ifndef LISTING_INTERVAL_SECS +#define LISTING_INTERVAL_SECS 10 +#endif + +/* Minimum time between two attempts to create the wrapper of a package + * module that can't be run. Longer, since each attempt logs errors. Tests can + * define it before including this file. */ +#ifndef LISTING_RETRY_INTERVAL_SECS +#define LISTING_RETRY_INTERVAL_SECS 60 +#endif + +/* How long to wait for a listing thread to stop before giving up on it */ +#define LISTING_THREAD_STOP_TIMEOUT_SECS 5 + +/* Minimum time between two logs of a persisting failure to list packages */ +#define LISTING_ERROR_LOG_INTERVAL_SECS 60 + +/* The installed packages of a package module, listed by its own thread. + * Shared by all watchers of the package module. */ +typedef struct +{ + char *key; /* see PackageModuleKey() */ + PackageModuleBody *module; /* copy of the package module body */ + size_t refcount; /* watchers using it, under listings_mutex */ + StoppableThread *thread; + time_t last_error_logged; /* only used by the listing thread */ + + pthread_mutex_t mutex; /* protects the fields below */ + StringSet *installed; /* names, NULL if the listing failed */ + time_t listed_at; /* start of the latest listing, 0 if none */ +} PackageListing; + +/* All listings, under listings_mutex. Only changed on the main thread. */ +static pthread_mutex_t listings_mutex = PTHREAD_MUTEX_INITIALIZER; +static Seq *listings = NULL; + +typedef struct +{ + char *name; + PackageListing *listing; + bool observed; /* whether a listing was observed yet */ + bool last_installed; /* what the last observed listing said */ +} PackageWatcherState; + +/** + * @brief Returns what tells two package modules apart when listing. + * + * Two bodies with the same name, path, interpreter and options list the + * same packages, so their watchers share a package listing. + * + * @param module The package module body + * @return The key, to be freed by the caller + */ +static char *PackageModuleKey(const PackageModuleBody *module) +{ + assert(module != NULL); + + char *options = PackageModuleFormatOptions(module->options); + char *key = StringFormat( + "%s\n%s\n%s\n%s", + module->name, + (module->module_path != NULL) ? module->module_path : "", + (module->interpreter != NULL) ? module->interpreter : "", + options); + free(options); + return key; +} + +/** + * @brief Logs a problem listing the packages of a package module. + * + * The caller logs the actual problem at verbose level. A persisting problem is + * logged at most once every LISTING_ERROR_LOG_INTERVAL_SECS. Only called by + * the listing thread. + * + * @param listing The package listing + */ +static void LogListingError(PackageListing *listing) +{ + assert(listing != NULL); + + const time_t now = time(NULL); + if ((listing->last_error_logged == 0) + || (now - listing->last_error_logged + >= LISTING_ERROR_LOG_INTERVAL_SECS)) + { + Log(LOG_LEVEL_ERR, + "Problem listing installed packages with package module '%s' while checking for package events, run with verbose logging for details", + listing->module->name); + listing->last_error_logged = now; + } +} + +/** + * @brief Adds the name of a record of a 'list-installed' reply. + * + * @param installed The names of the installed packages + * @param name The name of the record, may be NULL + * @param version The version of the record, may be NULL + * @param arch The architecture of the record, may be NULL + */ +static void AddInstalledPackage( + StringSet *installed, + const char *name, + const char *version, + const char *arch) +{ + assert(installed != NULL); + + if (name == NULL) + { + /* No record yet, i.e. before the first Name= line or an empty reply. + * Skipped, so that callers don't need to check. */ + return; + } + if (version == NULL || arch == NULL) + { + Log(LOG_LEVEL_VERBOSE, + "Malformed response from package module for package '%s'", + name); + return; + } + if (!StringSetContains(installed, name)) + { + StringSetAdd(installed, xstrdup(name)); + } +} + +/** + * @brief Parses a 'list-installed' reply of a package module. + * + * @param response The lines of the reply + * @param module_name The name of the package module, for logging + * @return The names of the installed packages, or NULL if the reply has + * errors + */ +static StringSet *ParseInstalledPackages( + const Rlist *response, const char *module_name) +{ + assert(module_name != NULL); + + StringSet *installed = StringSetNew(); + const char *name = NULL; + const char *version = NULL; + const char *arch = NULL; + bool failed = false; + for (const Rlist *rp = response; rp != NULL; rp = rp->next) + { + const char *line = RlistScalarValue(rp); + if (StringStartsWith(line, "Name=")) + { + /* A new record, meaning the previous one, is complete. If there is + * no previous one, then it's skipped. */ + AddInstalledPackage(installed, name, version, arch); + name = line + strlen("Name="); + version = NULL; + arch = NULL; + } + else if (StringStartsWith(line, "Version=")) + { + version = line + strlen("Version="); + } + else if (StringStartsWith(line, "Architecture=")) + { + arch = line + strlen("Architecture="); + } + else if ( + StringStartsWith(line, "Error=") + || StringStartsWith(line, "ErrorMessage=")) + { + Log(LOG_LEVEL_VERBOSE, + "Package module '%s' replied with an error: %s", + module_name, + line); + /* Not returning yet, so that all error lines get logged, e.g. an + * ErrorMessage= line with the details after an Error= line */ + failed = true; + } + } + /* The last record, if any */ + AddInstalledPackage(installed, name, version, arch); + + if (failed) + { + /* Never taken as "nothing installed" */ + StringSetDestroy(installed); + return NULL; + } + return installed; +} + +/** + * @brief Lists the installed packages with the package module. + * + * @param listing The listing + * @param wrapper The wrapper of the package module + * @return The names of the installed packages, or NULL on failure + */ +static StringSet *ListInstalledPackages( + PackageListing *listing, const PackageModuleWrapper *wrapper) +{ + assert(listing != NULL); + assert(wrapper != NULL); + + char *options = PackageModuleFormatOptions(listing->module->options); + Rlist *response = NULL; + const int ret = PackageWrapperCommunicate( + wrapper, "list-installed", options, &response); + free(options); + + if (ret != 0) + { + Log(LOG_LEVEL_VERBOSE, + "Package module '%s' failed to list installed packages", + listing->module->name); + LogListingError(listing); + return NULL; + } + + StringSet *installed = + ParseInstalledPackages(response, listing->module->name); + RlistDestroy(response); + + if (installed == NULL) + { + LogListingError(listing); + } + return installed; +} + +/** + * @brief The routine of the listing thread of a package module. + * + * Lists the installed packages ahead of the checks, every + * LISTING_INTERVAL_SECS, so that a check sees a recent listing whatever its + * interval. + * + * @param thread The listing thread + * @param arg The listing + */ +static void ListingThreadMain(StoppableThread *thread, void *arg) +{ +#ifndef __MINGW32__ + MaskTerminationSignalsInThread(); +#endif + + PackageListing *listing = arg; + assert(listing != NULL); + + /* Created on the first listing, so that negotiating the API version + * runs the package module on this thread, not on the main one */ + PackageModuleWrapper *wrapper = NULL; + + while (!StoppableThreadShouldStop(thread)) + { + const time_t start = time(NULL); + if (wrapper == NULL) + { + wrapper = + NewPackageModuleWrapper(listing->module, LISTING_TIMEOUT_SECS); + } + const time_t wait_secs = (wrapper != NULL) + ? LISTING_INTERVAL_SECS + : LISTING_RETRY_INTERVAL_SECS; + + StringSet *installed = NULL; + if (wrapper != NULL) + { + installed = ListInstalledPackages(listing, wrapper); + } + else + { + Log(LOG_LEVEL_VERBOSE, + "Package module '%s' can't be run", + listing->module->name); + LogListingError(listing); + } + + ThreadLock(&listing->mutex); + StringSetDestroy(listing->installed); + listing->installed = installed; + listing->listed_at = start; + ThreadUnlock(&listing->mutex); + + /* Counted from the start, so that a slow listing doesn't delay the + * next one. Sleeps in a loop, since a sleep can end early. */ + const time_t next = start + wait_secs; + time_t now; + while (!StoppableThreadShouldStop(thread) + && ((now = time(NULL)) < next)) + { + StoppableThreadSleep(thread, next - now); + } + } + + DeletePackageModuleWrapper(wrapper); +} + +/** + * @brief Destroys a listing whose thread has stopped. + * + * @param listing The listing + */ +static void PackageListingDestroy(PackageListing *listing) +{ + assert(listing != NULL); + + free(listing->key); + PackageModuleBodyDestroy(listing->module); + StringSetDestroy(listing->installed); + pthread_mutex_destroy(&listing->mutex); + free(listing); +} + +/** + * @brief Gets the listing of a package module, shared by its watchers. + * + * Creates it, and starts its thread, if no watcher uses the package module + * yet. Only called on the main thread. + * + * @param module The package module body, copied + * @return The listing, to be released with PackageListingRelease(), or NULL + * if its thread can't be started + */ +static PackageListing *PackageListingAcquire(const PackageModuleBody *module) +{ + assert(module != NULL); + + char *key = PackageModuleKey(module); + + ThreadLock(&listings_mutex); + if (listings == NULL) + { + listings = SeqNew(1, NULL); + } + + for (size_t i = 0; i < SeqLength(listings); i++) + { + PackageListing *listing = SeqAt(listings, i); + if (StringEqual(listing->key, key)) + { + listing->refcount++; + ThreadUnlock(&listings_mutex); + free(key); + return listing; + } + } + + PackageListing *listing = xcalloc(1, sizeof(PackageListing)); + listing->key = key; + listing->module = PackageModuleBodyCopy(module); + pthread_mutex_init(&listing->mutex, NULL); + listing->refcount = 1; + + listing->thread = StoppableThreadStart(ListingThreadMain, listing); + if (listing->thread == NULL) + { + ThreadUnlock(&listings_mutex); + PackageListingDestroy(listing); + return NULL; + } + + SeqAppend(listings, listing); + ThreadUnlock(&listings_mutex); + return listing; +} + +/** + * @brief Releases a listing got with PackageListingAcquire(). + * + * The last watcher to release it stops its thread and destroys it. If the + * thread doesn't stop in time, e.g. because the package module hangs, it's + * given up on, and the listing is leaked on purpose, since the thread may + * still use it. It's then only freed when cf-reactor exits. Only called on + * the main thread. + * + * @param listing The listing + */ +static void PackageListingRelease(PackageListing *listing) +{ + assert(listing != NULL); + + ThreadLock(&listings_mutex); + assert(listing->refcount > 0); + listing->refcount--; + if (listing->refcount > 0) + { + ThreadUnlock(&listings_mutex); + return; + } + + for (size_t i = 0; i < SeqLength(listings); i++) + { + if (SeqAt(listings, i) == listing) + { + SeqSoftRemove(listings, i); + break; + } + } + ThreadUnlock(&listings_mutex); + + if (StoppableThreadStop(listing->thread, LISTING_THREAD_STOP_TIMEOUT_SECS)) + { + PackageListingDestroy(listing); + } +} + +/** + * @brief Creates the state of a package watcher for the given package module. + * + * @param name The name of the package to watch + * @param module The package module body, copied + * @return The new state, or NULL if the listing thread can't be started + */ +static PackageWatcherState *PackageWatcherStateNewFromModule( + const char *name, const PackageModuleBody *module) +{ + assert(name != NULL); + assert(module != NULL); + + PackageListing *listing = PackageListingAcquire(module); + if (listing == NULL) + { + return NULL; + } + + PackageWatcherState *state = xcalloc(1, sizeof(PackageWatcherState)); + state->name = xstrdup(name); + state->listing = listing; + return state; +} + +void *PackageWatcherStateNew(const EvalContext *ctx, const Rlist *args) +{ + assert(ctx != NULL); + assert(args != NULL); + + const char *name = RlistScalarValue(args); + const PackageModuleBody *module = NULL; + if (args->next != NULL) + { + const char *module_name = RlistScalarValue(args->next); + module = GetPackageModuleFromContext(ctx, module_name); + if (module == NULL) + { + Log(LOG_LEVEL_ERR, + "No package module body '%s' to watch package '%s' with", + module_name, + name); + return NULL; + } + } + else + { + module = GetDefaultPackageModuleFromContext(ctx); + if (module == NULL) + { + Log(LOG_LEVEL_ERR, + "No default package module to watch package '%s' with, " + "set 'package_module' in the 'when' body or in body common control", + name); + return NULL; + } + } + + return PackageWatcherStateNewFromModule(name, module); +} + +void PackageWatcherStateDestroy(void *_state) +{ + PackageWatcherState *state = _state; + if (state != NULL) + { + PackageListingRelease(state->listing); + free(state->name); + free(state); + } +} + +/** + * @brief The body of PackageLookup(), only called by it, with the mutex of + * the listing held. + * + * @param listing The listing + * @param name The name of the package + * @param[out] listed Whether there is a listing yet + * @param[out] installed Whether the package is installed, only set if listed + * @return false if the latest listing failed + */ +static bool _PackageLookupInner( + const PackageListing *listing, + const char *name, + bool *listed, + bool *installed) +{ + assert(listing != NULL); + assert(name != NULL); + assert(listed != NULL); + assert(installed != NULL); + + *listed = (listing->listed_at != 0); + if (!*listed) + { + return true; + } + if (listing->installed == NULL) + { + return false; + } + *installed = StringSetContains(listing->installed, name); + return true; +} + +/** + * @brief Looks up the package in the latest listing of its package module. + * + * @param state The watcher state + * @param[out] listed Whether there is a listing yet + * @param[out] installed Whether the package is installed, only set if listed + * @return false if the latest listing failed + */ +static bool PackageLookup( + PackageWatcherState *state, bool *listed, bool *installed) +{ + assert(state != NULL); + + PackageListing *listing = state->listing; + ThreadLock(&listing->mutex); + const bool ok = + _PackageLookupInner(listing, state->name, listed, installed); + ThreadUnlock(&listing->mutex); + return ok; +} + +/** + * @brief Records an observed listing. + * + * The first observed listing is only the reference for the next ones, so it + * never fires. + * + * @param state The watcher state + * @param installed Whether the package is installed + * @param happened Whether the event happened since the last observation + * @param event The name of the event for logging (e.g. "installation") + * @return WATCHER_CHECK_EVENT if the event fired, WATCHER_CHECK_NO_EVENT + * otherwise + */ +static WatcherCheckResult PackageWatcherRecordObservation( + PackageWatcherState *state, + bool installed, + bool happened, + const char *event) +{ + assert(state != NULL); + assert(event != NULL); + + happened = happened && state->observed; + if (happened) + { + Log(LOG_LEVEL_VERBOSE, + "Observed %s of package '%s'", + event, + state->name); + } + + state->observed = true; + state->last_installed = installed; + return happened ? WATCHER_CHECK_EVENT : WATCHER_CHECK_NO_EVENT; +} + +WatcherCheckResult PackageWatcherCheckPackageInstalled(void *_state) +{ + assert(_state != NULL); + PackageWatcherState *state = _state; + + bool listed = false; + bool installed = false; + if (!PackageLookup(state, &listed, &installed)) + { + /* Unknown state, keep the last observation as is */ + return WATCHER_CHECK_ERROR; + } + if (!listed) + { + return WATCHER_CHECK_NO_EVENT; + } + + /* Logs only if the package was uninstalled, or on the first observation. + */ + if (!installed && (state->last_installed || !state->observed)) + { + Log(LOG_LEVEL_VERBOSE, + "Package '%s' is not installed, watching for its installation", + state->name); + } + + const bool happened = !state->last_installed && installed; + return PackageWatcherRecordObservation( + state, installed, happened, "installation"); +} + +WatcherCheckResult PackageWatcherCheckPackageUninstalled(void *_state) +{ + assert(_state != NULL); + PackageWatcherState *state = _state; + + bool listed = false; + bool installed = false; + if (!PackageLookup(state, &listed, &installed)) + { + /* Unknown state, keep the last observation as is */ + return WATCHER_CHECK_ERROR; + } + if (!listed) + { + return WATCHER_CHECK_NO_EVENT; + } + + if (installed && !state->last_installed) + { + Log(LOG_LEVEL_VERBOSE, + "Package '%s' is installed, watching for its uninstallation", + state->name); + } + + const bool happened = state->last_installed && !installed; + return PackageWatcherRecordObservation( + state, installed, happened, "uninstallation"); +} diff --git a/cf-reactor/package_watcher.h b/cf-reactor/package_watcher.h new file mode 100644 index 00000000000..cc9b26f3a81 --- /dev/null +++ b/cf-reactor/package_watcher.h @@ -0,0 +1,70 @@ +/* + 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_WATCHER_H +#define CFENGINE_PACKAGE_WATCHER_H + +#include +#include + +/** + * @brief Creates the state of a package_installed or package_uninstalled + * watcher. + * + * @param ctx The context, to look up the package module + * @param args The name of the package to watch, optionally followed by the + * name of the package module to list the packages with + * @return The new state, to be destroyed with PackageWatcherStateDestroy(), + * or NULL if there is no such package module + */ +void *PackageWatcherStateNew(const EvalContext *ctx, const Rlist *args); + +/** + * @brief Destroys the state of a package watcher. + * + * @param state The watcher state, may be NULL + */ +void PackageWatcherStateDestroy(void *state); + +/** + * @brief Checks for the package_installed event. + * + * @param state The watcher state + * @return WATCHER_CHECK_EVENT if the package was installed since the last + * check, WATCHER_CHECK_ERROR if the last listing failed, + * WATCHER_CHECK_NO_EVENT otherwise + */ +WatcherCheckResult PackageWatcherCheckPackageInstalled(void *state); + +/** + * @brief Checks for the package_uninstalled event. + * + * @param state The watcher state + * @return WATCHER_CHECK_EVENT if the package was uninstalled since the last + * check, WATCHER_CHECK_ERROR if the last listing failed, + * WATCHER_CHECK_NO_EVENT otherwise + */ +WatcherCheckResult PackageWatcherCheckPackageUninstalled(void *state); + +#endif /* CFENGINE_PACKAGE_WATCHER_H */ diff --git a/cf-reactor/reactor_transform.c b/cf-reactor/reactor_transform.c index 565e5a1094f..28ddd214e67 100644 --- a/cf-reactor/reactor_transform.c +++ b/cf-reactor/reactor_transform.c @@ -42,6 +42,8 @@ #include // REACTOR_INTERVAL_RANGE, REACTOR_FILE_DELETED, REACTOR_*_TYPES, BundleIsReactorEventsBundle() #include // CheckConstraintTypeMatch(), PromiseTypeSyntaxGet(), BodySyntaxGetConstraintSyntax() #include +#include +#include #include /* Resolves the agent, common or reactor bundle (looked up in that order) @@ -132,11 +134,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 +155,19 @@ 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 } +}; + +static const EventArg PACKAGE_ARGS[] = +{ + { REACTOR_PACKAGE_MODULE, RVAL_TYPE_SCALAR, true }, + { NULL, RVAL_TYPE_NOPROMISEE, false } }; #define EventTypeNew(name, type, args, new_state, destroy_state, check) \ @@ -168,6 +178,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 +192,12 @@ 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), + EventTypeNew(REACTOR_PACKAGE_INSTALLED, RVAL_TYPE_SCALAR, PACKAGE_ARGS, + PackageWatcherStateNew, PackageWatcherStateDestroy, PackageWatcherCheckPackageInstalled), + EventTypeNew(REACTOR_PACKAGE_UNINSTALLED, RVAL_TYPE_SCALAR, PACKAGE_ARGS, + PackageWatcherStateNew, PackageWatcherStateDestroy, PackageWatcherCheckPackageUninstalled), EventTypeNewNull() }; @@ -264,8 +284,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 +300,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 +368,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 +401,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/cf-serverd/cf-serverd-functions.c b/cf-serverd/cf-serverd-functions.c index b52be4d7c79..ee6c0334bde 100644 --- a/cf-serverd/cf-serverd-functions.c +++ b/cf-serverd/cf-serverd-functions.c @@ -374,7 +374,7 @@ static void DeleteAuthList(Auth **list, Auth **list_tail) *list_tail = NULL; } -/* Must not be called unless ACTIVE_THREADS is zero: */ +/* Must not be called unless ConnectionsInFlight() is zero: */ static void ClearAuthAndACLs(void) { /* Must have no currently open connections to free the ACLs. */ @@ -674,6 +674,24 @@ static void GracefulStop() } #endif // HAVE_SYSTEMD_SD_DAEMON_H +/* Number of connections still being handled. + * + * A connection is added to SERVER_ACCESS.connectionlist before its handler + * thread increments ACTIVE_THREADS, and removed only after the thread has + * decremented it again, so ACTIVE_THREADS alone can be zero while a + * connection is still in flight. + * + * Must be called with cft_server_children locked. + */ +static int ConnectionsInFlight(void) +{ + ThreadLock(cft_count); + const int listed = (int) ListLen(SERVER_ACCESS.connectionlist); + ThreadUnlock(cft_count); + + return MAX(ACTIVE_THREADS, listed); +} + /* Wait for connection-handler threads to finish their work. * * @return Number of live threads remaining after waiting. @@ -684,7 +702,7 @@ static int WaitOnThreads(int graceful_time) for (int i = graceful_time; i > 0; i--) { ThreadLock(cft_server_children); - result = ACTIVE_THREADS; + result = ConnectionsInFlight(); ThreadUnlock(cft_server_children); if (result == 0) @@ -749,15 +767,15 @@ static void CollectCallIfDue(EvalContext *ctx) /* Check for new policy just before spawning a thread. * - * Server reconfiguration can only happen when no threads are active, - * so this is a good time to do it; but we do still have to check for - * running threads. */ + * Server reconfiguration can only happen when no connections are being + * handled, so this is a good time to do it; but we do still have to check + * for connections in flight. */ static void PolicyUpdateIfSafe(EvalContext *ctx, Policy **policy, GenericAgentConfig *config) { ThreadLock(cft_server_children); int prior = COLLECT_INTERVAL; - if (ACTIVE_THREADS == 0) + if (ConnectionsInFlight() == 0) { CheckFileChanges(ctx, policy, config); } diff --git a/cf-serverd/server_classic.c b/cf-serverd/server_classic.c index 8fc439cb10b..3f05f32552a 100644 --- a/cf-serverd/server_classic.c +++ b/cf-serverd/server_classic.c @@ -1538,7 +1538,7 @@ bool BusyWithClassicConnection(EvalContext *ctx, ServerConnectionState *conn) return false; } - GetServerLiteral(ctx, conn, sendbuffer, recvbuffer, encrypted); + GetServerLiteral(ctx, conn, sendbuffer, sizeof(sendbuffer), recvbuffer, encrypted); return true; case PROTOCOL_COMMAND_CONTEXT_SECURE: diff --git a/cf-serverd/server_common.c b/cf-serverd/server_common.c index 3d26e8ecddf..fb4b0b28b22 100644 --- a/cf-serverd/server_common.c +++ b/cf-serverd/server_common.c @@ -925,7 +925,7 @@ bool CompareLocalHash(const char *filename, const unsigned char digest[EVP_MAX_M } } -void GetServerLiteral(EvalContext *ctx, ServerConnectionState *conn, char *sendbuffer, char *recvbuffer, int encrypted) +void GetServerLiteral(EvalContext *ctx, ServerConnectionState *conn, char *sendbuffer, size_t sendbuffer_size, char *recvbuffer, int encrypted) { char handle[CF_BUFSIZE], out[CF_BUFSIZE]; int cipherlen; @@ -934,13 +934,13 @@ void GetServerLiteral(EvalContext *ctx, ServerConnectionState *conn, char *sendb if (ReturnLiteralData(ctx, handle, out)) { - memset(sendbuffer, 0, CF_BUFSIZE); - snprintf(sendbuffer, CF_BUFSIZE, "%s", out); + memset(sendbuffer, 0, sendbuffer_size); + snprintf(sendbuffer, sendbuffer_size, "%s", out); } else { - memset(sendbuffer, 0, CF_BUFSIZE); - snprintf(sendbuffer, CF_BUFSIZE, "BAD: Not found"); + memset(sendbuffer, 0, sendbuffer_size); + snprintf(sendbuffer, sendbuffer_size, "BAD: Not found"); } if (encrypted) diff --git a/cf-serverd/server_common.h b/cf-serverd/server_common.h index 4e41925e0b7..ab764ef95d8 100644 --- a/cf-serverd/server_common.h +++ b/cf-serverd/server_common.h @@ -48,7 +48,7 @@ int StatFile(ServerConnectionState *conn, char *sendbuffer, char *ofilename); void ReplyServerContext(ServerConnectionState *conn, int encrypted, Item *classes); int CfOpenDirectory(ServerConnectionState *conn, char *sendbuffer, char *oldDirname); int CfSecOpenDirectory(ServerConnectionState *conn, char *sendbuffer, char *dirname); -void GetServerLiteral(EvalContext *ctx, ServerConnectionState *conn, char *sendbuffer, char *recvbuffer, int encrypted); +void GetServerLiteral(EvalContext *ctx, ServerConnectionState *conn, char *sendbuffer, size_t sendbuffer_size, char *recvbuffer, int encrypted); bool GetServerQuery(ServerConnectionState *conn, char *recvbuffer, int encrypted); bool CompareLocalHash(const char *filename, const unsigned char digest[EVP_MAX_MD_SIZE + 1], char sendbuffer[CFD_FALSE_SIZE]); diff --git a/cf-serverd/server_tls.c b/cf-serverd/server_tls.c index c89ad3b4ab3..cae1e3bda02 100644 --- a/cf-serverd/server_tls.c +++ b/cf-serverd/server_tls.c @@ -1003,7 +1003,7 @@ bool BusyWithNewProtocol(EvalContext *ctx, ServerConnectionState *conn) return true; } - GetServerLiteral(ctx, conn, sendbuffer, recvbuffer, encrypted); + GetServerLiteral(ctx, conn, sendbuffer, sizeof(sendbuffer), recvbuffer, encrypted); return true; } case PROTOCOL_COMMAND_CONTEXT: diff --git a/ci/configure.sh b/ci/configure.sh index e47d02b6e12..8a7d986415d 100755 --- a/ci/configure.sh +++ b/ci/configure.sh @@ -4,7 +4,8 @@ set -ex thisdir="$(dirname "$0")" cd "$thisdir"/.. -OPTS="--config-cache --enable-debug" +# include any existing OPTS in calling environment +OPTS="$OPTS --config-cache --enable-debug" if [ -f /etc/os-release ]; then source /etc/os-release diff --git a/ci/dependencies.sh b/ci/dependencies.sh index cef2ea8f98a..8d6d6af0181 100755 --- a/ci/dependencies.sh +++ b/ci/dependencies.sh @@ -24,6 +24,7 @@ build_lmdb() { cd lmdb/libraries/liblmdb make $GAINROOT make install prefix=/usr + $GAINROOT ldconfig ) } @@ -49,7 +50,7 @@ if [ -f /etc/os-release ]; then build_lmdb build_librsync elif [ "$ID" = "rhel" ] || [[ "$ID_LIKE" =~ "rhel" ]]; then - if [ "$VERSION_MAJOR" -ge "10" ]; then + if [ "$VERSION_MAJOR" -ge "8" ]; then # note that having a redhat subscription makes things easier: lmdb-devel and librsync-devel are available from codeready-builder repo if subscription-manager status; then $GAINROOT subscription-manager config --rhsm.manage_repos=1 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/cf-windows-functions.h b/libpromises/cf-windows-functions.h index 733fa4e92e2..5adb72ff4af 100644 --- a/libpromises/cf-windows-functions.h +++ b/libpromises/cf-windows-functions.h @@ -39,6 +39,7 @@ bool NovaWin_FileExists(const char *fileName); bool NovaWin_IsDir(char *fileName); int NovaWin_TakeFileOwnership(char *path); int NovaWin_SetFileOwnership(char *path, SID *sid); +DWORD NovaWin_GetFileOwnerSid(const char *path, SID *sid, DWORD sid_sz); off_t NovaWin_GetDiskUsage(char *file, CfSize type); /* win_log.c */ 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/evalfunction.c b/libpromises/evalfunction.c index 7707b18d0cb..d42df8c6fca 100644 --- a/libpromises/evalfunction.c +++ b/libpromises/evalfunction.c @@ -5193,7 +5193,7 @@ static FnCallResult FnCallFileStatDetails(ARG_UNUSED EvalContext *ctx, } char data[CF_BUFSIZE]; - int datasize = lgetxattr(path, current, data, sizeof(data)); + int datasize = lgetxattr(path, current, data, sizeof(data) - 1); if (datasize < 0) { if (errno == ENOTSUP) @@ -5208,6 +5208,8 @@ static FnCallResult FnCallFileStatDetails(ARG_UNUSED EvalContext *ctx, } else { + data[datasize] = '\0'; + if (!StringIsPrintable(data)) { Log(LOG_LEVEL_INFO, "Skipping extended attribute of '%s', it has non-printable data: '%s=%s'", @@ -6302,7 +6304,8 @@ static FnCallResult FnCallFormat(EvalContext *ctx, ARG_UNUSED const Policy *poli char piece[CF_BUFSIZE]; memset(piece, 0, CF_BUFSIZE); - const char bad_modifiers[] = "hLqjzt"; + /* '*' and '$' make printf fetch arguments we never pass */ + const char bad_modifiers[] = "hLqjzt*$"; const size_t length = strlen(bad_modifiers); for (size_t b = 0; b < length; b++) { @@ -6317,6 +6320,18 @@ static FnCallResult FnCallFormat(EvalContext *ctx, ARG_UNUSED const Policy *poli } } + /* "%ls" would read the char * argument as wchar_t * */ + if (strchr(format_piece, 'l') != NULL && + (strrchr(format_piece, 's') != NULL || + strrchr(format_piece, 'S') != NULL)) + { + Log(LOG_LEVEL_ERR, "format() does not allow modifier character 'l' in format specifier '%s'.", + format_piece); + BufferDestroy(buf); + SeqDestroy(s); + return FnFailure(); + } + if (strrchr(format_piece, 'd') != NULL || strrchr(format_piece, 'o') != NULL || strrchr(format_piece, 'x') != NULL) { long x = 0; diff --git a/libpromises/files_links.c b/libpromises/files_links.c index eda0be116b9..ec4762abb07 100644 --- a/libpromises/files_links.c +++ b/libpromises/files_links.c @@ -536,7 +536,13 @@ bool KillGhostLink(EvalContext *ctx, const char *name, const Attributes *attr, c } } - strcat(linkpath, linkbuf); + if (strlcat(linkpath, linkbuf, sizeof(linkpath)) >= sizeof(linkpath)) + { + Log(LOG_LEVEL_ERR, + "Internal limit reached in KillGhostLink()," + " path too long: '%s' + '%s'", linkpath, linkbuf); + return false; + } CompressPath(tmp, sizeof(tmp), linkpath); if (stat(tmp, &statbuf) == -1) /* link points nowhere */ 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..90e22410a58 100644 --- a/libpromises/mod_reactor.c +++ b/libpromises/mod_reactor.c @@ -51,6 +51,11 @@ 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), + ConstraintSyntaxNewString(REACTOR_PACKAGE_INSTALLED, CF_ANYSTRING, "Package to react for on installation", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewString(REACTOR_PACKAGE_UNINSTALLED, CF_ANYSTRING, "Package to react for on uninstallation", SYNTAX_STATUS_NORMAL), + ConstraintSyntaxNewString(REACTOR_PACKAGE_MODULE, ".*", "Package module to list the installed packages with, instead of the default one", 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..c7a815c91ab 100644 --- a/libpromises/mod_reactor.h +++ b/libpromises/mod_reactor.h @@ -36,6 +36,11 @@ #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" +#define REACTOR_PACKAGE_INSTALLED "package_installed" +#define REACTOR_PACKAGE_UNINSTALLED "package_uninstalled" +#define REACTOR_PACKAGE_MODULE "package_module" /* 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/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/plucked.sub.cf b/tests/acceptance/plucked.sub.cf index ef7bda18137..6b6c3eceecf 100644 --- a/tests/acceptance/plucked.sub.cf +++ b/tests/acceptance/plucked.sub.cf @@ -63,9 +63,9 @@ body contain in_dir_shell(dir) # ``` { chdir => "$(dir)"; - useshell => "true"; # canonical "useshell" but this is backwards-compatible + useshell => "true"; } body contain in_shell @@ -79,9 +79,8 @@ body contain in_shell # contain => in_shell; # ``` { - useshell => "true"; - # canonical "useshell" but this is backwards-compatible + useshell => "true"; } body contain in_shell_bg @@ -90,9 +89,8 @@ body contain in_shell_bg # parser strictness enhancements. Backgrounding is handeled by the body action # background attribute. { - useshell => "true"; - # canonical "useshell" but this is backwards-compatible + useshell => "true"; } body contain in_shell_and_silent @@ -107,9 +105,8 @@ body contain in_shell_and_silent # comment => "Silently run command in shell"; # ``` { - useshell => "true"; - # canonical "useshell" but this is backwards-compatible + useshell => "true"; no_output => "true"; } @@ -126,9 +123,8 @@ body contain in_dir_shell_and_silent(dir) # comment => "Silently run command in shell"; # ``` { - useshell => "true"; - # canonical "useshell" but this is backwards-compatible + useshell => "true"; no_output => "true"; chdir => "$(dir)"; } @@ -752,10 +748,18 @@ bundle edit_line manage_variable_values_ini(tab, sectionName) { vars: "index" slist => getindices("$(tab)[$(sectionName)]"); + "_escaped_section" string => escape("$(sectionName)"); + + classes: + "section_$(sectionName)_exists" + expression => regline("^\[$(_escaped_section)\]", "$(edit.filename)"), + if => fileexists("$(edit.filename)"), + comment => "Check if the section header already exists in the file"; delete_lines: ".*" select_region => INI_section(escape("$(sectionName)")), + if => canonify("section_$(sectionName)_exists"), comment => "Remove all entries in the region so there are no extra entries"; insert_lines: @@ -764,7 +768,12 @@ bundle edit_line manage_variable_values_ini(tab, sectionName) comment => "Insert lines"; "$(index)=$($(tab)[$(sectionName)][$(index)])" - select_region => INI_section(escape("$(sectionName)")); + select_region => INI_section(escape("$(sectionName)")), + if => canonify("section_$(sectionName)_exists"); + + "$(index)=$($(tab)[$(sectionName)][$(index)])" + location => after("^\[$(_escaped_section)\]"), + if => not(canonify("section_$(sectionName)_exists")); } bundle edit_line set_variable_values_ini(tab, sectionName) @@ -786,18 +795,26 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); + "_escaped_section" string => escape("$(sectionName)"); classes: "edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])", "dontchange"), comment => "Create conditions to make changes"; + "section_$(sectionName)_exists" + expression => regline("^\[$(_escaped_section)\]", "$(edit.filename)"), + if => fileexists("$(edit.filename)"), + comment => "Check if the section header already exists in the file"; + field_edits: # If the line is there, but commented out, first uncomment it "#+\s*$(index)\s*=.*" select_region => INI_section(escape("$(sectionName)")), edit_field => col("\s*=\s*", "1", "$(index)", "set"), - if => "edit_$(cindex[$(index)])"; + if => and( + "edit_$(cindex[$(index)])", canonify("section_$(sectionName)_exists") + ); # match a line starting like the key something "\s*$(index)\s*=.*" @@ -808,7 +825,9 @@ bundle edit_line set_variable_values_ini(tab, sectionName) classes => results( "bundle", "set_variable_values_ini_not_$(cindex[$(index)])" ), - if => "edit_$(cindex[$(index)])"; + if => and( + "edit_$(cindex[$(index)])", canonify("section_$(sectionName)_exists") + ); insert_lines: "[$(sectionName)]" @@ -817,7 +836,20 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; + if => and( + "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired)", + "edit_$(cindex[$(index)])", + canonify("section_$(sectionName)_exists") + ), + handle => "insert_key_in_existing_section_$(sectionName)_$(cindex[$(index)])"; + + "$(index)=$($(tab)[$(sectionName)][$(index)])" + location => after("^\[$(_escaped_section)\]"), + if => and( + "edit_$(cindex[$(index)])", + not(canonify("section_$(sectionName)_exists")) + ), + handle => "insert_key_in_new_section_$(sectionName)_$(cindex[$(index)])"; } bundle edit_line insert_ini_section(name, config) 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..e938ea23a78 100644 --- a/tests/reactor/lib.sh +++ b/tests/reactor/lib.sh @@ -1,21 +1,71 @@ # Helpers sourced by the tests. Expects CF_REACTOR and LOG to be exported by -# run.sh. +# run_test.sh. +# +# Only portable tools are used, as the tests are run on all the platforms (e.g. +# no timeout(1) or grep -m). + +# 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. Fails if it never succeeded. Runs in a subshell without +# tracing, so that set -x doesn't log every poll. +wait_until() { + ( + set +x + polls=$((WAIT_TIMEOUT * 10)) + until "$@"; do + [ "$polls" -gt 0 ] || exit 1 + polls=$((polls - 1)) + sleep 0.1 + done + ) +} + +# Print the current time in seconds since the Epoch. +now() { + date +%s +} + +# 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 +73,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 +83,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 @@ -84,6 +128,45 @@ check_events_unordered() { fi } +# Create package module $1 in $WORKDIR. It lists the packages in the file +# given as its option (default_options), see set_installed_packages. +create_package_module() { + mkdir -p "$WORKDIR/modules/packages" + cat > "$WORKDIR/modules/packages/$1" <<'MODULE' +#!/bin/sh +case "$1" in + supports-api-version) + echo 1 + ;; + list-installed) + while read -r line; do + case "$line" in + options=*) file=${line#options=} ;; + esac + done + if [ -f "$file" ]; then + cat "$file" + fi + ;; + *) + exit 1 + ;; +esac +MODULE + chmod 700 "$WORKDIR/modules/packages/$1" +} + +# Set the packages listed from file $1 to the remaining arguments. Replaces +# the file atomically, since the package module may read it meanwhile. +set_installed_packages() { + file=$1 + shift + for name in "$@"; do + printf 'Name=%s\nVersion=1.0\nArchitecture=generic\n' "$name" + done > "$file.tmp" + mv "$file.tmp" "$file" +} + # Start cf-reactor in the background with policy file $1, logging to $LOG. # It is stopped when the test exits. start_reactor() { 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..e1d78bb0135 --- /dev/null +++ b/tests/reactor/run_test.sh @@ -0,0 +1,94 @@ +#!/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 + +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/009_package_events.cf b/tests/reactor/tests/01_event_types/009_package_events.cf new file mode 100644 index 00000000000..83bdfcd6e54 --- /dev/null +++ b/tests/reactor/tests/01_event_types/009_package_events.cf @@ -0,0 +1,46 @@ +####################################################### +# +# Test package_installed and package_uninstalled events +# +####################################################### +body common control +{ + inputs => { "../../lib.cf" }; + bundlesequence => { "test" }; + version => "1.0"; + package_module => "reactor_test"; +} + +# Lists the packages in the file given as option +body package_module reactor_test +{ + default_options => { "$(test.installed_file)" }; +} + +body when installed(name) +{ + package_installed => "$(name)"; +} + +body when uninstalled(name) +{ + package_uninstalled => "$(name)"; +} + +bundle reactor test +{ + vars: + "package" string => getenv("TEST_PACKAGE", 999); + "installed_file" string => getenv("INSTALLED_FILE", 999); + "events_file" string => getenv("EVENTS_FILE", 999); + "promise_name" string => basename("$(this.promise_filename)", ".cf"); + + events: + "$(promise_name)_installed" + when => installed("$(package)"), + then => record_event("$(events_file)", "installed"); + + "$(promise_name)_uninstalled" + when => uninstalled("$(package)"), + then => record_event("$(events_file)", "uninstalled"); +} diff --git a/tests/reactor/tests/01_event_types/009_package_events.sh b/tests/reactor/tests/01_event_types/009_package_events.sh new file mode 100644 index 00000000000..5e9fb35bec9 --- /dev/null +++ b/tests/reactor/tests/01_event_types/009_package_events.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# check that package_installed and package_uninstalled fire once when a +# package is installed and uninstalled, with the default package module of +# body common control. Only the exact name matches: another package whose +# name starts with it stays installed. + +set -ex + +. "$(dirname "$0")/../../lib.sh" + +POLICY_FILE="$1" + +TEST_PACKAGE="reactor_test_pkg" +OTHER_PACKAGE="reactor_test_pkg-dev" +INSTALLED_FILE="$WORKDIR/tmp/installed" +EVENTS_FILE="$WORKDIR/tmp/events" +export TEST_PACKAGE INSTALLED_FILE EVENTS_FILE + +create_package_module reactor_test +set_installed_packages "$INSTALLED_FILE" "$OTHER_PACKAGE" + +start_reactor "$POLICY_FILE" + +wait_for_watchers 2 + +# checked once the first listing is done +wait_for_log "Package '$TEST_PACKAGE' is not installed, watching for its installation" + +set_installed_packages "$INSTALLED_FILE" "$OTHER_PACKAGE" "$TEST_PACKAGE" +wait_for_lines "$EVENTS_FILE" 1 +check_event "$EVENTS_FILE" 1 "installed" + +wait_for_log "Package '$TEST_PACKAGE' is installed, watching for its uninstallation" + +set_installed_packages "$INSTALLED_FILE" "$OTHER_PACKAGE" +wait_for_lines "$EVENTS_FILE" 2 +check_event "$EVENTS_FILE" 2 "uninstalled" diff --git a/tests/reactor/tests/01_event_types/010_package_module.cf b/tests/reactor/tests/01_event_types/010_package_module.cf new file mode 100644 index 00000000000..c5e5601b252 --- /dev/null +++ b/tests/reactor/tests/01_event_types/010_package_module.cf @@ -0,0 +1,55 @@ +####################################################### +# +# Test the package_module attribute of package events +# +####################################################### +body common control +{ + inputs => { "../../lib.cf" }; + bundlesequence => { "test" }; + version => "1.0"; + + # no default package_module +} + +# Lists the packages in the file given as option +body package_module reactor_test +{ + default_options => { "$(test.installed_file)" }; +} + +body when installed(name) +{ + package_installed => "$(name)"; +} + +body when installed_with(name, module) +{ + package_installed => "$(name)"; + package_module => "$(module)"; +} + +bundle reactor test +{ + vars: + "package" string => getenv("TEST_PACKAGE", 999); + "installed_file" string => getenv("INSTALLED_FILE", 999); + "events_file" string => getenv("EVENTS_FILE", 999); + "promise_name" string => basename("$(this.promise_filename)", ".cf"); + + events: + # no package_module, and no default one + "$(promise_name)_no_default" + when => installed("$(package)"), + then => record_event("$(events_file)", "no default installed"); + + # a package module body that doesn't exist + "$(promise_name)_unknown" + when => installed_with("$(package)", "no_such_module"), + then => record_event("$(events_file)", "unknown installed"); + + # last, so that its log lines come after the errors of the others + "$(promise_name)_module" + when => installed_with("$(package)", "reactor_test"), + then => record_event("$(events_file)", "installed"); +} diff --git a/tests/reactor/tests/01_event_types/010_package_module.sh b/tests/reactor/tests/01_event_types/010_package_module.sh new file mode 100644 index 00000000000..52bc3ed1650 --- /dev/null +++ b/tests/reactor/tests/01_event_types/010_package_module.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# check the package_module attribute of package events: the named package +# module is used, and the events promise fails if there is no default package +# module or no package module body of that name + +set -ex + +. "$(dirname "$0")/../../lib.sh" + +POLICY_FILE="$1" + +TEST_PACKAGE="reactor_test_pkg" +INSTALLED_FILE="$WORKDIR/tmp/installed" +EVENTS_FILE="$WORKDIR/tmp/events" +export TEST_PACKAGE INSTALLED_FILE EVENTS_FILE + +create_package_module reactor_test +set_installed_packages "$INSTALLED_FILE" + +start_reactor "$POLICY_FILE" + +# logged on registration, in the order of the promises +wait_for_log "No default package module to watch package '$TEST_PACKAGE' with" +wait_for_log "No package module body 'no_such_module' to watch package '$TEST_PACKAGE' with" + +# only the watcher with the existing package module +wait_for_watchers 1 + +wait_for_log "Package '$TEST_PACKAGE' is not installed, watching for its installation" + +set_installed_packages "$INSTALLED_FILE" "$TEST_PACKAGE" +wait_for_lines "$EVENTS_FILE" 1 +check_event "$EVENTS_FILE" 1 "installed" 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..5a40e6ac708 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -159,6 +159,8 @@ check_PROGRAMS = \ client_code_test \ watcher_test \ file_watcher_test \ + package_watcher_test \ + user_watcher_test \ stoppable_thread_test \ wakeup_channel_test @@ -241,6 +243,12 @@ watcher_test_SOURCES = watcher_test.c \ file_watcher_test_SOURCES = file_watcher_test.c \ ../../cf-reactor/file_watcher.c +# package_watcher.c is included by the test, to test its static functions +package_watcher_test_SOURCES = package_watcher_test.c \ + ../../cf-reactor/stoppable_thread.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/evalfunction_test.c b/tests/unit/evalfunction_test.c index 32f004b8bb9..fcfd2f18a53 100644 --- a/tests/unit/evalfunction_test.c +++ b/tests/unit/evalfunction_test.c @@ -180,6 +180,40 @@ static void test_module_protocol_percent_no_delimiter(void) EvalContextDestroy(ctx); } +static FnCallResult format_single_testcase(const char *format, const char *arg) +{ + Rlist *args = NULL; + RlistAppendScalar(&args, format); + RlistAppendScalar(&args, arg); + FnCall *call = FnCallNew("format", args); + + FnCallResult res = FnCallFormat(NULL, NULL, call, args); + + FnCallDestroy(call); + return res; +} + +static void test_format_rejects_unpassed_arguments(void) +{ + FnCallResult res = format_single_testcase("%04d", "100"); + assert_int_equal(res.status, FNCALL_SUCCESS); + assert_string_equal("0100", (char *) res.rval.item); + RvalDestroy(res.rval); + + res = format_single_testcase("%-6s|", "abc"); + assert_int_equal(res.status, FNCALL_SUCCESS); + assert_string_equal("abc |", (char *) res.rval.item); + RvalDestroy(res.rval); + + /* Each of these makes printf read an argument that is never passed. */ + const char *const bad[] = { "%*d", "%.*f", "%*s", "%2$s", "%1$*2$d", "%ls", "%lS" }; + for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) + { + res = format_single_testcase(bad[i], "7"); + assert_int_equal(res.status, FNCALL_FAILURE); + } +} + int main() { PRINT_TEST_BANNER(); @@ -188,6 +222,7 @@ int main() unit_test(test_hostinnetgroup_not_found), unit_test(test_basename), unit_test(test_module_protocol_percent_no_delimiter), + unit_test(test_format_rejects_unpassed_arguments), }; return run_tests(tests); 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/files_links_test.c b/tests/unit/files_links_test.c index 6b2fae5739d..0b1eefc73c6 100644 --- a/tests/unit/files_links_test.c +++ b/tests/unit/files_links_test.c @@ -25,7 +25,10 @@ #include #include -#include /* ExpandLinks */ +#include /* ExpandLinks, KillGhostLink */ +#include /* Attributes */ +#include /* symlink, unlink */ +#include /* errno */ /* A single path component longer than the internal node buffer * (CF_MAXLINKSIZE) overflowed the stack in ExpandLinks(): the parse into @@ -44,12 +47,48 @@ static void test_expand_links_long_component(void) assert_false(ret); } +/* KillGhostLink() rebuilds the absolute target of a relative symlink by + * appending the readlink() result onto the link's directory in a CF_BUFSIZE + * buffer. The append was an unbounded strcat(), so a symlink whose relative + * target is near CF_BUFSIZE overflowed the stack. It must bail out instead. */ +static void test_kill_ghost_link_long_target(void) +{ + char tmpl[] = "/tmp/cfghostXXXXXX"; + const char *dir = mkdtemp(tmpl); + assert_true(dir != NULL); + + char linkpath[256]; + xsnprintf(linkpath, sizeof(linkpath), "%s/l", dir); + + char target[CF_BUFSIZE]; + memset(target, 'x', sizeof(target) - 1); + target[sizeof(target) - 1] = '\0'; + if (symlink(target, linkpath) != 0) + { + /* Some platforms (e.g. macOS) refuse targets this long, in which + * case the overflow cannot be reached and there's nothing to test. */ + assert_int_equal(errno, ENAMETOOLONG); + rmdir(dir); + return; + } + + Attributes attr = { 0 }; + attr.link.when_no_file = cfa_delete; + PromiseResult result = PROMISE_RESULT_NOOP; + + assert_false(KillGhostLink(NULL, linkpath, &attr, NULL, &result)); + + unlink(linkpath); + rmdir(dir); +} + int main() { PRINT_TEST_BANNER(); const UnitTest tests[] = { unit_test(test_expand_links_long_component), + unit_test(test_kill_ghost_link_long_target), }; return run_tests(tests); diff --git a/tests/unit/package_watcher_test.c b/tests/unit/package_watcher_test.c new file mode 100644 index 00000000000..7f04817ec7b --- /dev/null +++ b/tests/unit/package_watcher_test.c @@ -0,0 +1,329 @@ +#include +#include + +/* Included, to test its static functions */ +#include + +#include + +/* How long to wait for the listing thread before failing */ +#define WAIT_TIMEOUT_SECS 10 + +/* Returns a body of a package module that doesn't exist, with the given + * option if not NULL. */ +static PackageModuleBody *NewMissingModuleBody(const char *option) +{ + PackageModuleBody *module = xcalloc(1, sizeof(PackageModuleBody)); + module->name = xstrdup("missing"); + module->module_path = xstrdup("/nonexistent/package_watcher_test"); + if (option != NULL) + { + RlistAppendScalar(&module->options, option); + } + return module; +} + +static bool IsListed(PackageListing *listing) +{ + ThreadLock(&listing->mutex); + const bool listed = (listing->listed_at != 0); + ThreadUnlock(&listing->mutex); + return listed; +} + +/* Waits until the listing thread has finished its first listing */ +static void WaitForFirstListing(PackageListing *listing) +{ + const time_t deadline = time(NULL) + WAIT_TIMEOUT_SECS; + while (!IsListed(listing)) + { + assert_true(time(NULL) < deadline); + usleep(10000); /* 20ms */ + } +} + +/* A listing without a thread, whose result the tests set */ +static PackageListing *NewFakeListing(void) +{ + PackageListing *listing = xcalloc(1, sizeof(PackageListing)); + pthread_mutex_init(&listing->mutex, NULL); + return listing; +} + +/* Sets the result of a fake listing: only name installed, or nothing if + * name is NULL */ +static void SetFakeListing(PackageListing *listing, const char *name) +{ + StringSetDestroy(listing->installed); + listing->installed = StringSetNew(); + if (name != NULL) + { + StringSetAdd(listing->installed, xstrdup(name)); + } + listing->listed_at = time(NULL); +} + +static void SetFakeListingFailed(PackageListing *listing) +{ + StringSetDestroy(listing->installed); + listing->installed = NULL; + listing->listed_at = time(NULL); +} + +/* Returns a state watching package foo in a fake listing */ +static PackageWatcherState *NewFakeState(void) +{ + PackageWatcherState *state = xcalloc(1, sizeof(PackageWatcherState)); + state->name = xstrdup("foo"); + state->listing = NewFakeListing(); + return state; +} + +static void DestroyFakeState(PackageWatcherState *state) +{ + PackageListingDestroy(state->listing); + free(state->name); + free(state); +} + +static void test_parse_installed_packages(void) +{ + Rlist *response = NULL; + RlistAppendScalar(&response, "Name=foo"); + RlistAppendScalar(&response, "Version=1.0"); + RlistAppendScalar(&response, "Architecture=amd64"); + RlistAppendScalar(&response, "Name=bar"); + RlistAppendScalar(&response, "Version=2.0"); + RlistAppendScalar(&response, "Architecture=amd64"); + /* The same package for another architecture */ + RlistAppendScalar(&response, "Name=bar"); + RlistAppendScalar(&response, "Version=2.0"); + RlistAppendScalar(&response, "Architecture=i386"); + + StringSet *installed = ParseInstalledPackages(response, "fake"); + assert_true(installed != NULL); + assert_int_equal(StringSetSize(installed), 2); + assert_true(StringSetContains(installed, "foo")); + assert_true(StringSetContains(installed, "bar")); + + StringSetDestroy(installed); + RlistDestroy(response); +} + +static void test_parse_skips_malformed_records(void) +{ + Rlist *response = NULL; + /* No architecture */ + RlistAppendScalar(&response, "Name=foo"); + RlistAppendScalar(&response, "Version=1.0"); + RlistAppendScalar(&response, "Name=bar"); + RlistAppendScalar(&response, "Version=2.0"); + RlistAppendScalar(&response, "Architecture=amd64"); + /* No version, in the last record */ + RlistAppendScalar(&response, "Name=baz"); + RlistAppendScalar(&response, "Architecture=amd64"); + + /* The malformed records of foo and baz are skipped, and the listing + * still succeeds with the complete record of bar */ + StringSet *installed = ParseInstalledPackages(response, "fake"); + assert_true(installed != NULL); + assert_int_equal(StringSetSize(installed), 1); + assert_true(StringSetContains(installed, "bar")); + + StringSetDestroy(installed); + RlistDestroy(response); +} + +static void test_parse_empty_reply(void) +{ + /* Nothing installed, not a failure */ + StringSet *installed = ParseInstalledPackages(NULL, "fake"); + assert_true(installed != NULL); + assert_int_equal(StringSetSize(installed), 0); + StringSetDestroy(installed); +} + +static void test_parse_error_reply(void) +{ + Rlist *response = NULL; + RlistAppendScalar(&response, "Name=foo"); + RlistAppendScalar(&response, "Version=1.0"); + RlistAppendScalar(&response, "Architecture=amd64"); + RlistAppendScalar(&response, "ErrorMessage=Something went wrong"); + + assert_true(ParseInstalledPackages(response, "fake") == NULL); + RlistDestroy(response); +} + +static void test_check_installed_transitions(void) +{ + PackageWatcherState *state = NewFakeState(); + + /* Not listed yet: no observation */ + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + assert_false(state->observed); + + /* Not installed (the first observation is the reference) */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + + /* Installed: the event happens once, not while it stays installed */ + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_EVENT); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + + /* Uninstalled, then installed again: the event happens again */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_EVENT); + + /* Another package installed: no event */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + SetFakeListing(state->listing, "bar"); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + + DestroyFakeState(state); +} + +static void test_check_installed_at_start(void) +{ + PackageWatcherState *state = NewFakeState(); + + /* Installed at the first observation: the reference, not an event */ + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + + DestroyFakeState(state); +} + +static void test_check_uninstalled_transitions(void) +{ + PackageWatcherState *state = NewFakeState(); + + /* Installed (the first observation is the reference) */ + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + + /* Uninstalled: the event happens once, not while it stays uninstalled */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_EVENT); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + + /* Installed, then uninstalled again: the event happens again */ + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_EVENT); + + DestroyFakeState(state); +} + +static void test_check_uninstalled_absent_at_start(void) +{ + PackageWatcherState *state = NewFakeState(); + + /* Not installed at the first observation: the reference, not an event */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + + DestroyFakeState(state); +} + +static void test_check_failed_listing(void) +{ + PackageWatcherState *state = NewFakeState(); + + /* A failed first listing: an error, and no observation */ + SetFakeListingFailed(state->listing); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_ERROR); + assert_false(state->observed); + + /* Not installed (the reference) */ + SetFakeListing(state->listing, NULL); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_NO_EVENT); + + /* A failed listing is not taken as "nothing installed", and keeps the + * last observation, so the installation is seen after it */ + SetFakeListingFailed(state->listing); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_ERROR); + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_EVENT); + + /* Installed, then a failed listing: no uninstallation */ + SetFakeListingFailed(state->listing); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_ERROR); + SetFakeListing(state->listing, "foo"); + assert_int_equal(PackageWatcherCheckPackageUninstalled(state), WATCHER_CHECK_NO_EVENT); + + DestroyFakeState(state); +} + +static void test_listings_shared(void) +{ + PackageModuleBody *module = NewMissingModuleBody(NULL); + PackageModuleBody *other = NewMissingModuleBody("option=1"); + + /* The same module shares a listing, other options don't */ + PackageListing *first = PackageListingAcquire(module); + PackageListing *second = PackageListingAcquire(module); + PackageListing *third = PackageListingAcquire(other); + assert_true(first != NULL); + assert_true(third != NULL); + assert_true(first == second); + assert_true(first != third); + assert_int_equal(first->refcount, 2); + assert_int_equal(SeqLength(listings), 2); + + /* Removed once the last watcher releases it */ + PackageListingRelease(first); + assert_int_equal(SeqLength(listings), 2); + PackageListingRelease(second); + assert_int_equal(SeqLength(listings), 1); + PackageListingRelease(third); + assert_int_equal(SeqLength(listings), 0); + + PackageModuleBodyDestroy(module); + PackageModuleBodyDestroy(other); +} + +static void test_listing_module_cannot_run(void) +{ + PackageModuleBody *module = NewMissingModuleBody(NULL); + + PackageWatcherState *state = PackageWatcherStateNewFromModule("foo", module); + assert_true(state != NULL); + + /* A failed listing, not "nothing installed" */ + WaitForFirstListing(state->listing); + assert_int_equal(PackageWatcherCheckPackageInstalled(state), WATCHER_CHECK_ERROR); + assert_false(state->observed); + + PackageWatcherStateDestroy(state); + assert_int_equal(SeqLength(listings), 0); + PackageModuleBodyDestroy(module); +} + +int main() +{ + PRINT_TEST_BANNER(); + const UnitTest tests[] = + { + unit_test(test_parse_installed_packages), + unit_test(test_parse_skips_malformed_records), + unit_test(test_parse_empty_reply), + unit_test(test_parse_error_reply), + unit_test(test_check_installed_transitions), + unit_test(test_check_installed_at_start), + unit_test(test_check_uninstalled_transitions), + unit_test(test_check_uninstalled_absent_at_start), + unit_test(test_check_failed_listing), + unit_test(test_listings_shared), + unit_test(test_listing_module_cannot_run), + }; + + return run_tests(tests); +} diff --git a/tests/unit/protocol_test.c b/tests/unit/protocol_test.c index 29b30d31a5d..39829e3a694 100644 --- a/tests/unit/protocol_test.c +++ b/tests/unit/protocol_test.c @@ -8,6 +8,13 @@ #include /* GetCommandClassic */ +#include +#include +#include + +#include +#include + /* * The protocol consists of the following commands: @@ -450,13 +457,58 @@ static void test_user_name(void) assert_true(IsUserNameValid(valid_user_name)); } +/* + * BusyWithNewProtocol() hands GetServerLiteral() a sendbuffer of only + * CF_BUFSIZE - CF_INBAND_OFFSET bytes. The function must not write past the + * size it is given, so guard bytes placed right after a buffer of that size + * have to survive the call. + */ +static void test_get_server_literal_sendbuffer_bound(void) +{ + int sv[2]; + assert_int_equal(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0); + + ServerConnectionState *conn = xcalloc(1, sizeof(*conn)); + conn->conn_info = ConnectionInfoNew(); + ConnectionInfoSetSocket(conn->conn_info, sv[0]); + ConnectionInfoSetProtocolVersion(conn->conn_info, CF_PROTOCOL_CLASSIC); + + ConnectionInfo *client = ConnectionInfoNew(); + ConnectionInfoSetSocket(client, sv[1]); + ConnectionInfoSetProtocolVersion(client, CF_PROTOCOL_CLASSIC); + + EvalContext *ctx = EvalContextNew(); + + char sendbuffer[CF_MSGSIZE + CF_INBAND_OFFSET]; + memset(sendbuffer, 0x5a, sizeof(sendbuffer)); + + char recvbuffer[] = "VAR literal_handle"; + GetServerLiteral(ctx, conn, sendbuffer, CF_MSGSIZE, recvbuffer, 0); + + for (size_t i = CF_MSGSIZE; i < sizeof(sendbuffer); i++) + { + assert_int_equal(sendbuffer[i], 0x5a); + } + + char reply[CF_BUFSIZE]; + int len = ReceiveTransaction(client, reply, NULL); + assert_true(len > 0); + assert_string_equal(reply, "BAD: Not found"); + + EvalContextDestroy(ctx); + ConnectionInfoDestroy(&client); + ConnectionInfoDestroy(&conn->conn_info); + free(conn); +} + int main() { PRINT_TEST_BANNER(); const UnitTest tests[] = { unit_test(test_command_parser), - unit_test(test_user_name) + unit_test(test_user_name), + unit_test(test_get_server_literal_sendbuffer_bound) }; return run_tests(tests); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 10b5513121a..9aa8cc01a1e 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -82,8 +82,8 @@ static void test_diff_install_cancels_removal(void) char output[4096]; assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo [1.2.3]' would be installed") != NULL); - assert_true(strstr(output, "would be removed") == NULL); + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "would be removed")); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -98,7 +98,7 @@ static void test_diff_version_and_architecture(void) char output[4096]; assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo-x86_64 [1.2.3]' would be installed") != NULL); + assert_true(StringContains(output, "Package 'foo-x86_64 [1.2.3]' would be installed")); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -141,8 +141,106 @@ static void test_manifest_install_cancels_removal(void) char output[4096]; assert_true(call_with_captured_stdout(&ManifestPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo [1.2.3]' would be present") != NULL); - assert_true(strstr(output, "would be absent") == NULL); + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be present")); + assert_false(StringContains(output, "would be absent")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* Two recorded installations of the same package without a version must be + * reported once, not abort on comparing the empty versions. */ +static void test_diff_repeated_unversioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo' would be installed")); + /* Reported once: the report has no other line. */ + assert_int_equal(StringCountTokens(output, strlen(output), "\n"), 1); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* A versioned installation record is more specific than an unversioned one + * of the same package, so it is the one to report. */ +static void test_diff_unversioned_then_versioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,1.2.3,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "Package 'foo' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* An unversioned installation record must never replace an existing + * versioned installation record of the same package. */ +static void test_diff_versioned_then_unversioned_install(void) +{ + write_pkgs_ops("i,foo,1.2.3,\r\n" + "i,foo,,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "Package 'foo' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* When two installations are recorded with different versions and the first + * has the higher version, the first record is kept. */ +static void test_diff_install_higher_version_first_keeps_first(void) +{ + write_pkgs_ops("i,foo,2.0.0,\r\n" + "i,foo,1.0.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [2.0.0]' would be installed")); + assert_false(StringContains(output, "Package 'foo [1.0.0]' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* When two installations are recorded with different versions and the last + * has the higher version, the last record replaces the first. */ +static void test_diff_install_higher_version_last_keeps_last(void) +{ + write_pkgs_ops("i,foo,1.0.0,\r\n" + "i,foo,2.0.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [2.0.0]' would be installed")); + assert_false(StringContains(output, "Package 'foo [1.0.0]' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* Two recorded installations of the same package with the same version + * must be reported once. */ +static void test_diff_repeated_versioned_install(void) +{ + write_pkgs_ops("i,foo,1.2.3,\r\n" + "i,foo,1.2.3,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + /* Reported once: the report has no other line. */ + assert_int_equal(StringCountTokens(output, strlen(output), "\n"), 1); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -161,6 +259,12 @@ int main() unit_test(test_diff_removal_with_version), unit_test(test_diff_removal_with_other_version_keeps_install), unit_test(test_manifest_install_cancels_removal), + unit_test(test_diff_repeated_unversioned_install), + unit_test(test_diff_unversioned_then_versioned_install), + unit_test(test_diff_versioned_then_unversioned_install), + unit_test(test_diff_install_higher_version_first_keeps_first), + unit_test(test_diff_install_higher_version_last_keeps_last), + unit_test(test_diff_repeated_versioned_install), }; int ret = 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,