From a0c46e98e61bb39b2e3077fdadbfff79d9f4fdd5 Mon Sep 17 00:00:00 2001 From: Petr Stodulka Date: Wed, 8 Apr 2020 10:16:06 +0200 Subject: [PATCH] Add scanclienablerepo actor The actor produce CustomTargetRepository messages for repoids obtained from the cmdline when user use the --enablerepo option. The values are stored inside LEAPP_ENABLE_REPOS envar now, which is stored inside the configuration of the actor. --- .../actors/scanclienablerepo/actor.py | 18 +++++ .../scanclienablerepo/libraries/library.py | 11 +++ .../scanclienablerepo/tests/test_unit.py | 75 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 repos/system_upgrade/el7toel8/actors/scanclienablerepo/actor.py create mode 100644 repos/system_upgrade/el7toel8/actors/scanclienablerepo/libraries/library.py create mode 100644 repos/system_upgrade/el7toel8/actors/scanclienablerepo/tests/test_unit.py diff --git a/repos/system_upgrade/el7toel8/actors/scanclienablerepo/actor.py b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/actor.py new file mode 100644 index 0000000000..7d3d6fb3ca --- /dev/null +++ b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/actor.py @@ -0,0 +1,18 @@ +from leapp.actors import Actor +from leapp.libraries.actor import library +from leapp.models import CustomTargetRepository +from leapp.tags import FactsPhaseTag, IPUWorkflowTag + + +class ScanCLIenablrepo(Actor): + """ + Produce CustomTargetRepository based on the LEAPP_ENABLE_REPOS in config. + """ + + name = 'scanclienablerepo' + consumes = () + produces = (CustomTargetRepository) + tags = (FactsPhaseTag, IPUWorkflowTag) + + def process(self): + library.process() diff --git a/repos/system_upgrade/el7toel8/actors/scanclienablerepo/libraries/library.py b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/libraries/library.py new file mode 100644 index 0000000000..841c17df47 --- /dev/null +++ b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/libraries/library.py @@ -0,0 +1,11 @@ +from leapp.libraries.stdlib import api +from leapp.libraries.common import config +from leapp.models import CustomTargetRepository + + +def process(): + if not config.get_env('LEAPP_ENABLE_REPOS'): + return + api.current_logger().info('The --enablerepo option has been used,') + for repoid in config.get_env('LEAPP_ENABLE_REPOS').split(','): + api.produce(CustomTargetRepository(repoid=repoid)) diff --git a/repos/system_upgrade/el7toel8/actors/scanclienablerepo/tests/test_unit.py b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/tests/test_unit.py new file mode 100644 index 0000000000..8e6c013724 --- /dev/null +++ b/repos/system_upgrade/el7toel8/actors/scanclienablerepo/tests/test_unit.py @@ -0,0 +1,75 @@ +from collections import namedtuple +import os + +import pytest + +from leapp.libraries.actor import library +from leapp.libraries.common.config import architecture +from leapp.libraries.common.testutils import produce_mocked +from leapp.libraries.stdlib import api +from leapp.models import CustomTargetRepository +from leapp import models + + +class CurrentActorMocked(object): + def __init__(self, kernel='3.10.0-957.43.1.el7.x86_64', release_id='rhel', + src_ver='7.6', dst_ver='8.1', arch=architecture.ARCH_X86_64, + envars=None): + + if envars: + envarsList = [models.EnvVar(name=key, value=value) for key, value in envars.items()] + else: + envarsList = [] + + version = namedtuple('Version', ['source', 'target'])(src_ver, dst_ver) + os_release = namedtuple('OS_release', ['release_id', 'version_id'])(release_id, src_ver) + args = (version, kernel, os_release, arch, envarsList) + conf_fields = ['version', 'kernel', 'os_release', 'architecture', 'leapp_env_vars'] + self.configuration = namedtuple('configuration', conf_fields)(*args) + self._common_folder = '../../files' + + def __call__(self): + return self + + def get_common_folder_path(self, folder): + return os.path.join(self._common_folder, folder) + + +class LoggerMocked(object): + def __init__(self): + self.infomsg = None + self.debugmsg = None + + def info(self, msg): + self.infomsg = msg + + def debug(self, msg): + self.debugmsg = msg + + def __call__(self): + return self + + +def test_no_enabledrepos(monkeypatch): + monkeypatch.setattr(api, 'produce', produce_mocked()) + monkeypatch.setattr(api, 'current_logger', LoggerMocked()) + monkeypatch.setattr(api, 'current_actor', CurrentActorMocked()) + library.process() + assert not api.current_logger.infomsg + assert not api.produce.called + + +@pytest.mark.parametrize('envars,result', [ + ({'LEAPP_ENABLE_REPOS': 'repo1'}, [CustomTargetRepository(repoid='repo1')]) + ({'LEAPP_ENABLE_REPOS': 'repo1,repo2'}, [CustomTargetRepository(repoid='repo1'), + CustomTargetRepository(repoid='repo2')]) +]) +def test_enabledrepos(monkeypatch, envars, result): + monkeypatch.setattr(api, 'produce', produce_mocked()) + monkeypatch.setattr(api, 'current_logger', LoggerMocked()) + monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(envars=envars)) + library.process() + assert api.current_logger.infomsg + assert api.produce.called == len(result) + for i in result: + assert i in api.produce.model_instances