test_xmlutil.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import pytest
  2. import salt.utils.xmlutil as xml
  3. from salt._compat import ElementTree as ET
  4. @pytest.fixture
  5. def xml_doc():
  6. return ET.fromstring(
  7. """
  8. <domain>
  9. <name>test01</name>
  10. <memory unit="MiB">1024</memory>
  11. <cpu>
  12. <topology sockets="1"/>
  13. </cpu>
  14. <vcpus>
  15. <vcpu enabled="yes" id="1"/>
  16. </vcpus>
  17. </domain>
  18. """
  19. )
  20. def test_change_xml_text(xml_doc):
  21. ret = xml.change_xml(
  22. xml_doc, {"name": "test02"}, [{"path": "name", "xpath": "name"}]
  23. )
  24. assert ret
  25. assert "test02" == xml_doc.find("name").text
  26. def test_change_xml_text_nochange(xml_doc):
  27. ret = xml.change_xml(
  28. xml_doc, {"name": "test01"}, [{"path": "name", "xpath": "name"}]
  29. )
  30. assert not ret
  31. def test_change_xml_text_notdefined(xml_doc):
  32. ret = xml.change_xml(xml_doc, {}, [{"path": "name", "xpath": "name"}])
  33. assert not ret
  34. def test_change_xml_text_removed(xml_doc):
  35. ret = xml.change_xml(xml_doc, {"name": None}, [{"path": "name", "xpath": "name"}])
  36. assert ret
  37. assert xml_doc.find("name") is None
  38. def test_change_xml_text_add(xml_doc):
  39. ret = xml.change_xml(
  40. xml_doc,
  41. {"cpu": {"vendor": "ACME"}},
  42. [{"path": "cpu:vendor", "xpath": "cpu/vendor"}],
  43. )
  44. assert ret
  45. assert "ACME" == xml_doc.find("cpu/vendor").text
  46. def test_change_xml_convert(xml_doc):
  47. ret = xml.change_xml(
  48. xml_doc,
  49. {"mem": 2},
  50. [{"path": "mem", "xpath": "memory", "convert": lambda v: v * 1024}],
  51. )
  52. assert ret
  53. assert "2048" == xml_doc.find("memory").text
  54. def test_change_xml_attr(xml_doc):
  55. ret = xml.change_xml(
  56. xml_doc,
  57. {"cpu": {"topology": {"cores": 4}}},
  58. [
  59. {
  60. "path": "cpu:topology:cores",
  61. "xpath": "cpu/topology",
  62. "get": lambda n: int(n.get("cores")) if n.get("cores") else None,
  63. "set": lambda n, v: n.set("cores", str(v)),
  64. "del": xml.del_attribute("cores"),
  65. }
  66. ],
  67. )
  68. assert ret
  69. assert "4" == xml_doc.find("cpu/topology").get("cores")
  70. def test_change_xml_attr_unchanged(xml_doc):
  71. ret = xml.change_xml(
  72. xml_doc,
  73. {"cpu": {"topology": {"sockets": 1}}},
  74. [
  75. {
  76. "path": "cpu:topology:sockets",
  77. "xpath": "cpu/topology",
  78. "get": lambda n: int(n.get("sockets")) if n.get("sockets") else None,
  79. "set": lambda n, v: n.set("sockets", str(v)),
  80. "del": xml.del_attribute("sockets"),
  81. }
  82. ],
  83. )
  84. assert not ret
  85. def test_change_xml_attr_remove(xml_doc):
  86. ret = xml.change_xml(
  87. xml_doc,
  88. {"cpu": {"topology": {"sockets": None}}},
  89. [
  90. {
  91. "path": "cpu:topology:sockets",
  92. "xpath": "./cpu/topology",
  93. "get": lambda n: int(n.get("sockets")) if n.get("sockets") else None,
  94. "set": lambda n, v: n.set("sockets", str(v)),
  95. "del": xml.del_attribute("sockets"),
  96. }
  97. ],
  98. )
  99. assert ret
  100. assert xml_doc.find("cpu") is None
  101. def test_change_xml_not_simple_value(xml_doc):
  102. ret = xml.change_xml(
  103. xml_doc,
  104. {"cpu": {"topology": {"sockets": None}}},
  105. [{"path": "cpu", "xpath": "vcpu", "get": lambda n: int(n.text)}],
  106. )
  107. assert not ret
  108. def test_change_xml_template(xml_doc):
  109. ret = xml.change_xml(
  110. xml_doc,
  111. {"cpu": {"vcpus": {2: {"enabled": True}, 4: {"enabled": False}}}},
  112. [
  113. {
  114. "path": "cpu:vcpus:{id}:enabled",
  115. "xpath": "vcpus/vcpu[@id='$id']",
  116. "convert": lambda v: "yes" if v else "no",
  117. "get": lambda n: n.get("enabled"),
  118. "set": lambda n, v: n.set("enabled", v),
  119. "del": xml.del_attribute("enabled", ["id"]),
  120. },
  121. ],
  122. )
  123. assert ret
  124. assert xml_doc.find("vcpus/vcpu[@id='1']") is None
  125. assert "yes" == xml_doc.find("vcpus/vcpu[@id='2']").get("enabled")
  126. assert "no" == xml_doc.find("vcpus/vcpu[@id='4']").get("enabled")
  127. def test_change_xml_template_remove(xml_doc):
  128. ret = xml.change_xml(
  129. xml_doc,
  130. {"cpu": {"vcpus": None}},
  131. [
  132. {
  133. "path": "cpu:vcpus:{id}:enabled",
  134. "xpath": "vcpus/vcpu[@id='$id']",
  135. "convert": lambda v: "yes" if v else "no",
  136. "get": lambda n: n.get("enabled"),
  137. "set": lambda n, v: n.set("enabled", v),
  138. "del": xml.del_attribute("enabled", ["id"]),
  139. },
  140. ],
  141. )
  142. assert ret
  143. assert xml_doc.find("vcpus") is None