8000 fix(anta.cli): Catch command failure for debug commands by gmuloc · Pull Request #404 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(anta.cli): Catch command failure for debug commands #404

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 1 commit into from
Sep 21, 2023
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
15 changes: 11 additions & 4 deletions anta/cli/debug/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import asyncio
import logging
import sys
from typing import Literal

import click
Expand Down Expand Up @@ -49,9 +50,12 @@ def run_cmd(command: str, ofmt: Literal["json", "text"], version: Literal["1", "
v: Literal[1, "latest"] = version if version == "latest" else 1
c = AntaCommand(command=command, ofmt=ofmt, version=v, revision=revision)
asyncio.run(device.collect(c))
if ofmt == "json":
if c.failed:
console.print(f"[bold red] Command '{c.command}' failed to execute!")
sys.exit(1)
elif ofmt == "json":
console.print(c.json_output)
if ofmt == "text":
elif ofmt == "text":
console.print(c.text_output)


Expand Down Expand Up @@ -79,7 +83,10 @@ def run_template(template: str, params: list[str], ofmt: Literal["json", "text"]
t = AntaTemplate(template=template, ofmt=ofmt, version=v, revision=revision)
c = t.render(**template_params) # type: ignore
asyncio.run(device.collect(c))
if ofmt == "json":
if c.failed:
console.print(f"[bold red] Command '{c.command}' failed to execute!")
sys.exit(1)
elif ofmt == "json":
console.print(c.json_output)
if ofmt == "text":
elif ofmt == "text":
console.print(c.text_output)
43 changes: 34 additions & 9 deletions tests/units/cli/debug/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ def test_get_device(test_inventory: AntaInventory, device_name: str, expected_ra
assert isinstance(result, AntaDevice)


class TestEAPIException(Exception):
"""
Dummy exception for the tests
"""


# TODO complete test cases
@pytest.mark.parametrize(
"command, ofmt, version, revision, device",
"command, ofmt, version, revision, device, failed",
[
pytest.param("show version", "json", None, None, "dummy", id="json command"),
pytest.param("show version", "text", None, None, "dummy", id="text command"),
pytest.param("show version", None, "1", None, "dummy", id="version"),
pytest.param("show version", None, None, 3, "dummy", id="revision"),
# pytest.param("show version", None, None, 3, "mocked_device", id="non existing device"),
pytest.param("show version", "json", None, None, "dummy", False, id="json command"),
pytest.param("show version", "text", None, None, "dummy", False, id="text command"),
pytest.param("show version", None, "1", None, "dummy", False, id="version"),
pytest.param("show version", None, None, 3, "dummy", False, id="revision"),
pytest.param("show version", None, None, None, "dummy", True, id="command fails"),
],
)
def test_run_cmd(
click_runner: CliRunner, command: str, ofmt: Literal["json", "text"], version: Literal["1", "latest"] | None, revision: int | None, device: str
click_runner: CliRunner, command: str, ofmt: Literal["json", "text"], version: Literal["1", "latest"] | None, revision: int | None, device: str, failed: bool
) -> None:
"""
Test `anta debug run-cmd`
Expand Down Expand Up @@ -90,10 +96,20 @@ def test_run_cmd(
if revision is not None:
cli_args.extend(["--revision", str(revision)])

# failed
expected_failed = None
if failed:
expected_failed = TestEAPIException("Command failed to run")

# exit code
expected_exit_code = 1 if failed else 0

def expected_result() -> Any:
"""
Helper to return some dummy payload for collect depending on outformat
"""
if failed:
return None
if expected_ofmt == "json":
return {"dummy": 42}
if expected_ofmt == "text":
Expand All @@ -105,14 +121,23 @@ async def dummy_collect(c: AntaCommand) -> None:
mocking collect coroutine
"""
c.output = expected_result()
if c.output is None:
c.failed = expected_failed

with patch("anta.device.AsyncEOSDevice.collect") as mocked_collect:
mocked_collect.side_effect = dummy_collect
result = click_runner.invoke(anta, cli_args, env=env, auto_envvar_prefix="ANTA")

mocked_collect.assert_awaited_with(
AntaCommand(
command=command, version=expected_version, revision=revision, ofmt=expected_ofmt, output=expected_result(), template=None, failed=None, params=None
command=command,
version=expected_version,
revision=revision,
ofmt=expected_ofmt,
output=expected_result(),
template=None,
failed=expected_failed,
params=None,
)
)
assert result.exit_code == 0
assert result.exit_code == expected_exit_code
0