Skip to content

Commit

Permalink
feat(anta.cli): Better error message when invalid inventory or catalo…
Browse files Browse the repository at this point in the history
…gs are parsed (#1000)

* Feat(anta.cli): Better error message when invalid inventory or catalogs are parsed

* test: Add tests
  • Loading branch information
gmuloc authored Jan 8, 2025
1 parent d21e443 commit 68c664d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions tests/data/invalid_inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
anta_inventory:
- host: 172.20.20.101
name: DC1-SPINE1
tags: ["SPINE", "DC1"]
20 changes: 20 additions & 0 deletions tests/units/cli/nrfu/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit 68c664d

Please sign in to comment.