File: test__models.py

package info (click to toggle)
anta 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,048 kB
  • sloc: python: 48,164; sh: 28; javascript: 9; makefile: 4
file content (435 lines) | stat: -rw-r--r-- 18,113 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Unit tests for the asynceapi._models module."""

import logging
from typing import TYPE_CHECKING
from uuid import UUID

import pytest

from asynceapi._constants import EapiCommandFormat
from asynceapi._errors import EapiReponseError
from asynceapi._models import EapiCommandResult, EapiRequest, EapiResponse

if TYPE_CHECKING:
    from asynceapi._types import EapiComplexCommand, EapiSimpleCommand


class TestEapiRequest:
    """Test cases for the EapiRequest class."""

    def test_init_with_defaults(self) -> None:
        """Test initialization with just required parameters."""
        commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version", "show interfaces"]
        req = EapiRequest(commands=commands)

        # Check required attributes
        assert req.commands == commands

        # Check default values
        assert req.version == "latest"
        assert req.format == EapiCommandFormat.JSON
        assert req.timestamps is False
        assert req.auto_complete is False
        assert req.expand_aliases is False
        assert req.stop_on_error is True

        # Check that ID is generated as a UUID hex string
        try:
            UUID(str(req.id))
            is_valid_uuid = True
        except ValueError:
            is_valid_uuid = False
        assert is_valid_uuid

    def test_init_with_custom_values(self) -> None:
        """Test initialization with custom parameter values."""
        commands: list[EapiSimpleCommand | EapiComplexCommand] = [{"cmd": "enable", "input": "password"}, "show version"]
        req = EapiRequest(
            commands=commands,
            version=1,
            format=EapiCommandFormat.TEXT,
            timestamps=True,
            auto_complete=True,
            expand_aliases=True,
            stop_on_error=False,
            id="custom-id-123",
        )

        # Check all attributes match expected values
        assert req.commands == commands
        assert req.version == 1
        assert req.format == EapiCommandFormat.TEXT
        assert req.timestamps is True
        assert req.auto_complete is True
        assert req.expand_aliases is True
        assert req.stop_on_error is False
        assert req.id == "custom-id-123"

    def test_to_jsonrpc(self) -> None:
        """Test conversion to JSON-RPC dictionary."""
        commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version", "show interfaces"]
        req = EapiRequest(commands=commands, version=1, format=EapiCommandFormat.TEXT, id="test-id-456")

        jsonrpc = req.to_jsonrpc()

        # Check that structure matches expected JSON-RPC format
        assert jsonrpc["jsonrpc"] == "2.0"
        assert jsonrpc["method"] == "runCmds"
        assert jsonrpc.get("id") == "test-id-456"

        # Check params matches our configuration
        params = jsonrpc["params"]
        assert params.get("version") == 1
        assert params["cmds"] == commands
        assert params.get("format") == EapiCommandFormat.TEXT
        assert params.get("timestamps") is False
        assert params.get("autoComplete") is False
        assert params.get("expandAliases") is False
        assert params.get("stopOnError") is True

    def test_to_jsonrpc_with_complex_commands(self) -> None:
        """Test JSON-RPC conversion with complex commands."""
        commands: list[EapiSimpleCommand | EapiComplexCommand] = [
            {"cmd": "enable", "input": "password"},
            {"cmd": "configure", "input": ""},
            {"cmd": "hostname test-device"},
        ]
        req = EapiRequest(commands=commands)

        jsonrpc = req.to_jsonrpc()

        # Verify commands are passed correctly
        assert jsonrpc["params"]["cmds"] == commands

    def test_immutability(self) -> None:
        """Test that the dataclass is truly immutable (frozen)."""
        commands: list[EapiSimpleCommand | EapiComplexCommand] = ["show version"]
        req = EapiRequest(commands=commands)

        # Attempting to modify any attribute should raise an error
        with pytest.raises(AttributeError):
            req.commands = ["new command"]  # type: ignore[misc]

        with pytest.raises(AttributeError):
            req.id = "new-id"  # type: ignore[misc]


class TestEapiResponse:
    """Test cases for the EapiResponse class."""

    def test_init_and_properties(self) -> None:
        """Test basic initialization and properties."""
        # Create mock command results
        result1 = EapiCommandResult(command="show version", output={"version": "4.33.2F-40713977.4332F (engineering build)"})
        result2 = EapiCommandResult(command="show hostname", output={"hostname": "DC1-LEAF1A"})

        # Create response with results
        results = {0: result1, 1: result2}
        response = EapiResponse(request_id="test-123", _results=results)

        # Check attributes
        assert response.request_id == "test-123"
        assert response.error_code is None
        assert response.error_message is None

        # Check properties
        assert response.success is True
        assert len(response.results) == 2
        assert response.results[0] == result1
        assert response.results[1] == result2

    def test_error_response(self) -> None:
        """Test initialization with error information."""
        result = EapiCommandResult(command="show bad command", output=None, errors=["Invalid input (at token 1: 'bad')"], success=False)
        results = {0: result}
        response = EapiResponse(
            request_id="test-456", _results=results, error_code=1002, error_message="CLI command 1 of 1 'show bad command' failed: invalid command"
        )

        assert response.request_id == "test-456"
        assert response.error_code == 1002
        assert response.error_message == "CLI command 1 of 1 'show bad command' failed: invalid command"
        assert response.success is False
        assert len(response.results) == 1
        assert response.results[0].success is False
        assert "Invalid input (at token 1: 'bad')" in response.results[0].errors

    def test_len_and_iteration(self) -> None:
        """Test __len__ and __iter__ methods."""
        # Create 3 command results
        results = {
            0: EapiCommandResult(command="cmd1", output="out1"),
            1: EapiCommandResult(command="cmd2", output="out2"),
            2: EapiCommandResult(command="cmd3", output="out3"),
        }
        response = EapiResponse(request_id="test-789", _results=results)

        # Test __len__
        assert len(response) == 3

        # Test __iter__
        iterated_results = list(response)
        assert len(iterated_results) == 3
        assert [r.command for r in iterated_results] == ["cmd1", "cmd2", "cmd3"]

    def test_from_jsonrpc_success(self) -> None:
        """Test from_jsonrpc with successful response."""
        # Mock request
        request = EapiRequest(commands=["show version", "show hostname"], format=EapiCommandFormat.JSON)

        # Mock response data
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "test-id-123",
            "result": [{"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}, {"hostname": "DC1-LEAF1A"}],
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify response object
        assert response.request_id == "test-id-123"
        assert response.success is True
        assert response.error_code is None
        assert response.error_message is None

        # Verify results
        assert len(response) == 2
        assert response.results[0].command == "show version"
        assert response.results[0].output == {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}
        assert response.results[0].success is True
        assert response.results[1].command == "show hostname"
        assert response.results[1].output == {"hostname": "DC1-LEAF1A"}
        assert response.results[1].success is True

    def test_from_jsonrpc_text_format(self) -> None:
        """Test from_jsonrpc with TEXT format responses."""
        # Mock request with TEXT format
        request = EapiRequest(commands=["show version", "show hostname"], format=EapiCommandFormat.TEXT)

        # Mock response data
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "text-format-id",
            "result": [{"output": "Arista cEOSLab\n\nSoftware image version: 4.33.2F-40713977.4332F"}, {"output": "Hostname: DC1-LEAF1A\nFQDN:     DC1-LEAF1A\n"}],
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify results contain the text output
        assert len(response) == 2
        assert response.results[0].output is not None
        assert "Arista cEOSLab" in response.results[0].output
        assert response.results[1].output is not None
        assert "Hostname: DC1-LEAF1A" in response.results[1].output

    def test_from_jsonrpc_with_timestamps(self) -> None:
        """Test from_jsonrpc with timestamps enabled."""
        # Mock request with timestamps
        request = EapiRequest(commands=["show version"], format=EapiCommandFormat.JSON, timestamps=True)

        # Mock response data with timestamps
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "timestamp-id",
            "result": [
                {
                    "modelName": "cEOSLab",
                    "version": "4.33.2F-40713977.4332F (engineering build)",
                    "_meta": {"execStartTime": 1741014072.2534037, "execDuration": 0.0024061203002929688},
                }
            ],
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify timestamp data is processed
        assert len(response) == 1
        assert response.results[0].start_time == 1741014072.2534037
        assert response.results[0].duration == 0.0024061203002929688

        # Verify _meta is removed from output
        assert response.results[0].output is not None
        assert "_meta" not in response.results[0].output

    def test_from_jsonrpc_error_stop_on_error_true(self) -> None:
        """Test from_jsonrpc with error and stop_on_error=True."""
        # Mock request with stop_on_error=True
        request = EapiRequest(commands=["show bad command", "show version", "show hostname"], stop_on_error=True)

        # Mock error response
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "error-id",
            "error": {
                "code": 1002,
                "message": "CLI command 1 of 3 'show bad command' failed: invalid command",
                "data": [{"errors": ["Invalid input (at token 1: 'bad')"]}],
            },
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify error info
        assert response.request_id == "error-id"
        assert response.error_code == 1002
        assert response.error_message == "CLI command 1 of 3 'show bad command' failed: invalid command"
        assert response.success is False

        # Verify results - should have entries for all commands
        assert len(response) == 3

        # First command failed
        assert response.results[0].command == "show bad command"
        assert response.results[0].output is None
        assert response.results[0].success is False
        assert response.results[0].errors == ["Invalid input (at token 1: 'bad')"]

        # Remaining commands weren't executed due to stop_on_error=True
        assert response.results[1].command == "show version"
        assert response.results[1].output is None
        assert response.results[1].success is False
        assert "Command not executed due to previous error" in response.results[1].errors
        assert response.results[1].executed is False

        assert response.results[2].command == "show hostname"
        assert response.results[2].output is None
        assert response.results[2].success is False
        assert "Command not executed due to previous error" in response.results[2].errors
        assert response.results[2].executed is False

    def test_from_jsonrpc_error_stop_on_error_false(self) -> None:
        """Test from_jsonrpc with error and stop_on_error=False."""
        # Mock request with stop_on_error=False
        request = EapiRequest(commands=["show bad command", "show version", "show hostname"], stop_on_error=False)

        # Mock response with error for first command but others succeed
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "error-continue-id",
            "error": {
                "code": 1002,
                "message": "CLI command 1 of 3 'show bad command' failed: invalid command",
                "data": [
                    {"errors": ["Invalid input (at token 1: 'bad')"]},
                    {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"},
                    {"hostname": "DC1-LEAF1A"},
                ],
            },
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify error info
        assert response.request_id == "error-continue-id"
        assert response.error_code == 1002
        assert response.error_message == "CLI command 1 of 3 'show bad command' failed: invalid command"
        assert response.success is False

        # Verify individual command results
        assert len(response) == 3

        # First command failed
        assert response.results[0].command == "show bad command"
        assert response.results[0].output is None
        assert response.results[0].success is False
        assert response.results[0].errors == ["Invalid input (at token 1: 'bad')"]

        # Remaining commands succeeded
        assert response.results[1].command == "show version"
        assert response.results[1].output == {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}
        assert response.results[1].success is True

        assert response.results[2].command == "show hostname"
        assert response.results[2].output == {"hostname": "DC1-LEAF1A"}
        assert response.results[2].success is True

    def test_from_jsonrpc_raise_on_error(self) -> None:
        """Test from_jsonrpc with raise_on_error=True."""
        # Mock request
        request = EapiRequest(commands=["show bad command"])

        # Mock error response
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "raise-error-id",
            "error": {
                "code": 1002,
                "message": "CLI command 1 of 1 'show bad command' failed: invalid command",
                "data": [{"errors": ["Invalid input (at token 1: 'bad')"]}],
            },
        }

        # Should raise EapiReponseError
        with pytest.raises(EapiReponseError) as excinfo:
            EapiResponse.from_jsonrpc(jsonrpc_response, request, raise_on_error=True)

        # Verify the exception contains the response
        assert excinfo.value.response.request_id == "raise-error-id"
        assert excinfo.value.response.error_code == 1002
        assert excinfo.value.response.error_message == "CLI command 1 of 1 'show bad command' failed: invalid command"

    def test_from_jsonrpc_string_data(self, caplog: pytest.LogCaptureFixture) -> None:
        """Test from_jsonrpc with string data response."""
        caplog.set_level(logging.WARNING)

        # Mock request
        request = EapiRequest(commands=["show bgp ipv4 unicast summary", "show bad command"])

        # Mock response with JSON string
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "EapiExplorer-1",
            "error": {
                "code": 1002,
                "message": "CLI command 2 of 2 'show bad command' failed: invalid command",
                "data": [
                    '{"vrfs":{"default":{"vrf":"default","routerId":"10.1.0.11","asn":"65101","peers":{}}}}\n',
                    {"errors": ["Invalid input (at token 1: 'bad')"]},
                ],
            },
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify string was parsed as JSON
        assert response.results[0].output == {"vrfs": {"default": {"vrf": "default", "routerId": "10.1.0.11", "asn": "65101", "peers": {}}}}

        # Now test with a non-JSON string
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "EapiExplorer-1",
            "error": {
                "code": 1002,
                "message": "CLI command 2 of 2 'show bad command' failed: invalid command",
                "data": ["This is not JSON", {"errors": ["Invalid input (at token 1: 'bad')"]}],
            },
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify WARNING log message
        assert "Invalid JSON response for command: show bgp ipv4 unicast summary. Storing as text: This is not JSON" in caplog.text

        # Verify string is kept as is
        assert response.results[0].output == "This is not JSON"

    def test_from_jsonrpc_complex_commands(self) -> None:
        """Test from_jsonrpc with complex command structures."""
        # Mock request with complex commands
        request = EapiRequest(commands=[{"cmd": "enable", "input": "password"}, "show version"])

        # Mock response
        jsonrpc_response = {
            "jsonrpc": "2.0",
            "id": "complex-cmd-id",
            "result": [{}, {"modelName": "cEOSLab", "version": "4.33.2F-40713977.4332F (engineering build)"}],
        }

        response = EapiResponse.from_jsonrpc(jsonrpc_response, request)

        # Verify command strings are extracted correctly
        assert response.results[0].command == "enable"
        assert response.results[1].command == "show version"