Skip to content

Commit

Permalink
feat(anta.tests): Added the testcase to verify hostname, location and…
Browse files Browse the repository at this point in the history
… contact (#523)

* issue-521: Added the testcase to verify hostname, location and contact details

* issue-521: updated snmp testcases

* issue-521: updated categories

* issue-521: updated doc string

* issue-521: updated doc string for testcase
  • Loading branch information
MaheshGSLAB authored Feb 7, 2024
1 parent ec8c171 commit 6e26ba4
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 1 deletion.
40 changes: 40 additions & 0 deletions anta/tests/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.
"""
Test functions related to the EOS various services settings
"""
from __future__ import annotations

# Mypy does not understand AntaTest.Input typing
# mypy: disable-error-code=attr-defined
from anta.models import AntaCommand, AntaTest


class VerifyHostname(AntaTest):
"""
This class verifies the hostname of a device.
Expected results:
* success: The test will pass if the hostname matches the provided input.
* failure: The test will fail if the hostname does not match the provided input.
"""

name = "VerifyHostname"
description = "Verifies the hostname of a device."
categories = ["services"]
commands = [AntaCommand(command="show hostname")]

class Input(AntaTest.Input):
"""Defines the input parameters for this test case."""

hostname: str
"""Expected hostname of the device."""

@AntaTest.anta_test
def test(self) -> None:
hostname = self.instance_commands[0].json_output["hostname"]

if hostname != self.inputs.hostname:
self.result.is_failure(f"Expected `{self.inputs.hostname}` as the hostname, but found `{hostname}` instead.")
else:
self.result.is_success()
60 changes: 60 additions & 0 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,63 @@ def test(self) -> None:
self.result.is_failure(f"SNMP IPv6 ACL(s) not configured or active in vrf {self.inputs.vrf}: {not_configured_acl_list}")
else:
self.result.is_success()


class VerifySnmpLocation(AntaTest):
"""
This class verifies the SNMP location of a device.
Expected results:
* success: The test will pass if the SNMP location matches the provided input.
* failure: The test will fail if the SNMP location does not match the provided input.
"""

name = "VerifySnmpLocation"
description = "Verifies the SNMP location of a device."
categories = ["snmp"]
commands = [AntaCommand(command="show snmp")]

class Input(AntaTest.Input):
"""Defines the input parameters for this test case."""

location: str
"""Expected SNMP location of the device."""

@AntaTest.anta_test
def test(self) -> None:
location = self.instance_commands[0].json_output["location"]["location"]

if location != self.inputs.location:
self.result.is_failure(f"Expected `{self.inputs.location}` as the location, but found `{location}` instead.")
else:
self.result.is_success()


class VerifySnmpContact(AntaTest):
"""
This class verifies the SNMP contact of a device.
Expected results:
* success: The test will pass if the SNMP contact matches the provided input.
* failure: The test will fail if the SNMP contact does not match the provided input.
"""

name = "VerifySnmpContact"
description = "Verifies the SNMP contact of a device."
categories = ["snmp"]
commands = [AntaCommand(command="show snmp")]

class Input(AntaTest.Input):
"""Defines the input parameters for this test case."""

contact: str
"""Expected SNMP contact details of the device."""

@AntaTest.anta_test
def test(self) -> None:
contact = self.instance_commands[0].json_output["contact"]["contact"]

if contact != self.inputs.contact:
self.result.is_failure(f"Expected `{self.inputs.contact}` as the contact, but found `{contact}` instead.")
else:
self.result.is_success()
1 change: 1 addition & 0 deletions docs/api/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This section describes all the available tests provided by ANTA package.
- [Routing BGP](tests.routing.bgp.md)
- [Routing OSPF](tests.routing.ospf.md)
- [Security](tests.security.md)
- [Services](tests.services.md)
- [SNMP](tests.snmp.md)
- [Software](tests.software.md)
- [STP](tests.stp.md)
Expand Down
13 changes: 13 additions & 0 deletions docs/api/tests.services.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
~ 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.
-->

# ANTA catalog for services tests

::: anta.tests.services
options:
show_root_heading: false
show_root_toc_entry: false
merge_init_into_class: false
8 changes: 8 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ anta.tests.security:
common_name: Arista Networks Internal IT Root Cert Authority
encryption_algorithm: RSA
key_size: 4096

anta.tests.services:
- VerifyBannerLogin:
login_banner: |
# Copyright (c) 2023-2024 Arista Networks, Inc.
Expand All @@ -237,6 +239,8 @@ anta.tests.security:
# 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.
- VerifyHostname:
hostname: s1-spine1

anta.tests.snmp:
- VerifySnmpStatus:
Expand All @@ -247,6 +251,10 @@ anta.tests.snmp:
- VerifySnmpIPv6Acl:
number: 3
vrf: default
- VerifySnmpLocation:
location: New York
- VerifySnmpContact:
contact: [email protected]

anta.tests.software:
- VerifyEOSVersion:
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ nav:
- BGP: api/tests.routing.bgp.md
- OSPF: api/tests.routing.ospf.md
- Security: api/tests.security.md
- Services: api/tests.services.md
- SNMP: api/tests.snmp.md
- STP: api/tests.stp.md
- Software: api/tests.software.md
Expand Down
32 changes: 32 additions & 0 deletions tests/units/anta_tests/test_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.
"""
Tests for anta.tests.services.py
"""
from __future__ import annotations

from typing import Any

from anta.tests.services import VerifyHostname
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611

DATA: list[dict[str, Any]] = [
{
"name": "success",
"test": VerifyHostname,
"eos_data": [{"hostname": "s1-spine1", "fqdn": "s1-spine1.fun.aristanetworks.com"}],
"inputs": {"hostname": "s1-spine1"},
"expected": {"result": "success"},
},
{
"name": "failure-incorrect-hostname",
"test": VerifyHostname,
"eos_data": [{"hostname": "s1-spine2", "fqdn": "s1-spine1.fun.aristanetworks.com"}],
"inputs": {"hostname": "s1-spine1"},
"expected": {
"result": "failure",
"messages": ["Expected `s1-spine1` as the hostname, but found `s1-spine2` instead."],
},
},
]
52 changes: 51 additions & 1 deletion tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from typing import Any

from anta.tests.snmp import VerifySnmpIPv4Acl, VerifySnmpIPv6Acl, VerifySnmpStatus
from anta.tests.snmp import VerifySnmpContact, VerifySnmpIPv4Acl, VerifySnmpIPv6Acl, VerifySnmpLocation, VerifySnmpStatus
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -75,4 +75,54 @@
"inputs": {"number": 1, "vrf": "MGMT"},
"expected": {"result": "failure", "messages": ["SNMP IPv6 ACL(s) not configured or active in vrf MGMT: ['ACL_IPV6_SNMP']"]},
},
{
"name": "success",
"test": VerifySnmpLocation,
"eos_data": [
{
"location": {"location": "New York"},
}
],
"inputs": {"location": "New York"},
"expected": {"result": "success"},
},
{
"name": "failure-incorrect-location",
"test": VerifySnmpLocation,
"eos_data": [
{
"location": {"location": "Europe"},
}
],
"inputs": {"location": "New York"},
"expected": {
"result": "failure",
"messages": ["Expected `New York` as the location, but found `Europe` instead."],
},
},
{
"name": "success",
"test": VerifySnmpContact,
"eos_data": [
{
"contact": {"contact": "[email protected]"},
}
],
"inputs": {"contact": "[email protected]"},
"expected": {"result": "success"},
},
{
"name": "failure-incorrect-contact",
"test": VerifySnmpContact,
"eos_data": [
{
"contact": {"contact": "[email protected]"},
}
],
"inputs": {"contact": "[email protected]"},
"expected": {
"result": "failure",
"messages": ["Expected `[email protected]` as the contact, but found `[email protected]` instead."],
},
},
]

0 comments on commit 6e26ba4

Please sign in to comment.