Skip to content

Commit

Permalink
Add tests for Checktargetrepos and reword report messages
Browse files Browse the repository at this point in the history
  • Loading branch information
drehak authored and pirat89 committed Apr 9, 2020
1 parent a3ba615 commit 66b077e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def process():
' is not used (the --no-rhsm option or the LEAPP_NO_RHSM=1'
' environment variable has been set) so leapp is not able to'
' obtain YUM/DNF repositories with the content for the target'
' system in the standard way and has to be delivered by user'
' manually.'
' system in the standard way. The content has to be delivered'
' by the user manually.'
),
reporting.Remediation(hint=(
'Create the repository file according to instructions in the'
Expand All @@ -73,12 +73,12 @@ def process():
])
elif not (is_ctrf or is_re):
# Some custom repositories have been discovered, but the custom repo
# file not - neither the --enablerepo option is usedd. Inform about
# file not - neither the --enablerepo option is used. Inform about
# the official recommended way.
reporting.create_report([
reporting.Title('CustomTargetRepositories discovered, but no new provided mechanisms used.'),
reporting.Title('Detected "CustomTargetRepositories" without using new provided mechanisms used.'),
reporting.Summary(
'Red Hat provides now official way how to use custom'
'Red Hat now provides an official way for using custom'
' repositories during the in-place upgrade through'
' the referred custom repository file or through the'
' --enablerepo option for leapp. The CustomTargetRepositories'
Expand All @@ -89,7 +89,6 @@ def process():
' during the upgrade (see the referred document) or create'
' the empty custom repository file to acknowledge this report'
' message.'

)),
reporting.Severity(reporting.Severity.INFO),
reporting.ExternalLink(url=_IPU_DOC_URL, title='UPGRADING TO RHEL 8'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
from collections import namedtuple

import pytest

# TODO: unit tests are required (@drehak?)
def test_sth():
pass
from leapp.models import (CustomTargetRepository, CustomTargetRepositoryFile, EnvVar, Report,
RepositoryData, RHELTargetRepository, TargetRepositories)
from leapp.libraries.actor import library
from leapp import reporting
from leapp.libraries.stdlib import api
from leapp.libraries.common import rhsm
from leapp.libraries.common.testutils import create_report_mocked


class MockedConsume(object):
def __init__(self, *args):
self._msgs = []
for arg in args:
if not arg:
continue
if isinstance(arg, list):
self._msgs.extend(arg)
else:
self._msgs.append(arg)

def __call__(self, model):
return iter([msg for msg in self._msgs if isinstance(msg, model)])


class CurrentActorMocked(object):
def __init__(self, envars=None):
if envars:
envarsList = [EnvVar(name=key, value=value) for key, value in envars.items()]
else:
envarsList = []
self.configuration = namedtuple('configuration', ['leapp_env_vars'])(envarsList)

def __call__(self):
return self


_RHEL_REPOS = [
RHELTargetRepository(repoid='repo1'),
RHELTargetRepository(repoid='repo2'),
RHELTargetRepository(repoid='repo3'),
RHELTargetRepository(repoid='repo4'),
]

_CUSTOM_REPOS = [
CustomTargetRepository(repoid='repo1', name='repo1name', baseurl='repo1url', enabled=True),
CustomTargetRepository(repoid='repo2', name='repo2name', baseurl='repo2url', enabled=False),
CustomTargetRepository(repoid='repo3', name='repo3name', baseurl=None, enabled=True),
CustomTargetRepository(repoid='repo4', name='repo4name', baseurl=None, enabled=True),
]

_TARGET_REPOS_CUSTOM = TargetRepositories(rhel_repos=_RHEL_REPOS, custom_repos=_CUSTOM_REPOS)
_TARGET_REPOS_NO_CUSTOM = TargetRepositories(rhel_repos=_RHEL_REPOS)
_CUSTOM_TARGET_REPOFILE = CustomTargetRepositoryFile(file='/etc/leapp/files/leapp_upgrade_repositories.repo')


def test_checktargetrepos_rhsm(monkeypatch):
monkeypatch.setattr(reporting, 'create_report', create_report_mocked())
monkeypatch.setattr(rhsm, 'skip_rhsm', lambda: False)
monkeypatch.setattr(api, 'consume', MockedConsume())
library.process()
assert reporting.create_report.called == 0


@pytest.mark.parametrize('enable_repos', [True, False])
@pytest.mark.parametrize('custom_target_repos', [True, False])
@pytest.mark.parametrize('custom_target_repofile', [True, False])
def test_checktargetrepos_no_rhsm(monkeypatch, enable_repos, custom_target_repos, custom_target_repofile):
mocked_consume = MockedConsume(_TARGET_REPOS_CUSTOM if custom_target_repos else _TARGET_REPOS_NO_CUSTOM)
if custom_target_repofile:
mocked_consume._msgs.append(_CUSTOM_TARGET_REPOFILE)
envvars = {'LEAPP_ENABLE_REPOS': 'hill,spencer'} if enable_repos else {}
monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(envvars))

monkeypatch.setattr(reporting, 'create_report', create_report_mocked())
monkeypatch.setattr(rhsm, 'skip_rhsm', lambda: True)
monkeypatch.setattr(api, 'consume', mocked_consume)

library.process()

if not custom_target_repos:
assert reporting.create_report.called == 1
assert 'inhibitor' in reporting.create_report.report_fields.get('flags', [])
elif not enable_repos and custom_target_repos and not custom_target_repofile:
assert reporting.create_report.called == 1
assert 'inhibitor' not in reporting.create_report.report_fields.get('flags', [])
else:
assert reporting.create_report.called == 0

0 comments on commit 66b077e

Please sign in to comment.