Skip to content

Commit

Permalink
test: Add tests (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc authored Mar 25, 2024
1 parent 94451b1 commit 23d706a
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 48 deletions.
92 changes: 78 additions & 14 deletions tests/units/anta_tests/test_field_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Test inputs for anta.tests.field_notices."""

from __future__ import annotations

from typing import Any
Expand All @@ -21,7 +20,9 @@
"modelName": "DCS-7280QRA-C36S",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-8.0.0-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-8.0.0-3255441"}
],
},
},
],
Expand All @@ -38,12 +39,17 @@
"modelName": "DCS-7280QRA-C36S",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-4.0.1-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-4.0.1-3255441"}
],
},
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["device is running incorrect version of aboot (4.0.1)"]},
"expected": {
"result": "failure",
"messages": ["device is running incorrect version of aboot (4.0.1)"],
},
},
{
"name": "failure-4.1",
Expand All @@ -55,12 +61,17 @@
"modelName": "DCS-7280QRA-C36S",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-4.1.0-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-4.1.0-3255441"}
],
},
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["device is running incorrect version of aboot (4.1.0)"]},
"expected": {
"result": "failure",
"messages": ["device is running incorrect version of aboot (4.1.0)"],
},
},
{
"name": "failure-6.0",
Expand All @@ -72,12 +83,17 @@
"modelName": "DCS-7280QRA-C36S",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-6.0.1-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-6.0.1-3255441"}
],
},
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["device is running incorrect version of aboot (6.0.1)"]},
"expected": {
"result": "failure",
"messages": ["device is running incorrect version of aboot (6.0.1)"],
},
},
{
"name": "failure-6.1",
Expand All @@ -89,12 +105,17 @@
"modelName": "DCS-7280QRA-C36S",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-6.1.1-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-6.1.1-3255441"}
],
},
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["device is running incorrect version of aboot (6.1.1)"]},
"expected": {
"result": "failure",
"messages": ["device is running incorrect version of aboot (6.1.1)"],
},
},
{
"name": "skipped-model",
Expand All @@ -106,12 +127,17 @@
"modelName": "vEOS-lab",
"details": {
"deviations": [],
"components": [{"name": "Aboot", "version": "Aboot-veos-8.0.0-3255441"}],
"components": [
{"name": "Aboot", "version": "Aboot-veos-8.0.0-3255441"}
],
},
},
],
"inputs": None,
"expected": {"result": "skipped", "messages": ["device is not impacted by FN044"]},
"expected": {
"result": "skipped",
"messages": ["device is not impacted by FN044"],
},
},
{
"name": "success-JPE",
Expand Down Expand Up @@ -207,7 +233,10 @@
},
],
"inputs": None,
"expected": {"result": "skipped", "messages": ["Platform is not impacted by FN072"]},
"expected": {
"result": "skipped",
"messages": ["Platform is not impacted by FN072"],
},
},
{
"name": "skipped-range-JPE",
Expand All @@ -225,6 +254,38 @@
"inputs": None,
"expected": {"result": "skipped", "messages": ["Device not exposed"]},
},
{
"name": "skipped-range-K-JPE",
"test": VerifyFieldNotice72Resolution,
"eos_data": [
{
"modelName": "DCS-7280SR3K-48YC8",
"serialNumber": "JPE2134000",
"details": {
"deviations": [],
"components": [{"name": "FixedSystemvrm1", "version": "5"}],
},
},
],
"inputs": None,
"expected": {"result": "skipped", "messages": ["Device not exposed"]},
},
{
"name": "skipped-range-JAS",
"test": VerifyFieldNotice72Resolution,
"eos_data": [
{
"modelName": "DCS-7280SR3-48YC8",
"serialNumber": "JAS2041000",
"details": {
"deviations": [],
"components": [{"name": "FixedSystemvrm1", "version": "5"}],
},
},
],
"inputs": None,
"expected": {"result": "skipped", "messages": ["Device not exposed"]},
},
{
"name": "skipped-range-K-JAS",
"test": VerifyFieldNotice72Resolution,
Expand Down Expand Up @@ -287,6 +348,9 @@
},
],
"inputs": None,
"expected": {"result": "error", "messages": ["Error in running test - FixedSystemvrm1 not found"]},
"expected": {
"result": "error",
"messages": ["Error in running test - FixedSystemvrm1 not found"],
},
},
]
11 changes: 7 additions & 4 deletions tests/units/cli/exec/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ async def dummy_collect(self: AntaDevice, command: AntaCommand) -> None:
command.output = per_device_command_output.get(self.name, "")

# Need to patch the child device class
with patch("anta.device.AsyncEOSDevice.collect", side_effect=dummy_collect, autospec=True) as mocked_collect, patch(
"anta.inventory.AntaInventory.connect_inventory",
side_effect=mock_connect_inventory,
) as mocked_connect_inventory:
with (
patch("anta.device.AsyncEOSDevice.collect", side_effect=dummy_collect, autospec=True) as mocked_collect,
patch(
"anta.inventory.AntaInventory.connect_inventory",
side_effect=mock_connect_inventory,
) as mocked_connect_inventory,
):
mocked_collect.side_effect = dummy_collect
await clear_counters_utils(test_inventory, tags=tags)

Expand Down
23 changes: 14 additions & 9 deletions tests/units/cli/get/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,20 @@ def mock_cvp_connect(_self: CvpClient, *_args: str, **_kwargs: str) -> None:
raise CvpApiError(msg="mocked CvpApiError")

# always get a token
with patch("anta.cli.get.commands.get_cv_token", return_value="dummy_token"), patch(
"cvprac.cvp_client.CvpClient.connect",
autospec=True,
side_effect=mock_cvp_connect,
) as mocked_cvp_connect, patch("cvprac.cvp_client.CvpApi.get_inventory", autospec=True, return_value=[]) as mocked_get_inventory, patch(
"cvprac.cvp_client.CvpApi.get_devices_in_container",
autospec=True,
return_value=[],
) as mocked_get_devices_in_container:
with (
patch("anta.cli.get.commands.get_cv_token", return_value="dummy_token"),
patch(
"cvprac.cvp_client.CvpClient.connect",
autospec=True,
side_effect=mock_cvp_connect,
) as mocked_cvp_connect,
patch("cvprac.cvp_client.CvpApi.get_inventory", autospec=True, return_value=[]) as mocked_get_inventory,
patch(
"cvprac.cvp_client.CvpApi.get_devices_in_container",
autospec=True,
return_value=[],
) as mocked_get_devices_in_container,
):
result = click_runner.invoke(anta, cli_args)

if not cvp_connect_failure:
Expand Down
17 changes: 16 additions & 1 deletion tests/units/cli/test__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import patch

from anta.cli import anta
import pytest

from anta.cli import anta, cli
from anta.cli.utils import ExitCode

if TYPE_CHECKING:
Expand Down Expand Up @@ -47,3 +50,15 @@ def test_anta_get_help(click_runner: CliRunner) -> None:
result = click_runner.invoke(anta, ["get", "--help"])
assert result.exit_code == ExitCode.OK
assert "Usage: anta get" in result.output


def test_uncaught_failure_anta(caplog: pytest.LogCaptureFixture) -> None:
"""Test uncaught failure when running ANTA cli."""
with (
pytest.raises(SystemExit) as e_info,
patch("anta.cli.anta", side_effect=ZeroDivisionError()),
):
cli()
assert "CRITICAL" in caplog.text
assert "Uncaught Exception when running ANTA CLI" in caplog.text
assert e_info.value.code == 1
Loading

0 comments on commit 23d706a

Please sign in to comment.