test_xmlutil.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """
  2. tests.unit.xmlutil_test
  3. ~~~~~~~~~~~~~~~~~~~~
  4. """
  5. import salt.utils.xmlutil as xml
  6. # Import Salt libs
  7. from salt._compat import ElementTree as ET
  8. # Import Salt Testing libs
  9. from tests.support.unit import TestCase
  10. class XMLUtilTestCase(TestCase):
  11. """
  12. Tests that salt.utils.xmlutil properly parses XML data and returns as a properly formatted
  13. dictionary. The default method of parsing will ignore attributes and return only the child
  14. items. The full method will include parsing attributes.
  15. """
  16. def setUp(self):
  17. # Populate our use cases for specific XML formats.
  18. self.cases = {
  19. "a": {
  20. "xml": "<parent>data</parent>",
  21. "legacy": {"parent": "data"},
  22. "full": "data",
  23. },
  24. "b": {
  25. "xml": '<parent value="data">data</parent>',
  26. "legacy": {"parent": "data"},
  27. "full": {"parent": "data", "value": "data"},
  28. },
  29. "c": {
  30. "xml": '<parent><child>data</child><child value="data">data</child>'
  31. '<child value="data"/><child/></parent>',
  32. "legacy": {
  33. "child": [
  34. "data",
  35. {"child": "data"},
  36. {"child": None},
  37. {"child": None},
  38. ]
  39. },
  40. "full": {
  41. "child": [
  42. "data",
  43. {"child": "data", "value": "data"},
  44. {"value": "data"},
  45. None,
  46. ]
  47. },
  48. },
  49. "d": {
  50. "xml": '<parent value="data" another="data"><child>data</child></parent>',
  51. "legacy": {"child": "data"},
  52. "full": {"child": "data", "another": "data", "value": "data"},
  53. },
  54. "e": {
  55. "xml": '<parent value="data" another="data"><child value="data">data</child></parent>',
  56. "legacy": {"child": "data"},
  57. "full": {
  58. "child": {"child": "data", "value": "data"},
  59. "another": "data",
  60. "value": "data",
  61. },
  62. },
  63. "f": {
  64. "xml": '<parent><child><sub-child value="data">data</sub-child></child>'
  65. "<child>data</child></parent>",
  66. "legacy": {"child": [{"sub-child": "data"}, {"child": "data"}]},
  67. "full": {
  68. "child": [
  69. {"sub-child": {"value": "data", "sub-child": "data"}},
  70. "data",
  71. ]
  72. },
  73. },
  74. }
  75. def test_xml_case_a(self):
  76. xmldata = ET.fromstring(self.cases["a"]["xml"])
  77. defaultdict = xml.to_dict(xmldata)
  78. self.assertEqual(defaultdict, self.cases["a"]["legacy"])
  79. def test_xml_case_a_legacy(self):
  80. xmldata = ET.fromstring(self.cases["a"]["xml"])
  81. defaultdict = xml.to_dict(xmldata, False)
  82. self.assertEqual(defaultdict, self.cases["a"]["legacy"])
  83. def test_xml_case_a_full(self):
  84. xmldata = ET.fromstring(self.cases["a"]["xml"])
  85. defaultdict = xml.to_dict(xmldata, True)
  86. self.assertEqual(defaultdict, self.cases["a"]["full"])
  87. def test_xml_case_b(self):
  88. xmldata = ET.fromstring(self.cases["b"]["xml"])
  89. defaultdict = xml.to_dict(xmldata)
  90. self.assertEqual(defaultdict, self.cases["b"]["legacy"])
  91. def test_xml_case_b_legacy(self):
  92. xmldata = ET.fromstring(self.cases["b"]["xml"])
  93. defaultdict = xml.to_dict(xmldata, False)
  94. self.assertEqual(defaultdict, self.cases["b"]["legacy"])
  95. def test_xml_case_b_full(self):
  96. xmldata = ET.fromstring(self.cases["b"]["xml"])
  97. defaultdict = xml.to_dict(xmldata, True)
  98. self.assertEqual(defaultdict, self.cases["b"]["full"])
  99. def test_xml_case_c(self):
  100. xmldata = ET.fromstring(self.cases["c"]["xml"])
  101. defaultdict = xml.to_dict(xmldata)
  102. self.assertEqual(defaultdict, self.cases["c"]["legacy"])
  103. def test_xml_case_c_legacy(self):
  104. xmldata = ET.fromstring(self.cases["c"]["xml"])
  105. defaultdict = xml.to_dict(xmldata, False)
  106. self.assertEqual(defaultdict, self.cases["c"]["legacy"])
  107. def test_xml_case_c_full(self):
  108. xmldata = ET.fromstring(self.cases["c"]["xml"])
  109. defaultdict = xml.to_dict(xmldata, True)
  110. self.assertEqual(defaultdict, self.cases["c"]["full"])
  111. def test_xml_case_d(self):
  112. xmldata = ET.fromstring(self.cases["d"]["xml"])
  113. defaultdict = xml.to_dict(xmldata)
  114. self.assertEqual(defaultdict, self.cases["d"]["legacy"])
  115. def test_xml_case_d_legacy(self):
  116. xmldata = ET.fromstring(self.cases["d"]["xml"])
  117. defaultdict = xml.to_dict(xmldata, False)
  118. self.assertEqual(defaultdict, self.cases["d"]["legacy"])
  119. def test_xml_case_d_full(self):
  120. xmldata = ET.fromstring(self.cases["d"]["xml"])
  121. defaultdict = xml.to_dict(xmldata, True)
  122. self.assertEqual(defaultdict, self.cases["d"]["full"])
  123. def test_xml_case_e(self):
  124. xmldata = ET.fromstring(self.cases["e"]["xml"])
  125. defaultdict = xml.to_dict(xmldata)
  126. self.assertEqual(defaultdict, self.cases["e"]["legacy"])
  127. def test_xml_case_e_legacy(self):
  128. xmldata = ET.fromstring(self.cases["e"]["xml"])
  129. defaultdict = xml.to_dict(xmldata, False)
  130. self.assertEqual(defaultdict, self.cases["e"]["legacy"])
  131. def test_xml_case_e_full(self):
  132. xmldata = ET.fromstring(self.cases["e"]["xml"])
  133. defaultdict = xml.to_dict(xmldata, True)
  134. self.assertEqual(defaultdict, self.cases["e"]["full"])
  135. def test_xml_case_f(self):
  136. xmldata = ET.fromstring(self.cases["f"]["xml"])
  137. defaultdict = xml.to_dict(xmldata)
  138. self.assertEqual(defaultdict, self.cases["f"]["legacy"])
  139. def test_xml_case_f_legacy(self):
  140. xmldata = ET.fromstring(self.cases["f"]["xml"])
  141. defaultdict = xml.to_dict(xmldata, False)
  142. self.assertEqual(defaultdict, self.cases["f"]["legacy"])
  143. def test_xml_case_f_full(self):
  144. xmldata = ET.fromstring(self.cases["f"]["xml"])
  145. defaultdict = xml.to_dict(xmldata, True)
  146. self.assertEqual(defaultdict, self.cases["f"]["full"])