2025-02-05 11:32:35 +01:00
|
|
|
# Copyright (c) 2023-2024 Arista Networks, Inc.
|
|
|
|
# Use of this source code is governed by the Apache License 2.0
|
|
|
|
# that can be found in the LICENSE file.
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Tests for anta.cli.nrfu."""
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
from typing import TYPE_CHECKING
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
from anta.cli import anta
|
|
|
|
from anta.cli.utils import ExitCode
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
# TODO: write unit tests for ignore-status and ignore-error
|
|
|
|
|
|
|
|
|
|
|
|
def test_anta_nrfu_help(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test anta nrfu --help."""
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--help"])
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
assert "Usage: anta nrfu" in result.output
|
|
|
|
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
def test_anta_nrfu_wrong_subcommand(click_runner: CliRunner) -> None:
|
|
|
|
"""Test anta nrfu toast."""
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "oook"])
|
|
|
|
assert result.exit_code == ExitCode.USAGE_ERROR
|
|
|
|
assert "Usage: anta nrfu" in result.output
|
|
|
|
assert "No such command 'oook'." in result.output
|
|
|
|
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
def test_anta_nrfu(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test anta nrfu, catalog is given via env."""
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu"])
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
assert "ANTA Inventory contains 3 devices" in result.output
|
|
|
|
assert "Tests catalog contains 1 tests" in result.output
|
|
|
|
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
def test_anta_nrfu_dry_run(click_runner: CliRunner) -> None:
|
|
|
|
"""Test anta nrfu --dry-run, catalog is given via env."""
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--dry-run"])
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
assert "ANTA Inventory contains 3 devices" in result.output
|
|
|
|
assert "Tests catalog contains 1 tests" in result.output
|
|
|
|
assert "Dry-run" in result.output
|
|
|
|
|
|
|
|
|
2025-02-05 11:54:55 +01:00
|
|
|
def test_anta_nrfu_wrong_catalog_format(click_runner: CliRunner) -> None:
|
|
|
|
"""Test anta nrfu --dry-run, catalog is given via env."""
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--dry-run", "--catalog-format", "toto"])
|
|
|
|
assert result.exit_code == ExitCode.USAGE_ERROR
|
|
|
|
assert "Invalid value for '--catalog-format': 'toto' is not one of 'yaml', 'json'." in result.output
|
|
|
|
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
def test_anta_password_required(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test that password is provided."""
|
2025-02-05 11:54:55 +01:00
|
|
|
env = {"ANTA_PASSWORD": None}
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu"], env=env)
|
|
|
|
|
|
|
|
assert result.exit_code == ExitCode.USAGE_ERROR
|
|
|
|
assert "EOS password needs to be provided by using either the '--password' option or the '--prompt' option." in result.output
|
|
|
|
|
|
|
|
|
|
|
|
def test_anta_password(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test that password can be provided either via --password or --prompt."""
|
2025-02-05 11:54:55 +01:00
|
|
|
env = {"ANTA_PASSWORD": None}
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--password", "secret"], env=env)
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--prompt"], input="password\npassword\n", env=env)
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
|
|
|
|
def test_anta_enable_password(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test that enable password can be provided either via --enable-password or --prompt."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# Both enable and enable-password
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable", "--enable-password", "secret"])
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
# enable and prompt y
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable", "--prompt"], input="y\npassword\npassword\n")
|
|
|
|
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" in result.output
|
|
|
|
assert "Please enter a password to enter EOS privileged EXEC mode" in result.output
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
# enable and prompt N
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable", "--prompt"], input="N\n")
|
|
|
|
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" in result.output
|
|
|
|
assert "Please enter a password to enter EOS privileged EXEC mode" not in result.output
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable", "--enable-password", "blah", "--prompt"], input="y\npassword\npassword\n")
|
|
|
|
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" not in result.output
|
|
|
|
assert "Please enter a password to enter EOS privileged EXEC mode" not in result.output
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
# enabled-password without enable
|
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable-password", "blah"])
|
|
|
|
assert result.exit_code == ExitCode.USAGE_ERROR
|
|
|
|
assert "Providing a password to access EOS Privileged EXEC mode requires '--enable' option." in result.output
|
|
|
|
|
|
|
|
|
|
|
|
def test_anta_enable_alone(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test that enable can be provided either without enable-password."""
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--enable"])
|
|
|
|
assert result.exit_code == ExitCode.OK
|
|
|
|
|
|
|
|
|
|
|
|
def test_disable_cache(click_runner: CliRunner) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test that disable_cache is working on inventory."""
|
2025-02-05 11:32:35 +01:00
|
|
|
result = click_runner.invoke(anta, ["nrfu", "--disable-cache"])
|
|
|
|
stdout_lines = result.stdout.split("\n")
|
|
|
|
# All caches should be disabled from the inventory
|
|
|
|
for line in stdout_lines:
|
|
|
|
if "disable_cache" in line:
|
|
|
|
assert "True" in line
|
|
|
|
assert result.exit_code == ExitCode.OK
|
2025-02-05 11:54:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|