test_pyenv.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.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.states.pyenv
  16. """
  17. def setup_loader_modules(self):
  18. return {pyenv: {}}
  19. # 'installed' function tests: 1
  20. def test_installed(self):
  21. """
  22. Test to verify that the specified python is installed with pyenv.
  23. """
  24. name = "python-2.7.6"
  25. ret = {"name": name, "changes": {}, "result": None, "comment": ""}
  26. with patch.dict(pyenv.__opts__, {"test": True}):
  27. comt = "python 2.7.6 is set to be installed"
  28. ret.update({"comment": comt})
  29. self.assertDictEqual(pyenv.installed(name), ret)
  30. with patch.dict(pyenv.__opts__, {"test": False}):
  31. mock_f = MagicMock(side_effect=[False, False, True])
  32. mock_fa = MagicMock(side_effect=[False, True])
  33. mock_str = MagicMock(return_value="2.7.6")
  34. mock_lst = MagicMock(return_value=["2.7.6"])
  35. with patch.dict(
  36. pyenv.__salt__,
  37. {
  38. "pyenv.is_installed": mock_f,
  39. "pyenv.install": mock_fa,
  40. "pyenv.default": mock_str,
  41. "pyenv.versions": mock_lst,
  42. },
  43. ):
  44. comt = "pyenv failed to install"
  45. ret.update({"comment": comt, "result": False})
  46. self.assertDictEqual(pyenv.installed(name), ret)
  47. comt = "Requested python exists."
  48. ret.update({"comment": comt, "result": True, "default": True})
  49. self.assertDictEqual(pyenv.installed(name), ret)
  50. self.assertDictEqual(pyenv.installed(name), ret)
  51. # 'absent' function tests: 1
  52. def test_absent(self):
  53. """
  54. Test to verify that the specified python is not installed with pyenv.
  55. """
  56. name = "python-2.7.6"
  57. ret = {"name": name, "changes": {}, "result": None, "comment": ""}
  58. with patch.dict(pyenv.__opts__, {"test": True}):
  59. comt = "python 2.7.6 is set to be uninstalled"
  60. ret.update({"comment": comt})
  61. self.assertDictEqual(pyenv.absent(name), ret)
  62. with patch.dict(pyenv.__opts__, {"test": False}):
  63. mock_f = MagicMock(side_effect=[False, True])
  64. mock_t = MagicMock(return_value=True)
  65. mock_str = MagicMock(return_value="2.7.6")
  66. mock_lst = MagicMock(return_value=["2.7.6"])
  67. with patch.dict(
  68. pyenv.__salt__,
  69. {
  70. "pyenv.is_installed": mock_f,
  71. "pyenv.uninstall_python": mock_t,
  72. "pyenv.default": mock_str,
  73. "pyenv.versions": mock_lst,
  74. },
  75. ):
  76. comt = "pyenv not installed, 2.7.6 not either"
  77. ret.update({"comment": comt, "result": True})
  78. self.assertDictEqual(pyenv.absent(name), ret)
  79. comt = "Successfully removed python"
  80. ret.update(
  81. {
  82. "comment": comt,
  83. "result": True,
  84. "default": True,
  85. "changes": {"2.7.6": "Uninstalled"},
  86. }
  87. )
  88. self.assertDictEqual(pyenv.absent(name), ret)
  89. # 'install_pyenv' function tests: 1
  90. def test_install_pyenv(self):
  91. """
  92. Test to install pyenv if not installed.
  93. """
  94. name = "python-2.7.6"
  95. ret = {"name": name, "changes": {}, "result": None, "comment": ""}
  96. with patch.dict(pyenv.__opts__, {"test": True}):
  97. comt = "pyenv is set to be installed"
  98. ret.update({"comment": comt})
  99. self.assertDictEqual(pyenv.install_pyenv(name), ret)
  100. with patch.dict(pyenv.__opts__, {"test": False}):
  101. mock_t = MagicMock(return_value=True)
  102. mock_str = MagicMock(return_value="2.7.6")
  103. mock_lst = MagicMock(return_value=["2.7.6"])
  104. with patch.dict(
  105. pyenv.__salt__,
  106. {
  107. "pyenv.install_python": mock_t,
  108. "pyenv.default": mock_str,
  109. "pyenv.versions": mock_lst,
  110. },
  111. ):
  112. comt = "Successfully installed python"
  113. ret.update(
  114. {
  115. "comment": comt,
  116. "result": True,
  117. "default": False,
  118. "changes": {None: "Installed"},
  119. }
  120. )
  121. self.assertDictEqual(pyenv.install_pyenv(name), ret)