test_alternatives.py 7.9 KB

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