Skip to content

Commit

Permalink
fix: set should be supported by add_filter (#4333)
Browse files Browse the repository at this point in the history
- Per the original design 'set()' should be supported by add_filter().
  However, it was not supported in the code. Fixed it and added test.

Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce authored Jan 14, 2025
1 parent 340d7a6 commit dee4d55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
11 changes: 5 additions & 6 deletions insights/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,19 @@ def inner(comp, patterns):
if comp in _CACHE:
del _CACHE[comp]

types = six.string_types + (list, set)
if not isinstance(patterns, types):
if not isinstance(patterns, (six.string_types, list, set)):
raise TypeError("Filter patterns must be of type string, list, or set.")

if isinstance(patterns, six.string_types):
patterns = {patterns: max_match}
elif isinstance(patterns, list):
patterns = dict((pt, max_match) for pt in patterns)
# here patterns is a dict
patterns = [patterns]

for pat in patterns:
if not pat:
raise Exception("Filter patterns must not be empty.")

patterns = dict((pt, max_match) for pt in patterns)
# here patterns is a dict

FILTERS[comp].update(max_matchs(FILTERS[comp], patterns))

if max_match is None or type(max_match) is not int or max_match <= 0:
Expand Down
19 changes: 18 additions & 1 deletion insights/tests/core/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_add_filter_to_LocalSpecsHasFilters():


# General Parser
def test_add_filter_to_PsAux():
def test_add_filter_to_parser_patterns_string():
"""
"filters" added to Specs.x will add to DefaultSpecs.x
"""
Expand Down Expand Up @@ -167,6 +167,23 @@ def test_add_filter_to_parser_patterns_list():
assert not parser_filters


def test_add_filter_to_parser_patterns_set():
filters_set = set(["bash", "systemd", "Network"])
filters.add_filter(PsAux, filters_set)

spec_filters = filters.get_filters(Specs.ps_aux)
assert all(f in spec_filters for f in filters_set)

parser_filters = filters.get_filters(PsAux)
assert not parser_filters


def test_add_filter_to_parser_patterns_tupple():
filters_tup = ("bash", "systemd", "Network")
with pytest.raises(TypeError):
filters.add_filter(PsAux, filters_tup)


def test_add_filter_exception_spec_not_filterable():
with pytest.raises(Exception):
filters.add_filter(Specs.ps_auxcww, "bash")
Expand Down

0 comments on commit dee4d55

Please sign in to comment.