From 32702c7c7d1c445b9ab95e0d1bbdfdf8f06d4303 Mon Sep 17 00:00:00 2001 From: Petr Stodulka Date: Wed, 27 Apr 2022 11:25:40 +0200 Subject: [PATCH] Skip comment lines when parsing grub configuration file Added simple unit-test for default grub info to see the valid lines can be parsed as expected. --- .../systemfacts/libraries/systemfacts.py | 21 ++++++++- .../tests/test_systemfacts_grub.py | 46 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 repos/system_upgrade/common/actors/systemfacts/tests/test_systemfacts_grub.py diff --git a/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py b/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py index 0de8b383..81aea6f5 100644 --- a/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py +++ b/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py @@ -9,6 +9,7 @@ import re import six from leapp import reporting +from leapp.exceptions import StopActorExecutionError from leapp.libraries.common import repofileutils from leapp.libraries.common.config import architecture from leapp.libraries.stdlib import api, CalledProcessError, run @@ -289,9 +290,25 @@ def _default_grub_info(): ]) else: for line in run(['cat', default_grb_fpath], split=True)['stdout']: - if not line.strip(): + line = line.strip() + if not line or line[0] == '#': + # skip comments and empty lines continue - name, value = tuple(map(type(line).strip, line.split('=', 1))) + try: + name, value = tuple(map(type(line).strip, line.split('=', 1))) + except ValueError as e: + # we do not want to really continue when we cannot parse this file + # TODO(pstodulk): rewrite this in the form we produce inhibitor + # with problematic lines. This is improvement just in comparison + # to the original hard crash. + raise StopActorExecutionError( + 'Failed parsing of {}'.format(default_grb_fpath), + details={ + 'error': str(e), + 'problematic line': str(line) + } + ) + yield DefaultGrub( name=name, value=value diff --git a/repos/system_upgrade/common/actors/systemfacts/tests/test_systemfacts_grub.py b/repos/system_upgrade/common/actors/systemfacts/tests/test_systemfacts_grub.py new file mode 100644 index 00000000..08552771 --- /dev/null +++ b/repos/system_upgrade/common/actors/systemfacts/tests/test_systemfacts_grub.py @@ -0,0 +1,46 @@ +import os + +from leapp.libraries.actor import systemfacts +from leapp.models import DefaultGrub + + +class RunMocked(object): + def __init__(self, cmd_result): + self.called = 0 + self.cmd_result = cmd_result + self.split = False + self.cmd = None + + def __call__(self, cmd, split=False): + self.cmd = cmd + self.split = split + self.called += 1 + return self.cmd_result + + +def test_default_grub_info_valid(monkeypatch): + mocked_run = RunMocked({ + 'stdout': [ + 'line="whatever else here"', + 'newline="whatever"', + '# comment here', + 'why_not=value', + ' # whitespaces around comment ', + ' ', + ' last=last really' + ], + }) + expected_result = [ + DefaultGrub(name='line', value='"whatever else here"'), + DefaultGrub(name='newline', value='"whatever"'), + DefaultGrub(name='why_not', value='value'), + DefaultGrub(name='last', value='last really'), + ] + monkeypatch.setattr(systemfacts, 'run', mocked_run) + monkeypatch.setattr(os.path, 'isfile', lambda dummy: True) + for msg in systemfacts._default_grub_info(): + expected_msg = expected_result.pop(0) + assert msg.name == expected_msg.name + assert msg.value == expected_msg.value + assert mocked_run.called + assert not expected_result -- 2.35.1