Skip to content

Commit

Permalink
Raise proper error when ModelViolationError occurs
Browse files Browse the repository at this point in the history
This error occurs when repo file has invalid definition, specifically
when the 'name' entry of the config files is invalid. Also add test for
the systemfacts actor.

Jira: RHEL-19249
  • Loading branch information
tomasfratrik committed Aug 29, 2024
1 parent 9f2f172 commit 94ffbb8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,18 @@ def get_sysctls_status():

def get_repositories_status():
""" Get a basic information about YUM repositories installed in the system """
return RepositoriesFacts(repositories=repofileutils.get_parsed_repofiles())
try:
return RepositoriesFacts(repositories=repofileutils.get_parsed_repofiles())
except repofileutils.InvalidRepoDefinition as e:
raise StopActorExecutionError(
message=str(e),
details={
'hint': 'For more directions on how to resolve the issue, see: {url}.'
.format(
url='https://access.redhat.com/solutions/6969001'
)
}
)


def get_selinux_status():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@

import pytest

from leapp.libraries.actor.systemfacts import _get_system_groups, _get_system_users, anyendswith, anyhasprefix, aslist
from leapp.exceptions import StopActorExecutionError
from leapp.libraries.actor.systemfacts import (
_get_system_groups,
_get_system_users,
anyendswith,
anyhasprefix,
aslist,
get_repositories_status
)
from leapp.libraries.common import repofileutils
from leapp.libraries.common.testutils import logger_mocked
from leapp.libraries.stdlib import api
from leapp.snactor.fixture import current_actor_libraries
Expand Down Expand Up @@ -116,3 +125,16 @@ def __init__(self, gr_name, gr_gid, gr_mem):
assert group_name not in api.current_logger().dbgmsg[0]
else:
assert not api.current_logger().dbgmsg


def test_failed_parsed_repofiles(monkeypatch):
def _raise_invalidrepo_error():
raise repofileutils.InvalidRepoDefinition(msg='mocked error',
repofile='/etc/yum.repos.d/mock.repo',
repoid='mocked repoid')

monkeypatch.setattr(repofileutils, 'get_parsed_repofiles', _raise_invalidrepo_error)
monkeypatch.setattr(api, 'current_logger', logger_mocked())

with pytest.raises(StopActorExecutionError):
get_repositories_status()
14 changes: 13 additions & 1 deletion repos/system_upgrade/common/libraries/repofileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
api.current_logger().warning('repofileutils.py: failed to import dnf')


class InvalidRepoDefinition(Exception):
def __init__(self, msg, repofile, repoid):
message = 'Invalid repository definition: {repoid} in: {repofile}: {msg}'.format(
repoid=repoid, repofile=repofile, msg=msg)
super(InvalidRepoDefinition, self).__init__(message)
self.repofile = repofile
self.repoid = repoid


def _parse_repository(repoid, repo_data):
def asbool(x):
return x == '1'
Expand Down Expand Up @@ -38,7 +47,10 @@ def parse_repofile(repofile):
with open(repofile, mode='r') as fp:
cp = utils.parse_config(fp, strict=False)
for repoid in cp.sections():
data.append(_parse_repository(repoid, dict(cp.items(repoid))))
try:
data.append(_parse_repository(repoid, dict(cp.items(repoid))))
except Exception as e:
raise InvalidRepoDefinition(e, repofile=repofile, repoid=repoid)
return RepositoryFile(file=repofile, data=data)


Expand Down

0 comments on commit 94ffbb8

Please sign in to comment.