1
0

test_xmlutil.py 5.9 KB

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