test_alternatives.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.alternatives as alternatives
  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 AlternativesTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.alternatives
  16. """
  17. def setup_loader_modules(self):
  18. return {alternatives: {}}
  19. # 'install' function tests: 1
  20. def test_install(self):
  21. """
  22. Test to install new alternative for defined <name>
  23. """
  24. name = "pager"
  25. link = "/usr/bin/pager"
  26. path = "/usr/bin/less"
  27. priority = 5
  28. ret = {
  29. "name": name,
  30. "link": link,
  31. "path": path,
  32. "priority": priority,
  33. "result": None,
  34. "changes": {},
  35. "comment": "",
  36. }
  37. bad_link = "/bin/pager"
  38. err = "the primary link for {0} must be {1}".format(name, link)
  39. mock_cinst = MagicMock(side_effect=[True, False])
  40. mock_cexist = MagicMock(
  41. side_effect=[True, False, False, True, False, False, False, True]
  42. )
  43. mock_out = MagicMock(side_effect=["", err, ""])
  44. mock_path = MagicMock(return_value=path)
  45. mock_link = MagicMock(return_value=link)
  46. with patch.dict(
  47. alternatives.__salt__,
  48. {
  49. "alternatives.check_installed": mock_cinst,
  50. "alternatives.check_exists": mock_cexist,
  51. "alternatives.install": mock_out,
  52. "alternatives.show_current": mock_path,
  53. "alternatives.show_link": mock_link,
  54. },
  55. ):
  56. comt = "Alternative {0} for {1} is already registered".format(path, name)
  57. ret.update({"comment": comt, "result": True})
  58. self.assertDictEqual(alternatives.install(name, link, path, priority), ret)
  59. comt = "Alternative will be set for {0} to {1} with priority {2}".format(
  60. name, path, priority
  61. )
  62. ret.update({"comment": comt, "result": None})
  63. with patch.dict(alternatives.__opts__, {"test": True}):
  64. self.assertDictEqual(
  65. alternatives.install(name, link, path, priority), ret
  66. )
  67. comt = "Alternative for {0} set to path {1} with priority {2}".format(
  68. name, path, priority
  69. )
  70. ret.update(
  71. {
  72. "comment": comt,
  73. "result": True,
  74. "changes": {
  75. "name": name,
  76. "link": link,
  77. "path": path,
  78. "priority": priority,
  79. },
  80. }
  81. )
  82. with patch.dict(alternatives.__opts__, {"test": False}):
  83. self.assertDictEqual(
  84. alternatives.install(name, link, path, priority), ret
  85. )
  86. comt = "Alternative for {0} not installed: {1}".format(name, err)
  87. ret.update(
  88. {"comment": comt, "result": False, "changes": {}, "link": bad_link}
  89. )
  90. with patch.dict(alternatives.__opts__, {"test": False}):
  91. self.assertDictEqual(
  92. alternatives.install(name, bad_link, path, priority), ret
  93. )
  94. comt = "Alternative {0} for {1} registered with priority {2} and not set to default".format(
  95. path, name, priority
  96. )
  97. ret.update(
  98. {
  99. "comment": comt,
  100. "result": True,
  101. "changes": {
  102. "name": name,
  103. "link": link,
  104. "path": path,
  105. "priority": priority,
  106. },
  107. "link": link,
  108. }
  109. )
  110. with patch.dict(alternatives.__opts__, {"test": False}):
  111. self.assertDictEqual(
  112. alternatives.install(name, link, path, priority), ret
  113. )
  114. # 'remove' function tests: 1
  115. def test_remove(self):
  116. """
  117. Test to removes installed alternative for defined <name> and <path>
  118. or fallback to default alternative, if some defined before.
  119. """
  120. name = "pager"
  121. path = "/usr/bin/less"
  122. ret = {"name": name, "path": path, "result": None, "changes": {}, "comment": ""}
  123. mock = MagicMock(side_effect=[True, True, True, False, False])
  124. mock_bool = MagicMock(return_value=True)
  125. mock_show = MagicMock(side_effect=[False, True, True, False])
  126. with patch.dict(
  127. alternatives.__salt__,
  128. {
  129. "alternatives.check_exists": mock,
  130. "alternatives.show_current": mock_show,
  131. "alternatives.remove": mock_bool,
  132. },
  133. ):
  134. comt = "Alternative for {0} will be removed".format(name)
  135. ret.update({"comment": comt})
  136. with patch.dict(alternatives.__opts__, {"test": True}):
  137. self.assertDictEqual(alternatives.remove(name, path), ret)
  138. comt = "Alternative for {0} removed".format(name)
  139. ret.update({"comment": comt, "result": True})
  140. with patch.dict(alternatives.__opts__, {"test": False}):
  141. self.assertDictEqual(alternatives.remove(name, path), ret)
  142. comt = "Alternative for pager removed. Falling back to path True"
  143. ret.update({"comment": comt, "result": True, "changes": {"path": True}})
  144. with patch.dict(alternatives.__opts__, {"test": False}):
  145. self.assertDictEqual(alternatives.remove(name, path), ret)
  146. comt = "Alternative for {0} is set to it's default path True".format(name)
  147. ret.update({"comment": comt, "result": True, "changes": {}})
  148. self.assertDictEqual(alternatives.remove(name, path), ret)
  149. comt = "Alternative for {0} doesn't exist".format(name)
  150. ret.update({"comment": comt, "result": False})
  151. self.assertDictEqual(alternatives.remove(name, path), ret)
  152. # 'auto' function tests: 1
  153. def test_auto(self):
  154. """
  155. Test to instruct alternatives to use the highest priority
  156. path for <name>
  157. """
  158. name = "pager"
  159. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  160. mock = MagicMock(side_effect=[" auto mode", " ", " "])
  161. mock_auto = MagicMock(return_value=True)
  162. with patch.dict(
  163. alternatives.__salt__,
  164. {"alternatives.display": mock, "alternatives.auto": mock_auto},
  165. ):
  166. comt = "{0} already in auto mode".format(name)
  167. ret.update({"comment": comt})
  168. self.assertDictEqual(alternatives.auto(name), ret)
  169. comt = "{0} will be put in auto mode".format(name)
  170. ret.update({"comment": comt, "result": None})
  171. with patch.dict(alternatives.__opts__, {"test": True}):
  172. self.assertDictEqual(alternatives.auto(name), ret)
  173. ret.update({"comment": "", "result": True, "changes": {"result": True}})
  174. with patch.dict(alternatives.__opts__, {"test": False}):
  175. self.assertDictEqual(alternatives.auto(name), ret)
  176. # 'set_' function tests: 1
  177. def test_set(self):
  178. """
  179. Test to sets alternative for <name> to <path>, if <path> is defined
  180. as an alternative for <name>.
  181. """
  182. name = "pager"
  183. path = "/usr/bin/less"
  184. ret = {"name": name, "path": path, "result": True, "changes": {}, "comment": ""}
  185. mock = MagicMock(side_effect=[path, path, ""])
  186. mock_bool = MagicMock(return_value=True)
  187. mock_show = MagicMock(side_effect=[path, False, False, False, False])
  188. with patch.dict(
  189. alternatives.__salt__,
  190. {
  191. "alternatives.display": mock,
  192. "alternatives.show_current": mock_show,
  193. "alternatives.set": mock_bool,
  194. },
  195. ):
  196. comt = "Alternative for {0} already set to {1}".format(name, path)
  197. ret.update({"comment": comt})
  198. self.assertDictEqual(alternatives.set_(name, path), ret)
  199. comt = "Alternative for {0} will be set to path /usr/bin/less".format(name)
  200. ret.update({"comment": comt, "result": None})
  201. with patch.dict(alternatives.__opts__, {"test": True}):
  202. self.assertDictEqual(alternatives.set_(name, path), ret)
  203. comt = "Alternative for {0} not updated".format(name)
  204. ret.update({"comment": comt, "result": True})
  205. with patch.dict(alternatives.__opts__, {"test": False}):
  206. self.assertDictEqual(alternatives.set_(name, path), ret)
  207. comt = "Alternative {0} for {1} doesn't exist".format(path, name)
  208. ret.update({"comment": comt, "result": False})
  209. self.assertDictEqual(alternatives.set_(name, path), ret)