diff --git a/anta/cli/utils.py b/anta/cli/utils.py index e740f8c56..508424dd0 100644 --- a/anta/cli/utils.py +++ b/anta/cli/utils.py @@ -17,6 +17,7 @@ from anta.catalog import AntaCatalog from anta.inventory import AntaInventory from anta.inventory.exceptions import InventoryIncorrectSchemaError, InventoryRootKeyError +from anta.logger import anta_log_exception if TYPE_CHECKING: from click import Option @@ -242,7 +243,8 @@ def wrapper( insecure=insecure, disable_cache=disable_cache, ) - except (TypeError, ValueError, YAMLError, OSError, InventoryIncorrectSchemaError, InventoryRootKeyError): + except (TypeError, ValueError, YAMLError, OSError, InventoryIncorrectSchemaError, InventoryRootKeyError) as e: + anta_log_exception(e, f"Failed to parse the inventory: {inventory}", logger) ctx.exit(ExitCode.USAGE_ERROR) return f(*args, inventory=i, **kwargs) @@ -319,7 +321,8 @@ def wrapper( try: file_format = catalog_format.lower() c = AntaCatalog.parse(catalog, file_format=file_format) # type: ignore[arg-type] - except (TypeError, ValueError, YAMLError, OSError): + except (TypeError, ValueError, YAMLError, OSError) as e: + anta_log_exception(e, f"Failed to parse the catalog: {catalog}", logger) ctx.exit(ExitCode.USAGE_ERROR) return f(*args, catalog=c, **kwargs) diff --git a/tests/data/invalid_inventory.yml b/tests/data/invalid_inventory.yml new file mode 100644 index 000000000..5199a15fa --- /dev/null +++ b/tests/data/invalid_inventory.yml @@ -0,0 +1,5 @@ +--- +anta_inventory: + - host: 172.20.20.101 + name: DC1-SPINE1 + tags: ["SPINE", "DC1"] diff --git a/tests/units/cli/nrfu/test__init__.py b/tests/units/cli/nrfu/test__init__.py index f81c67e9e..bbc892839 100644 --- a/tests/units/cli/nrfu/test__init__.py +++ b/tests/units/cli/nrfu/test__init__.py @@ -5,14 +5,18 @@ from __future__ import annotations +from pathlib import Path from typing import TYPE_CHECKING from anta.cli import anta from anta.cli.utils import ExitCode if TYPE_CHECKING: + import pytest from click.testing import CliRunner +DATA_DIR: Path = Path(__file__).parents[3].resolve() / "data" + # TODO: write unit tests for ignore-status and ignore-error @@ -123,3 +127,19 @@ def test_hide(click_runner: CliRunner) -> None: """Test the `--hide` option of the `anta nrfu` command.""" result = click_runner.invoke(anta, ["nrfu", "--hide", "success", "text"]) assert "SUCCESS" not in result.output + + +def test_invalid_inventory(caplog: pytest.LogCaptureFixture, click_runner: CliRunner) -> None: + """Test invalid inventory.""" + result = click_runner.invoke(anta, ["nrfu", "--inventory", str(DATA_DIR / "invalid_inventory.yml")]) + assert "CRITICAL" in caplog.text + assert "Failed to parse the inventory" in caplog.text + assert result.exit_code == ExitCode.USAGE_ERROR + + +def test_invalid_catalog(caplog: pytest.LogCaptureFixture, click_runner: CliRunner) -> None: + """Test invalid catalog.""" + result = click_runner.invoke(anta, ["nrfu", "--catalog", str(DATA_DIR / "test_catalog_not_a_list.yml")]) + assert "CRITICAL" in caplog.text + assert "Failed to parse the catalog" in caplog.text + assert result.exit_code == ExitCode.USAGE_ERROR