diff --git a/cf-agent/cf-agent.c b/cf-agent/cf-agent.c index df31d767f2f..ddcb93e4d4b 100644 --- a/cf-agent/cf-agent.c +++ b/cf-agent/cf-agent.c @@ -223,7 +223,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 changed files of a --simulate run to the given file as JSON (renames are not included)", + "Write the change set of a --simulate run to the given file as JSON", NULL }; diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index e9ce55351fd..bfbdd60cfb1 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -1240,6 +1240,31 @@ static bool AddChangedFilesToJson(JsonElement *files) 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 ((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) { assert(output_file != NULL); @@ -1248,7 +1273,9 @@ bool WriteChangesJson(const char *output_file) JsonObjectAppendInteger(json, "format_version", 1); JsonElement *files = JsonArrayCreate(16); JsonObjectAppendArray(json, "files", files); - if (!AddChangedFilesToJson(files)) + JsonElement *renames = JsonArrayCreate(16); + JsonObjectAppendArray(json, "renames", renames); + if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames)) { JsonDestroy(json); return false; diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected index 55a6bcec74a..d7006261d1b 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -20,7 +20,13 @@ "type": "symbolic link" } ], - "format_version": 1 + "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 index 9912f410628..e0a71459a85 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -12,7 +12,6 @@ bundle agent main "$(G.testroot)/modify-me" content => "after"; "$(G.testroot)/delete-me" delete => tidy; - # Renames are not in the --simulate-json output yet. "$(G.testroot)/rename-me" rename => to("renamed"); "$(G.testroot)/link" link_from => symlink("$(G.testroot)/modify-me"); } diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 41aae73203b..1fffe875e3c 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -299,6 +299,7 @@ static void test_json_created_file(void) JsonPrimitiveGetAsInteger(JsonObjectGet(json, "format_version")), 1); JsonElement *files = JsonObjectGetAsArray(json, "files"); assert_int_equal(JsonLength(files), 1); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "renames")), 0); JsonElement *file_info = JsonArrayGetAsObject(files, 0); assert_string_equal(JsonObjectGetAsString(file_info, "path"), path); assert_string_equal(JsonObjectGetAsString(file_info, "change"), @@ -329,6 +330,24 @@ static void test_json_deleted_file(void) JsonDestroy(json); } +static void test_json_renamed_file(void) +{ + 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 *renames = JsonObjectGetAsArray(json, "renames"); + assert_int_equal(JsonLength(renames), 1); + JsonElement *rename = JsonArrayGetAsObject(renames, 0); + 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)); +} + static void test_json_write_failure(void) { write_records(CHROOT_RENAMES_LIST_FILE, NULL, 0); @@ -383,6 +402,7 @@ int main() unit_test(test_diff_repeated_versioned_install), unit_test(test_json_created_file), unit_test(test_json_deleted_file), + unit_test(test_json_renamed_file), unit_test(test_json_write_failure), #ifndef __MINGW32__ unit_test(test_json_output_link_not_written_through),