From 0f6511b3be42f1500e5a41950e6e0bbec03a9108 Mon Sep 17 00:00:00 2001 From: Xiangce Liu Date: Thu, 2 Jan 2025 17:35:07 +0800 Subject: [PATCH] validate the max_match in test Signed-off-by: Xiangce Liu --- insights/core/filters.py | 3 +++ insights/tests/core/test_filters.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/insights/core/filters.py b/insights/core/filters.py index 79a32d8e7..06d818ddb 100644 --- a/insights/core/filters.py +++ b/insights/core/filters.py @@ -107,6 +107,9 @@ def inner(comp, patterns): FILTERS[comp].update(max_matchs(FILTERS[comp], patterns)) + if max_match is not None and (type(max_match) is not int or max_match <= 0): + raise Exception("Invalid argument 'max_match' to the add_filter: {0}".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..adf40ea47 100644 --- a/insights/tests/core/test_filters.py +++ b/insights/tests/core/test_filters.py @@ -200,6 +200,20 @@ 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 'max_match' to the add_filter: str" in str(ex) + + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', 0) + assert "Invalid argument 'max_match' to the add_filter: 0" in str(ex) + + with pytest.raises(Exception) as ex: + filters.add_filter(Specs.ps_aux, 'abctest', -1) + assert "Invalid argument 'max_match' to the add_filter: -1" in str(ex) + + def test_get_filters(): _filter = 'A filter' filters.add_filter(MySpecs.has_filters, _filter)