Skip to content

Commit

Permalink
Doc: Update test pattern in documentation (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc committed Jan 10, 2023
1 parent 630bff2 commit 85dca38
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions docs/usage-as-python-lib.md
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 comments on commit 85dca38

Please sign in to comment.