8000 feat(anta): Implement AntaTest Abstract class for test definition by titom73 · Pull Request #173 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(anta): Implement AntaTest Abstract class for test definition #173

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 29 commits into from
Apr 27, 2023

Conversation

titom73
Copy link
Contributor
@titom73 titom73 commented Apr 19, 2023

Overview

Implement a virtual class to create EOS tests. It provides following features:

  • Metadata definition: name / description / categories / tags
  • Collect commands with option for format and API version
  • Exception management for collecting data from EOS
  • ASYNC mechanism

Example

Below is a test example based on PR change:

from anta.models import AntaTest, AntaTestCommand

class VerifyZeroTouch(AntaTest):
    """
    Verifies ZeroTouch is disabled.
    """

    name = "verify_zerotouch"
    description = "Verifies ZeroTouch is disabled."
    categories = ["configuration"]
    commands = [AntaTestCommand(command="show zerotouch")]

    @AntaTest.anta_test
    def test(self) -> None:
        command_output = self.instance_commands[0].output[0]
        assert isinstance(command_output, dict)
        if command_output["mode"] == "disabled":
            self.result.is_success()
        else:
            self.result.is_failure("ZTP is NOT disabled")

New classes for commands gathering

Commands can be abstracted to use dynamic parameters provided by tests catalog:

from anta.models import AntaTestDynamiCommand, AntaTestTemplate

# Run a command to get version
class RunArbitra
8000
ryCommand(AntaTest):

    """
    Run an EOS command and return result
    Based on AntaTest to build relevant output for pytest
    """

    name = "Run aributrary EOS command"
    description = "To be used only with anta debug commands"
    commands = [AntaTestCommand(command="show version")]
    categories = ["debug"]

    @AntaTest.anta_test
    def test(self) -> None:
        """
        Fake test function
        CLI should only call self.collect()
        """

def my_static_run():
    run_command = RunArbitraryCommand(device=device_anta)


# Dynamic command to run show interface <interface name>
class RunArbitraryTemplateCommand(AntaTest):

    """
    Run an EOS command and return result
    Based on AntaTest to build relevant output for pytest
    """

    name = "Run aributrary EOS command"
    description = "To be used only with anta debug commands"
    template = AntaTestTemplate(template='show interfaces {ifd}')
    categories = ["debug"]

    @AntaTest.anta_test
    def test(self) -> None:
        """
        Fake test function
        CLI should only call self.collect()
        """

def my_dynamic_run():
    params = [{'ifd': 'Ethernet1'}, {'ifd':'Ethernet2'}]
    run_command1 = RunArbitraryTemplateCommand(device_anta, params)

Todo

  • Adapt anta.runner.main to support new class management
  • Feature enhancements:
    • Move collect to InventoryDevice
    • Implement dynamic tests (command with parameters)
  • Linting and Typing

To be covered by other PRs

  • Convert existing tests
  • Update existing pytest coverage
  • Update documentation
  • Validate CLI

Side additions

Cli to collect EOS output

To help building pytest coverage, a new CLI has been created to collect command output based on AntaTest output. So it can be used in pytest as-is

# Command helps
anta debug run-cmd --help
Usage: anta debug run-cmd [OPTIONS]

  Run arbitrary command to an EOS device and get result using eAPI

Options:
  -c, --command TEXT             Command to run on EOS using eAPI  [required]
  --ofmt [text|json]             eAPI format to use. can be text or json
  --api-version, --version TEXT  Version of the command through eAPI
  -d, --device TEXT              Device from inventory to use  [required]
  --log-level, --log TEXT        Logging level of the command
  --help                         Show this message and exit.

# Example
$ anta debug run-cmd -c "show ip interface brief" --device <a device name from your inventory>
run command show ip interface brief on spine01
[
    {
        'interfaces': {
...
        }
    }
]

Cli to collect EOS command with template

To help building pytest coverage, a new CLI has been created to collect command output based on AntaTest output using template command:

❯ anta debug run-template --params '[{"ifd": "Ethernet1"}, {"ifd":"Ethernet2"}]' -d spine01 --template "show lldp neighbors {ifd}"
run dynmic command show lldp neighbors {ifd} with [{"ifd": "Ethernet1"}, {"ifd":"Ethernet2"}] on spine01
run_command = show lldp neighbors Ethernet1 spine01
{
  "tablesLastChangeTime": 1682499969.858846,
   ...
}
run_command = show lldp neighbors Ethernet2 spine01
{
  "tablesLastChangeTime": 1682499969.858847,
  ...
}

@titom73 titom73 added the framework-enhancement New feature or request label Apr 19, 2023
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

10000

@gmuloc gmuloc force-pushed the feat/anta-test-object branch from d1612ac to d52ccbf Compare April 24, 2023 15:11
@github-actions
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

@titom73 titom73 force-pushed the feat/anta-test-object branch from 1710e25 to 729ef94 Compare April 25, 2023 05:38
@titom73 titom73 marked this pull request as ready for review April 25, 2023 09:57
@github-actions
Copy link
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions
Copy link
Contributor

Conflicts have been resolved. A maintainer will review the pull request shortly.

@titom73
Copy link
Contributor Author
titom73 commented Apr 26, 2023

Ready for shipping and starting phase 2

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

Successfully merging this pull request may close these issues.

2 participants
0