Skip to content
Merged
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
29 changes: 27 additions & 2 deletions cf-agent/simulate_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -931,7 +956,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;
Expand Down
5 changes: 3 additions & 2 deletions libpromises/changes_chroot.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ bool RecordFileEvaluatedInChroot(const char *path)
return ret;
}

bool RecordPkgOperationInChroot(const char *op, const char *name, const char *arch, const char *version)
bool RecordPkgOperationInChroot(const char *op, const char *name, const char *version, const char *arch)
{
assert(op != NULL);
assert(name != NULL);
Expand Down Expand Up @@ -297,10 +297,11 @@ bool RecordPkgOperationInChroot(const char *op, const char *name, const char *ar
return false;
}

/* Record format: op,name,version,architecture */
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);
Expand Down
2 changes: 1 addition & 1 deletion libpromises/changes_chroot.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ void PrepareChangesChroot(const char *path);
bool RecordFileChangedInChroot(const char *path);
bool RecordFileRenamedInChroot(const char *old_name, const char *new_name);
bool RecordFileEvaluatedInChroot(const char *path);
bool RecordPkgOperationInChroot(const char *op, const char *name, const char *arch, const char *version);
bool RecordPkgOperationInChroot(const char *op, const char *name, const char *version, const char *arch);

#endif /* CFENGINE_CHANGES_CHROOT_H */
161 changes: 157 additions & 4 deletions tests/unit/simulate_mode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <eval_context.h> /* SetChangesChroot(), ToChangesChroot() */
#include <changes_chroot.h> /* CHROOT_PKGS_OPS_FILE */
#include <file_lib.h> /* DeleteDirectoryTree() */
#include <string_lib.h> /* StringContains() */

#include <simulate_mode.h>

Expand Down Expand Up @@ -81,8 +82,53 @@ 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));
}

/* 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(StringContains(output, "Package 'foo-x86_64 [1.2.3]' would be installed"));

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(StringContains(output, "Package 'foo [1.0]' would be removed"));

unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE));
}

/* A removal with a version that differs from the recorded installation would
* fail, so only the installation is reported. */
static void test_diff_removal_with_other_version_keeps_install(void)
{
write_pkgs_ops("i,foo,2.0,\r\n"
"r,foo,1.0,\r\n");

char output[4096];
assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output)));

assert_true(StringContains(output, "Package 'foo [2.0]' would be installed"));
assert_false(StringContains(output, "would be removed"));

unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE));
}
Expand All @@ -95,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));
}
Expand All @@ -111,7 +255,16 @@ int main()
const UnitTest tests[] =
{
unit_test(test_diff_install_cancels_removal),
unit_test(test_diff_version_and_architecture),
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);
Expand Down
Loading