test_ini_manage.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.ini_manage as ini_manage
  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. # pylint: disable=no-member
  14. class IniManageTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.ini_manage
  17. """
  18. def setup_loader_modules(self):
  19. return {ini_manage: {}}
  20. # 'options_present' function tests: 1
  21. def test_options_present(self):
  22. """
  23. Test to verify options present in file.
  24. """
  25. name = "salt"
  26. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  27. with patch.dict(ini_manage.__opts__, {"test": True}):
  28. comt = ""
  29. ret.update({"comment": comt, "result": True})
  30. self.assertDictEqual(ini_manage.options_present(name), ret)
  31. changes = {
  32. "first": "who is on",
  33. "second": "what is on",
  34. "third": "I don't know",
  35. }
  36. with patch.dict(
  37. ini_manage.__salt__, {"ini.set_option": MagicMock(return_value=changes)}
  38. ):
  39. with patch.dict(ini_manage.__opts__, {"test": False}):
  40. comt = "Changes take effect"
  41. ret.update({"comment": comt, "result": True, "changes": changes})
  42. self.assertDictEqual(ini_manage.options_present(name), ret)
  43. original = {
  44. "mysection": {
  45. "first": "who is on",
  46. "second": "what is on",
  47. "third": "I don't know",
  48. }
  49. }
  50. desired = {"mysection": {"first": "who is on", "second": "what is on"}}
  51. changes = {
  52. "mysection": {
  53. "first": "who is on",
  54. "second": "what is on",
  55. "third": {"after": None, "before": "I don't know"},
  56. }
  57. }
  58. with patch.dict(
  59. ini_manage.__salt__, {"ini.get_ini": MagicMock(return_value=original)}
  60. ):
  61. with patch.dict(
  62. ini_manage.__salt__,
  63. {"ini.remove_option": MagicMock(return_value="third")},
  64. ):
  65. with patch.dict(
  66. ini_manage.__salt__,
  67. {"ini.get_option": MagicMock(return_value="I don't know")},
  68. ):
  69. with patch.dict(
  70. ini_manage.__salt__,
  71. {"ini.set_option": MagicMock(return_value=desired)},
  72. ):
  73. with patch.dict(ini_manage.__opts__, {"test": False}):
  74. comt = "Changes take effect"
  75. ret.update(
  76. {"comment": comt, "result": True, "changes": changes}
  77. )
  78. self.assertDictEqual(
  79. ini_manage.options_present(name, desired, strict=True),
  80. ret,
  81. )
  82. # 'options_absent' function tests: 1
  83. def test_options_absent(self):
  84. """
  85. Test to verify options absent in file.
  86. """
  87. name = "salt"
  88. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  89. with patch.dict(ini_manage.__opts__, {"test": True}):
  90. comt = "No changes detected."
  91. ret.update({"comment": comt, "result": True})
  92. self.assertDictEqual(ini_manage.options_absent(name), ret)
  93. with patch.dict(ini_manage.__opts__, {"test": False}):
  94. comt = "No anomaly detected"
  95. ret.update({"comment": comt, "result": True})
  96. self.assertDictEqual(ini_manage.options_absent(name), ret)
  97. original = {"Tables": {"key1": "1", "key2": "2", "key3": "3", "key4": "4"}}
  98. sections = {"Tables": ["key2", "key3"]}
  99. changes = {"Tables": {"key2": "2", "key3": "3"}}
  100. with patch.dict(
  101. ini_manage.__salt__,
  102. {"ini.remove_option": MagicMock(side_effect=["2", "3"])},
  103. ):
  104. with patch.dict(ini_manage.__opts__, {"test": False}):
  105. comt = "Changes take effect"
  106. ret.update({"comment": comt, "result": True, "changes": changes})
  107. self.assertDictEqual(ini_manage.options_absent(name, sections), ret)
  108. # 'sections_present' function tests: 1
  109. def test_sections_present(self):
  110. """
  111. Test to verify sections present in file.
  112. """
  113. name = "salt"
  114. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  115. with patch.dict(ini_manage.__opts__, {"test": True}):
  116. with patch.dict(
  117. ini_manage.__salt__, {"ini.get_ini": MagicMock(return_value=None)}
  118. ):
  119. comt = "No changes detected."
  120. ret.update({"comment": comt, "result": True})
  121. self.assertDictEqual(ini_manage.sections_present(name), ret)
  122. changes = {
  123. "first": "who is on",
  124. "second": "what is on",
  125. "third": "I don't know",
  126. }
  127. with patch.dict(
  128. ini_manage.__salt__, {"ini.set_option": MagicMock(return_value=changes)}
  129. ):
  130. with patch.dict(ini_manage.__opts__, {"test": False}):
  131. comt = "Changes take effect"
  132. ret.update({"comment": comt, "result": True, "changes": changes})
  133. self.assertDictEqual(ini_manage.sections_present(name), ret)
  134. # 'sections_absent' function tests: 1
  135. def test_sections_absent(self):
  136. """
  137. Test to verify sections absent in file.
  138. """
  139. name = "salt"
  140. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  141. with patch.dict(ini_manage.__opts__, {"test": True}):
  142. with patch.dict(
  143. ini_manage.__salt__, {"ini.get_ini": MagicMock(return_value=None)}
  144. ):
  145. comt = "No changes detected."
  146. ret.update({"comment": comt, "result": True})
  147. self.assertDictEqual(ini_manage.sections_absent(name), ret)
  148. with patch.dict(ini_manage.__opts__, {"test": False}):
  149. comt = "No anomaly detected"
  150. ret.update({"comment": comt, "result": True})
  151. self.assertDictEqual(ini_manage.sections_absent(name), ret)