Skip to content
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

test: Add first tests for Anta CLI #248

Merged
merged 1 commit into from
Jul 5, 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
46 changes: 36 additions & 10 deletions anta/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import logging
import pathlib
from typing import Any, Callable, Dict, List, Tuple

import click
Expand All @@ -15,26 +16,51 @@
from anta.cli.get import commands as get_commands
from anta.cli.nrfu import commands as check_commands
from anta.cli.utils import parse_catalog, parse_inventory, setup_logging
from anta.inventory import AntaInventory
from anta.result_manager.models import TestResult


@click.group()
@click.pass_context
@click.version_option(__version__)
@click.option("--username", show_envvar=True, help="Username to connect to EOS", required=True, is_eager=True)
@click.option("--password", show_envvar=True, help="Password to connect to EOS", required=True, is_eager=True)
@click.option("--timeout", show_envvar=True, default=5, help="Global connection timeout", show_default=True, is_eager=True)
@click.option("--insecure/--secure", show_envvar=True, default=False, help="Disable SSH Host Key validation", show_default=True, is_eager=True)
@click.option("--enable-password", show_envvar=True, help="Enable password if required to connect", is_eager=True)
@click.option(
"--username",
show_envvar=True,
help="Username to connect to EOS",
required=True,
)
@click.option(
"--password",
show_envvar=True,
help="Password to connect to EOS",
required=True,
)
@click.option(
"--timeout",
show_envvar=True,
default=5,
help="Global connection timeout",
show_default=True,
)
@click.option(
"--insecure/--secure",
show_envvar=True,
is_flag=True,
default=False,
help="Disable SSH Host Key validation",
show_default=True,
)
@click.option(
"--enable-password",
show_envvar=True,
help="Enable password if required to connect",
)
@click.option(
"--inventory",
"-i",
show_envvar=True,
required=True,
help="Path to the inventory YAML file",
type=click.Path(file_okay=True, dir_okay=False, exists=True, readable=True),
callback=parse_inventory,
type=click.Path(file_okay=True, dir_okay=False, exists=True, readable=True, path_type=pathlib.Path),
)
@click.option(
"--log-level",
Expand All @@ -57,11 +83,11 @@
)
@click.option("--ignore-status", show_envvar=True, is_flag=True, default=False, help="Always exit with success")
@click.option("--ignore-error", show_envvar=True, is_flag=True, default=False, help="Only report failures and not errors")
def anta(ctx: click.Context, inventory: AntaInventory, ignore_status: bool, ignore_error: bool, **kwargs: Dict[str, Any]) -> None:
def anta(ctx: click.Context, inventory: str, ignore_status: bool, ignore_error: bool, **kwargs: Dict[str, Any]) -> None:
# pylint: disable=unused-argument
"""Arista Network Test Automation (ANTA) CLI"""
ctx.ensure_object(dict)
ctx.obj["inventory"] = inventory
ctx.obj["inventory"] = parse_inventory(ctx, inventory)
ctx.obj["ignore_status"] = ignore_status
ctx.obj["ignore_error"] = ignore_error

Expand Down
3 changes: 1 addition & 2 deletions anta/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def parse_tags(ctx: click.Context, param: Option, value: str) -> List[str]:
return None


def parse_inventory(ctx: click.Context, param: Option, value: str) -> AntaInventory:
# pylint: disable=unused-argument
def parse_inventory(ctx: click.Context, value: str) -> AntaInventory:
"""
Click option callback to parse an ANTA inventory YAML file
"""
Expand Down
2 changes: 2 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ signature-mutators=click.decorators.option
[MAIN]
load-plugins=pylint_pydantic
extension-pkg-whitelist=pydantic
ignore-paths = ^tests/units/anta_tests/.*/data.py$,
^tests/units/anta_tests/routing/.*/data.py$,
9 changes: 9 additions & 0 deletions tests/lib/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pytest
from aioeapi import Device
from click.testing import CliRunner

from anta.device import AntaDevice

Expand All @@ -28,3 +29,11 @@ def mocked_device(hw_model: str = "unknown_hw") -> MagicMock:
mock.hw_model = hw_model

return mock


@pytest.fixture
def click_runner() -> CliRunner:
"""
Convenience fixture to return a click.CliRunner for cli testing
"""
return CliRunner()
Empty file added tests/units/cli/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions tests/units/cli/test__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Tests for anta.cli.__init__.py
"""

from __future__ import annotations

from click.testing import CliRunner

from anta.cli import anta


def test_anta(click_runner: CliRunner) -> None:
"""
Test anta main entrypoint
"""
result = click_runner.invoke(anta)
assert result.exit_code == 0
assert "Usage" in result.output


def test_anta_help(click_runner: CliRunner) -> None:
"""
Test anta --help
"""
result = click_runner.invoke(anta, ["--help"])
assert result.exit_code == 0
assert "Usage" in result.output