Skip to content

Commit

Permalink
Merge pull request #63858 from Ch3LL/master_fix-rpm-ver
Browse files Browse the repository at this point in the history
Port #63849 to master
  • Loading branch information
garethgreenaway authored Mar 10, 2023
2 parents 365aa66 + ebfdf66 commit 30570ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
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

0 comments on commit 30570ad

Please sign in to comment.