Skip to content

Instantly share code, notes, and snippets.

@titom73
Last active November 13, 2023 16:37
Show Gist options
  • Save titom73/c0d3c9fc77e4b44a2bab7ab47eed8286 to your computer and use it in GitHub Desktop.
Save titom73/c0d3c9fc77e4b44a2bab7ab47eed8286 to your computer and use it in GitHub Desktop.
Anta Hackathon Solution
"""
Test functions related to system-level features and protocols
"""
## Please refer to https://github.com/titom73/atd-anta-demo/blob/main/docs/demo-tests.md for complete description
from typing import Optional
from anta.models import AntaTest, AntaCommand
class VerifyLagState(AntaTest):
name = "VerifyLagState"
description = "Check LAG are running LACP and are in Collecting and Distributing"
categories = ["custom_system"]
commands = [AntaCommand(command="show lacp interface")]
class Input(AntaTest.Input):
ifd: str
@AntaTest.anta_test
def test(self) -> None:
ifd = self.inputs.ifd
command_output = self.instance_commands[0].json_output
self.result.is_success()
# Test logic
ifd_found = False
for po, po_data in command_output['portChannels'].items():
if ifd in po_data['interfaces'].keys():
ifd_found = True
data = po_data['interfaces'][ifd]
if not data['actorPortState']['collecting'] or not data['actorPortState']['distributing']:
self.result.is_failure(f'Interface {ifd} is not in correct state: {data["actorPortState"]}')
if not ifd_found:
self.result.is_failure(f'interface {ifd} was not found')
from typing import Optional, List
from pydantic import BaseModel
from anta.models import AntaTest, AntaCommand, AntaTemplate
class VerifyRouteSummary(AntaTest):
name = "VerifyRouteSummary"
description = "Check number of static routes in a given VRF"
categories = ["custom_system"]
commands = [AntaTemplate(template="show ip route vrf {vrf} summary")]
class Input(AntaTest.Input):
class Vrf(BaseModel): # BaseModel is a pydantic model
vrf: str
connected: int
vrfs: List[Vrf]
def render(self, template: AntaTemplate) -> list[AntaCommand]:
return [template.render(vrf=vrf.vrf) for vrf in self.inputs.vrfs]
@AntaTest.anta_test
def test(self) -> None:
vrfs = self.inputs.vrfs
command_output = self.instance_commands[0].json_output
self.result.is_success()
for vrf in self.inputs.vrfs:
if command_output['vrfs'][vrf.vrf]['connected'] != vrf.connected:
self.result.is_error(f'Device has an incorrect number of connected routes: expected: {vrf.connected} / configured: {command_output["vrfs"][vrf.vrf]["connected"]}')
---
anta_inventory:
hosts:
- host: 192.168.0.10
name: s1-spine1
tags: ['spine']
- host: 192.168.0.11
name: s1-spine2
tags: ['spine']
- host: 192.168.0.12
name: s1-leaf1
tags: ['leaf']
- host: 192.168.0.13
name: s1-leaf2
tags: ['leaf']
- host: 192.168.0.14
name: s1-leaf3
tags: ['leaf']
- host: 192.168.0.15
name: s1-leaf4
tags: ['leaf']
---
anta.tests.interfaces:
- VerifyInterfaceUtilization:
- VerifyInterfaceErrors:
- VerifyInterfaceDiscards:
- VerifyInterfaceErrDisabled:
- VerifyInterfacesStatus:
interfaces:
- interface: Port-Channel4
state: up
protocol_status: up
- interface: Ethernet4
state: up
protocol_status: up
# filters:
# tags: ['leaf']
# anta_custom.dummy:
# - VerifyLagState:
# ifd: Ethernet4
# - VerifyLagState:
# ifd: Ethernet1
# - VerifyRouteSummary:
# vrfs:
# - vrf: Tenant_A_OP_Zone
# connected: 3
# filters:
# tags: ['leaf']
@titom73
Copy link
Author

titom73 commented Nov 11, 2023

Known issue in setup

  • Incorrect password in .envrc: export ANTA_USERNAME=ansible instead of arista
  • [ ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment