diff --git a/insights/core/filters.py b/insights/core/filters.py index 79a32d8e7..2deabdcdc 100644 --- a/insights/core/filters.py +++ b/insights/core/filters.py @@ -107,6 +107,11 @@ def inner(comp, patterns): FILTERS[comp].update(max_matchs(FILTERS[comp], patterns)) + if max_match is None or type(max_match) is not int or max_match <= 0: + raise Exception( + "Invalid argument: {0}. It can only be a positive integer.".format(max_match) + ) + if not plugins.is_datasource(component): deps = get_dependency_datasources(component) if deps: diff --git a/insights/tests/core/test_filters.py b/insights/tests/core/test_filters.py index 57d3d8a27..0e431ede5 100644 --- a/insights/tests/core/test_filters.py +++ b/insights/tests/core/test_filters.py @@ -200,6 +200,24 @@ def test_add_filter_exception_None(): filters.add_filter(Specs.ps_aux, None) +def test_add_filter_exception_with_invalid_max_match(): + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', 'str') + assert "Invalid argument: str" in str(ex) + + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', 0) + assert "Invalid argument: 0" in str(ex) + + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', -1) + assert "Invalid argument: -1" in str(ex) + + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', None) + assert "Invalid argument: None" in str(ex) + + def test_get_filters(): _filter = 'A filter' filters.add_filter(MySpecs.has_filters, _filter)