8000 feat(anta.cli): Better error message when invalid inventory or catalogs are parsed by gmuloc · Pull Request #1000 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(anta.cli): Better error message when invalid inventory or catalogs are parsed #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
0