diff --git a/insights/core/filters.py b/insights/core/filters.py index 2deabdcdc..fdcb5ff16 100644 --- a/insights/core/filters.py +++ b/insights/core/filters.py @@ -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: diff --git a/insights/tests/core/test_filters.py b/insights/tests/core/test_filters.py index 0e431ede5..f481b3473 100644 --- a/insights/tests/core/test_filters.py +++ b/insights/tests/core/test_filters.py @@ -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 """ @@ -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")