diff --git a/src/tests/ofc22/Dockerfile b/src/tests/ofc22/Dockerfile index 4817bd93a313b74851e01bba53174dd14f280a18..93b4a77d0f5881f95d109d7a04d989ab693b01ef 100644 --- a/src/tests/ofc22/Dockerfile +++ b/src/tests/ofc22/Dockerfile @@ -93,7 +93,7 @@ export PYTHONPATH=/var/teraflow pytest --verbose --log-level=INFO /var/teraflow/tests/ofc22/tests/test_functional_bootstrap.py --junitxml=/opt/results/report_bootstrap.xml pytest --verbose --log-level=INFO /var/teraflow/tests/ofc22/tests/test_functional_create_service.py --junitxml=/opt/results/report_create_service.xml pytest --verbose --log-level=INFO /var/teraflow/tests/ofc22/tests/test_functional_delete_service.py --junitxml=/opt/results/report_delete_service.xml -pytest --verbose --log-level=INFO /var/teraflow/tests/ofc22/tests/test_functional_cleanup.py --junitxml=/opt/results/report_cleanup.xml +pytest --verbose --timeout=120 --log-level=INFO /var/teraflow/tests/ofc22/tests/test_functional_cleanup.py --junitxml=/opt/results/report_cleanup.xml EOF RUN chmod ug+x ./run_tests.sh diff --git a/src/tests/ofc22/tests/conftest.py b/src/tests/ofc22/tests/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..6650864b04d132c29ae80daaf47ea9550a1829fb --- /dev/null +++ b/src/tests/ofc22/tests/conftest.py @@ -0,0 +1,63 @@ +# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Acknowledgement: https://stackoverflow.com/questions/46766899/pytest-timeout-fail-test-instead-killing-whole-test-run + +import pytest, signal + + +class Termination(SystemExit): + pass + + +class TimeoutExit(BaseException): + pass + + +def _terminate(signum, frame): + raise Termination("Runner is terminated from outside.") + + +def _timeout(signum, frame): + raise TimeoutExit("Runner timeout is reached, runner is terminating.") + + +@pytest.hookimpl +def pytest_addoption(parser): + parser.addoption( + '--timeout', action='store', dest='timeout', type=int, default=None, + help="number of seconds before each test failure") + + +@pytest.hookimpl +def pytest_configure(config): + # Install the signal handlers that we want to process. + signal.signal(signal.SIGTERM, _terminate) + signal.signal(signal.SIGALRM, _timeout) + + +@pytest.hookimpl(hookwrapper=True) +def pytest_runtest_protocol(item, nextitem): + + # Set the per-test timeout (an alarm signal). + if item.config.option.timeout is not None: + signal.alarm(item.config.option.timeout) + + try: + # Run the setup, test body, and teardown stages. + yield + finally: + # Disable the alarm when the test passes or fails. + # I.e. when we get into the framework's body. + signal.alarm(0)