| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Unit tests for the asynceapi._models module.""" |
| 5 |
|
| 6 |
import logging |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
from uuid import UUID |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from asynceapi._constants import EapiCommandFormat |
| 13 |
from asynceapi._errors import EapiReponseError |
| 14 |
from asynceapi._models import EapiCommandResult, EapiRequest, EapiResponse |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from asynceapi._types import EapiComplexCommand, EapiSimpleCommand |
| 18 |
|
| 19 |
|
| 20 |
class TestEapiRequest: |
| 21 |
"""Test cases for the EapiRequest class.""" |
| 22 |
|
| 23 |
def test_init_with_defaults(self) -> None: |
| 24 |
"""Test initialization with just required parameters.""" |
| 25 |
commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version", "show interfaces"] |
| 26 |
req = EapiRequest(commands=commands) |
| 27 |
|
| 28 |
|
| 29 |
assert req.commands == commands |
| 30 |
|
| 31 |
|
| 32 |
assert req.version == "latest" |
| 33 |
assert req.format == EapiCommandFormat.JSON |
| 34 |
assert req.timestamps is False |
| 35 |
assert req.auto_complete is False |
| 36 |
assert req.expand_aliases is False |
| 37 |
assert req.stop_on_error is True |
| 38 |
|
| 39 |
|
| 40 |
try: |
| 41 |
UUID(str(req.id)) |
| 42 |
is_valid_uuid = True |
| 43 |
except ValueError: |
| 44 |
is_valid_uuid = False |
| 45 |
assert is_valid_uuid |
| 46 |
|
| 47 |
def test_init_with_custom_values(self) -> None: |
| 48 |
"""Test initialization with custom parameter values.""" |
| 49 |
commands: list[EapiSimpleCommand | EapiComplexCommand] = [{"cmd": "enable", "input": "password"}, "show version"] |
| 50 |
req = EapiRequest( |
| 51 |
commands=commands, |
| 52 |
version=1, |
| 53 |
format=EapiCommandFormat.TEXT, |
| 54 |
timestamps=True, |
| 55 |
auto_complete=True, |
| 56 |
expand_aliases=True, |
| 57 |
stop_on_error=False, |
| 58 |
id="custom-id-123", |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
assert req.commands == commands |
| 63 |
assert req.version == 1 |
| 64 |
assert req.format == EapiCommandFormat.TEXT |
| 65 |
assert req.timestamps is True |
| 66 |
assert req.auto_complete is True |
| 67 |
assert req.expand_aliases is True |
| 68 |
assert req.stop_on_error is False |
| 69 |
assert req.id == "custom-id-123" |
| 70 |
|
| 71 |
def test_to_jsonrpc(self) -> None: |
| 72 |
"""Test conversion to JSON-RPC dictionary.""" |
| 73 |
commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version", "show interfaces"] |
| 74 |
req = EapiRequest(commands=commands, version=1, format=EapiCommandFormat.TEXT, id="test-id-456") |
| 75 |
|
| 76 |
jsonrpc = req.to_jsonrpc() |
| 77 |
|
| 78 |
|
| 79 |
assert jsonrpc["jsonrpc"] == "2.0" |
| 80 |
assert jsonrpc["method"] == "runCmds" |
| 81 |
assert jsonrpc.get("id") == "test-id-456" |
| 82 |
|
| 83 |
|
| 84 |
params = jsonrpc["params"] |
| 85 |
assert params.get("version") == 1 |
| 86 |
assert params["cmds"] == commands |
| 87 |
assert params.get("format") == EapiCommandFormat.TEXT |
| 88 |
assert params.get("timestamps") is False |
| 89 |
assert params.get("autoComplete") is False |
| 90 |
assert params.get("expandAliases") is False |
| 91 |
assert params.get("stopOnError") is True |
| 92 |
|
| 93 |
def test_to_jsonrpc_with_complex_commands(self) -> None: |
| 94 |
"""Test JSON-RPC conversion with complex commands.""" |
| 95 |
commands: list[EapiSimpleCommand | EapiComplexCommand] = [ |
| 96 |
{"cmd": "enable", "input": "password"}, |
| 97 |
{"cmd": "configure", "input": ""}, |
| 98 |
{"cmd": "hostname test-device"}, |
| 99 |
] |
| 100 |
req = EapiRequest(commands=commands) |
| 101 |
|
| 102 |
jsonrpc = req.to_jsonrpc() |
| 103 |
|
| 104 |
|
| 105 |
assert jsonrpc["params"]["cmds"] == commands |
| 106 |
|
| 107 |
def test_immutability(self) -> None: |
| 108 |
"""Test that the dataclass is truly immutable (frozen).""" |
| 109 |
commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version"] |
| 110 |
req = EapiRequest(commands=commands) |
| 111 |
|
| 112 |
|
| 113 |
with pytest.raises(AttributeError): |
| 114 |
req.commands = ["new command"] |
| 115 |
|
| 116 |
with pytest.raises(AttributeError): |
| 117 |
req.id = "new-id" |
| 118 |
|
| 119 |
|
| 120 |
class TestEapiResponse: |
| 121 |
"""Test cases for the EapiResponse class.""" |
| 122 |
|
| 123 |
def test_init_and_properties(self) -> None: |
| 124 |
"""Test basic initialization and properties.""" |
| 125 |
|
| 126 |
result1 = EapiCommandResult(command="show version", output={"version": "4.33.2F-40713977.4332F (engineering build)"}) |
| 127 |
result2 = EapiCommandResult(command="show hostname", output={"hostname": "DC1-LEAF1A"}) |
| 128 |
|
| 129 |
|
| 130 |
results = {0: result1, 1: result2} |
| 131 |
response = EapiResponse(request_id="test-123", _results=results) |
| 132 |
|
| 133 |
|
| 134 |
assert response.request_id == "test-123" |
| 135 |
assert response.error_code is None |
| 136 |
assert response.error_message is None |
| 137 |
|
| 138 |
|
| 139 |
assert response.success is True |
| 140 |
assert len(response.results) == 2 |
| 141 |
assert response.results[0] == result1 |
| 142 |
assert response.results[1] == result2 |
| 143 |
|
| 144 |
def test_error_response(self) -> None: |
| 145 |
"""Test initialization with error information.""" |
| 146 |
result = EapiCommandResult(command="show bad command", output=None, errors=["Invalid input (at token 1: 'bad')"], success=False) |
| 147 |
results = {0: result} |
| 148 |
response = EapiResponse( |
| 149 |
request_id="test-456", _results=results, error_code=1002, error_message="CLI command 1 of 1 'show bad command' failed: invalid command" |
| 150 |
) |
| 151 |
|
| 152 |
assert response.request_id == "test-456" |
| 153 |
assert response.error_code == 1002 |
| 154 |
assert response.error_message == "CLI command 1 of 1 'show bad command' failed: invalid command" |
| 155 |
assert response.success is False |
| 156 |
assert len(response.results) == 1 |
| 157 |
assert response.results[0].success is False |
| 158 |
assert "Invalid input (at token 1: 'bad')" in response.results[0].errors |
| 159 |
|
| 160 |
def test_len_and_iteration(self) -> None: |
| 161 |
"""Test __len__ and __iter__ methods.""" |
| 162 |
|
| 163 |
results = { |
| 164 |
0: EapiCommandResult(command="cmd1", output="out1"), |
| 165 |
1: EapiCommandResult(command="cmd2", output="out2"), |
| 166 |
2: EapiCommandResult(command="cmd3", output="out3"), |
| 167 |
} |
| 168 |
response = EapiResponse(request_id="test-789", _results=results) |
| 169 |
|
| 170 |
|
| 171 |
assert len(response) == 3 |
| 172 |
|
| 173 |
|
| 174 |
iterated_results = list(response) |
| 175 |
assert len(iterated_results) == 3 |
| 176 |
assert [r.command for r in iterated_results] == ["cmd1", "cmd2", "cmd3"] |
| 177 |
|
| 178 |
def test_from_jsonrpc_success(self) -> None: |
| 179 |
"""Test from_jsonrpc with successful response.""" |
| 180 |
|
| 181 |
request = EapiRequest(commands=["show version", "show hostname"], format=EapiCommandFormat.JSON) |
| 182 |
|
| 183 |
|
| 184 |
jsonrpc_response = { |
| 185 |
"jsonrpc": "2.0", |
| 186 |
"id": "test-id-123", |
| 187 |
"result": [{"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}, {"hostname": "DC1-LEAF1A"}], |
| 188 |
} |
| 189 |
|
| 190 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 191 |
|
| 192 |
|
| 193 |
assert response.request_id == "test-id-123" |
| 194 |
assert response.success is True |
| 195 |
assert response.error_code is None |
| 196 |
assert response.error_message is None |
| 197 |
|
| 198 |
|
| 199 |
assert len(response) == 2 |
| 200 |
assert response.results[0].command == "show version" |
| 201 |
assert response.results[0].output == {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"} |
| 202 |
assert response.results[0].success is True |
| 203 |
assert response.results[1].command == "show hostname" |
| 204 |
assert response.results[1].output == {"hostname": "DC1-LEAF1A"} |
| 205 |
assert response.results[1].success is True |
| 206 |
|
| 207 |
def test_from_jsonrpc_text_format(self) -> None: |
| 208 |
"""Test from_jsonrpc with TEXT format responses.""" |
| 209 |
|
| 210 |
request = EapiRequest(commands=["show version", "show hostname"], format=EapiCommandFormat.TEXT) |
| 211 |
|
| 212 |
|
| 213 |
jsonrpc_response = { |
| 214 |
"jsonrpc": "2.0", |
| 215 |
"id": "text-format-id", |
| 216 |
"result": [{"output": "Arista cEOSLab\n\nSoftware image version: 4.33.2F-40713977.4332F"}, {"output": "Hostname: DC1-LEAF1A\nFQDN: DC1-LEAF1A\n"}], |
| 217 |
} |
| 218 |
|
| 219 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 220 |
|
| 221 |
|
| 222 |
assert len(response) == 2 |
| 223 |
assert response.results[0].output is not None |
| 224 |
assert "Arista cEOSLab" in response.results[0].output |
| 225 |
assert response.results[1].output is not None |
| 226 |
assert "Hostname: DC1-LEAF1A" in response.results[1].output |
| 227 |
|
| 228 |
def test_from_jsonrpc_with_timestamps(self) -> None: |
| 229 |
"""Test from_jsonrpc with timestamps enabled.""" |
| 230 |
|
| 231 |
request = EapiRequest(commands=["show version"], format=EapiCommandFormat.JSON, timestamps=True) |
| 232 |
|
| 233 |
|
| 234 |
jsonrpc_response = { |
| 235 |
"jsonrpc": "2.0", |
| 236 |
"id": "timestamp-id", |
| 237 |
"result": [ |
| 238 |
{ |
| 239 |
"modelName": "cEOSLab", |
| 240 |
"version": "4.33.2F-40713977.4332F (engineering build)", |
| 241 |
"_meta": {"execStartTime": 1741014072.2534037, "execDuration": 0.0024061203002929688}, |
| 242 |
} |
| 243 |
], |
| 244 |
} |
| 245 |
|
| 246 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 247 |
|
| 248 |
|
| 249 |
assert len(response) == 1 |
| 250 |
assert response.results[0].start_time == 1741014072.2534037 |
| 251 |
assert response.results[0].duration == 0.0024061203002929688 |
| 252 |
|
| 253 |
|
| 254 |
assert response.results[0].output is not None |
| 255 |
assert "_meta" not in response.results[0].output |
| 256 |
|
| 257 |
def test_from_jsonrpc_error_stop_on_error_true(self) -> None: |
| 258 |
"""Test from_jsonrpc with error and stop_on_error=True.""" |
| 259 |
|
| 260 |
request = EapiRequest(commands=["show bad command", "show version", "show hostname"], stop_on_error=True) |
| 261 |
|
| 262 |
|
| 263 |
jsonrpc_response = { |
| 264 |
"jsonrpc": "2.0", |
| 265 |
"id": "error-id", |
| 266 |
"error": { |
| 267 |
"code": 1002, |
| 268 |
"message": "CLI command 1 of 3 'show bad command' failed: invalid command", |
| 269 |
"data": [{"errors": ["Invalid input (at token 1: 'bad')"]}], |
| 270 |
}, |
| 271 |
} |
| 272 |
|
| 273 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 274 |
|
| 275 |
|
| 276 |
assert response.request_id == "error-id" |
| 277 |
assert response.error_code == 1002 |
| 278 |
assert response.error_message == "CLI command 1 of 3 'show bad command' failed: invalid command" |
| 279 |
assert response.success is False |
| 280 |
|
| 281 |
|
| 282 |
assert len(response) == 3 |
| 283 |
|
| 284 |
|
| 285 |
assert response.results[0].command == "show bad command" |
| 286 |
assert response.results[0].output is None |
| 287 |
assert response.results[0].success is False |
| 288 |
assert response.results[0].errors == ["Invalid input (at token 1: 'bad')"] |
| 289 |
|
| 290 |
|
| 291 |
assert response.results[1].command == "show version" |
| 292 |
assert response.results[1].output is None |
| 293 |
assert response.results[1].success is False |
| 294 |
assert "Command not executed due to previous error" in response.results[1].errors |
| 295 |
assert response.results[1].executed is False |
| 296 |
|
| 297 |
assert response.results[2].command == "show hostname" |
| 298 |
assert response.results[2].output is None |
| 299 |
assert response.results[2].success is False |
| 300 |
assert "Command not executed due to previous error" in response.results[2].errors |
| 301 |
assert response.results[2].executed is False |
| 302 |
|
| 303 |
def test_from_jsonrpc_error_stop_on_error_false(self) -> None: |
| 304 |
"""Test from_jsonrpc with error and stop_on_error=False.""" |
| 305 |
|
| 306 |
request = EapiRequest(commands=["show bad command", "show version", "show hostname"], stop_on_error=False) |
| 307 |
|
| 308 |
|
| 309 |
jsonrpc_response = { |
| 310 |
"jsonrpc": "2.0", |
| 311 |
"id": "error-continue-id", |
| 312 |
"error": { |
| 313 |
"code": 1002, |
| 314 |
"message": "CLI command 1 of 3 'show bad command' failed: invalid command", |
| 315 |
"data": [ |
| 316 |
{"errors": ["Invalid input (at token 1: 'bad')"]}, |
| 317 |
{"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}, |
| 318 |
{"hostname": "DC1-LEAF1A"}, |
| 319 |
], |
| 320 |
}, |
| 321 |
} |
| 322 |
|
| 323 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 324 |
|
| 325 |
|
| 326 |
assert response.request_id == "error-continue-id" |
| 327 |
assert response.error_code == 1002 |
| 328 |
assert response.error_message == "CLI command 1 of 3 'show bad command' failed: invalid command" |
| 329 |
assert response.success is False |
| 330 |
|
| 331 |
|
| 332 |
assert len(response) == 3 |
| 333 |
|
| 334 |
|
| 335 |
assert response.results[0].command == "show bad command" |
| 336 |
assert response.results[0].output is None |
| 337 |
assert response.results[0].success is False |
| 338 |
assert response.results[0].errors == ["Invalid input (at token 1: 'bad')"] |
| 339 |
|
| 340 |
|
| 341 |
assert response.results[1].command == "show version" |
| 342 |
assert response.results[1].output == {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"} |
| 343 |
assert response.results[1].success is True |
| 344 |
|
| 345 |
assert response.results[2].command == "show hostname" |
| 346 |
assert response.results[2].output == {"hostname": "DC1-LEAF1A"} |
| 347 |
assert response.results[2].success is True |
| 348 |
|
| 349 |
def test_from_jsonrpc_raise_on_error(self) -> None: |
| 350 |
"""Test from_jsonrpc with raise_on_error=True.""" |
| 351 |
|
| 352 |
request = EapiRequest(commands=["show bad command"]) |
| 353 |
|
| 354 |
|
| 355 |
jsonrpc_response = { |
| 356 |
"jsonrpc": "2.0", |
| 357 |
"id": "raise-error-id", |
| 358 |
"error": { |
| 359 |
"code": 1002, |
| 360 |
"message": "CLI command 1 of 1 'show bad command' failed: invalid command", |
| 361 |
"data": [{"errors": ["Invalid input (at token 1: 'bad')"]}], |
| 362 |
}, |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
with pytest.raises(EapiReponseError) as excinfo: |
| 367 |
EapiResponse.from_jsonrpc(jsonrpc_response, request, raise_on_error=True) |
| 368 |
|
| 369 |
|
| 370 |
assert excinfo.value.response.request_id == "raise-error-id" |
| 371 |
assert excinfo.value.response.error_code == 1002 |
| 372 |
assert excinfo.value.response.error_message == "CLI command 1 of 1 'show bad command' failed: invalid command" |
| 373 |
|
| 374 |
def test_from_jsonrpc_string_data(self, caplog: pytest.LogCaptureFixture) -> None: |
| 375 |
"""Test from_jsonrpc with string data response.""" |
| 376 |
caplog.set_level(logging.WARNING) |
| 377 |
|
| 378 |
|
| 379 |
request = EapiRequest(commands=["show bgp ipv4 unicast summary", "show bad command"]) |
| 380 |
|
| 381 |
|
| 382 |
jsonrpc_response = { |
| 383 |
"jsonrpc": "2.0", |
| 384 |
"id": "EapiExplorer-1", |
| 385 |
"error": { |
| 386 |
"code": 1002, |
| 387 |
"message": "CLI command 2 of 2 'show bad command' failed: invalid command", |
| 388 |
"data": [ |
| 389 |
'{"vrfs":{"default":{"vrf":"default","routerId":"10.1.0.11","asn":"65101","peers":{}}}}\n', |
| 390 |
{"errors": ["Invalid input (at token 1: 'bad')"]}, |
| 391 |
], |
| 392 |
}, |
| 393 |
} |
| 394 |
|
| 395 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 396 |
|
| 397 |
|
| 398 |
assert response.results[0].output == {"vrfs": {"default": {"vrf": "default", "routerId": "10.1.0.11", "asn": "65101", "peers": {}}}} |
| 399 |
|
| 400 |
|
| 401 |
jsonrpc_response = { |
| 402 |
"jsonrpc": "2.0", |
| 403 |
"id": "EapiExplorer-1", |
| 404 |
"error": { |
| 405 |
"code": 1002, |
| 406 |
"message": "CLI command 2 of 2 'show bad command' failed: invalid command", |
| 407 |
"data": ["This is not JSON", {"errors": ["Invalid input (at token 1: 'bad')"]}], |
| 408 |
}, |
| 409 |
} |
| 410 |
|
| 411 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 412 |
|
| 413 |
|
| 414 |
assert "Invalid JSON response for command: show bgp ipv4 unicast summary. Storing as text: This is not JSON" in caplog.text |
| 415 |
|
| 416 |
|
| 417 |
assert response.results[0].output == "This is not JSON" |
| 418 |
|
| 419 |
def test_from_jsonrpc_complex_commands(self) -> None: |
| 420 |
"""Test from_jsonrpc with complex command structures.""" |
| 421 |
|
| 422 |
request = EapiRequest(commands=[{"cmd": "enable", "input": "password"}, "show version"]) |
| 423 |
|
| 424 |
|
| 425 |
jsonrpc_response = { |
| 426 |
"jsonrpc": "2.0", |
| 427 |
"id": "complex-cmd-id", |
| 428 |
"result": [{}, {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}], |
| 429 |
} |
| 430 |
|
| 431 |
response = EapiResponse.from_jsonrpc(jsonrpc_response, request) |
| 432 |
|
| 433 |
|
| 434 |
assert response.results[0].command == "enable" |
| 435 |
assert response.results[1].command == "show version" |