Skip to content

Result Manager module

ResultManager

ResultManager()

Helper to manage Test Results and generate reports.

Examples:

Create Inventory:

inventory_anta = AntaInventory.parse(
    inventory_file='examples/inventory.yml',
    username='ansible',
    password='ansible',
    timeout=0.5
)

Create Result Manager:

manager = ResultManager()

Run tests for all connected devices:

for device in inventory_anta.get_inventory():
    manager.add_test_result(
        VerifyNTP(device=device).test()
    )
    manager.add_test_result(
        VerifyEOSVersion(device=device).test(version='4.28.3M')
    )

Print result in native format:

manager.get_results()
[
    TestResult(
        host=IPv4Address('192.168.0.10'),
        test='VerifyNTP',
        result='failure',
        message="device is not running NTP correctly"
    ),
    TestResult(
        host=IPv4Address('192.168.0.10'),
        test='VerifyEOSVersion',
        result='success',
        message=None
    ),
]

The status of the class is initialized to “unset”

Then when adding a test with a status that is NOT ‘error’ the following table shows the updated status:

Current Status Added test Status Updated Status
unset Any Any
skipped unset, skipped skipped
skipped success success
skipped failure failure
success unset, skipped, success success
success failure failure
failure unset, skipped success, failure failure

If the status of the added test is error, the status is untouched and the error_status is set to True.

Source code in anta/result_manager/__init__.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def __init__(self) -> None:
    """
    Class constructor.

    The status of the class is initialized to "unset"

    Then when adding a test with a status that is NOT 'error' the following
    table shows the updated status:

    | Current Status |         Added test Status       | Updated Status |
    | -------------- | ------------------------------- | -------------- |
    |      unset     |              Any                |       Any      |
    |     skipped    |         unset, skipped          |     skipped    |
    |     skipped    |            success              |     success    |
    |     skipped    |            failure              |     failure    |
    |     success    |     unset, skipped, success     |     success    |
    |     success    |            failure              |     failure    |
    |     failure    | unset, skipped success, failure |     failure    |

    If the status of the added test is error, the status is untouched and the
    error_status is set to True.
    """
    logger.debug("Instantiate result-manager")
    self._result_entries = ListResult()
    # Initialize status
    self.status = "unset"
    self.error_status = False

__update_status

__update_status(test_status: str) -> None

Update ResultManager status based on the table above.

Source code in anta/result_manager/__init__.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def __update_status(self, test_status: str) -> None:
    """
    Update ResultManager status based on the table above.
    """
    if test_status not in RESULT_OPTIONS:
        raise ValueError("{test_status} is not a valid result option")
    if test_status == "error":
        self.error_status = True
        return

    if self.status == "unset":
        self.status = test_status
    elif self.status == "skipped" and test_status in {"success", "failure"}:
        self.status = test_status
    elif self.status == "success" and test_status == "failure":
        self.status = "failure"

add_test_result

add_test_result(entry: TestResult) -> None

Add a result to the list

Parameters:

Name Type Description Default
entry TestResult

TestResult data to add to the report

required
Source code in anta/result_manager/__init__.py
114
115
116
117
118
119
120
121
122
def add_test_result(self, entry: TestResult) -> None:
    """Add a result to the list

    Args:
        entry (TestResult): TestResult data to add to the report
    """
    logger.debug(entry)
    self._result_entries.append(entry)
    self.__update_status(entry.result)

add_test_results

add_test_results(entries: List[TestResult]) -> None

Add a list of results to the list

Parameters:

Name Type Description Default
entries List[TestResult]

list of TestResult data to add to the report

required
Source code in anta/result_manager/__init__.py
124
125
126
127
128
129
130
131
def add_test_results(self, entries: List[TestResult]) -> None:
    """Add a list of results to the list

    Args:
        entries (List[TestResult]): list of TestResult data to add to the report
    """
    for e in entries:
        self.add_test_result(e)

get_hosts

get_hosts() -> List[str]

Get list of IP addresses in current manager.

Returns:

Type Description
List[str]

List[str]: List of IP addresses.

