test_alternatives.py 7.8 KB

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