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

fix(anta)!: Make multiple tags filtering work properly (and fall pytest cleaning) #827

Merged
merged 42 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2a3012e
fix: typo in VerifyLoggingSourceIntf
mtache Sep 12, 2024
395dc2e
test: configure pytest-asyncio
mtache Sep 12, 2024
014997b
test: fix duplicate unit tests in tests/units/anta_tests
mtache Sep 12, 2024
d10e8e8
test: misc cleanup
mtache Sep 12, 2024
6ecc9dd
test: remove tests/data/json_data.py
mtache Sep 12, 2024
d6f598f
test: move tests/lib/anta.py
mtache Sep 12, 2024
25c8aa7
test: update conftest.py
mtache Sep 12, 2024
28640b2
refactor: move default_anta_env() from tests.lib.utils
mtache Sep 12, 2024
c961240
test: cleanup unused data
mtache Sep 12, 2024
0a195a2
refactor: tests/units/result_manager
mtache Sep 12, 2024
63d4558
refactor: tests/units/test_device.py
mtache Sep 12, 2024
fb6d780
chore: update gitignore
mtache Sep 12, 2024
a69d679
refactor: tests/units/test_catalog.py
mtache Sep 12, 2024
c35f184
refactor: cleanup pytest
mtache Sep 12, 2024
6b51132
refactor: addressing comments
mtache Sep 12, 2024
02441a0
refactor: cleanup outdated documentation
mtache Sep 13, 2024
70e89a0
refactor: tests/units/inventory
mtache Sep 13, 2024
58d22ad
refactor: tests/units/test_models.py
mtache Sep 13, 2024
ca065c8
refactor: moved fixtures in their own module
mtache Sep 13, 2024
0ec1114
fix: make Python<3.12 happy again
mtache Sep 13, 2024
86958bf
Ignore F401 in tests/units/anta_tests
mtache Sep 13, 2024
803bb8d
chore: disable various pylint rules
mtache Sep 13, 2024
b6a3ac8
docs: udpate contribution
mtache Sep 13, 2024
9e6a521
chore: disable other pylint rules
mtache Sep 13, 2024
25c0210
Fix tests/units/anta_tests
mtache Sep 13, 2024
f8ca8a7
Refactor: Oneliners for test data
gmuloc Sep 13, 2024
c1e0ec8
Apply suggestions from code review
gmuloc Sep 13, 2024
304b1d2
Update docs/contribution.md
gmuloc Sep 13, 2024
6e7caf2
Update tests/units/cli/conftest.py
gmuloc Sep 13, 2024
1bf8a29
test: add unit test for runner
mtache Sep 16, 2024
37c0f42
test: update error msg for Python < 3.12
mtache Sep 16, 2024
7fba8e9
Update tests/conftest.py
mtache Sep 16, 2024
25cd7a0
Update tests/units/test_runner.py
mtache Sep 16, 2024
53d9b09
test: add more unit tests
mtache Sep 16, 2024
33de90e
Merge branch 'main' into test/refactor
mtache Sep 16, 2024
abff55a
test: update tests/units/test_models.py
mtache Sep 17, 2024
427f72e
doc: update outdated doc
mtache Sep 17, 2024
dcb9533
Merge branch 'main' into test/refactor
gmuloc Sep 17, 2024
efa8498
Apply suggestions from code review
gmuloc Sep 17, 2024
1ae3433
WIP
gmuloc Sep 17, 2024
13c41e4
Merge pull request #1 from gmuloc/test/refactor
mtache Sep 18, 2024
f73b986
test: Fix issue in ci
gmuloc Sep 18, 2024
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
22 changes: 5 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
__pycache__
*.pyc
.pages
.coverage
.pytest_cache
.mypy_cache
.ruff_cache
.cache
build
dist
*.egg-info
Expand Down Expand Up @@ -46,14 +48,13 @@ htmlcov/
.tox/
.nox/
.coverage
coverage_html_report
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
mtache marked this conversation as resolved.
Show resolved Hide resolved
cover/
report.html

Expand Down Expand Up @@ -97,17 +98,4 @@ venv.bak/
/site

# VScode settings
.vscode
test.env
tech-support/
tech-support/*
2*

**/report.html
.*report.html

# direnv file
.envrc

