Skip to content

Commit

Permalink
Merge pull request #6069 from clebergnu/utils_kernel_version
Browse files Browse the repository at this point in the history
avocado.utils.kernel: fix kernel version parsing
  • Loading branch information
richtja authored Nov 25, 2024
2 parents 7c4af2d + f452aa5 commit 5b65f4f
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
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):
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)
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 5b65f4f

Please sign in to comment.