test_pyenv.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.modules.pyenv as pyenv
  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 PyenvTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.pyenv
  16. """
  17. def setup_loader_modules(self):
  18. return {pyenv: {}}
  19. # 'install' function tests: 1
  20. def test_install(self):
  21. """
  22. Test if it install pyenv systemwide
  23. """
  24. mock_opt = MagicMock(return_value="salt stack")
  25. mock_ret = MagicMock(return_value=0)
  26. with patch.dict(
  27. pyenv.__salt__, {"config.option": mock_opt, "cmd.retcode": mock_ret}
  28. ):
  29. self.assertTrue(pyenv.install())
  30. # 'update' function tests: 1
  31. def test_update(self):
  32. """
  33. Test if it updates the current versions of pyenv and python-Build
  34. """
  35. mock_opt = MagicMock(return_value="salt stack")
  36. with patch.dict(pyenv.__salt__, {"config.option": mock_opt}):
  37. self.assertFalse(pyenv.update())
  38. # 'is_installed' function tests: 1
  39. def test_is_installed(self):
  40. """
  41. Test if it check if pyenv is installed.
  42. """
  43. mock_cmd = MagicMock(return_value=True)
  44. mock_opt = MagicMock(return_value="salt stack")
  45. with patch.dict(
  46. pyenv.__salt__, {"config.option": mock_opt, "cmd.has_exec": mock_cmd}
  47. ):
  48. self.assertTrue(pyenv.is_installed())
  49. # 'install_python' function tests: 1
  50. def test_install_python(self):
  51. """
  52. Test if it install a python implementation.
  53. """
  54. mock_opt = MagicMock(return_value="salt stack")
  55. mock_cmd = MagicMock(return_value=True)
  56. mock_all = MagicMock(
  57. return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
  58. )
  59. with patch.dict(pyenv.__grains__, {"os": "Linux"}):
  60. mock_all = MagicMock(
  61. return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
  62. )
  63. with patch.dict(
  64. pyenv.__salt__,
  65. {
  66. "config.option": mock_opt,
  67. "cmd.has_exec": mock_cmd,
  68. "cmd.run_all": mock_all,
  69. },
  70. ):
  71. self.assertEqual(pyenv.install_python("2.0.0-p0"), "error")
  72. mock_all = MagicMock(
  73. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  74. )
  75. with patch.dict(
  76. pyenv.__salt__,
  77. {
  78. "config.option": mock_opt,
  79. "cmd.has_exec": mock_cmd,
  80. "cmd.run_all": mock_all,
  81. },
  82. ):
  83. self.assertFalse(pyenv.install_python("2.0.0-p0"))
  84. # 'uninstall_python' function tests: 1
  85. def test_uninstall_python(self):
  86. """
  87. Test if it uninstall a python implementation.
  88. """
  89. mock_opt = MagicMock(return_value="salt stack")
  90. mock_cmd = MagicMock(return_value=True)
  91. mock_all = MagicMock(
  92. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  93. )
  94. with patch.dict(
  95. pyenv.__salt__,
  96. {
  97. "cmd.has_exec": mock_cmd,
  98. "config.option": mock_opt,
  99. "cmd.run_all": mock_all,
  100. },
  101. ):
  102. self.assertTrue(pyenv.uninstall_python("2.0.0-p0"))
  103. # 'versions' function tests: 1
  104. def test_versions(self):
  105. """
  106. Test if it list the installed versions of python.
  107. """
  108. mock_opt = MagicMock(return_value="salt stack")
  109. mock_cmd = MagicMock(return_value=True)
  110. mock_all = MagicMock(
  111. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  112. )
  113. with patch.dict(
  114. pyenv.__salt__,
  115. {
  116. "cmd.has_exec": mock_cmd,
  117. "config.option": mock_opt,
  118. "cmd.run_all": mock_all,
  119. },
  120. ):
  121. self.assertListEqual(pyenv.versions(), [])
  122. # 'default' function tests: 1
  123. def test_default(self):
  124. """
  125. Test if it returns or sets the currently defined default python.
  126. """
  127. mock_opt = MagicMock(return_value="salt stack")
  128. mock_cmd = MagicMock(return_value=True)
  129. mock_all = MagicMock(
  130. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  131. )
  132. with patch.dict(
  133. pyenv.__salt__,
  134. {
  135. "cmd.has_exec": mock_cmd,
  136. "config.option": mock_opt,
  137. "cmd.run_all": mock_all,
  138. },
  139. ):
  140. self.assertEqual(pyenv.default(), "")
  141. self.assertTrue(pyenv.default("2.0.0-p0"))
  142. # 'list_' function tests: 1
  143. def test_list(self):
  144. """
  145. Test if it list the installable versions of python.
  146. """
  147. mock_opt = MagicMock(return_value="salt stack")
  148. mock_cmd = MagicMock(return_value=True)
  149. mock_all = MagicMock(
  150. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  151. )
  152. with patch.dict(
  153. pyenv.__salt__,
  154. {
  155. "cmd.has_exec": mock_cmd,
  156. "config.option": mock_opt,
  157. "cmd.run_all": mock_all,
  158. },
  159. ):
  160. self.assertListEqual(pyenv.list_(), [])
  161. # 'rehash' function tests: 1
  162. def test_rehash(self):
  163. """
  164. Test if it run pyenv rehash to update the installed shims.
  165. """
  166. mock_opt = MagicMock(return_value="salt stack")
  167. mock_cmd = MagicMock(return_value=True)
  168. mock_all = MagicMock(
  169. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  170. )
  171. with patch.dict(
  172. pyenv.__salt__,
  173. {
  174. "cmd.has_exec": mock_cmd,
  175. "config.option": mock_opt,
  176. "cmd.run_all": mock_all,
  177. },
  178. ):
  179. self.assertTrue(pyenv.rehash())
  180. # 'do' function tests: 1
  181. def test_do(self):
  182. """
  183. Test if it execute a python command with pyenv's
  184. shims from the user or the system.
  185. """
  186. mock_opt = MagicMock(return_value="salt stack")
  187. mock_cmd = MagicMock(return_value=True)
  188. mock_all = MagicMock(
  189. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  190. )
  191. with patch.dict(
  192. pyenv.__salt__,
  193. {
  194. "cmd.has_exec": mock_cmd,
  195. "config.option": mock_opt,
  196. "cmd.run_all": mock_all,
  197. },
  198. ):
  199. self.assertFalse(pyenv.do("gem list bundler"))
  200. mock_all = MagicMock(
  201. return_value={"retcode": 0, "stdout": "salt", "stderr": "error"}
  202. )
  203. with patch.dict(
  204. pyenv.__salt__,
  205. {
  206. "config.option": mock_opt,
  207. "cmd.has_exec": mock_cmd,
  208. "cmd.run_all": mock_all,
  209. },
  210. ):
  211. self.assertEqual(pyenv.do("gem list bundler"), "salt")
  212. # 'do_with_python' function tests: 1
  213. def test_do_with_python(self):
  214. """
  215. Test if it execute a python command with pyenv's
  216. shims using a specific python version.
  217. """
  218. mock_opt = MagicMock(return_value="salt stack")
  219. mock_cmd = MagicMock(return_value=True)
  220. mock_all = MagicMock(
  221. return_value={"retcode": True, "stdout": "salt", "stderr": "error"}
  222. )
  223. with patch.dict(
  224. pyenv.__salt__,
  225. {
  226. "cmd.has_exec": mock_cmd,
  227. "config.option": mock_opt,
  228. "cmd.run_all": mock_all,
  229. },
  230. ):
  231. self.assertFalse(pyenv.do_with_python("2.0.0-p0", "gem list bundler"))