From 98255c78ec52053c508170238951daff90f4f5c6 Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Mon, 19 Sep 2022 18:05:30 +0200 Subject: [PATCH] fix: issue when searching for value when dict_name == attr When searching for value using find_value_in_config_hierarchy the method might return the entire dictionary with name according to dict_name when the search attribute name is the same as dict_name and the dict doesn't have a default value nor value according to provided key. Signed-off-by: Petr Vobornik --- src/mrack/utils.py | 3 +++ tests/unit/conftest.py | 31 ++++++++++++++++++++++++++++++- tests/unit/test_config_search.py | 25 +++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/mrack/utils.py b/src/mrack/utils.py index 1f083d5e..1a42f0f1 100644 --- a/src/mrack/utils.py +++ b/src/mrack/utils.py @@ -69,6 +69,9 @@ def get_value_or_dict_value(config_dict, attr, dict_name, key): value = get_config_value(value_dict, key) if value: return value + # avoid to return the whole value_dict if dict name is the same as attr + if attr == dict_name: + return None value = config_dict.get(attr) return value diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index c652dcc3..35cb9f7a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -89,10 +89,39 @@ def aws_config(): @pytest.fixture -def provisioning_config(openstack_config, aws_config): +def beaker_config(): + return { + "strategy": "abort", + "distros": { + "rhel-8.5": "RHEL-8.5%", + "fedora-34": "Fedora-34%", + }, + "distro_variants": { + "default": "BaseeOS", + "RHEL-7.9%": "Server", + "Fedora-34%": "Server", + }, + "distro_tags": { + "RHEL-9.0%": "CTS_NIGHTLY", + }, + "pubkey": "config/id_rsa.pub", + "reserve_duration": 86400, + "timeout:": 230, + "flavors": { + "ipaserver": "test.medium", + "ipaclient": "test.micro", + "ad": "test.medium", + "default": "test.nano", + }, + } + + +@pytest.fixture +def provisioning_config(openstack_config, aws_config, beaker_config): raw = { "openstack": openstack_config, "aws": aws_config, + "beaker": beaker_config, "users": { "rhel-8.5": "cloud-user", "fedora-34": "fedora", diff --git a/tests/unit/test_config_search.py b/tests/unit/test_config_search.py index b537bd2e..d8532379 100644 --- a/tests/unit/test_config_search.py +++ b/tests/unit/test_config_search.py @@ -53,6 +53,31 @@ def test_find_value_in_config_hierarchy( ) assert value == "Administrator" + # Check that dictionary is not returned if dict name is the same as attr and + # the dict doesn't have the value for given key + value = find_value_in_config_hierarchy( + provisioning_config, + "beaker", + metahost1, + host1_osp, + "distro_tags", + "distro_tags", + "RHEL-8.5%", + ) + assert value is None + + # Check that finding value when dict name is the same as attr works + value = find_value_in_config_hierarchy( + provisioning_config, + "beaker", + metahost1, + host1_osp, + "distro_tags", + "distro_tags", + "RHEL-9.0%", + ) + assert value == "CTS_NIGHTLY" + def test_find_simple_value_in_hierarchy( provisioning_config, host1_aws, metahost1, host_win_aws, metahost_win