-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISV-4971] Skip subset of static checks for FBC operators (#672)
- Loading branch information
Showing
6 changed files
with
174 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
operator-pipeline-images/operatorcert/static_tests/helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" Static tests helper utilities. """ | ||
|
||
import logging | ||
|
||
from functools import wraps | ||
from typing import Any, Callable, Iterator | ||
from operator_repo import Operator, Bundle | ||
|
||
|
||
LOGGER = logging.getLogger("operator-cert") | ||
|
||
|
||
def skip_fbc(func: Callable[..., Any]) -> Callable[..., Any]: | ||
""" | ||
Decorator to skip a static check for FBC enabled operators. | ||
First argument of the decorated function should be either an Operator or a Bundle, | ||
otherwise the 'check' will be executed as usual. | ||
""" | ||
|
||
@wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> Iterator[Any]: | ||
first_arg = args[0] | ||
if isinstance(first_arg, Bundle): | ||
operator = first_arg.operator | ||
elif isinstance(first_arg, Operator): | ||
operator = first_arg | ||
|
||
config = operator.config if operator else {} | ||
if not config.get("fbc", {}).get("enabled", False): | ||
yield from func(*args, **kwargs) | ||
|
||
LOGGER.info( | ||
"Skipping %s for FBC enabled operator %s", | ||
func.__name__, | ||
operator.operator_name, | ||
) | ||
yield from [] | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
operator-pipeline-images/tests/static_tests/test_helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Any, Iterator | ||
|
||
from operatorcert.static_tests.helpers import skip_fbc | ||
from operator_repo import Operator, Bundle | ||
from unittest.mock import call, MagicMock, patch | ||
|
||
|
||
@patch("operatorcert.static_tests.helpers.LOGGER") | ||
@patch.object(Operator, "probe", return_value=True) | ||
@patch.object(Bundle, "probe", return_value=True) | ||
def test_skip__fbc( | ||
mock_bundle: MagicMock, mock_operator: MagicMock, mock_logger: MagicMock | ||
) -> None: | ||
|
||
@skip_fbc | ||
def check_bundle(bundle: Bundle) -> Iterator[str]: | ||
yield "processed" | ||
|
||
@skip_fbc | ||
def check_operator(operator: Operator) -> Iterator[str]: | ||
yield "processed" | ||
|
||
operator = Operator("test-operator") | ||
operator.config = {"fbc": {"enabled": True}} | ||
bundle = Bundle("test-bundle", operator) | ||
|
||
assert list(check_bundle(bundle)) == [] | ||
assert list(check_operator(operator)) == [] | ||
|
||
mock_logger.assert_has_calls( | ||
[ | ||
call.info( | ||
"Skipping %s for FBC enabled operator %s", | ||
"check_bundle", | ||
"test-operator", | ||
), | ||
call.info( | ||
"Skipping %s for FBC enabled operator %s", | ||
"check_operator", | ||
"test-operator", | ||
), | ||
] | ||
) | ||
|
||
operator.config = {"fbc": {"enabled": False}} | ||
assert list(check_bundle(bundle)) == ["processed"] | ||
assert list(check_operator(operator)) == ["processed"] | ||
|
||
operator.config = {} | ||
assert list(check_bundle(bundle)) == ["processed"] | ||
assert list(check_operator(operator)) == ["processed"] |