From d5cc2c4e085f048b6ae92411348343dba564f79e Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:38:28 -0400 Subject: [PATCH 1/5] Added --simulate-json option to write the simulated change set as JSON With --simulate-json=FILE, cf-agent also writes the files and renames of a --simulate run to FILE as one JSON document. Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/cf-agent.c | 37 +++ cf-agent/simulate_mode.c | 276 ++++++++++++++++++ cf-agent/simulate_mode.h | 2 + .../29_simulate_mode/simulate_json.cf | 55 ++++ .../simulate_json.cf.expected | 46 +++ .../29_simulate_mode/simulate_json.cf.sub | 28 ++ tests/unit/simulate_mode_test.c | 272 +++++++++++++++++ 7 files changed, 716 insertions(+) create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf.expected create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf.sub diff --git a/cf-agent/cf-agent.c b/cf-agent/cf-agent.c index 4ffe508e72f..763af541535 100644 --- a/cf-agent/cf-agent.c +++ b/cf-agent/cf-agent.c @@ -112,6 +112,7 @@ static bool ALLCLASSESREPORT = false; /* GLOBAL_P */ static bool ALWAYS_VALIDATE = false; /* GLOBAL_P */ static bool CFPARANOID = false; /* GLOBAL_P */ static bool PERFORM_DB_CHECK = false; +static char *SIMULATE_JSON_FILE = NULL; static const Rlist *ACCESSLIST = NULL; /* GLOBAL_P */ @@ -221,6 +222,7 @@ static const struct option OPTIONS[] = {"skip-bootstrap-service-start", no_argument, 0, 0 }, {"skip-db-check", optional_argument, 0, 0 }, {"simulate", required_argument, 0, 0}, + {"simulate-json", required_argument, 0, 0}, {NULL, 0, 0, '\0'} }; @@ -257,6 +259,7 @@ static const char *const HINTS[] = "Do not start CFEngine services as part of the bootstrap process", "Do not run database integrity checks and repairs at startup", "Run in simulate mode, either 'manifest', 'manifest-full' or 'diff'", + "Write the change set of a --simulate run to the given file as JSON", NULL }; @@ -385,6 +388,21 @@ int main(int argc, char *argv[]) Nova_NoteAgentExecutionPerformance(config->input_file, start); + /* Before GenericAgentFinalize(), which deinitializes the crypto library + * needed for the file digests. */ + if (SIMULATE_JSON_FILE != NULL) + { + const Class *failsafe_class = + EvalContextClassGet(ctx, NULL, "failsafe_fallback"); + const bool failsafe_fallback = + ((failsafe_class != NULL) && !failsafe_class->is_soft); + if (!WriteChangesJson(SIMULATE_JSON_FILE, failsafe_fallback) && + (ret == 0)) + { + ret = EXIT_FAILURE; + } + } + GenericAgentFinalize(ctx, config); StringSetDestroy(SINGLE_COPY_CACHE); @@ -795,6 +813,18 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } } + else if (StringEqual(option_name, "simulate-json")) + { + if (!IsAbsoluteFileName(optarg)) + { + Log(LOG_LEVEL_ERR, + "Invalid argument for --simulate-json, an absolute path required, not '%s'", + optarg); + DoCleanupAndExit(EXIT_FAILURE); + } + free(SIMULATE_JSON_FILE); + SIMULATE_JSON_FILE = xstrdup(optarg); + } break; } default: @@ -822,6 +852,13 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } + if ((SIMULATE_JSON_FILE != NULL) && !ChrootChanges()) + { + Log(LOG_LEVEL_ERR, + "Option --simulate-json can only be used together with --simulate"); + DoCleanupAndExit(EXIT_FAILURE); + } + FreeFixedStringArray(argc_new, argv_new); return config; diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 77ed2c84c23..3e4b1ff960c 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -41,13 +41,22 @@ #include /* StringMap */ #include /* GetCsvLineNext() */ #include /* GetGroupName(), GetUserName() */ +#include /* JsonElement */ +#include /* StringWriter() */ +#include /* HashFile(), HashPrintSafe() */ +#include /* CF_PERMS_DEFAULT */ +#include /* RAND_bytes() */ #include #define DELIM_CHAR '=' /* Taken from coreutils/lib/stat-macros.h. */ +#ifndef __MINGW32__ #define CHMOD_MODE_BITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) +#else +#define CHMOD_MODE_BITS (S_IRWXU | S_IRWXG | S_IRWXO) +#endif static inline void PrintDelimiter() { @@ -1051,3 +1060,270 @@ bool ManifestPkgOperations() return true; } + + +#define CHANGES_JSON_FORMAT_VERSION 1 + +/* Returns NULL for a path that exists neither in the changes chroot nor + * outside of it, e.g. a file created and deleted by the same run. */ +static JsonElement *ChangedFileAsJson(const char *path) +{ + assert(path != NULL); + + const char *chrooted_path = ToChangesChroot(path); + struct stat st_orig; + struct stat st; + const bool existed = (lstat(path, &st_orig) == 0); + if (lstat(chrooted_path, &st) == -1) + { + if (!existed) + { + return NULL; + } + JsonElement *file_info = JsonObjectCreate(2); + JsonObjectAppendString(file_info, "path", path); + JsonObjectAppendString(file_info, "change", "deleted"); + return file_info; + } + + JsonElement *file_info = JsonObjectCreate(8); + JsonObjectAppendString(file_info, "path", path); + JsonObjectAppendString(file_info, "change", + existed ? "modified" : "created"); + JsonObjectAppendString(file_info, "type", + GetFileTypeDescription(st.st_mode)); + + /* The permissions of a symbolic link are meaningless and differ between + * platforms. */ + if (!S_ISLNK(st.st_mode)) + { + char perms[5]; + xsnprintf(perms, sizeof(perms), "%04o", + st.st_mode & CHMOD_MODE_BITS); + JsonObjectAppendString(file_info, "permissions", perms); + } + + /* 64-bit, so that e.g. uid 4294967294 is not written as -2. */ + JsonObjectAppendInteger64(file_info, "uid", (int64_t) st.st_uid); + JsonObjectAppendInteger64(file_info, "gid", (int64_t) st.st_gid); + + if (S_ISREG(st.st_mode)) + { + JsonObjectAppendInteger64(file_info, "size", (int64_t) st.st_size); + + /* HashFile() cannot report failure, it leaves the digest zeroed. */ + static const unsigned char no_digest[EVP_MAX_MD_SIZE + 1] = {0}; + unsigned char digest[EVP_MAX_MD_SIZE + 1]; + HashFile(chrooted_path, digest, HASH_METHOD_SHA256, false); + if (memcmp(digest, no_digest, sizeof(digest)) != 0) + { + char digest_str[CF_HOSTKEY_STRING_SIZE]; + HashPrintSafe(digest_str, sizeof(digest_str), digest, + HASH_METHOD_SHA256, false); + JsonObjectAppendString(file_info, "sha256", digest_str); + } + else + { + Log(LOG_LEVEL_ERR, + "Failed to compute the SHA-256 digest of '%s'", + chrooted_path); + } + } +#ifndef __MINGW32__ + else if (S_ISLNK(st.st_mode)) + { + char target[PATH_MAX] = {0}; + if (readlink(chrooted_path, target, sizeof(target) - 1) > 0) + { + const char *real_target = target; + if (IsAbsoluteFileName(target)) + { + real_target = ToNormalRoot(target); + } + JsonObjectAppendString(file_info, "target", real_target); + } + } +#endif /* !__MINGW32__ */ + + return file_info; +} + +/* Appends the length-prefixed strings in #list_file (in the changes chroot) + * to #records. A missing #list_file means no records. */ +static bool ReadChrootRecords(const char *list_file, Seq *records) +{ + assert(list_file != NULL); + assert(records != NULL); + + const char *path = ToChangesChroot(list_file); + if (access(path, F_OK) != 0) + { + return true; + } + + int fd = safe_open(path, O_RDONLY); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, "Failed to open '%s': %s", path, GetErrorStr()); + return false; + } + + char *record; + int ret; + while ((ret = ReadLenPrefixedString(fd, &record)) > 0) + { + SeqAppend(records, record); + } + close(fd); + + if (ret < 0) + { + Log(LOG_LEVEL_ERR, "Failed to read '%s'", path); + return false; + } + return true; +} + +static bool AddChangedFilesToJson(JsonElement *files) +{ + assert(files != NULL); + + Seq *paths = SeqNew(16, free); + const bool success = ReadChrootRecords(CHROOT_CHANGES_LIST_FILE, paths); + if (success) + { + /* Each file should only be reported once. */ + StringSet *reported = StringSetNew(); + const size_t n_paths = SeqLength(paths); + for (size_t i = 0; i < n_paths; i++) + { + const char *path = SeqAt(paths, i); + if (!StringSetContains(reported, path)) + { + JsonElement *file_info = ChangedFileAsJson(path); + if (file_info != NULL) + { + JsonArrayAppendObject(files, file_info); + } + StringSetAdd(reported, xstrdup(path)); + } + } + StringSetDestroy(reported); + } + SeqDestroy(paths); + return success; +} + +static bool AddRenamedFilesToJson(JsonElement *renames) +{ + assert(renames != NULL); + + Seq *names = SeqNew(16, free); + bool success = ReadChrootRecords(CHROOT_RENAMES_LIST_FILE, names); + const size_t n_names = SeqLength(names); + if (success && ((n_names % 2) != 0)) + { + /* Pairs of the original and the new name, see + * RecordFileRenamedInChroot(). */ + Log(LOG_LEVEL_ERR, "Invalid data about renamed files"); + success = false; + } + for (size_t i = 0; success && (i < n_names); i += 2) + { + JsonElement *rename = JsonObjectCreate(2); + JsonObjectAppendString(rename, "old_name", SeqAt(names, i)); + JsonObjectAppendString(rename, "new_name", SeqAt(names, i + 1)); + JsonArrayAppendObject(renames, rename); + } + SeqDestroy(names); + return success; +} + +bool WriteChangesJson(const char *output_file, bool failsafe_fallback) +{ + assert(output_file != NULL); + + JsonElement *json = JsonObjectCreate(4); + JsonObjectAppendInteger(json, "format_version", + CHANGES_JSON_FORMAT_VERSION); + JsonObjectAppendBool(json, "failsafe_fallback", failsafe_fallback); + JsonElement *files = JsonArrayCreate(16); + JsonObjectAppendArray(json, "files", files); + JsonElement *renames = JsonArrayCreate(16); + JsonObjectAppendArray(json, "renames", renames); + if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames)) + { + JsonDestroy(json); + return false; + } + + Log(LOG_LEVEL_INFO, "Writing the simulated change set to '%s'", + output_file); + + Writer *writer = StringWriter(); + JsonWrite(writer, json, 0); + WriterWrite(writer, "\n"); + JsonDestroy(json); + char *document = StringWriterClose(writer); + + /* Written to a new file which is then renamed into place, so that a + * failed write leaves an existing #output_file intact and a symbolic + * link at #output_file is replaced rather than followed. */ + unsigned char random_bytes[8]; + if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1) + { + Log(LOG_LEVEL_ERR, "Failed to generate a temporary name for '%s'", + output_file); + free(document); + return false; + } + char random_hex[2 * sizeof(random_bytes) + 1]; + StringBytesToHex(random_hex, sizeof(random_hex), + random_bytes, sizeof(random_bytes)); + char *tmp_file; + xasprintf(&tmp_file, "%s.%s" CF_NEW, output_file, random_hex); + + bool success = false; + int fd = safe_open_create_perms(tmp_file, O_WRONLY | O_CREAT | O_EXCL, + CF_PERMS_DEFAULT); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to create '%s' for the simulated change set: %s", + tmp_file, GetErrorStr()); + } + else + { + const size_t document_len = strlen(document); + if (FullWrite(fd, document, document_len) != (ssize_t) document_len) + { + Log(LOG_LEVEL_ERR, + "Failed to write the simulated change set to '%s': %s", + tmp_file, GetErrorStr()); + close(fd); + } + else if (close(fd) == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to close the simulated change set '%s': %s", + tmp_file, GetErrorStr()); + } + else if (rename(tmp_file, output_file) == -1) + { + Log(LOG_LEVEL_ERR, "Failed to rename '%s' to '%s': %s", + tmp_file, output_file, GetErrorStr()); + } + else + { + success = true; + } + + if (!success) + { + unlink(tmp_file); + } + } + free(tmp_file); + free(document); + return success; +} diff --git a/cf-agent/simulate_mode.h b/cf-agent/simulate_mode.h index 4e855813015..8eca34a9852 100644 --- a/cf-agent/simulate_mode.h +++ b/cf-agent/simulate_mode.h @@ -50,4 +50,6 @@ bool DiffChangedFiles(StringSet **audited_files); bool DiffPkgOperations(); bool ManifestPkgOperations(); +bool WriteChangesJson(const char *output_file, bool failsafe_fallback); + #endif /* _SIMULATE_H_ */ diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf b/tests/acceptance/29_simulate_mode/simulate_json.cf new file mode 100644 index 00000000000..4cd7cbb4125 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf @@ -0,0 +1,55 @@ +body common control +{ + inputs => { "../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +bundle agent init +{ + files: + "$(G.testroot)/modify-me" + create => "true", + content => "before"; + + "$(G.testroot)/delete-me" create => "true"; + "$(G.testroot)/rename-me" create => "true"; +} + +bundle agent test +{ + meta: + "test_soft_fail" + string => "(solaris|aix|hpux|windows)", + # ENT-6540 exotics fail to delete chroot + # ENT-10254 tests fail on Windows due to CRLF + meta => { "ENT-6540", "ENT-10254" }; + + "description" + string => "Test that --simulate-json writes the change set as JSON"; + + commands: + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate=manifest --simulate-json=$(this.promise_filename).json.temp > $(this.promise_filename).prose.temp 2>&1" + contain => in_shell; + + "$(G.sed) -e 's,$(sys.workdir),WORKDIR,g' -e 's/\"uid\": [0-9]*/\"uid\": UID/' -e 's/\"gid\": [0-9]*/\"gid\": GID/' $(this.promise_filename).json.temp > $(this.promise_filename).actual" + contain => in_shell; + + # The option requires --simulate and an absolute path. + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate-json=$(this.promise_filename).unused.temp > /dev/null 2>&1; echo \"exit status $? without --simulate\" >> $(this.promise_filename).actual" + contain => in_shell; + + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate=manifest --simulate-json=relative.json > /dev/null 2>&1; echo \"exit status $? with a relative path\" >> $(this.promise_filename).actual" + contain => in_shell; +} + +bundle agent check +{ + methods: + "check" + usebundle => dcs_check_diff( + "$(this.promise_filename).actual", + "$(this.promise_filename).expected", + "$(this.promise_filename)" + ); +} diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected new file mode 100644 index 00000000000..a6d25ed4110 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -0,0 +1,46 @@ +{ + "failsafe_fallback": false, + "files": [ + { + "change": "created", + "gid": GID, + "path": "WORKDIR/tmp/create-me", + "permissions": "0600", + "sha256": "406effb1e9c59672c66a598c2b21e331b23b16c54024e96d6df3e7c173549791", + "size": 7, + "type": "regular file", + "uid": UID + }, + { + "change": "modified", + "gid": GID, + "path": "WORKDIR/tmp/modify-me", + "permissions": "0600", + "sha256": "f39592393ef0859cb196a52693d2cea00fb2df784b3c04ae54aa7cadb8e562f8", + "size": 5, + "type": "regular file", + "uid": UID + }, + { + "change": "deleted", + "path": "WORKDIR/tmp/delete-me" + }, + { + "change": "created", + "gid": GID, + "path": "WORKDIR/tmp/link", + "target": "WORKDIR/tmp/modify-me", + "type": "symbolic link", + "uid": UID + } + ], + "format_version": 1, + "renames": [ + { + "new_name": "WORKDIR/tmp/renamed", + "old_name": "WORKDIR/tmp/rename-me" + } + ] +} +exit status 1 without --simulate +exit status 1 with a relative path diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub new file mode 100644 index 00000000000..e0a71459a85 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -0,0 +1,28 @@ +body common control +{ + inputs => { "../default.sub.cf" }; +} + +bundle agent main +{ + files: + "$(G.testroot)/create-me" + create => "true", + content => "created"; + + "$(G.testroot)/modify-me" content => "after"; + "$(G.testroot)/delete-me" delete => tidy; + "$(G.testroot)/rename-me" rename => to("renamed"); + "$(G.testroot)/link" link_from => symlink("$(G.testroot)/modify-me"); +} + +body rename to(name) +{ + newname => "$(name)"; +} + +body link_from symlink(target) +{ + source => "$(target)"; + link_type => "symlink"; +} diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 662e2fe0e57..6c25efd0a7b 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -27,10 +27,16 @@ #include /* SetChangesChroot(), ToChangesChroot() */ #include /* CHROOT_PKGS_OPS_FILE */ #include /* DeleteDirectoryTree() */ +#include /* MakeParentDirectory() */ +#include /* JsonParseFile() */ +#include /* WriteLenPrefixedString() */ +#include /* FileWriter() */ #include static char CHROOT_DIR[] = "/tmp/simulate_mode_test_chroot.XXXXXX"; +static char ORIG_DIR[] = "/tmp/simulate_mode_test_orig.XXXXXX"; +static char OUTPUT_FILE[PATH_MAX]; /* #csv contains "op,name,version,architecture" records terminated by "\r\n", * just like the records written by RecordPkgOperationInChroot(). */ @@ -101,23 +107,289 @@ static void test_manifest_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +static void reset_records(void) +{ + unlink(ToChangesChroot(CHROOT_CHANGES_LIST_FILE)); + unlink(ToChangesChroot(CHROOT_RENAMES_LIST_FILE)); + unlink(OUTPUT_FILE); +} + +static void write_records(const char *records_file, + const char *const records[], size_t n) +{ + FILE *file = fopen(ToChangesChroot(records_file), "w"); + assert_true(file != NULL); + Writer *writer = FileWriter(file); + for (size_t i = 0; i < n; i++) + { + assert_true(WriteLenPrefixedString(writer, records[i])); + } + WriterClose(writer); +} + +static void create_file(const char *path, const char *content) +{ + FILE *file = fopen(path, "w"); + assert_true(file != NULL); + assert_true(fputs(content, file) >= 0); + fclose(file); +} + +static void create_chroot_file(const char *path, const char *content, + mode_t mode) +{ + char chrooted[PATH_MAX]; + strlcpy(chrooted, ToChangesChroot(path), sizeof(chrooted)); + assert_true(MakeParentDirectory(chrooted, true, NULL)); + + create_file(chrooted, content); + assert_int_equal(chmod(chrooted, mode), 0); +} + +static JsonElement *write_and_parse_changes(void) +{ + assert_true(WriteChangesJson(OUTPUT_FILE, false)); + + JsonElement *json = NULL; + assert_int_equal(JsonParseFile(OUTPUT_FILE, 1024 * 1024, &json), + JSON_PARSE_OK); + assert_true(json != NULL); + return json; +} + +/* Returns the only element of the #key array in #json. */ +static JsonElement *get_single(JsonElement *json, const char *key) +{ + JsonElement *array = JsonObjectGetAsArray(json, key); + assert_int_equal(JsonLength(array), 1); + return JsonArrayGetAsObject(array, 0); +} + +static void test_empty_change_set(void) +{ + reset_records(); + + JsonElement *json = write_and_parse_changes(); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(json, "format_version")), 1); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "files")), 0); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "renames")), 0); + assert_true(JsonObjectGet(json, "failsafe_fallback") != NULL); + assert_false(JsonObjectGetAsBool(json, "failsafe_fallback")); + JsonDestroy(json); + + assert_true(WriteChangesJson(OUTPUT_FILE, true)); + assert_int_equal(JsonParseFile(OUTPUT_FILE, 1024 * 1024, &json), + JSON_PARSE_OK); + assert_true(JsonObjectGetAsBool(json, "failsafe_fallback")); + JsonDestroy(json); +} + +static void test_created_file(void) +{ + reset_records(); + + /* Recorded three times, reported once. */ + const char *const path = "/simulate-test/created \"file\" \\"; + const char *const paths[] = {path, path, path}; + create_chroot_file(path, "Hello, CFEngine!\n", 0640); + write_records(CHROOT_CHANGES_LIST_FILE, paths, 3); + + struct stat st; + assert_int_equal(lstat(ToChangesChroot(path), &st), 0); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "path"), path); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "created"); + assert_string_equal(JsonObjectGetAsString(file_info, "type"), + "regular file"); + assert_string_equal(JsonObjectGetAsString(file_info, "permissions"), + "0640"); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "uid")), + (long) st.st_uid); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "gid")), + (long) st.st_gid); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "size")), 17); + assert_string_equal( + JsonObjectGetAsString(file_info, "sha256"), + "9be7023e1f91bae9d1f734b49c579cc2091c71924ae7494c9a5a3a8006527615"); + JsonDestroy(json); +} + +static void test_deleted_file(void) +{ + reset_records(); + + /* Exists outside of the chroot, but not in it. */ + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/deleted-file", ORIG_DIR); + create_file(path, ""); + const char *const paths[] = {path}; + write_records(CHROOT_CHANGES_LIST_FILE, paths, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "path"), path); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "deleted"); + assert_true(JsonObjectGet(file_info, "type") == NULL); + assert_true(JsonObjectGet(file_info, "sha256") == NULL); + JsonDestroy(json); +} + +static void test_created_and_deleted_file_not_reported(void) +{ + reset_records(); + + /* Exists neither in the chroot nor outside of it. */ + const char *const path = "/simulate-test/created-and-deleted"; + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "files")), 0); + JsonDestroy(json); +} + +static void test_modified_file(void) +{ + reset_records(); + + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/modified-file", ORIG_DIR); + create_file(path, "contents before the run\n"); + create_chroot_file(path, "new contents after the run\n", 0644); + const char *const paths[] = {path}; + write_records(CHROOT_CHANGES_LIST_FILE, paths, 1); + + /* The digest is of the contents after the run. */ + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "modified"); + assert_string_equal( + JsonObjectGetAsString(file_info, "sha256"), + "6ba024f3c03f13f9a8c1bb444829640c68553009facb418bb4552dd7ddebe427"); + JsonDestroy(json); +} + +#ifndef __MINGW32__ +static void test_created_symlink(void) +{ + reset_records(); + + const char *const target = "/simulate-test/link-target"; + const char *const path = "/simulate-test/created-link"; + create_chroot_file(target, "", 0644); + + /* Links in the chroot point to chrooted paths. */ + char chrooted_target[PATH_MAX]; + strlcpy(chrooted_target, ToChangesChroot(target), + sizeof(chrooted_target)); + assert_int_equal(symlink(chrooted_target, ToChangesChroot(path)), 0); + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "type"), + "symbolic link"); + assert_string_equal(JsonObjectGetAsString(file_info, "target"), target); + assert_true(JsonObjectGet(file_info, "permissions") == NULL); + JsonDestroy(json); +} +#endif /* !__MINGW32__ */ + +static void test_renamed_file(void) +{ + reset_records(); + + const char *const names[] = {"/simulate-test/old", "/simulate-test/new"}; + write_records(CHROOT_RENAMES_LIST_FILE, names, 2); + + JsonElement *json = write_and_parse_changes(); + JsonElement *rename = get_single(json, "renames"); + assert_string_equal(JsonObjectGetAsString(rename, "old_name"), names[0]); + assert_string_equal(JsonObjectGetAsString(rename, "new_name"), names[1]); + JsonDestroy(json); + + /* An original name without the new name is an error. */ + write_records(CHROOT_RENAMES_LIST_FILE, names, 1); + assert_false(WriteChangesJson(OUTPUT_FILE, false)); +} + +static void test_write_failure(void) +{ + reset_records(); + + char bad_output[PATH_MAX]; + xsnprintf(bad_output, sizeof(bad_output), "%s/no-such-dir/out.json", + ORIG_DIR); + assert_false(WriteChangesJson(bad_output, false)); +} + +#ifndef __MINGW32__ +static void test_output_symlink_not_followed(void) +{ + reset_records(); + + char link_target[PATH_MAX]; + xsnprintf(link_target, sizeof(link_target), "%s/link-target", ORIG_DIR); + create_file(link_target, "do not overwrite\n"); + assert_int_equal(symlink(link_target, OUTPUT_FILE), 0); + + JsonDestroy(write_and_parse_changes()); + + struct stat st; + assert_int_equal(lstat(OUTPUT_FILE, &st), 0); + assert_true(S_ISREG(st.st_mode)); + + char buf[64] = {0}; + FILE *file = fopen(link_target, "r"); + assert_true(file != NULL); + assert_true(fread(buf, 1, sizeof(buf) - 1, file) > 0); + fclose(file); + assert_string_equal(buf, "do not overwrite\n"); +} +#endif /* !__MINGW32__ */ + int main() { PRINT_TEST_BANNER(); assert_true(mkdtemp(CHROOT_DIR) != NULL); SetChangesChroot(CHROOT_DIR); + assert_true(mkdtemp(ORIG_DIR) != NULL); + xsnprintf(OUTPUT_FILE, sizeof(OUTPUT_FILE), "%s/out.json", ORIG_DIR); const UnitTest tests[] = { unit_test(test_diff_install_cancels_removal), unit_test(test_manifest_install_cancels_removal), + unit_test(test_empty_change_set), + unit_test(test_created_file), + unit_test(test_deleted_file), + unit_test(test_created_and_deleted_file_not_reported), + unit_test(test_modified_file), +#ifndef __MINGW32__ + unit_test(test_created_symlink), +#endif + unit_test(test_renamed_file), + unit_test(test_write_failure), +#ifndef __MINGW32__ + unit_test(test_output_symlink_not_followed), +#endif }; int ret = run_tests(tests); DeleteDirectoryTree(CHROOT_DIR); rmdir(CHROOT_DIR); + DeleteDirectoryTree(ORIG_DIR); + rmdir(ORIG_DIR); return ret; } From bd158d462ceff5c0217de074b5731daba921c52b Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:29:08 -0400 Subject: [PATCH 2/5] Fixed simulate mode swapping package version and architecture RecordPkgOperationInChroot() wrote the architecture before the version, the reverse of the order its readers expect. Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- libpromises/changes_chroot.c | 2 +- tests/unit/simulate_mode_test.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libpromises/changes_chroot.c b/libpromises/changes_chroot.c index 82fd1ff30bc..964b6c43364 100644 --- a/libpromises/changes_chroot.c +++ b/libpromises/changes_chroot.c @@ -299,8 +299,8 @@ bool RecordPkgOperationInChroot(const char *op, const char *name, const char *ve CsvWriterField(csv_writer, op); CsvWriterField(csv_writer, name); - CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(version)); + CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterNewRecord(csv_writer); CsvWriterClose(csv_writer); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 6c25efd0a7b..27c122065d9 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -93,6 +93,21 @@ static void test_diff_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A package recorded with both a version and an architecture must be + * reported with each in its own place. */ +static void test_diff_version_and_architecture(void) +{ + assert_true(RecordPkgOperationInChroot(CHROOT_PKG_OPERATION_INSTALL, + "foo", "1.2.3", "x86_64")); + + 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); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -368,6 +383,7 @@ int main() const UnitTest tests[] = { unit_test(test_diff_install_cancels_removal), + unit_test(test_diff_version_and_architecture), unit_test(test_manifest_install_cancels_removal), unit_test(test_empty_change_set), unit_test(test_created_file), From da443fd48f1b9b966e74def64eaa15dec05df081 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 14:40:26 -0400 Subject: [PATCH 3/5] Fixed simulate diff mode not reporting a removal with a version A recorded removal with a version was only reported when it cancelled a recorded installation of the same version. Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 2 +- tests/unit/simulate_mode_test.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 3e4b1ff960c..1077e7a7eda 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -874,7 +874,7 @@ bool DiffPkgOperations() { MapRemove(installed, name_arch); } - else + else if (inst_record != NULL) { /* Keeping the install message, the removal would make no change. */ insert_new_msg = false; diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 27c122065d9..69ee6a1d4b2 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -108,6 +108,20 @@ static void test_diff_version_and_architecture(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A removal with a version and no install record to cancel is a removal to + * report. */ +static void test_diff_removal_with_version(void) +{ + write_pkgs_ops("r,foo,1.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo [1.0]' would be removed") != NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -384,6 +398,7 @@ int main() { unit_test(test_diff_install_cancels_removal), unit_test(test_diff_version_and_architecture), + unit_test(test_diff_removal_with_version), unit_test(test_manifest_install_cancels_removal), unit_test(test_empty_change_set), unit_test(test_created_file), From c1bf747ea73573f946271714bdcd29970a0f1460 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:43:05 -0400 Subject: [PATCH 4/5] Shared the reduction of recorded package operations PkgOperationRecord now stores the package fields and the simulate modes format their messages when printing, so that the reduction can be reused by other renderers. Changelog: None Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 116 ++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 49 deletions(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 1077e7a7eda..60bf703235d 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -655,15 +655,19 @@ bool DiffChangedFiles(StringSet **audited_files) typedef struct PkgOperationRecord_ { - char *msg; + char *pkg_name; + char *pkg_arch; char *pkg_ver; } PkgOperationRecord; -static PkgOperationRecord *PkgOperationRecordNew(char *msg, char *pkg_ver) +static PkgOperationRecord *PkgOperationRecordNew(const char *pkg_name, + const char *pkg_arch, + const char *pkg_ver) { PkgOperationRecord *ret = xmalloc(sizeof(PkgOperationRecord)); - ret->msg = msg; - ret->pkg_ver = pkg_ver; + ret->pkg_name = xstrdup(pkg_name); + ret->pkg_arch = SafeStringDuplicate(pkg_arch); + ret->pkg_ver = SafeStringDuplicate(pkg_ver); return ret; } @@ -672,7 +676,8 @@ static void PkgOperationRecordDestroy(PkgOperationRecord *pkg_op) { if (pkg_op != NULL) { - free(pkg_op->msg); + free(pkg_op->pkg_name); + free(pkg_op->pkg_arch); free(pkg_op->pkg_ver); free(pkg_op); } @@ -736,12 +741,34 @@ static inline char *GetPkgOperationMsg(ChrootPkgOperationCode op, const char *pk return msg; } -bool DiffPkgOperations() +static void PrintPkgOperations(Map *pkg_ops, ChrootPkgOperationCode op) +{ + MapIterator i = MapIteratorInit(pkg_ops); + MapKeyValue *item; + while ((item = MapIteratorNext(&i))) + { + const PkgOperationRecord *record = item->value; + char *msg = GetPkgOperationMsg(op, record->pkg_name, + record->pkg_arch, record->pkg_ver); + puts(msg); + free(msg); + } +} + +/* Reduces the recorded package operations to the net sets of packages that + * would be installed and removed (maps of PkgOperationRecord), or sets both + * to NULL if no package operations were recorded. */ +static bool CollectPkgOperations(Map **installed_out, Map **removed_out) { + assert(installed_out != NULL); + assert(removed_out != NULL); + + *installed_out = NULL; + *removed_out = NULL; + const char *pkgs_ops_csv_file = ToChangesChroot(CHROOT_PKGS_OPS_FILE); if (access(pkgs_ops_csv_file, F_OK) != 0) { - Log(LOG_LEVEL_INFO, "No package operations done by the agent run"); return true; } @@ -838,9 +865,8 @@ bool DiffPkgOperations() PkgOperationRecord *prev_record = MapGet(installed, name_arch); if ((prev_record == NULL) || PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver)) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(installed, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -883,9 +909,8 @@ bool DiffPkgOperations() if (insert_new_msg) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_REMOVE, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(removed, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -895,6 +920,27 @@ bool DiffPkgOperations() } fclose(csv_file); + *installed_out = installed; + *removed_out = removed; + + return true; +} + +bool DiffPkgOperations() +{ + Map *installed; + Map *removed; + if (!CollectPkgOperations(&installed, &removed)) + { + return false; + } + + if (installed == NULL) + { + Log(LOG_LEVEL_INFO, "No package operations done by the agent run"); + return true; + } + if ((MapSize(installed) == 0) && (MapSize(removed) == 0)) { Log(LOG_LEVEL_INFO, "No differences in installed packages to report"); @@ -906,21 +952,8 @@ bool DiffPkgOperations() } Log(LOG_LEVEL_INFO, "Showing differences in installed packages"); - MapIterator i = MapIteratorInit(installed); - MapKeyValue *item; - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } - i = MapIteratorInit(removed); - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } + PrintPkgOperations(installed, CHROOT_PKG_OPERATION_CODE_INSTALL); + PrintPkgOperations(removed, CHROOT_PKG_OPERATION_CODE_REMOVE); MapDestroy(installed); MapDestroy(removed); @@ -991,9 +1024,8 @@ bool ManifestPkgOperations() (!NULL_OR_EMPTY(pkg_ver) && !NULL_OR_EMPTY(prev_record->pkg_ver) && PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver))) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_PRESENT, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(present, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -1020,9 +1052,8 @@ bool ManifestPkgOperations() if ((prev_record == NULL) || (!NULL_OR_EMPTY(prev_record->pkg_ver) && NULL_OR_EMPTY(pkg_ver))) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_ABSENT, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(absent, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -1039,21 +1070,8 @@ bool ManifestPkgOperations() assert((MapSize(present) != 0) || (MapSize(absent) != 0)); Log(LOG_LEVEL_INFO, "Manifesting present and absent packages"); - MapIterator i = MapIteratorInit(present); - MapKeyValue *item; - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } - i = MapIteratorInit(absent); - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } + PrintPkgOperations(present, CHROOT_PKG_OPERATION_CODE_PRESENT); + PrintPkgOperations(absent, CHROOT_PKG_OPERATION_CODE_ABSENT); MapDestroy(present); MapDestroy(absent); From 29fce4663966c01175095b19fe494ddeed437898 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:47:50 -0400 Subject: [PATCH 5/5] Added the net package operations to the --simulate-json change set The "packages" array lists each package that a --simulate run would install or remove, with its version and architecture when known. Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 69 ++++++++++- .../29_simulate_mode/simulate_json.cf | 10 ++ .../simulate_json.cf.expected | 6 + .../29_simulate_mode/simulate_json.cf.sub | 12 ++ tests/unit/simulate_mode_test.c | 107 ++++++++++++++++++ 5 files changed, 202 insertions(+), 2 deletions(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 60bf703235d..f510428edb4 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -1257,11 +1257,73 @@ static bool AddRenamedFilesToJson(JsonElement *renames) return success; } +static void JsonArrayAppendPkgOperations(JsonElement *packages, Map *pkg_ops, + const char *operation) +{ + assert(packages != NULL); + assert(pkg_ops != NULL); + + MapIterator i = MapIteratorInit(pkg_ops); + MapKeyValue *item; + while ((item = MapIteratorNext(&i))) + { + const PkgOperationRecord *record = item->value; + JsonElement *op_info = JsonObjectCreate(4); + JsonObjectAppendString(op_info, "operation", operation); + JsonObjectAppendString(op_info, "name", record->pkg_name); + if (!NULL_OR_EMPTY(record->pkg_ver)) + { + JsonObjectAppendString(op_info, "version", record->pkg_ver); + } + if (!NULL_OR_EMPTY(record->pkg_arch)) + { + JsonObjectAppendString(op_info, "architecture", record->pkg_arch); + } + JsonArrayAppendObject(packages, op_info); + } +} + +static int ComparePkgOperations(const JsonElement *a, const JsonElement *b, + ARG_UNUSED void *user_data) +{ + int ret = StringSafeCompare(JsonObjectGetAsString(a, "name"), + JsonObjectGetAsString(b, "name")); + if (ret == 0) + { + ret = StringSafeCompare(JsonObjectGetAsString(a, "architecture"), + JsonObjectGetAsString(b, "architecture")); + } + return ret; +} + +static bool AddPkgOperationsToJson(JsonElement *packages) +{ + assert(packages != NULL); + + Map *installed; + Map *removed; + if (!CollectPkgOperations(&installed, &removed)) + { + return false; + } + if (installed != NULL) + { + JsonArrayAppendPkgOperations(packages, installed, "install"); + JsonArrayAppendPkgOperations(packages, removed, "remove"); + MapDestroy(installed); + MapDestroy(removed); + + /* The maps have no stable order. */ + JsonSort(packages, ComparePkgOperations, NULL); + } + return true; +} + bool WriteChangesJson(const char *output_file, bool failsafe_fallback) { assert(output_file != NULL); - JsonElement *json = JsonObjectCreate(4); + JsonElement *json = JsonObjectCreate(5); JsonObjectAppendInteger(json, "format_version", CHANGES_JSON_FORMAT_VERSION); JsonObjectAppendBool(json, "failsafe_fallback", failsafe_fallback); @@ -1269,7 +1331,10 @@ bool WriteChangesJson(const char *output_file, bool failsafe_fallback) JsonObjectAppendArray(json, "files", files); JsonElement *renames = JsonArrayCreate(16); JsonObjectAppendArray(json, "renames", renames); - if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames)) + JsonElement *packages = JsonArrayCreate(16); + JsonObjectAppendArray(json, "packages", packages); + if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames) || + !AddPkgOperationsToJson(packages)) { JsonDestroy(json); return false; diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf b/tests/acceptance/29_simulate_mode/simulate_json.cf index 4cd7cbb4125..325bff5f713 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf @@ -14,6 +14,16 @@ bundle agent init "$(G.testroot)/delete-me" create => "true"; "$(G.testroot)/rename-me" create => "true"; + + "$(sys.workdir)/modules/packages/simulate_json" + create => "true", + perms => m("755"), + content => '#!/bin/sh +case "$1" in + supports-api-version) echo 1 ;; + get-package-data) echo PackageType=repo; echo Name=`cut -c6-` ;; + *) cat > /dev/null ;; +esac'; } bundle agent test diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected index a6d25ed4110..a82d973846a 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -35,6 +35,12 @@ } ], "format_version": 1, + "packages": [ + { + "name": "simulate-json-pkg", + "operation": "install" + } + ], "renames": [ { "new_name": "WORKDIR/tmp/renamed", diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub index e0a71459a85..945fc6aaae9 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -14,6 +14,13 @@ bundle agent main "$(G.testroot)/delete-me" delete => tidy; "$(G.testroot)/rename-me" rename => to("renamed"); "$(G.testroot)/link" link_from => symlink("$(G.testroot)/modify-me"); + + packages: + # Logs "Error installing package": the simulated installation is validated + # against the installed packages, where it is not. The record is the point. + "simulate-json-pkg" + policy => "present", + package_module => simulate_json; } body rename to(name) @@ -26,3 +33,8 @@ body link_from symlink(target) source => "$(target)"; link_type => "symlink"; } + +body package_module simulate_json +{ + query_installed_ifelapsed => "0"; +} diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 69ee6a1d4b2..4e8596e84df 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -140,6 +140,7 @@ static void reset_records(void) { unlink(ToChangesChroot(CHROOT_CHANGES_LIST_FILE)); unlink(ToChangesChroot(CHROOT_RENAMES_LIST_FILE)); + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); unlink(OUTPUT_FILE); } @@ -203,6 +204,7 @@ static void test_empty_change_set(void) JsonPrimitiveGetAsInteger(JsonObjectGet(json, "format_version")), 1); assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "files")), 0); assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "renames")), 0); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "packages")), 0); assert_true(JsonObjectGet(json, "failsafe_fallback") != NULL); assert_false(JsonObjectGetAsBool(json, "failsafe_fallback")); JsonDestroy(json); @@ -350,6 +352,109 @@ static void test_renamed_file(void) assert_false(WriteChangesJson(OUTPUT_FILE, false)); } +/* Checks that the package operations recorded in #csv reduce to the single + * #operation on the package #name, #version and #arch (NULL for omitted), + * or to no operation at all if #operation is NULL. */ +static void check_pkg_operations(const char *csv, const char *operation, + const char *name, const char *version, + const char *arch) +{ + reset_records(); + write_pkgs_ops(csv); + + JsonElement *json = write_and_parse_changes(); + if (operation == NULL) + { + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "packages")), + 0); + JsonDestroy(json); + return; + } + + JsonElement *op_info = get_single(json, "packages"); + assert_string_equal(JsonObjectGetAsString(op_info, "operation"), + operation); + assert_string_equal(JsonObjectGetAsString(op_info, "name"), name); + if (version == NULL) + { + assert_true(JsonObjectGet(op_info, "version") == NULL); + } + else + { + assert_string_equal(JsonObjectGetAsString(op_info, "version"), + version); + } + if (arch == NULL) + { + assert_true(JsonObjectGet(op_info, "architecture") == NULL); + } + else + { + assert_string_equal(JsonObjectGetAsString(op_info, "architecture"), + arch); + } + JsonDestroy(json); +} + +static void test_pkg_operations(void) +{ + check_pkg_operations("i,pkg,1.0,x86_64\r\n", + "install", "pkg", "1.0", "x86_64"); + check_pkg_operations("i,pkg,,x86_64\r\n", + "install", "pkg", NULL, "x86_64"); + check_pkg_operations("r,pkg,,x86_64\r\n", + "remove", "pkg", NULL, "x86_64"); + check_pkg_operations("r,pkg,,\r\n", "remove", "pkg", NULL, NULL); + check_pkg_operations("r,pkg,1.0,\r\n", "remove", "pkg", "1.0", NULL); + + /* Net operations only: cancelled ones are not reported and a newer + * version wins. */ + check_pkg_operations("r,pkg,,\r\np,pkg,,\r\n", NULL, NULL, NULL, NULL); + check_pkg_operations("i,pkg,2.0,\r\na,pkg,2.0,\r\n", + NULL, NULL, NULL, NULL); + check_pkg_operations("r,pkg,,\r\ni,pkg,1.0,\r\n", + "install", "pkg", "1.0", NULL); + check_pkg_operations("i,pkg,1.0,\r\ni,pkg,2.0,\r\n", + "install", "pkg", "2.0", NULL); + + /* The record written by RecordPkgOperationInChroot() itself. */ + reset_records(); + assert_true(RecordPkgOperationInChroot(CHROOT_PKG_OPERATION_INSTALL, + "pkg", "1.0", "x86_64")); + JsonElement *json = write_and_parse_changes(); + JsonElement *op_info = get_single(json, "packages"); + assert_string_equal(JsonObjectGetAsString(op_info, "operation"), + "install"); + assert_string_equal(JsonObjectGetAsString(op_info, "version"), "1.0"); + assert_string_equal(JsonObjectGetAsString(op_info, "architecture"), + "x86_64"); + JsonDestroy(json); +} + +static void test_pkg_operations_sorted(void) +{ + reset_records(); + write_pkgs_ops("i,zeta,1.0,x86_64\r\n" + "r,alpha,,\r\n" + "i,zeta,1.0,arm64\r\n"); + + JsonElement *json = write_and_parse_changes(); + JsonElement *packages = JsonObjectGetAsArray(json, "packages"); + assert_int_equal(JsonLength(packages), 3); + assert_string_equal( + JsonObjectGetAsString(JsonArrayGetAsObject(packages, 0), "name"), + "alpha"); + assert_string_equal( + JsonObjectGetAsString(JsonArrayGetAsObject(packages, 1), + "architecture"), + "arm64"); + assert_string_equal( + JsonObjectGetAsString(JsonArrayGetAsObject(packages, 2), + "architecture"), + "x86_64"); + JsonDestroy(json); +} + static void test_write_failure(void) { reset_records(); @@ -409,6 +514,8 @@ int main() unit_test(test_created_symlink), #endif unit_test(test_renamed_file), + unit_test(test_pkg_operations), + unit_test(test_pkg_operations_sorted), unit_test(test_write_failure), #ifndef __MINGW32__ unit_test(test_output_symlink_not_followed),