Skip to content
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

feat(anta.tests): Added testcase to verify domain name to IP address translation #534

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions anta/tests/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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 service settings
"""
from __future__ import annotations

from typing import List

from anta.models import AntaCommand, AntaTemplate, AntaTest

# Mypy does not understand AntaTest.Input typing
# mypy: disable-error-code=attr-defined


class VerifyDNSLookup(AntaTest):
"""
This class verifies the DNS(Domain name service) name to IP address translation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This class verifies the DNS(Domain name service) name to IP address translation.
This class verifies the DNS (Domain name service) name to IP address translation.


Expected Results:
* success: The test will pass if a domain name is resolved to an IP address.
* failure: The test will fail if a domain name does not resolve to an IP address.
* error: This test will error out if a domain name is not correct.
carl-baillargeon marked this conversation as resolved.
Show resolved Hide resolved
"""

name = "VerifyDNSLookup"
description = "Verifies the DNS(Domain name service) name to IP address translation."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description = "Verifies the DNS(Domain name service) name to IP address translation."
description = "Verifies the DNS (Domain name service) name to IP address translation."

categories = ["services"]
commands = [AntaTemplate(template="bash timeout 10 nslookup {domain}")]

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

domain_names: List[str]
"""List of domain names"""

def render(self, template: AntaTemplate) -> list[AntaCommand]:
return [template.render(domain=domain_name) for domain_name in self.inputs.domain_names]

@AntaTest.anta_test
def test(self) -> None:
self.result.is_success()
for command in self.instance_commands:
domain = command.params["domain"]
output = command.json_output["messages"][0]
if f"Can't find {domain}: No answer" in output:
carl-baillargeon marked this conversation as resolved.
Show resolved Hide resolved
self.result.is_failure(f"Domain {domain} is not resolved to an IP address.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the failed domains in a list or set and have a single failure message like: The following domain(s) are not resolved to an IP address: google.ca, arista.com

7 changes: 7 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ anta.tests.security:
encryption_algorithm: RSA
key_size: 4096

anta.tests.services:
- VerifyDNSLookup:
domain_names:
- arista.com
- www.google.com
- khatri.com

anta.tests.snmp:
- VerifySnmpStatus:
vrf: default
Expand Down
46 changes: 46 additions & 0 deletions tests/units/anta_tests/test_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.security.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tests for anta.tests.security.py
Tests for anta.tests.services.py

"""
from __future__ import annotations

from typing import Any

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

DATA: list[dict[str, Any]] = [
{
"name": "success",
"test": VerifyDNSLookup,
"eos_data": [
{
"messages": [
"Server:\t\t127.0.0.1\nAddress:\t127.0.0.1#53\n\nNon-authoritative answer:\nName:\tarista.com\nAddress: 151.101.130.132\nName:\tarista.com\n"
"Address: 151.101.2.132\nName:\tarista.com\nAddress: 151.101.194.132\nName:\tarista.com\nAddress: 151.101.66.132\n\n"
]
},
{"messages": ["Server:\t\t127.0.0.1\nAddress:\t127.0.0.1#53\n\nNon-authoritative answer:\nName:\twww.google.com\nAddress: 172.217.12.100\n\n"]},
],
"inputs": {"domain_names": ["arista.com", "www.google.com"]},
"expected": {"result": "success"},
},
{
"name": "failure",
"test": VerifyDNSLookup,
"eos_data": [
{
"messages": [
"Server:\t\t127.0.0.1\nAddress:\t127.0.0.1#53\n\nNon-authoritative answer:\nName:\tarista.com\nAddress: 151.101.130.132\nName:\tarista.com\n"
"Address: 151.101.2.132\nName:\tarista.com\nAddress: 151.101.194.132\nName:\tarista.com\nAddress: 151.101.66.132\n\n"
]
},
{"messages": ["Server:\t\t127.0.0.1\nAddress:\t127.0.0.1#53\n\nNon-authoritative answer:\nName:\twww.google.com\nAddress: 172.217.12.100\n\n"]},
{"messages": ["Server:\t\t127.0.0.1\nAddress:\t127.0.0.1#53\n\nNon-authoritative answer:\n*** Can't find khatri.com: No answer\n\n"]},
],
"inputs": {"domain_names": ["arista.com", "www.google.com", "khatri.com"]},
"expected": {"result": "failure", "messages": ["Domain khatri.com is not resolved to an IP address."]},
},
]