diff --git a/kernelci/legacy/lava/__init__.py b/kernelci/legacy/lava/__init__.py index 55a9742840..4b6a979d46 100644 --- a/kernelci/legacy/lava/__init__.py +++ b/kernelci/legacy/lava/__init__.py @@ -12,6 +12,7 @@ import sys from jinja2 import Environment, FileSystemLoader +from jinja2.exceptions import TemplateRuntimeError def add_kci_raise(jinja2_env): @@ -23,7 +24,7 @@ def add_kci_raise(jinja2_env): """ def template_exception(msg): - raise Exception(msg) + raise TemplateRuntimeError(msg) jinja2_env.globals["kci_raise"] = template_exception diff --git a/kernelci/runtime/__init__.py b/kernelci/runtime/__init__.py index 5911ffc953..3473378267 100644 --- a/kernelci/runtime/__init__.py +++ b/kernelci/runtime/__init__.py @@ -13,6 +13,7 @@ import requests import yaml from jinja2 import ChoiceLoader, Environment, FileSystemLoader +from jinja2.exceptions import TemplateRuntimeError from kernelci.config.base import get_system_arch @@ -136,7 +137,7 @@ def _get_jinja2_functions(cls): def kci_raise(msg): """Raise an exception""" - raise Exception(msg) + raise TemplateRuntimeError(msg) def kci_yaml_dump(data): """Dump data to YAML""" diff --git a/tests/test_runtime.py b/tests/test_runtime.py index 66f6972f80..81ea8b0644 100644 --- a/tests/test_runtime.py +++ b/tests/test_runtime.py @@ -14,13 +14,32 @@ import pytest import yaml from jinja2 import Environment, FileSystemLoader +from jinja2.exceptions import TemplateRuntimeError import kernelci.config +import kernelci.legacy.lava import kernelci.runtime import kernelci.runtime.lava from kernelci.runtime.pull_labs import compute_tuxrun_parameters +def test_kci_raise_uses_jinja_runtime_error(): + """The runtime helper raises a template-specific exception.""" + kci_raise = kernelci.runtime.Runtime._get_jinja2_functions()["kci_raise"] + + with pytest.raises(TemplateRuntimeError, match="invalid parameters"): + kci_raise("invalid parameters") + + +def test_legacy_kci_raise_uses_jinja_runtime_error(): + """The legacy LAVA helper raises a template-specific exception.""" + environment = Environment() + kernelci.legacy.lava.add_kci_raise(environment) + + with pytest.raises(TemplateRuntimeError, match="invalid parameters"): + environment.globals["kci_raise"]("invalid parameters") + + @pytest.mark.parametrize( "docker_image", [None, "example.com/lava/fastboot:latest"] )