Skip to content

Commit

Permalink
refactor(RHINENG-5484): refactor operating_system filter
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriciaDinizRH committed Dec 18, 2024
1 parent 4aabe41 commit e870dce
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
16 changes: 14 additions & 2 deletions api/filtering/db_custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def separate_operating_system_filters(filter_param) -> list[OsComparison]:
return [OsComparison(comparator=filter_param)]

# filter_param is a dict
for os_name in filter_param.keys():
for os_name in filter_param.keys(): # this doesn't account for "os_name" instead of os names
if os_name not in (os_names := _get_valid_os_names()):
raise ValidationException(f"operating_system filter only supports these OS names: {os_names}.")

Expand Down Expand Up @@ -147,9 +147,20 @@ def separate_operating_system_filters(filter_param) -> list[OsComparison]:
def build_operating_system_filter(filter_param: dict) -> tuple:
os_filter_list = [] # Top-level filter
os_range_filter_list = [] # Contains the OS filters that use range operations
separated_filters = separate_operating_system_filters(filter_param["operating_system"])
os_field = Host.system_profile_facts["operating_system"]

# if isinstance(filter_param["operating_system"], dict) and "name" in filter_param["operating_system"].keys():
# for os in filter_param["operating_system"]["name"].values():
# print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> found name", os_field["name"].astext == os)
# # os_filter_list.append(and_([os_field["name"].astext == os]))

# return os_filter_list

if "name" in filter_param.keys():
os_filter_list.append()

separated_filters = separate_operating_system_filters(filter_param["operating_system"])

for comparison in separated_filters:
comparator = POSTGRES_COMPARATOR_LOOKUP.get(comparison.comparator)

Expand All @@ -158,6 +169,7 @@ def build_operating_system_filter(filter_param: dict) -> tuple:
os_filter_list.append(os_field.astext.operate(comparator, None))

elif comparison.comparator == "eq":
print("~~~~~~~~~~~~~~~~~~~``", os_field["name"].astext == comparison.name)
os_filters = [
os_field["name"].astext == comparison.name,
os_field["major"].astext.cast(Integer) == comparison.major,
Expand Down
46 changes: 46 additions & 0 deletions tests/test_api_hosts_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,52 @@ def test_query_all_sp_filters_operating_system(db_create_host, api_get, sp_filte
assert nomatch_host_id_3 not in response_ids


@pytest.mark.parametrize(
"sp_filter_param",
(
"[name][eq]=CentOS",
"[name][eq]=centos",
"[name][eq]=CENTOS",
),
)
def test_query_sp_filters_operating_system_name(db_create_host, api_get, sp_filter_param):
# Create host with this OS
match_sp_data = {
"system_profile_facts": {
"operating_system": {
"name": "CentOS",
"major": "8",
"minor": "11",
}
}
}
match_host_id = str(db_create_host(extra_data=match_sp_data).id)

# Create host with differing OS
nomatch_sp_data = {
"system_profile_facts": {
"operating_system": {
"name": "RHEL",
"major": "7",
"minor": "12",
}
}
}
nomatch_host_id = str(db_create_host(extra_data=nomatch_sp_data).id)

url = build_hosts_url(query=f"?filter[system_profile][operating_system]{sp_filter_param}")

with patch("api.host.get_flag_value", return_value=True):
response_status, response_data = api_get(url)

assert response_status == 200

# Assert that only the matching host is returned
response_ids = [result["id"] for result in response_data["results"]]
assert match_host_id in response_ids
assert nomatch_host_id not in response_ids


@pytest.mark.parametrize(
"os_match_data_list,os_nomatch_data_list,sp_filter_param_list",
(
Expand Down

0 comments on commit e870dce

Please sign in to comment.