diff --git a/repos/system_upgrade/common/libraries/rpms.py b/repos/system_upgrade/common/libraries/rpms.py index a6cef13f49..faa97b3eed 100644 --- a/repos/system_upgrade/common/libraries/rpms.py +++ b/repos/system_upgrade/common/libraries/rpms.py @@ -1,4 +1,5 @@ from leapp.libraries import stdlib +from leapp.libraries.common.config.version import get_source_major_version from leapp.models import InstalledRPM @@ -114,3 +115,36 @@ def check_file_modification(config): """ output = _read_rpm_modifications(config) return _parse_config_modification(output, config) + + +def get_leapp_packages(source_version=None, component=None, include_deps=False): + """ + Get list of leapp packages. + + :param source_version: a list or string specifying source_versions. If not defined then all packages + for current source_target_version will be returned. + :param component: a list or string specifying leapp components. If defined then only packages related + to the specific component will be returned. + :param include_deps: a flag to control whether deps meta packages are to be filtered out. + """ + res = set() + # NOTE(ivasilev) mapping has the format component: {source_version: [pkg1, pkg2, ..]} + component_map = {'framework': {'7': ['leapp', 'leapp-deps', 'python2-leapp'], + '8': ['leapp', 'leapp-deps', 'python3-leapp']}, + 'repository': {'7': ['leapp-upgrade-el7toel8', 'leapp-upgrade-el7toel8-deps'], + '8': ['leapp-upgrade-el8toel9', 'leapp-upgrade-el8toel9-deps']}, + 'snactor': {'7': ['snactor'], + '8': ['snactor']}} + source_versions = [source_version] if isinstance(source_version, str) else (source_version or + [get_source_major_version()]) + components = [component] if isinstance(component, str) else component or list(component_map.keys()) + for comp in components: + for src_version in source_versions: + try: + res.update(component_map[comp][src_version]) + except KeyError: + continue + if not include_deps: + # filter out deps packages + return sorted([pkg for pkg in res if not pkg.endswith('-deps')]) + return sorted(res) diff --git a/repos/system_upgrade/common/libraries/tests/test_rpms.py b/repos/system_upgrade/common/libraries/tests/test_rpms.py index 39a32dcbd2..744f8ba2c6 100644 --- a/repos/system_upgrade/common/libraries/tests/test_rpms.py +++ b/repos/system_upgrade/common/libraries/tests/test_rpms.py @@ -1,4 +1,8 @@ -from leapp.libraries.common.rpms import _parse_config_modification +import pytest + +from leapp.libraries.common.rpms import _parse_config_modification, get_leapp_packages +from leapp.libraries.common.testutils import CurrentActorMocked +from leapp.libraries.stdlib import api def test_parse_config_modification(): @@ -30,3 +34,20 @@ def test_parse_config_modification(): "S.5....T. c /etc/ssh/sshd_config", ] assert _parse_config_modification(data, "/etc/ssh/sshd_config") + + +@pytest.mark.parametrize('source_version,component,include_deps,result', [ + (None, None, False, ['leapp', 'python3-leapp', 'leapp-upgrade-el8toel9', 'snactor']), + (None, None, True, ['leapp', 'leapp-deps', 'python3-leapp', 'leapp-upgrade-el8toel9', + 'leapp-upgrade-el8toel9-deps', 'snactor']), + ('7', None, False, ['leapp', 'python2-leapp', 'leapp-upgrade-el7toel8', 'snactor']), + (['7', '8'], None, False, ['leapp', 'python2-leapp', 'leapp-upgrade-el7toel8', + 'python3-leapp', 'leapp-upgrade-el8toel9', 'snactor']), + ('nosuchversion', None, False, []), + ('8', 'framework', False, ['leapp', 'python3-leapp']), + ('8', 'framework', True, ['leapp', 'python3-leapp', 'leapp-deps']), + ('8', 'nosuchcomponent', True, []), + ]) +def test_get_leapp_packages(source_version, component, include_deps, result, monkeypatch): + monkeypatch.setattr(api, 'current_actor', CurrentActorMocked(arch='x86_64', src_ver='8.9', dst_ver='9.3')) + assert set(get_leapp_packages(source_version, component, include_deps)) == set(result)