test_xml.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test cases for xml state
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.xml as xml
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class XMLTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.xml
  16. """
  17. def setup_loader_modules(self):
  18. return {xml: {}}
  19. def test_value_already_present(self):
  20. """
  21. Test for existing value_present
  22. """
  23. name = "testfile.xml"
  24. xpath = ".//list[@id='1']"
  25. value = "test value"
  26. state_return = {
  27. "name": name,
  28. "changes": {},
  29. "result": True,
  30. "comment": "{0} is already present".format(value),
  31. }
  32. with patch.dict(xml.__salt__, {"xml.get_value": MagicMock(return_value=value)}):
  33. self.assertDictEqual(xml.value_present(name, xpath, value), state_return)
  34. def test_value_update(self):
  35. """
  36. Test for updating value_present
  37. """
  38. name = "testfile.xml"
  39. xpath = ".//list[@id='1']"
  40. value = "test value"
  41. old_value = "not test value"
  42. state_return = {
  43. "name": name,
  44. "changes": {name: {"new": value, "old": old_value}},
  45. "result": True,
  46. "comment": "{0} updated".format(name),
  47. }
  48. with patch.dict(
  49. xml.__salt__, {"xml.get_value": MagicMock(return_value=old_value)}
  50. ):
  51. with patch.dict(
  52. xml.__salt__, {"xml.set_value": MagicMock(return_value=True)}
  53. ):
  54. self.assertDictEqual(
  55. xml.value_present(name, xpath, value), state_return
  56. )
  57. def test_value_update_test(self):
  58. """
  59. Test for value_present test=True
  60. """
  61. name = "testfile.xml"
  62. xpath = ".//list[@id='1']"
  63. value = "test value"
  64. old_value = "not test value"
  65. state_return = {
  66. "name": name,
  67. "changes": {name: {"old": old_value, "new": value}},
  68. "result": None,
  69. "comment": "{0} will be updated".format(name),
  70. }
  71. with patch.dict(
  72. xml.__salt__, {"xml.get_value": MagicMock(return_value=old_value)}
  73. ):
  74. self.assertDictEqual(
  75. xml.value_present(name, xpath, value, test=True), state_return
  76. )
  77. def test_value_update_invalid_xpath(self):
  78. """
  79. Test for value_present invalid xpath
  80. """
  81. name = "testfile.xml"
  82. xpath = ".//list[@id='1']"
  83. value = "test value"
  84. state_return = {
  85. "name": name,
  86. "changes": {},
  87. "result": False,
  88. "comment": "xpath query {0} not found in {1}".format(xpath, name),
  89. }
  90. with patch.dict(xml.__salt__, {"xml.get_value": MagicMock(return_value=False)}):
  91. self.assertDictEqual(
  92. xml.value_present(name, xpath, value, test=True), state_return
  93. )