Skip to content

Commit

Permalink
fix: issue when searching for value when dict_name == attr
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pvoborni authored and Tiboris committed Sep 20, 2022
1 parent 7a8bd24 commit 98255c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/mrack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_config_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98255c7

Please sign in to comment.