8000 feat(anta.tests): Added testcase to verify vlan internal allocation policy by MaheshGSLAB · Pull Request #528 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(anta.tests): Added testcase to verify vlan internal allocation policy #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions anta/tests/vlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 VLAN
"""
# Mypy does not understand AntaTest.Input typing
# mypy: disable-error-code=attr-defined

from typing import Literal

from anta.custom_types import Vlan
from anta.models import AntaCommand, AntaTest
from anta.tools.get_value import get_value
from anta.tools.utils import get_failed_logs


class VerifyVlanInternalPolicy(AntaTest):
"""
This class checks if the VLAN internal allocation policy is ascending or descending and
if the VLANs are within the specified range.

Expected Results:
* Success: The test will pass if the VLAN internal allocation policy is either ascending or descending
and the VLANs are within the specified range.
* Failure: The test will fail if the VLAN internal allocation policy is neither ascending nor descending
or the VLANs are outside the specified range.
"""

name = "VerifyVlanInternalPolicy"
description = "This test checks the VLAN internal allocation policy and the range of VLANs."
categories = ["vlan"]
commands = [AntaCommand(command="show vlan internal allocation policy")]

class Input(AntaTest.Input):
"""Inputs for the VerifyVlanInternalPolicy test."""

policy: Literal["ascending", "descending"]
"""The VLAN internal allocation policy."""
start_vlan_id: Vlan
"""The starting VLAN ID in the range."""
end_vlan_id: Vlan
"""The ending VLAN ID in the range."""

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

keys_to_verify = ["policy", "startVlanId", "endVlanId"]
actual_policy_output = {key: get_value(command_output, key) for key in keys_to_verify}
expected_policy_output = {"policy": self.inputs.policy, "startVlanId": self.inputs.start_vlan_id, "endVlanId": self.inputs.end_vlan_id}

# Check if the actual output matches the expected output
if actual_policy_output != expected_policy_output:
failed_log = "The VLAN internal allocation policy is not configured properly:"
failed_log += get_failed_logs(expected_policy_output, actual_policy_output)
self.result.is_failure(failed_log)
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 @@ -27,6 +27,7 @@ This section describes all the available tests provided by ANTA package.
- [Software](tests.software.md)
- [STP](tests.stp.md)
- [System](tests.system.md)
- [VLAN](tests.vlan.md)
- [VXLAN](tests.vxlan.md)


Expand Down
13 changes: 13 additions & 0 deletions docs/api/tests.vlan.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 vlan tests

::: anta.tests.vlan
options:
show_root_heading: false
show_root_toc_entry: false
merge_init_into_class: false
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ anta.tests.system:
- VerifyFileSystemUtilization:
- VerifyNTP:

anta.tests.vlan:
- VerifyVlanInternalPolicy:
policy: ascending
start_vlan_id: 1006
end_vlan_id: 4094

anta.tests.vxlan:
- VerifyVxlan1Interface:
- VerifyVxlanConfigSanity:
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ nav:
- Software: api/tests.software.md
- System: api/tests.system.md
- VXLAN: api/tests.vxlan.md
- VLAN: api/tests.vlan.md
- API Documentation:
- Inventory:
- Inventory module: api/inventory.md
Expand Down
37 changes: 37 additions & 0 deletions tests/units/anta_tests/test_vlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.vlan.py
"""
from __future__ import annotations

from typing import Any

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

DATA: list[dict[str, Any]] = [
{
"name": "success",
"test": VerifyVlanInternalPolicy,
"eos_data": [{"policy": "ascending", "startVlanId": 1006, "endVlanId": 4094}],
"inputs": {"policy": "ascending", "start_vlan_id": 1006, "end_vlan_id": 4094},
"expected": {"result": "success"},
},
{
"name": "failure-incorrect-policy",
"test": VerifyVlanInternalPolicy,
"eos_data": [{"policy": "descending", "startVlanId": 4094, "endVlanId": 1006}],
"inputs": {"policy": "ascending", "start_vlan_id": 1006, "end_vlan_id": 4094},
"expected": {
"result": "failure",
"messages": [
"The VLAN internal allocation policy is not configured properly:\n"
"Expected `ascending` as the policy, but found `descending` instead.\n"
"Expected `1006` as the startVlanId, but found `4094` instead.\n"
"Expected `4094` as the endVlanId, but found `1006` instead."
],
},
},
]
0