test_ini_manage.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # 'sections_present' function tests: 1
  98. def test_sections_present(self):
  99. """
  100. Test to verify sections present in file.
  101. """
  102. name = "salt"
  103. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  104. with patch.dict(ini_manage.__opts__, {"test": True}):
  105. with patch.dict(
  106. ini_manage.__salt__, {"ini.get_ini": MagicMock(return_value=None)}
  107. ):
  108. comt = "No changes detected."
  109. ret.update({"comment": comt, "result": True})
  110. self.assertDictEqual(ini_manage.sections_present(name), ret)
  111. changes = {
  112. "first": "who is on",
  113. "second": "what is on",
  114. "third": "I don't know",
  115. }
  116. with patch.dict(
  117. ini_manage.__salt__, {"ini.set_option": MagicMock(return_value=changes)}
  118. ):
  119. with patch.dict(ini_manage.__opts__, {"test": False}):
  120. comt = "Changes take effect"
  121. ret.update({"comment": comt, "result": True, "changes": changes})
  122. self.assertDictEqual(ini_manage.sections_present(name), ret)
  123. # 'sections_absent' function tests: 1
  124. def test_sections_absent(self):
  125. """
  126. Test to verify sections absent in file.
  127. """
  128. name = "salt"
  129. ret = {"name": name, "result": None, "comment": "", "changes": {}}
  130. with patch.dict(ini_manage.__opts__, {"test": True}):
  131. with patch.dict(
  132. ini_manage.__salt__, {"ini.get_ini": MagicMock(return_value=None)}
  133. ):
  134. comt = "No changes detected."
  135. ret.update({"comment": comt, "result": True})
  136. self.assertDictEqual(ini_manage.sections_absent(name), ret)
  137. with patch.dict(ini_manage.__opts__, {"test": False}):
  138. comt = "No anomaly detected"
  139. ret.update({"comment": comt, "result": True})
  140. self.assertDictEqual(ini_manage.sections_absent(name), ret)