From 25a89451f1d0ed33325de0f9ba3ee886f33a2f96 Mon Sep 17 00:00:00 2001 From: bdemers Date: Mon, 15 Feb 2016 17:10:02 -0500 Subject: [PATCH] Adding test that reproduces problem when using lxml<3.3.0 --- test/unit/transport/expected_edit_message.xml | 1 + test/unit/transport/expected_get_message.xml | 1 + test/unit/transport/test_clear_attributes.py | 75 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 test/unit/transport/expected_edit_message.xml create mode 100644 test/unit/transport/expected_get_message.xml create mode 100644 test/unit/transport/test_clear_attributes.py diff --git a/test/unit/transport/expected_edit_message.xml b/test/unit/transport/expected_edit_message.xml new file mode 100644 index 00000000..5accb157 --- /dev/null +++ b/test/unit/transport/expected_edit_message.xml @@ -0,0 +1 @@ + <__XML__MODE__exec_configure> <__XML__PARAM_value>1 <__XML__MODE_vlan> 1 \ No newline at end of file diff --git a/test/unit/transport/expected_get_message.xml b/test/unit/transport/expected_get_message.xml new file mode 100644 index 00000000..1a86ae10 --- /dev/null +++ b/test/unit/transport/expected_get_message.xml @@ -0,0 +1 @@ +show version \ No newline at end of file diff --git a/test/unit/transport/test_clear_attributes.py b/test/unit/transport/test_clear_attributes.py new file mode 100644 index 00000000..82a9424f --- /dev/null +++ b/test/unit/transport/test_clear_attributes.py @@ -0,0 +1,75 @@ +from ncclient import manager +from ncclient import transport +from ncclient.manager import Manager + +import re + +import os + +from mock import Mock +from mock import create_autospec + +from nose.tools import assert_equal + +from unittest import TestCase + +class Test_LXML_problem(TestCase): + + def test_clear_attributes_from_previous_message(self): + """test sending multiple messages results in valid xml""" + + test_dir = os.path.dirname(os.path.realpath(__file__)) + + # setup ncclient + device_handler = manager.make_device_handler({'name':'nexus'}) + session = transport.Session(device_handler) + def mock_send(self, message='Not Set'): + pass + mock_send_function = create_autospec(mock_send, return_value="") + session.send = Mock(side_effect=mock_send_function) + session._connected = True + mgr = Manager(session, device_handler) + mgr._async_mode = True + + # make first get call + mgr.get(filter=('subtree', 'show version')) + xml_string = mock_send_function.mock_calls[0][1][0] # first call, second arg, value + xml_string = self._normalize_uuid(xml_string) + assert_equal(self._read_test_string(test_dir + '/expected_get_message.xml'), xml_string) + mock_send_function.reset_mock() + + # make an edit-config call + config = ''' + + <__XML__MODE__exec_configure> + + + <__XML__PARAM_value>1 + <__XML__MODE_vlan> + + 1 + + + + + + + ''' + config = ' '.join(config.split()) + + + mgr.edit_config(target='running', config=config) + xml_string = mock_send_function.mock_calls[0][1][0] + xml_string = self._normalize_uuid(xml_string) + # using lxml 3.2.1 attributes from the get message bleed into the edit + assert_equal(self._read_test_string(test_dir + '/expected_edit_message.xml'), xml_string) + + def _normalize_uuid(self, x): + """replace the UUID, so we can compare apples to apples""" + return re.sub(r"urn:uuid:[a-z0-9\-]*", "urn:uuid:UUID-HERE", x) + + + def _read_test_string(self, file): + with open(file, 'r') as f: + text = f.read() + return text