From fc5ae67ab47144f1d5c5759bceed5090e104b02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Reh=C3=A1k?= Date: Tue, 7 Apr 2020 17:31:52 +0100 Subject: [PATCH] Add tests for Checktargetrepos and reword report messages --- .../checktargetrepos/libraries/library.py | 11 +-- .../tests/test_checktargetrepos.py | 92 ++++++++++++++++++- 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/repos/system_upgrade/el7toel8/actors/checktargetrepos/libraries/library.py b/repos/system_upgrade/el7toel8/actors/checktargetrepos/libraries/library.py index b91fd885e2..1f012428b7 100644 --- a/repos/system_upgrade/el7toel8/actors/checktargetrepos/libraries/library.py +++ b/repos/system_upgrade/el7toel8/actors/checktargetrepos/libraries/library.py @@ -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' @@ -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' @@ -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'), diff --git a/repos/system_upgrade/el7toel8/actors/checktargetrepos/tests/test_checktargetrepos.py b/repos/system_upgrade/el7toel8/actors/checktargetrepos/tests/test_checktargetrepos.py index 69626e4215..8db655250e 100644 --- a/repos/system_upgrade/el7toel8/actors/checktargetrepos/tests/test_checktargetrepos.py +++ b/repos/system_upgrade/el7toel8/actors/checktargetrepos/tests/test_checktargetrepos.py @@ -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