clab-atd-anta/*
clab-atd-anta/
.vscode
15 changes: 0 additions & 15 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,13 @@
"ruff.configuration": "pyproject.toml",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"pylint.importStrategy": "fromEnvironment",
"pylint.severity": {
"refactor": "Warning"
},
"pylint.args": [
"--load-plugins",
"pylint_pydantic",
"--rcfile=pyproject.toml"
],
"python.testing.pytestArgs": [
"tests"
],
"autoDocstring.docstringFormat": "numpy",
"autoDocstring.includeName": false,
"autoDocstring.includeExtendedSummary": true,
"autoDocstring.startOnNewLine": true,
"autoDocstring.guessTypes": true,
"python.languageServer": "Pylance",
"githubIssues.issueBranchTitle": "issues/${issueNumber}-${issueTitle}",
"editor.formatOnPaste": true,
"files.trimTrailingWhitespace": true,
"mypy.configFile": "pyproject.toml",
"workbench.remoteIndicator.showExtensionRecommendations": true,

}
6 changes: 3 additions & 3 deletions anta/tests/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ class VerifyLoggingSourceIntf(AntaTest):
```
"""

name = "VerifyLoggingSourceInt"
name = "VerifyLoggingSourceIntf"
description = "Verifies logging source-interface for a specified VRF."
categories: ClassVar[list[str]] = ["logging"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show logging", ofmt="text")]

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

interface: str
"""Source-interface to use as source IP of log messages."""
Expand All @@ -115,7 +115,7 @@ class Input(AntaTest.Input):

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyLoggingSourceInt."""
"""Main test function for VerifyLoggingSourceIntf."""
output = self.instance_commands[0].text_output
pattern = rf"Logging source-interface '{self.inputs.interface}'.*VRF {self.inputs.vrf}"
if re.search(pattern, _get_logging_states(self.logger, output)):
Expand Down
2 changes: 1 addition & 1 deletion docs/contribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Test example for `anta.tests.system.VerifyUptime` AntaTest.

``` python
# Import the generic test function
from tests.lib.anta import test # noqa: F401
from tests.units.anta_tests import test # noqa: F401

# Import your AntaTest
from anta.tests.system import VerifyUptime
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ addopts = "-ra -q -vv --cov --cov-report term:skip-covered --color yes"
log_level = "WARNING"
render_collapsed = true
testpaths = ["tests"]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"
filterwarnings = [
"error",
# cvprac is raising the next warning
"default:pkg_resources is deprecated:DeprecationWarning",
# Need to investigate the following - only occuring when running the full pytest suite
Expand Down Expand Up @@ -337,7 +338,8 @@ select = ["ALL",
# "D417",
]
ignore = [
"ANN101", # Missing type annotation for `self` in method - we know what self is..
"D203", # Ignoring conflicting D* warnings - one-blank-line-before-class
"D213", # Ignoring conflicting D* warnings - multi-line-summary-second-line
mtache marked this conversation as resolved.
Show resolved Hide resolved
gmuloc marked this conversation as resolved.
Show resolved Hide resolved
"COM812", # Ignoring conflicting rules that may cause conflicts when used with the formatter
"ISC001", # Ignoring conflicting rules that may cause conflicts when used with the formatter
"TD002", # We don't have require authors in TODO
Expand Down Expand Up @@ -382,6 +384,7 @@ runtime-evaluated-base-classes = ["pydantic.BaseModel", "anta.models.AntaTest.In
"SLF001", # Lots of private member accessed for test purposes
]
"tests/units/*" = [
"ARG002", # Sometimes we need to declare unused arguments when a parameter is not used but declared in @pytest.mark.parametrize
"BLE001", # Do not catch blind exception: `Exception` - already disabling this in pylint
"FBT001", # Boolean-typed positional argument in function definition
"PLR0913", # Too many arguments to function call (8 > 5)
Expand Down
40 changes: 2 additions & 38 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
# 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.
"""conftest.py - used to store anta specific fixtures used for tests."""
"""See https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files."""

from __future__ import annotations

import logging
from typing import Any

import pytest

# Load fixtures from dedicated file tests/lib/fixture.py
# As well as pytest_asyncio plugin to test co-routines
pytest_plugins = [
"tests.lib.fixture",
"pytest_asyncio",
]

# Enable nice assert messages
# https://docs.pytest.org/en/7.1.x/how-to/writing_plugins.html#assertion-rewriting
pytest.register_assert_rewrite("tests.lib.anta")

# Placeholder to disable logging of some external libs
for _ in ("asyncio", "httpx"):
logging.getLogger(_).setLevel(logging.CRITICAL)


def build_test_id(val: dict[str, Any]) -> str:
"""Build id for a unit test of an AntaTest subclass.

{
"name": "meaniful test name",
"test": <AntaTest instance>,
...
}
"""
return f"{val['test'].module}.{val['test'].__name__}-{val['name']}"


def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
"""Generate ANTA testts unit tests dynamically during test collection.

It will parametrize test cases based on the `DATA` data structure defined in `tests.units.anta_tests` modules.
See `tests/units/anta_tests/README.md` for more information on how to use it.
Test IDs are generated using the `build_test_id` function above.

Checking that only the function "test" is parametrized with data to allow for writing tests for helper functions
in each module.
"""
if "tests.units.anta_tests" in metafunc.module.__package__ and metafunc.function.__name__ == "test":
# This is a unit test for an AntaTest subclass
metafunc.parametrize("data", metafunc.module.DATA, ids=build_test_id)
pytest.register_assert_rewrite("tests.unit.anta_tests")
Loading
Loading