Skip to content

Commit

Permalink
avocado.utils.kernel: fix kernel version parsing
Browse files Browse the repository at this point in the history
This fixes two problems with the current version parse mechanism:

 1. Recent setuptools' version parse function, which has dropped
    "LegacyVersion" support, raises an InvalidVersion exception
    for common kernel version info as presented by the system and
    returned by os.uname()

 2. The import of the packaging module is broken on systems with no
    setuptools and only the "packaging" module.

This introduces a simple but custom version parse that solves both and
simplifies dependencies.

Fixes: #6058
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
clebergnu committed Nov 25, 2024
1 parent 26441ef commit f452aa5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
18 changes: 11 additions & 7 deletions avocado/utils/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
import logging
import multiprocessing
import os
import re

Check warning on line 23 in avocado/utils/kernel.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/kernel.py#L23

Added line #L23 was not covered by tests
import shutil
import tempfile

try:
import packaging
except ImportError:
from pkg_resources import packaging

from avocado.utils import archive, asset, build, distro, process

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -201,6 +197,14 @@ def __del__(self):
shutil.rmtree(self.work_dir)


def _parse_kernel_version(version):

Check warning on line 200 in avocado/utils/kernel.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/kernel.py#L200

Added line #L200 was not covered by tests
match = re.match(r"(\d+)\.(\d+)\.(\d+)-(\d+).*", version)
if match:
return tuple(map(int, match.groups()))
else:
raise AssertionError(f'Malformed kernel version "{version}"')


def check_version(version):
"""
This utility function compares the current kernel version with
Expand All @@ -210,6 +214,6 @@ def check_version(version):
:type version: string
:param version: version to be compared with current kernel version
"""
os_version = packaging.version.parse(os.uname()[2])
version = packaging.version.parse(version)
os_version = _parse_kernel_version(os.uname()[2])
version = _parse_kernel_version(version)

Check warning on line 218 in avocado/utils/kernel.py

View check run for this annotation

Codecov / codecov/patch

avocado/utils/kernel.py#L217-L218

Added lines #L217 - L218 were not covered by tests
assert os_version > version, "Old kernel"
2 changes: 1 addition & 1 deletion selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"job-api-7": 1,
"nrunner-interface": 70,
"nrunner-requirement": 28,
"unit": 678,
"unit": 681,
"jobs": 11,
"functional-parallel": 314,
"functional-serial": 7,
Expand Down
14 changes: 13 additions & 1 deletion selftests/unit/utils/kernel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from avocado.utils.kernel import KernelBuild
from avocado.utils.kernel import KernelBuild, _parse_kernel_version
from selftests.utils import setup_avocado_loggers

setup_avocado_loggers()
Expand All @@ -25,3 +25,15 @@ def test_build_overrided_url(self):
def tearDown(self):
# To make sure that the temporary workdir is cleaned up
del self.kernel


class Version(unittest.TestCase):
def test_basic(self):
self.assertEqual(_parse_kernel_version("1.2.3-100"), (1, 2, 3, 100))

def test_uname(self):
self.assertEqual(_parse_kernel_version("9.0.1-100.fc50.x86_64"), (9, 0, 1, 100))

def test_malformed_incomplete(self):
with self.assertRaises(AssertionError):
_parse_kernel_version("1.2")

0 comments on commit f452aa5

Please sign in to comment.