File: test__init__.py

package info (click to toggle)
anta 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,048 kB
  • sloc: python: 48,164; sh: 28; javascript: 9; makefile: 4
file content (145 lines) | stat: -rw-r--r-- 6,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Tests for anta.cli.nrfu."""

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


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


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


def test_anta_nrfu(click_runner: CliRunner) -> None:
    """Test anta nrfu, catalog is given via env."""
    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


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


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' (env var: 'ANTA_CATALOG_FORMAT'): 'toto' is not one of 'yaml', 'json'." in result.output


def test_anta_password_required(click_runner: CliRunner) -> None:
    """Test that password is provided."""
    env = {"ANTA_PASSWORD": None}
    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:
    """Test that password can be provided either via --password or --prompt."""
    env = {"ANTA_PASSWORD": None}
    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\n", env=env)
    assert result.exit_code == ExitCode.OK


def test_anta_enable_password(click_runner: CliRunner) -> None:
    """Test that enable password can be provided either via --enable-password or --prompt."""
    # 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\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\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:
    """Test that enable can be provided either without enable-password."""
    result = click_runner.invoke(anta, ["nrfu", "--enable"])
    assert result.exit_code == ExitCode.OK


def test_disable_cache(click_runner: CliRunner) -> None:
    """Test that disable_cache is working on inventory."""
    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


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