File: test_jxml.py

package info (click to toggle)
junos-eznc 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,496 kB
  • sloc: xml: 50,219; python: 20,961; makefile: 155; sh: 44; ruby: 2
file content (99 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download | duplicates (2)
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
import os
import unittest
from io import StringIO
import nose2
from unittest.mock import patch
from jnpr.junos.jxml import (
    NAME,
    INSERT,
    remove_namespaces,
    cscript_conf,
    normalize_xslt,
)
from lxml import etree
from ncclient.xml_ import NCElement

__author__ = "Nitin Kumar, Rick Sherman"
__credits__ = "Jeremy Schulman"


class Test_JXML(unittest.TestCase):
    def test_name(self):
        op = NAME("test")
        self.assertEqual(op["name"], "test")

    def test_insert(self):
        op = INSERT("test")
        self.assertEqual(op["insert"], "test")

    def test_remove_namespaces(self):
        xmldata = """<xsl:stylesheet xmlns:xsl="http://xml.juniper.net/junos">
                    <xsl:template>
                        <!-- Handle comments properly -->
                        <xsl:attribute name="{myname}">
                        </xsl:attribute>
                    </xsl:template>
                </xsl:stylesheet>"""
        import xml.etree.ElementTree as ET

        parser = ET.XMLParser()
        root = ET.parse(StringIO(xmldata), parser)
        test = remove_namespaces(root)
        for elem in test.iter():
            i = elem.tag.find("}")
            if i > 0:
                i = i + 1
        self.assertTrue(i <= 0)

    def test_cscript_conf(self):
        op = cscript_conf(self._read_file("get-configuration.xml"))
        self.assertTrue(isinstance(op, etree._Element))

    @patch("ncclient.manager.make_device_handler")
    def test_cscript_conf_return_none(self, dev_handler):
        dev_handler.side_effects = ValueError
        op = cscript_conf(self._read_file("get-configuration.xml"))
        self.assertTrue(op is None)

    def test_cscript_conf_output_tag_child_element(self):
        xmldata = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03" xmlns:junos="http://xml.juniper.net/junos/19.3R0/junos">
  <output>
    shutdown: [pid 8683]
    Shutdown NOW!
  </output>
</rpc-reply>
"""
        xmldata_without_ns = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03">
  <output>
    shutdown: [pid 8683]
    Shutdown NOW!
  </output>
</rpc-reply>
"""
        rpc_reply = NCElement(xmldata, normalize_xslt.encode("UTF-8"))
        self.assertEqual(str(rpc_reply), xmldata_without_ns)

    def test_cscript_conf_output_tag_not_first_child_element(self):
        xmldata = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03" xmlns:junos="http://xml.juniper.net/junos/19.3R0/junos">
  <xyz>
    <output>
      shutdown: [pid 8683]
      Shutdown NOW!
    </output>
  </xyz>
</rpc-reply>
"""
        xmldata_without_ns = """<rpc-reply message-id="urn:uuid:932d11dc-9ae5-4d25-81fa-8b50ea2d3a03">
  <xyz>
    <output>shutdown: [pid 8683] Shutdown NOW!</output>
  </xyz>
</rpc-reply>
"""
        rpc_reply = NCElement(xmldata, normalize_xslt.encode("UTF-8"))
        self.assertEqual(str(rpc_reply), xmldata_without_ns)

    def _read_file(self, fname):
        fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname)
        with open(fpath) as fp:
            foo = fp.read()
        return foo