8000 doc: Update test pattern in documentation by gmuloc · Pull Request #126 · aristanetworks/anta · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

doc: Update test pattern in documentation #126

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 3 commits into from
Jan 10, 2023
Merged
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
36 changes: 19 additions & 17 deletions docs/usage-as-python-lib.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ All tests return a TestResult structure with the following elements:
All tests are based on this structure:

```python
def <test name>(device: InventoryDevice, <list of args>) -> TestResult:
from anta.inventory.models import InventoryDevice
from anta.result_manager.models import TestResult
from anta.test import anta_test

# Use the decorator that wraps the function and inject result argument
@anta_test
async def <test name>(device: InventoryDevice, result: TestResut, <list of args>, minimum: int) -> TestResult:
"""
dosctring desccription

Args:
device (InventoryDevice): InventoryDevice instance containing all devices information.
result (TestResult): TestResult instance for the test, injected
automatically by the anta_test decorator.
minimum (int): example of test with int parameter

Returns:
TestResult instance with
Expand All @@ -113,26 +122,19 @@ def <test name>(device: InventoryDevice, <list of args>) -> TestResult:
* result = "error" if any exception is caught

"""
function_name = inspect.stack()[0][3]

# Test if options are valid (optional)
if not minimum:
result.is_skipped("verify_uptime was not run as no minimum were given")
return result
# Try to connect and execute command on device
try:
response = device.session.runCmds(1, ["show uptime"], "json")
logger.debug(f'query result is: {response}')
response_data = response[0]["upTime"]
# Check conditions
# ...

# Capture any exception to return failure reason
except (jsonrpc.AppError, KeyError, socket.timeout) as e:
logger.error(
f'exception raised for \
{inspect.stack()[0][3]} - {device.host}: {str(e)}')
result.is_error(str(e))

# Use await for the remote device call
response = await device.session.cli(command="show uptime", ofmt="json")
# Add a debug log entry
logger.debug(f'query result is: {response}')

response_data = response["upTime"]
# Check conditions on response_data
# ...

# Return data to caller
return result
Expand Down
0