Merge "Allow config overrides for TripleOHashInfo creation"

This commit is contained in:
Zuul 2021-07-14 13:02:00 +00:00 committed by Gerrit Code Review
commit 06f41a630f
3 changed files with 53 additions and 10 deletions

View File

@ -204,3 +204,29 @@ class TestGetHashInfo(unittest.TestCase):
None, None,
'tripleo-ci-testing', 'tripleo-ci-testing',
) )
def test_override_config_dlrn_url(self, mock_config):
expected_dlrn_url = 'https://foo.bar.baz/centos8-master/component/common/current-tripleo/commit.yaml' # noqa
with requests_mock.Mocker() as req_mock:
req_mock.get(
expected_dlrn_url,
text=test_fakes.TEST_COMMIT_YAML_COMPONENT,
)
mock_hash_info = thi.TripleOHashInfo(
'centos8', 'master', 'common', 'current-tripleo',
{'dlrn_url': 'https://foo.bar.baz'}
)
self.assertEqual(expected_dlrn_url, mock_hash_info.dlrn_url)
def test_override_config_dlrn_url_empty_ignored(self, mock_config):
expected_dlrn_url = 'https://trunk.rdoproject.org/centos8-master/component/common/current-tripleo/commit.yaml' # noqa
with requests_mock.Mocker() as req_mock:
req_mock.get(
expected_dlrn_url,
text=test_fakes.TEST_COMMIT_YAML_COMPONENT,
)
mock_hash_info = thi.TripleOHashInfo(
'centos8', 'master', 'common', 'current-tripleo',
{'dlrn_url': ''}
)
self.assertEqual(expected_dlrn_url, mock_hash_info.dlrn_url)

View File

@ -48,6 +48,11 @@ options:
required: false required: false
type: str type: str
default: current-tripleo default: current-tripleo
dlrn_url:
description: The url of the DLRN server to use for hash queries
required: false
type: str
default: https://trunk.rdoproject.org
author: author:
- Marios Andreou (@marios) - Marios Andreou (@marios)
@ -59,6 +64,7 @@ EXAMPLES = r'''
os_version: centos8 os_version: centos8
release: victoria release: victoria
component: tripleo component: tripleo
dlrn_url: 'https://foo.bar.baz'
''' '''
RETURN = r''' RETURN = r'''
@ -102,6 +108,9 @@ def run_module():
release=dict(type='str', required=False, default='master'), release=dict(type='str', required=False, default='master'),
component=dict(type='str', required=False, default=None), component=dict(type='str', required=False, default=None),
tag=dict(type='str', required=False, default='current-tripleo'), tag=dict(type='str', required=False, default='current-tripleo'),
dlrn_url=dict(type='str',
required=False,
default='https://trunk.rdoproject.org'),
) )
module = AnsibleModule( module = AnsibleModule(
@ -115,8 +124,10 @@ def run_module():
release = module.params.get('release') release = module.params.get('release')
component = module.params.get('component') component = module.params.get('component')
tag = module.params.get('tag') tag = module.params.get('tag')
dlrn_url = module.params.get('dlrn_url')
hash_result = TripleOHashInfo(os_version, release, component, tag) hash_result = TripleOHashInfo(os_version, release, component, tag,
config={'dlrn_url': dlrn_url})
result['commit_hash'] = hash_result.commit_hash result['commit_hash'] = hash_result.commit_hash
result['distro_hash'] = hash_result.distro_hash result['distro_hash'] = hash_result.distro_hash
result['full_hash'] = hash_result.full_hash result['full_hash'] = hash_result.full_hash

View File

@ -66,7 +66,7 @@ class TripleOHashInfo:
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
@classmethod @classmethod
def load_config(cls): def load_config(cls, passed_config=None):
""" """
This is a class method since we call it from the CLI entrypoint This is a class method since we call it from the CLI entrypoint
before the TripleOHashInfo object is created. The method will first before the TripleOHashInfo object is created. The method will first
@ -74,12 +74,15 @@ class TripleOHashInfo:
a local config.yaml for example for invocations from a source checkout a local config.yaml for example for invocations from a source checkout
directory. If the file is not found TripleOHashMissingConfig is raised. directory. If the file is not found TripleOHashMissingConfig is raised.
If any of the contants.CONFIG_KEYS is missing from config.yaml then If any of the contants.CONFIG_KEYS is missing from config.yaml then
TripleOHashInvalidConfig is raised. Returns a dictionary containing TripleOHashInvalidConfig is raised. If the passed_config dict contains
a given config value then that is used instead of the value from the
loaded config file. Returns a dictionary containing
the key->value for all the keys in constants.CONFIG_KEYS. the key->value for all the keys in constants.CONFIG_KEYS.
:param passed_config: dict with configuration overrides
:raises TripleOHashMissingConfig for missing config.yaml :raises TripleOHashMissingConfig for missing config.yaml
:raises TripleOHashInvalidConfig for missing keys in config.yaml :raises TripleOHashInvalidConfig for missing keys in config.yaml
:returns a config dictionary with the keys in constants.CONFIG_KEYS :return: a config dictionary with the keys in constants.CONFIG_KEYS
""" """
def _check_read_file(filepath): def _check_read_file(filepath):
@ -101,6 +104,7 @@ class TripleOHashInfo:
if _check_read_file(_local_config): if _check_read_file(_local_config):
return _local_config return _local_config
passed_config = passed_config or {}
result_config = {} result_config = {}
config_path = '' config_path = ''
local_config = _resolve_local_config_path() local_config = _resolve_local_config_path()
@ -117,9 +121,9 @@ class TripleOHashInfo:
) )
logging.info("Using config file at {}".format(config_path)) logging.info("Using config file at {}".format(config_path))
with open(config_path, 'r') as config_yaml: with open(config_path, 'r') as config_yaml:
conf_yaml = yaml.safe_load(config_yaml) loaded_config = yaml.safe_load(config_yaml)
for k in const.CONFIG_KEYS: for k in const.CONFIG_KEYS:
if k not in conf_yaml: if k not in loaded_config:
error_str = ( error_str = (
"Malformed config file - missing {}. Expected all" "Malformed config file - missing {}. Expected all"
"of these configuration items: {}" "of these configuration items: {}"
@ -128,8 +132,11 @@ class TripleOHashInfo:
) )
logging.error(error_str) logging.error(error_str)
raise exc.TripleOHashInvalidConfig(error_str) raise exc.TripleOHashInvalidConfig(error_str)
loaded_value = conf_yaml[k] # if the passed config contains the key then use that value
result_config[k] = loaded_value if passed_config.get(k):
result_config[k] = passed_config[k]
else:
result_config[k] = loaded_config[k]
return result_config return result_config
def __init__(self, os_version, release, component, tag, config=None): def __init__(self, os_version, release, component, tag, config=None):
@ -142,8 +149,7 @@ class TripleOHashInfo:
:param config: Use an existing config dictionary and don't load it :param config: Use an existing config dictionary and don't load it
""" """
if config is None: config = TripleOHashInfo.load_config(config)
config = TripleOHashInfo.load_config()
self.os_version = os_version self.os_version = os_version
self.release = release self.release = release