Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rpm_lowpkg version comparison logic when using rpm-vercmp #63849

Merged
merged 2 commits into from
Mar 10, 2023
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
1 change: 1 addition & 0 deletions changelog/63317.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix rpm_lowpkg version comparison logic when using rpm-vercmp and only one version has a release number.
24 changes: 15 additions & 9 deletions salt/modules/rpm_lowpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,19 @@ def version_cmp(ver1, ver2, ignore_epoch=False):
except AttributeError:
log.debug("rpmUtils.miscutils.compareEVR is not available")

# If one EVR is missing a release but not the other and they
# otherwise would be equal, ignore the release. This can happen if
# e.g. you are checking if a package version 3.2 is satisfied by
# 3.2-1.
(ver1_e, ver1_v, ver1_r) = salt.utils.pkg.rpm.version_to_evr(ver1)
(ver2_e, ver2_v, ver2_r) = salt.utils.pkg.rpm.version_to_evr(ver2)

if not ver1_r or not ver2_r:
ver1_r = ver2_r = ""

if cmp_func is None:
ver1 = f"{ver1_e}:{ver1_v}-{ver1_r}"
ver2 = f"{ver2_e}:{ver2_v}-{ver2_r}"
if salt.utils.path.which("rpmdev-vercmp"):
log.warning(
"Installing the rpmdevtools package may surface dev tools in"
Expand Down Expand Up @@ -790,16 +802,10 @@ def _prepend(ver):
" comparisons"
)
else:
# If one EVR is missing a release but not the other and they
# otherwise would be equal, ignore the release. This can happen if
# e.g. you are checking if a package version 3.2 is satisfied by
# 3.2-1.
(ver1_e, ver1_v, ver1_r) = salt.utils.pkg.rpm.version_to_evr(ver1)
(ver2_e, ver2_v, ver2_r) = salt.utils.pkg.rpm.version_to_evr(ver2)
if not ver1_r or not ver2_r:
ver1_r = ver2_r = ""

if HAS_PY_RPM:
ver1 = f"{ver1_v}-{ver1_r}"
ver2 = f"{ver2_v}-{ver2_r}"

# handle epoch version comparison first
# rpm_vercmp.vercmp does not handle epoch version comparison
ret = salt.utils.versions.version_cmp(ver1_e, ver2_e)
Expand Down
22 changes: 12 additions & 10 deletions tests/pytests/unit/modules/test_rpm_lowpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,18 @@ def test_version_cmp_rpm_all_libraries(rpm_lib):
pytest.skip("The Python RPM lib is not installed, skipping")

with patch_rpm, patch_py_rpm, patch_cmd:
assert -1 == rpm.version_cmp("1", "2")
assert -1 == rpm.version_cmp("2.9.1-6.el7_2.3", "2.9.1-6.el7.4")
assert 1 == rpm.version_cmp("3.2", "3.0")
assert 0 == rpm.version_cmp("3.0", "3.0")
assert 1 == rpm.version_cmp("1:2.9.1-6.el7_2.3", "2.9.1-6.el7.4")
assert -1 == rpm.version_cmp("1:2.9.1-6.el7_2.3", "1:2.9.1-6.el7.4")
assert 1 == rpm.version_cmp("2:2.9.1-6.el7_2.3", "1:2.9.1-6.el7.4")
assert 0 == rpm.version_cmp("3:2.9.1-6.el7.4", "3:2.9.1-6.el7.4")
assert -1 == rpm.version_cmp("3:2.9.1-6.el7.4", "3:2.9.1-7.el7.4")
assert 1 == rpm.version_cmp("3:2.9.1-8.el7.4", "3:2.9.1-7.el7.4")
assert rpm.version_cmp("1", "2") == -1
assert rpm.version_cmp("2.9.1-6.el7_2.3", "2.9.1-6.el7.4") == -1
assert rpm.version_cmp("3.2", "3.0") == 1
assert rpm.version_cmp("3.0", "3.0") == 0
assert rpm.version_cmp("1:2.9.1-6.el7_2.3", "2.9.1-6.el7.4") == 1
assert rpm.version_cmp("1:2.9.1-6.el7_2.3", "1:2.9.1-6.el7.4") == -1
assert rpm.version_cmp("2:2.9.1-6.el7_2.3", "1:2.9.1-6.el7.4") == 1
assert rpm.version_cmp("3:2.9.1-6.el7.4", "3:2.9.1-6.el7.4") == 0
assert rpm.version_cmp("3:2.9.1-6.el7.4", "3:2.9.1-7.el7.4") == -1
assert rpm.version_cmp("3:2.9.1-8.el7.4", "3:2.9.1-7.el7.4") == 1
assert rpm.version_cmp("3.23-6.el9", "3.23") == 0
assert rpm.version_cmp("3.23", "3.23-6.el9") == 0


def test_version_cmp_rpm():
Expand Down