Skip to content

ANTA catalog for VLAN tests

Module related to VLAN tests.

VerifyDynamicVlanSource

Verifies dynamic VLAN allocation for specified VLAN sources.

This test performs the following checks for each specified VLAN source:

  1. Validates source exists in dynamic VLAN table.
  2. Verifies at least one VLAN is allocated to the source.
  3. When strict mode is enabled (strict: true), ensures no other sources have VLANs allocated.
Expected Results
  • Success: The test will pass if all of the following conditions are met:
    • Each specified source exists in dynamic VLAN table.
    • Each specified source has at least one VLAN allocated.
    • In strict mode: No other sources have VLANs allocated.
  • Failure: The test will fail if any of the following conditions is met:
    • Specified source not found in configuration.
    • Source exists but has no VLANs allocated.
    • In strict mode: Non-specified sources have VLANs allocated.
Examples
anta.tests.vlan:
  - VerifyDynamicVlanSource:
      sources:
        - evpn
        - mlagsync
      strict: False

Inputs

Name Type Description Default
sources list[DynamicVlanSource]
The dynamic VLAN source list.
-
strict bool
If True, only specified sources are allowed to have VLANs allocated. Default is False.
False
Source code in anta/tests/vlan.py
 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
146
147
class VerifyDynamicVlanSource(AntaTest):
    """Verifies dynamic VLAN allocation for specified VLAN sources.

    This test performs the following checks for each specified VLAN source:

      1. Validates source exists in dynamic VLAN table.
      2. Verifies at least one VLAN is allocated to the source.
      3. When strict mode is enabled (`strict: true`), ensures no other sources have VLANs allocated.

    Expected Results
    ----------------
    * Success: The test will pass if all of the following conditions are met:
        - Each specified source exists in dynamic VLAN table.
        - Each specified source has at least one VLAN allocated.
        - In strict mode: No other sources have VLANs allocated.
    * Failure: The test will fail if any of the following conditions is met:
        - Specified source not found in configuration.
        - Source exists but has no VLANs allocated.
        - In strict mode: Non-specified sources have VLANs allocated.

    Examples
    --------
    ```yaml
    anta.tests.vlan:
      - VerifyDynamicVlanSource:
          sources:
            - evpn
            - mlagsync
          strict: False
    ```
    """

    categories: ClassVar[list[str]] = ["vlan"]
    commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show vlan dynamic", revision=1)]

    class Input(AntaTest.Input):
        """Input model for the VerifyDynamicVlanSource test."""

        sources: list[DynamicVlanSource]
        """The dynamic VLAN source list."""
        strict: bool = False
        """If True, only specified sources are allowed to have VLANs allocated. Default is False."""

    @AntaTest.anta_test
    def test(self) -> None:
        """Main test function for VerifyDynamicVlanSource."""
        self.result.is_success()
        command_output = self.instance_commands[0].json_output
        dynamic_vlans = command_output.get("dynamicVlans", {})

        # Get all configured sources and sources with VLANs allocated
        configured_sources = set(dynamic_vlans.keys())
        sources_with_vlans = {source for source, data in dynamic_vlans.items() if data.get("vlanIds")}
        expected_sources = set(self.inputs.sources)

        # Check if all specified sources exist in configuration
        missing_sources = expected_sources - configured_sources
        if missing_sources:
            self.result.is_failure(f"Dynamic VLAN source(s) not found in configuration: {', '.join(sorted(missing_sources))}")
            return

        # Check if configured sources have VLANs allocated
        sources_without_vlans = expected_sources - sources_with_vlans
        if sources_without_vlans:
            self.result.is_failure(f"Dynamic VLAN source(s) exist but have no VLANs allocated: {', '.join(sorted(sources_without_vlans))}")
            return

        # In strict mode, verify no other sources have VLANs allocated
        if self.inputs.strict:
            unexpected_sources = sources_with_vlans - expected_sources
            if unexpected_sources:
                self.result.is_failure(f"Strict mode enabled: Unexpected sources have VLANs allocated: {', '.join(sorted(unexpected_sources))}")

VerifyVlanInternalPolicy

Verifies 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.
Examples
anta.tests.vlan:
  - VerifyVlanInternalPolicy:
      policy: ascending
      start_vlan_id: 1006
      end_vlan_id: 4094

Inputs

Name Type Description Default
policy Literal['ascending', 'descending']
The VLAN internal allocation policy. Supported values: ascending, descending.
-
start_vlan_id Vlan
The starting VLAN ID in the range.
-
end_vlan_id Vlan
The ending VLAN ID in the range.
-
Source code in anta/tests/vlan.py
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
class VerifyVlanInternalPolicy(AntaTest):
    """Verifies 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.

    Examples
    --------
    ```yaml
    anta.tests.vlan:
      - VerifyVlanInternalPolicy:
          policy: ascending
          start_vlan_id: 1006
          end_vlan_id: 4094
    ```
    """

    description = "Verifies the VLAN internal allocation policy and the range of VLANs."
    categories: ClassVar[list[str]] = ["vlan"]
    commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show vlan internal allocation policy", revision=1)]

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

        policy: Literal["ascending", "descending"]
        """The VLAN internal allocation policy. Supported values: ascending, descending."""
        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:
        """Main test function for VerifyVlanInternalPolicy."""
        self.result.is_success()
        command_output = self.instance_commands[0].json_output

        if (policy := self.inputs.policy) != (act_policy := get_value(command_output, "policy")):
            self.result.is_failure(f"Incorrect VLAN internal allocation policy configured - Expected: {policy} Actual: {act_policy}")
            return

        if (start_vlan_id := self.inputs.start_vlan_id) != (act_vlan_id := get_value(command_output, "startVlanId")):
            self.result.is_failure(
                f"VLAN internal allocation policy: {self.inputs.policy} - Incorrect start VLAN id configured - Expected: {start_vlan_id} Actual: {act_vlan_id}"
            )

        if (end_vlan_id := self.inputs.end_vlan_id) != (act_vlan_id := get_value(command_output, "endVlanId")):
            self.result.is_failure(
                f"VLAN internal allocation policy: {self.inputs.policy} - Incorrect end VLAN id configured - Expected: {end_vlan_id} Actual: {act_vlan_id}"
            )