|
| 1 | +""" |
| 2 | +Unit tests for salt.utils.nxos |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | +import salt.utils.nxos as nxos |
| 8 | +from tests.support.mock import MagicMock, patch |
| 9 | + |
| 10 | + |
| 11 | +class TestNxapiClient: |
| 12 | + """ |
| 13 | + Test cases for NxapiClient class |
| 14 | + """ |
| 15 | + |
| 16 | + def test_parse_response_uds_read_limit(self): |
| 17 | + """ |
| 18 | + Test that response.read() is called with a safe limit when connecting over UDS |
| 19 | + """ |
| 20 | + # Create a mock response object |
| 21 | + mock_response = MagicMock() |
| 22 | + mock_json_data = { |
| 23 | + "ins_api": { |
| 24 | + "outputs": { |
| 25 | + "output": { |
| 26 | + "code": "200", |
| 27 | + "msg": "Success", |
| 28 | + "body": {"test": "data"}, |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + mock_response.read.return_value.decode.return_value = json.dumps(mock_json_data) |
| 34 | + |
| 35 | + # Create NxapiClient with UDS connection |
| 36 | + with patch("os.path.exists", return_value=True): |
| 37 | + client = nxos.NxapiClient() |
| 38 | + |
| 39 | + # Ensure we're using UDS connection |
| 40 | + assert client.nxargs["connect_over_uds"] is True |
| 41 | + |
| 42 | + # Call parse_response with the mock response |
| 43 | + command_list = ["show version"] |
| 44 | + result = client.parse_response(mock_response, command_list) |
| 45 | + |
| 46 | + # Verify response.read() was called with the 10MB limit |
| 47 | + expected_limit = 10 * 1024 * 1024 |
| 48 | + mock_response.read.assert_called_once_with(expected_limit) |
| 49 | + |
| 50 | + # Verify the result is correct |
| 51 | + assert result == [{"test": "data"}] |
| 52 | + |
| 53 | + def test_parse_response_uds_read_limit_value(self): |
| 54 | + """ |
| 55 | + Test that the max_safe_read limit is exactly 10MB |
| 56 | + """ |
| 57 | + # Create a mock response object |
| 58 | + mock_response = MagicMock() |
| 59 | + mock_json_data = { |
| 60 | + "ins_api": { |
| 61 | + "outputs": { |
| 62 | + "output": { |
| 63 | + "code": "200", |
| 64 | + "msg": "Success", |
| 65 | + "body": {"result": "ok"}, |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + mock_response.read.return_value.decode.return_value = json.dumps(mock_json_data) |
| 71 | + |
| 72 | + # Create NxapiClient with UDS connection |
| 73 | + with patch("os.path.exists", return_value=True): |
| 74 | + client = nxos.NxapiClient() |
| 75 | + |
| 76 | + # Parse response |
| 77 | + command_list = ["test command"] |
| 78 | + client.parse_response(mock_response, command_list) |
| 79 | + |
| 80 | + # Get the actual argument passed to read() |
| 81 | + call_args = mock_response.read.call_args |
| 82 | + actual_limit = call_args[0][0] if call_args[0] else call_args[1].get("amt") |
| 83 | + |
| 84 | + # Verify it's exactly 10MB (10485760 bytes) |
| 85 | + assert actual_limit == 10485760 |
| 86 | + assert actual_limit == 10 * 1024 * 1024 |
0 commit comments