From 7d591854b966d14b1555afad4adb9acda778e4ab Mon Sep 17 00:00:00 2001 From: Marios Andreou Date: Tue, 7 Sep 2021 15:02:09 +0300 Subject: [PATCH] Fix config issue loaded default config and ignoring overrides When dlrn_url is overridden it is being ignored and instead the default is used. This makes it that the passed config is honored. Adds molecule test case to check vanilla invocation and test that passed dlrn_url is honored - test cases brought from WIP patch in [1]. Co-Authored-By: Sorin Sbarnea [1] https://review.opendev.org/c/openstack/tripleo-repos/+/807700 Change-Id: Ie5354421a470e5a528307694b49f4e8d2bc24ddc --- molecule/default/converge.yml | 10 ++++++++ .../get_hash/tripleo_hash_info.py | 25 ++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 8c9e12c..6c345f4 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -12,3 +12,13 @@ # TODO: fix yum_config to correctly report changed state and uncomment # the line below which disables molecule idemptotence test. - molecule-idempotence-notest + + - name: "Check get_hash" + tripleo.repos.get_hash: + release: master + - name: "Check get_hash with invalid url" + tripleo.repos.get_hash: + release: master + dlrn_url: 'https://httpbin.org/status/404' + register: result + failed_when: result is success diff --git a/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py b/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py index 08bffa6..b6291a1 100644 --- a/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py +++ b/plugins/module_utils/tripleo_repos/get_hash/tripleo_hash_info.py @@ -29,17 +29,23 @@ try: from ansible.module_utils.urls import open_url def http_get(url: str) -> Tuple[str, int]: - response = open_url(url, method='GET') - return (response.read(), response.status) + try: + response = open_url(url, method='GET') + return (response.read(), response.status) + except Exception as e: + return (str(e), -1) except ImportError: from urllib.request import urlopen def http_get(url: str) -> Tuple[str, int]: # https://stackoverflow.com/questions/35122232/urllib-request-urlopen-return-bytes-but-i-cannot-decode-it - response = urlopen(url) - return ( - response.read().decode('utf-8'), - int(response.status)) + try: + response = urlopen(url) + return ( + response.read().decode('utf-8'), + int(response.status)) + except Exception as e: + return (str(e), -1) __metaclass__ = type @@ -145,10 +151,11 @@ class TripleOHashInfo: config_path = local_config else: logging.info("Using embedded config file") - return DEFAULT_CONFIG + loaded_config = DEFAULT_CONFIG logging.info("Using config file at %s", config_path) - with open(config_path, 'r') as config_yaml: - loaded_config = cls.load_yaml(config_yaml) + if config_path != '': + with open(config_path, 'r') as config_yaml: + loaded_config = cls.load_yaml(config_yaml) for k in CONFIG_KEYS: if k not in loaded_config: error_str = (