Source code in anta/result_manager/__init__.py
216
217
218
219
220
221
222
223
224
225
226
227
def get_hosts(self) -> List[str]:
    """
    Get list of IP addresses in current manager.

    Returns:
        List[str]: List of IP addresses.
    """
    result_list = []
    for testcase in self._result_entries:
        if str(testcase.name) not in result_list:
            result_list.append(str(testcase.name))
    return result_list

get_result_by_host

get_result_by_host(
    host_ip: str, output_format: str = "native"
) -> Any

Get list of test result for a given host.

Parameters:

Name Type Description Default
host_ip str

IP Address of the host to use to filter results.

required
output_format str

format selector. Can be either native/list. Defaults to ‘native’.

'native'

Returns:

Name Type Description
Any Any

List of results related to the host.

Source code in anta/result_manager/__init__.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def get_result_by_host(self, host_ip: str, output_format: str = "native") -> Any:
    """
    Get list of test result for a given host.

    Args:
        host_ip (str): IP Address of the host to use to filter results.
        output_format (str, optional): format selector. Can be either native/list. Defaults to 'native'.

    Returns:
        Any: List of results related to the host.
    """
    if output_format == "list":
        return [result for result in self._result_entries if str(result.name) == host_ip]

    result_manager_filtered = ListResult()
    for result in self._result_entries:
        if str(result.name) == host_ip:
            result_manager_filtered.append(result)
    return result_manager_filtered

get_result_by_test

get_result_by_test(
    test_name: str, output_format: str = "native"
) -> Any

Get list of test result for a given test.

Parameters:

Name Type Description Default
test_name str

Test name to use to filter results

required
output_format str

format selector. Can be either native/list. Defaults to ‘native’.

'native'

Returns:

Type Description
Any

list[TestResult]: List of results related to the test.

Source code in anta/result_manager/__init__.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def get_result_by_test(self, test_name: str, output_format: str = "native") -> Any:
    """
    Get list of test result for a given test.

    Args:
        test_name (str): Test name to use to filter results
        output_format (str, optional): format selector. Can be either native/list. Defaults to 'native'.

    Returns:
        list[TestResult]: List of results related to the test.
    """
    if output_format == "list":
        return [result for result in self._result_entries if str(result.test) == test_name]

    result_manager_filtered = ListResult()
    for result in self._result_entries:
        if result.test == test_name:
            result_manager_filtered.append(result)
    return result_manager_filtered

get_results

get_results(output_format: str = 'native') -> Any

Expose list of all test results in different format

Support multiple format
  • native: ListResults format
  • list: a list of TestResult
  • json: a native JSON format

Parameters:

Name Type Description Default
output_format str

format selector. Can be either native/list/json. Defaults to ‘native’.

'native'

Returns:

Name Type Description
any Any

List of results.

Source code in anta/result_manager/__init__.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def get_results(self, output_format: str = "native") -> Any:
    """
    Expose list of all test results in different format

    Support multiple format:
      - native: ListResults format
      - list: a list of TestResult
      - json: a native JSON format

    Args:
        output_format (str, optional): format selector. Can be either native/list/json. Defaults to 'native'.

    Returns:
        any: List of results.
    """
    if output_format == "list":
        return list(self._result_entries)

    if output_format == "json":
        return json.dumps(pydantic_to_dict(self._result_entries), indent=4)

    # Default return for native format.
    return self._result_entries

get_status

get_status(ignore_error: bool = False) -> str

Returns the current status including error_status if ignore_error is False

Source code in anta/result_manager/__init__.py
133
134
135
136
137
def get_status(self, ignore_error: bool = False) -> str:
    """
    Returns the current status including error_status if ignore_error is False
    """
    return "error" if self.error_status and not ignore_error else self.status

get_testcases

get_testcases() -> List[str]

Get list of name of all test cases in current manager.

Returns:

Type Description
List[str]

List[str]: List of names for all tests.

Source code in anta/result_manager/__init__.py
203
204
205
206
207
208
209
210
211
212
213
214
def get_testcases(self) -> List[str]:
    """
    Get list of name of all test cases in current manager.

    Returns:
        List[str]: List of names for all tests.
    """
    result_list = []
    for testcase in self._result_entries:
        if str(testcase.test) not in result_list:
            result_list.append(str(testcase.test))
    return result_list

Last update: July 19, 2023