From 44dc56ae4a6ba6067721bf3eda153f17694caa34 Mon Sep 17 00:00:00 2001 From: jyejare Date: Tue, 30 Apr 2024 16:46:34 +0530 Subject: [PATCH] Translating URLs for Ipv6 --- robottelo/config/__init__.py | 3 ++ robottelo/utils/url.py | 44 +++++++++++++++++++++++ tests/foreman/installer/test_installer.py | 4 +-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/robottelo/config/__init__.py b/robottelo/config/__init__.py index cca2258c7e6..80a6d7bb0f4 100644 --- a/robottelo/config/__init__.py +++ b/robottelo/config/__init__.py @@ -10,6 +10,7 @@ from robottelo.config.validators import VALIDATORS from robottelo.logging import logger, robottelo_root_dir +from robottelo.utils.url import ipv6_hostname_translation if not os.getenv('ROBOTTELO_DIR'): # dynaconf robottelo file uses ROBOTELLO_DIR for screenshots @@ -44,6 +45,8 @@ def get_settings(): f'Dynaconf validation failed, continuing for the sake of unit tests\n{err}' ) + ipv6_hostname_translation(settings) + return settings diff --git a/robottelo/utils/url.py b/robottelo/utils/url.py index cadcfb56d14..79c0f6bc1f5 100644 --- a/robottelo/utils/url.py +++ b/robottelo/utils/url.py @@ -1,5 +1,7 @@ from urllib.parse import urlparse +from robottelo.logging import logger + def is_url(url): try: @@ -7,3 +9,45 @@ def is_url(url): return all([result.scheme, result.netloc]) except (ValueError, AttributeError): return False + + +def is_ipv4_url(text): + # Did not find the better way to filter only URLs so skipping it simple + # and open for reviewers suggestions + if isinstance(text, str) and 'ipv4' in text and 'redhat.com' in text: + return True + return False + + +def ipv6_translator(settings_list, setting_major, settings): + """Translates the hostname containing ipv4 to ipv6 and updates the settings object""" + dotted_settings = '.'.join(setting_major) + for _key, _val in settings_list.items(): + if is_ipv4_url(_val): + settings.set(f'{dotted_settings}.{_key}', str(_val).replace('ipv4', 'ipv6')) + logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}') + elif isinstance(_val, list): + updated = False + new_list = _val + for i in range(len(new_list)): + if is_ipv4_url(new_list[i]): + new_list[i] = new_list[i].replace('ipv4', 'ipv6') + updated = True + if updated: + settings.set(f'{dotted_settings}.{_key}', new_list) + logger.debug(f'Setting translated to IPv6, Path: {dotted_settings}.{_key}') + elif isinstance(_val, dict): + new_setting_major = setting_major + [_key] + ipv6_translator(settings_list=_val, setting_major=new_setting_major, settings=settings) + + +def ipv6_hostname_translation(settings): + """Migrates any ipv4 containing hostname in conf to ipv6 hostname""" + settings_path = [] + if settings.server.is_ipv6: + all_settings = settings.loaded_by_loaders.items() + for loader_name, loader_settings in tuple(all_settings): + if loader_name.loader == 'yaml': + ipv6_translator(loader_settings, settings_path, settings) + else: + logger.debug('Ipv6 Hostname dynaconf migration hook is skipped for ipv4 testing') diff --git a/tests/foreman/installer/test_installer.py b/tests/foreman/installer/test_installer.py index 24a56771675..f5c42aa1d69 100644 --- a/tests/foreman/installer/test_installer.py +++ b/tests/foreman/installer/test_installer.py @@ -180,9 +180,7 @@ def sat_non_default_install(module_sat_ready_rhels): install_satellite(module_sat_ready_rhels[1], installer_args, enable_fapolicyd=True) sat = module_sat_ready_rhels[1] http_proxy = sat.enable_ipv6_http_proxy() - sat.execute( - 'dnf -y --disableplugin=foreman-protector install foreman-discovery-image' - ) + sat.execute('dnf -y --disableplugin=foreman-protector install foreman-discovery-image') yield sat sat.disable_ipv6_http_proxy(http_proxy)