test_rbenv.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.rbenv as rbenv
  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 RbenvTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.rbenv
  16. """
  17. def setup_loader_modules(self):
  18. return {rbenv: {}}
  19. # 'installed' function tests: 1
  20. def test_installed(self):
  21. """
  22. Test to verify that the specified ruby is installed with rbenv.
  23. """
  24. # rbenv.is_installed is used wherever test is False.
  25. mock_is = MagicMock(side_effect=[False, True, True, True, True])
  26. # rbenv.install is only called when an action is attempted
  27. # (ie. Successfully... or Failed...)
  28. mock_i = MagicMock(side_effect=[False, False, False])
  29. # rbenv.install_ruby is only called when rbenv is successfully
  30. # installed and an attempt to install a version of Ruby is
  31. # made.
  32. mock_ir = MagicMock(side_effect=[True, False])
  33. mock_def = MagicMock(return_value="2.3.4")
  34. mock_ver = MagicMock(return_value=["2.3.4", "2.4.1"])
  35. with patch.dict(
  36. rbenv.__salt__,
  37. {
  38. "rbenv.is_installed": mock_is,
  39. "rbenv.install": mock_i,
  40. "rbenv.default": mock_def,
  41. "rbenv.versions": mock_ver,
  42. "rbenv.install_ruby": mock_ir,
  43. },
  44. ):
  45. with patch.dict(rbenv.__opts__, {"test": True}):
  46. name = "1.9.3-p551"
  47. comt = "Ruby {0} is set to be installed".format(name)
  48. ret = {"name": name, "changes": {}, "comment": comt, "result": None}
  49. self.assertDictEqual(rbenv.installed(name), ret)
  50. name = "2.4.1"
  51. comt = "Ruby {0} is already installed".format(name)
  52. ret = {
  53. "name": name,
  54. "changes": {},
  55. "comment": comt,
  56. "default": False,
  57. "result": True,
  58. }
  59. self.assertDictEqual(rbenv.installed(name), ret)
  60. name = "2.3.4"
  61. comt = "Ruby {0} is already installed".format(name)
  62. ret = {
  63. "name": name,
  64. "changes": {},
  65. "comment": comt,
  66. "default": True,
  67. "result": True,
  68. }
  69. self.assertDictEqual(rbenv.installed(name), ret)
  70. with patch.dict(rbenv.__opts__, {"test": False}):
  71. name = "2.4.1"
  72. comt = "Rbenv failed to install"
  73. ret = {"name": name, "changes": {}, "comment": comt, "result": False}
  74. self.assertDictEqual(rbenv.installed(name), ret)
  75. comt = "Requested ruby exists"
  76. ret = {
  77. "name": name,
  78. "comment": comt,
  79. "default": False,
  80. "changes": {},
  81. "result": True,
  82. }
  83. self.assertDictEqual(rbenv.installed(name), ret)
  84. name = "2.3.4"
  85. comt = "Requested ruby exists"
  86. ret = {
  87. "name": name,
  88. "comment": comt,
  89. "default": True,
  90. "changes": {},
  91. "result": True,
  92. }
  93. self.assertDictEqual(rbenv.installed(name), ret)
  94. name = "1.9.3-p551"
  95. comt = "Successfully installed ruby"
  96. ret = {
  97. "name": name,
  98. "comment": comt,
  99. "default": False,
  100. "changes": {name: "Installed"},
  101. "result": True,
  102. }
  103. self.assertDictEqual(rbenv.installed(name), ret)
  104. comt = "Failed to install ruby"
  105. ret = {"name": name, "comment": comt, "changes": {}, "result": False}
  106. self.assertDictEqual(rbenv.installed(name), ret)
  107. # 'absent' function tests: 1
  108. def test_absent(self):
  109. """
  110. Test to verify that the specified ruby is not installed with rbenv.
  111. """
  112. # rbenv.is_installed is used for all tests here.
  113. mock_is = MagicMock(
  114. side_effect=[False, True, True, True, False, True, True, True, True, True]
  115. )
  116. # rbenv.uninstall_ruby is only called when an action is
  117. # attempted (ie. Successfully... or Failed...)
  118. mock_uninstalled = MagicMock(side_effect=[True, False, False, True])
  119. mock_def = MagicMock(return_value="2.3.4")
  120. mock_ver = MagicMock(return_value=["2.3.4", "2.4.1"])
  121. with patch.dict(
  122. rbenv.__salt__,
  123. {
  124. "rbenv.is_installed": mock_is,
  125. "rbenv.default": mock_def,
  126. "rbenv.versions": mock_ver,
  127. "rbenv.uninstall_ruby": mock_uninstalled,
  128. },
  129. ):
  130. with patch.dict(rbenv.__opts__, {"test": True}):
  131. name = "1.9.3-p551"
  132. comt = "Rbenv not installed, {0} not either".format(name)
  133. ret = {"name": name, "changes": {}, "comment": comt, "result": True}
  134. self.assertDictEqual(rbenv.absent(name), ret)
  135. comt = "Ruby {0} is already uninstalled".format(name)
  136. ret = {"name": name, "changes": {}, "comment": comt, "result": True}
  137. self.assertDictEqual(rbenv.absent(name), ret)
  138. name = "2.3.4"
  139. comt = "Ruby {0} is set to be uninstalled".format(name)
  140. ret = {
  141. "name": name,
  142. "changes": {},
  143. "comment": comt,
  144. "default": True,
  145. "result": None,
  146. }
  147. self.assertDictEqual(rbenv.absent("2.3.4"), ret)
  148. name = "2.4.1"
  149. comt = "Ruby {0} is set to be uninstalled".format(name)
  150. ret = {
  151. "name": name,
  152. "changes": {},
  153. "comment": comt,
  154. "default": False,
  155. "result": None,
  156. }
  157. self.assertDictEqual(rbenv.absent("2.4.1"), ret)
  158. with patch.dict(rbenv.__opts__, {"test": False}):
  159. name = "1.9.3-p551"
  160. comt = "Rbenv not installed, {0} not either".format(name)
  161. ret = {"name": name, "changes": {}, "comment": comt, "result": True}
  162. self.assertDictEqual(rbenv.absent(name), ret)
  163. comt = "Ruby {0} is already absent".format(name)
  164. ret = {"name": name, "changes": {}, "comment": comt, "result": True}
  165. self.assertDictEqual(rbenv.absent(name), ret)
  166. name = "2.3.4"
  167. comt = "Successfully removed ruby"
  168. ret = {
  169. "name": name,
  170. "changes": {name: "Uninstalled"},
  171. "comment": comt,
  172. "default": True,
  173. "result": True,
  174. }
  175. self.assertDictEqual(rbenv.absent(name), ret)
  176. comt = "Failed to uninstall ruby"
  177. ret = {
  178. "name": name,
  179. "changes": {},
  180. "comment": comt,
  181. "default": True,
  182. "result": False,
  183. }
  184. self.assertDictEqual(rbenv.absent(name), ret)
  185. name = "2.4.1"
  186. comt = "Failed to uninstall ruby"
  187. ret = {
  188. "name": name,
  189. "changes": {},
  190. "comment": comt,
  191. "default": False,
  192. "result": False,
  193. }
  194. self.assertDictEqual(rbenv.absent(name), ret)
  195. comt = "Successfully removed ruby"
  196. ret = {
  197. "name": name,
  198. "changes": {name: "Uninstalled"},
  199. "comment": comt,
  200. "default": False,
  201. "result": True,
  202. }
  203. self.assertDictEqual(rbenv.absent(name), ret)
  204. # 'install_rbenv' function tests: 1
  205. def test_install_rbenv(self):
  206. """
  207. Test to install rbenv if not installed.
  208. """
  209. name = "myqueue"
  210. ret = {"name": name, "changes": {}, "result": True, "comment": ""}
  211. mock_is = MagicMock(side_effect=[False, True, True, False, False])
  212. mock_i = MagicMock(side_effect=[False, True])
  213. with patch.dict(
  214. rbenv.__salt__, {"rbenv.is_installed": mock_is, "rbenv.install": mock_i}
  215. ):
  216. with patch.dict(rbenv.__opts__, {"test": True}):
  217. comt = "Rbenv is set to be installed"
  218. ret.update({"comment": comt, "result": None})
  219. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  220. comt = "Rbenv is already installed"
  221. ret.update({"comment": comt, "result": True})
  222. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  223. with patch.dict(rbenv.__opts__, {"test": False}):
  224. comt = "Rbenv is already installed"
  225. ret.update({"comment": comt, "result": True})
  226. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  227. comt = "Rbenv failed to install"
  228. ret.update({"comment": comt, "result": False})
  229. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  230. comt = "Rbenv installed"
  231. ret.update({"comment": comt, "result": True})
  232. self.assertDictEqual(rbenv.install_rbenv(name), ret)