Skip to content

Commit

Permalink
validate the max_match in test
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce committed Jan 2, 2025
1 parent 02c9dbb commit 8e03f6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions insights/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions insights/tests/core/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8e03f6a

Please sign in to comment.