123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- """
- tests.unit.xmlutil_test
- ~~~~~~~~~~~~~~~~~~~~
- """
- import salt.utils.xmlutil as xml
- # Import Salt libs
- from salt._compat import ElementTree as ET
- # Import Salt Testing libs
- from tests.support.unit import TestCase
- class XMLUtilTestCase(TestCase):
- """
- Tests that salt.utils.xmlutil properly parses XML data and returns as a properly formatted
- dictionary. The default method of parsing will ignore attributes and return only the child
- items. The full method will include parsing attributes.
- """
- def setUp(self):
- # Populate our use cases for specific XML formats.
- self.cases = {
- "a": {
- "xml": "<parent>data</parent>",
- "legacy": {"parent": "data"},
- "full": "data",
- },
- "b": {
- "xml": '<parent value="data">data</parent>',
- "legacy": {"parent": "data"},
- "full": {"parent": "data", "value": "data"},
- },
- "c": {
- "xml": '<parent><child>data</child><child value="data">data</child>'
- '<child value="data"/><child/></parent>',
- "legacy": {
- "child": [
- "data",
- {"child": "data"},
- {"child": None},
- {"child": None},
- ]
- },
- "full": {
- "child": [
- "data",
- {"child": "data", "value": "data"},
- {"value": "data"},
- None,
- ]
- },
- },
- "d": {
- "xml": '<parent value="data" another="data"><child>data</child></parent>',
- "legacy": {"child": "data"},
- "full": {"child": "data", "another": "data", "value": "data"},
- },
- "e": {
- "xml": '<parent value="data" another="data"><child value="data">data</child></parent>',
- "legacy": {"child": "data"},
- "full": {
- "child": {"child": "data", "value": "data"},
- "another": "data",
- "value": "data",
- },
- },
- "f": {
- "xml": '<parent><child><sub-child value="data">data</sub-child></child>'
- "<child>data</child></parent>",
- "legacy": {"child": [{"sub-child": "data"}, {"child": "data"}]},
- "full": {
- "child": [
- {"sub-child": {"value": "data", "sub-child": "data"}},
- "data",
- ]
- },
- },
- }
- def test_xml_case_a(self):
- xmldata = ET.fromstring(self.cases["a"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["a"]["legacy"])
- def test_xml_case_a_legacy(self):
- xmldata = ET.fromstring(self.cases["a"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["a"]["legacy"])
- def test_xml_case_a_full(self):
- xmldata = ET.fromstring(self.cases["a"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["a"]["full"])
- def test_xml_case_b(self):
- xmldata = ET.fromstring(self.cases["b"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["b"]["legacy"])
- def test_xml_case_b_legacy(self):
- xmldata = ET.fromstring(self.cases["b"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["b"]["legacy"])
- def test_xml_case_b_full(self):
- xmldata = ET.fromstring(self.cases["b"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["b"]["full"])
- def test_xml_case_c(self):
- xmldata = ET.fromstring(self.cases["c"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["c"]["legacy"])
- def test_xml_case_c_legacy(self):
- xmldata = ET.fromstring(self.cases["c"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["c"]["legacy"])
- def test_xml_case_c_full(self):
- xmldata = ET.fromstring(self.cases["c"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["c"]["full"])
- def test_xml_case_d(self):
- xmldata = ET.fromstring(self.cases["d"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["d"]["legacy"])
- def test_xml_case_d_legacy(self):
- xmldata = ET.fromstring(self.cases["d"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["d"]["legacy"])
- def test_xml_case_d_full(self):
- xmldata = ET.fromstring(self.cases["d"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["d"]["full"])
- def test_xml_case_e(self):
- xmldata = ET.fromstring(self.cases["e"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["e"]["legacy"])
- def test_xml_case_e_legacy(self):
- xmldata = ET.fromstring(self.cases["e"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["e"]["legacy"])
- def test_xml_case_e_full(self):
- xmldata = ET.fromstring(self.cases["e"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["e"]["full"])
- def test_xml_case_f(self):
- xmldata = ET.fromstring(self.cases["f"]["xml"])
- defaultdict = xml.to_dict(xmldata)
- self.assertEqual(defaultdict, self.cases["f"]["legacy"])
- def test_xml_case_f_legacy(self):
- xmldata = ET.fromstring(self.cases["f"]["xml"])
- defaultdict = xml.to_dict(xmldata, False)
- self.assertEqual(defaultdict, self.cases["f"]["legacy"])
- def test_xml_case_f_full(self):
- xmldata = ET.fromstring(self.cases["f"]["xml"])
- defaultdict = xml.to_dict(xmldata, True)
- self.assertEqual(defaultdict, self.cases["f"]["full"])
|