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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
29 changes: 28 additions & 1 deletion cf-agent/simulate_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion tests/acceptance/29_simulate_mode/simulate_json.cf.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion tests/acceptance/29_simulate_mode/simulate_json.cf.sub
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/simulate_mode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down