| 1 |
import os |
| 2 |
import unittest |
| 3 |
from io import StringIO |
| 4 |
import nose2 |
| 5 |
from unittest.mock import patch |
| 6 |
from jnpr.junos.jxml import ( |
| 7 |
NAME, |
| 8 |
INSERT, |
| 9 |
remove_namespaces, |
| 10 |
cscript_conf, |
| 11 |
normalize_xslt, |
| 12 |
) |
| 13 |
from lxml import etree |
| 14 |
from ncclient.xml_ import NCElement |
| 15 |
|
| 16 |
__author__ = "Nitin Kumar, Rick Sherman" |
| 17 |
__credits__ = "Jeremy Schulman" |
| 18 |
|
| 19 |
|
| 20 |
class Test_JXML(unittest.TestCase): |
| 21 |
def test_name(self): |
| 22 |
op = NAME("test") |
| 23 |
self.assertEqual(op["name"], "test") |
| 24 |
|
| 25 |
def test_insert(self): |
| 26 |
op = INSERT("test") |
| 27 |
self.assertEqual(op["insert"], "test") |
| 28 |
|
| 29 |
def test_remove_namespaces(self): |
| 30 |
xmldata = """<xsl:stylesheet xmlns:xsl="http://xml.juniper.net/junos"> |
| 31 |
<xsl:template> |
| 32 |
<!-- Handle comments properly --> |
| 33 |
<xsl:attribute name="{myname}"> |
| 34 |
</xsl:attribute> |
| 35 |
</xsl:template> |
| 36 |
</xsl:stylesheet>""" |
| 37 |
import xml.etree.ElementTree as ET |
| 38 |
|
| 39 |
parser = ET.XMLParser() |
| 40 |
root = ET.parse(StringIO(xmldata), parser) |
| 41 |
test = remove_namespaces(root) |
| 42 |
for elem in test.iter(): |
| 43 |
i = elem.tag.find("}") |
| 44 |
if i > 0: |
| 45 |
i = i + 1 |
| 46 |
self.assertTrue(i <= 0) |
| 47 |
|
| 48 |
def test_cscript_conf(self): |
| 49 |
op = cscript_conf(self._read_file("get-configuration.xml")) |
| 50 |
self.assertTrue(isinstance(op, etree._Element)) |
| 51 |
|
| 52 |
@patch("ncclient.manager.make_device_handler") |
| 53 |
def test_cscript_conf_return_none(self, dev_handler): |
| 54 |
dev_handler.side_effects = ValueError |
| 55 |
op = cscript_conf(self._read_file("get-configuration.xml")) |
| 56 |
self.assertTrue(op is None) |
| 57 |
|
| 58 |
def test_cscript_conf_output_tag_child_element(self): |
| 59 |
xmldata = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03" xmlns:junos="http://xml.juniper.net/junos/19.3R0/junos"> |
| 60 |
<output> |
| 61 |
shutdown: [pid 8683] |
| 62 |
Shutdown NOW! |
| 63 |
</output> |
| 64 |
</rpc-reply> |
| 65 |
""" |
| 66 |
xmldata_without_ns = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03"> |
| 67 |
<output> |
| 68 |
shutdown: [pid 8683] |
| 69 |
Shutdown NOW! |
| 70 |
</output> |
| 71 |
</rpc-reply> |
| 72 |
""" |
| 73 |
rpc_reply = NCElement(xmldata, normalize_xslt.encode("UTF-8")) |
| 74 |
self.assertEqual(str(rpc_reply), xmldata_without_ns) |
| 75 |
|
| 76 |
def test_cscript_conf_output_tag_not_first_child_element(self): |
| 77 |
xmldata = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03" xmlns:junos="http://xml.juniper.net/junos/19.3R0/junos"> |
| 78 |
<xyz> |
| 79 |
<output> |
| 80 |
shutdown: [pid 8683] |
| 81 |
Shutdown NOW! |
| 82 |
</output> |
| 83 |
</xyz> |
| 84 |
</rpc-reply> |
| 85 |
""" |
| 86 |
xmldata_without_ns = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03"> |
| 87 |
<xyz> |
| 88 |
<output>shutdown: [pid 8683] Shutdown NOW!</output> |
| 89 |
</xyz> |
| 90 |
</rpc-reply> |
| 91 |
""" |
| 92 |
rpc_reply = NCElement(xmldata, normalize_xslt.encode("UTF-8")) |
| 93 |
self.assertEqual(str(rpc_reply), xmldata_without_ns) |
| 94 |
|
| 95 |
def _read_file(self, fname): |
| 96 |
fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname) |
| 97 |
with open(fpath) as fp: |
| 98 |
foo = fp.read() |
| 99 |
return foo |