Skip to content

The package comparison option in the package module is not working as expected. #733

Open
@700grm

Description

@700grm

Software comparison does not work due to lexicographic order. Because of this, the Python statement assert 4.14.0 >= 10.14.0 is True, instead of False.

For example, let's test if the installed package dnf-4.14.0-5.el9_2.noarch is equal to or greater than the package version 10.14.0

The result will be True since version 4.14.0 is equal to version 4.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 4.14.0

The result will be True since the version of package 5.14.0 is greater than the version of package 4.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 5.14.0

However, if we compare it with version 10.14.0, the result will be True. The script will output that 5.14.0 is bigger than 10.14.0.

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version >= 10.14.0

In Python, when you use the >= operator to compare strings, it performs lexicographic (dictionary-style) comparison based on the ASCII values of the characters. In lexicographic comparison, characters are compared one by one from left to right.

In our specific case, 4.14.0 is indeed greater than 10.14.0 according to lexicographic comparison because the first character 4 is greater than 1. The comparison stops as soon as a character difference is found, so the rest of the characters don't matter in this context.

Here's a step-by-step comparison:

Compare the first characters: '4' vs. '1'. '4' is greater than '1', so '4.14.0' is considered greater.

As a result, the assertion assert '4.14.0' >= '10.14.0' is true because, in lexicographic order, 4.14.0 comes after 10.14.0.

I propose adding function to convert 4.14.0 to 004.014.000 for comparison purposes for example:

def pad_version(version_string):
    parts = version_string.split(".")
    compare_version = ".".join([part.zfill(3) for part in parts])
    return compare_version

# Example usage:
version_string = "4.14.0"
compare_version = pad_version(version_string)
print(compare_version)

# output
# 004.014.000

In this case, we will be able to execute a simple package comparison; however, we will have to write the version as 010.014.000 instead of 10.14.0

def test_firewalld_is_installed(host):
    dnf = host.package("dnf")
    assert dnf.version.compare >= 010.014.000

Maybe it doesn't look like the most beautiful solution; however, it will allow for easy-to-read package comparison code. And it will work with other distros and releases

I will appreciate your thought on this

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions