test_rbenv.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.rbenv as rbenv
  16. class RbenvTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.rbenv
  19. '''
  20. def setup_loader_modules(self):
  21. return {rbenv: {}}
  22. # 'installed' function tests: 1
  23. def test_installed(self):
  24. '''
  25. Test to verify that the specified ruby is installed with rbenv.
  26. '''
  27. # rbenv.is_installed is used wherever test is False.
  28. mock_is = MagicMock(side_effect=[False, True, True, True, True])
  29. # rbenv.install is only called when an action is attempted
  30. # (ie. Successfully... or Failed...)
  31. mock_i = MagicMock(side_effect=[False, False, False])
  32. # rbenv.install_ruby is only called when rbenv is successfully
  33. # installed and an attempt to install a version of Ruby is
  34. # made.
  35. mock_ir = MagicMock(side_effect=[True, False])
  36. mock_def = MagicMock(return_value='2.3.4')
  37. mock_ver = MagicMock(return_value=['2.3.4', '2.4.1'])
  38. with patch.dict(rbenv.__salt__,
  39. {'rbenv.is_installed': mock_is,
  40. 'rbenv.install': mock_i,
  41. 'rbenv.default': mock_def,
  42. 'rbenv.versions': mock_ver,
  43. 'rbenv.install_ruby': mock_ir}):
  44. with patch.dict(rbenv.__opts__, {'test': True}):
  45. name = '1.9.3-p551'
  46. comt = 'Ruby {0} is set to be installed'.format(name)
  47. ret = {'name': name, 'changes': {}, 'comment': comt,
  48. '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 = {'name': name, 'changes': {}, 'comment': comt,
  53. 'default': False, 'result': True}
  54. self.assertDictEqual(rbenv.installed(name), ret)
  55. name = '2.3.4'
  56. comt = 'Ruby {0} is already installed'.format(name)
  57. ret = {'name': name, 'changes': {}, 'comment': comt,
  58. 'default': True, 'result': True}
  59. self.assertDictEqual(rbenv.installed(name), ret)
  60. with patch.dict(rbenv.__opts__, {'test': False}):
  61. name = '2.4.1'
  62. comt = 'Rbenv failed to install'
  63. ret = {'name': name, 'changes': {}, 'comment': comt,
  64. 'result': False}
  65. self.assertDictEqual(rbenv.installed(name), ret)
  66. comt = 'Requested ruby exists'
  67. ret = {'name': name, 'comment': comt, 'default': False,
  68. 'changes': {}, 'result': True}
  69. self.assertDictEqual(rbenv.installed(name), ret)
  70. name = '2.3.4'
  71. comt = 'Requested ruby exists'
  72. ret = {'name': name, 'comment': comt, 'default': True,
  73. 'changes': {}, 'result': True}
  74. self.assertDictEqual(rbenv.installed(name), ret)
  75. name = '1.9.3-p551'
  76. comt = 'Successfully installed ruby'
  77. ret = {'name': name, 'comment': comt, 'default': False,
  78. 'changes': {name: 'Installed'}, 'result': True}
  79. self.assertDictEqual(rbenv.installed(name), ret)
  80. comt = 'Failed to install ruby'
  81. ret = {'name': name, 'comment': comt,
  82. 'changes': {}, 'result': False}
  83. self.assertDictEqual(rbenv.installed(name), ret)
  84. # 'absent' function tests: 1
  85. def test_absent(self):
  86. '''
  87. Test to verify that the specified ruby is not installed with rbenv.
  88. '''
  89. # rbenv.is_installed is used for all tests here.
  90. mock_is = MagicMock(side_effect=[False, True, True, True, False,
  91. True, True, True, True, True])
  92. # rbenv.uninstall_ruby is only called when an action is
  93. # attempted (ie. Successfully... or Failed...)
  94. mock_uninstalled = MagicMock(side_effect=[True, False, False, True])
  95. mock_def = MagicMock(return_value='2.3.4')
  96. mock_ver = MagicMock(return_value=['2.3.4', '2.4.1'])
  97. with patch.dict(rbenv.__salt__,
  98. {'rbenv.is_installed': mock_is,
  99. 'rbenv.default': mock_def,
  100. 'rbenv.versions': mock_ver,
  101. 'rbenv.uninstall_ruby': mock_uninstalled}):
  102. with patch.dict(rbenv.__opts__, {'test': True}):
  103. name = '1.9.3-p551'
  104. comt = 'Rbenv not installed, {0} not either'.format(name)
  105. ret = {'name': name, 'changes': {}, 'comment': comt,
  106. 'result': True}
  107. self.assertDictEqual(rbenv.absent(name), ret)
  108. comt = 'Ruby {0} is already uninstalled'.format(name)
  109. ret = {'name': name, 'changes': {}, 'comment': comt,
  110. 'result': True}
  111. self.assertDictEqual(rbenv.absent(name), ret)
  112. name = '2.3.4'
  113. comt = 'Ruby {0} is set to be uninstalled'.format(name)
  114. ret = {'name': name, 'changes': {}, 'comment': comt,
  115. 'default': True, 'result': None}
  116. self.assertDictEqual(rbenv.absent('2.3.4'), ret)
  117. name = '2.4.1'
  118. comt = 'Ruby {0} is set to be uninstalled'.format(name)
  119. ret = {'name': name, 'changes': {}, 'comment': comt,
  120. 'default': False, 'result': None}
  121. self.assertDictEqual(rbenv.absent('2.4.1'), ret)
  122. with patch.dict(rbenv.__opts__, {'test': False}):
  123. name = '1.9.3-p551'
  124. comt = 'Rbenv not installed, {0} not either'.format(name)
  125. ret = {'name': name, 'changes': {}, 'comment': comt,
  126. 'result': True}
  127. self.assertDictEqual(rbenv.absent(name), ret)
  128. comt = 'Ruby {0} is already absent'.format(name)
  129. ret = {'name': name, 'changes': {}, 'comment': comt,
  130. 'result': True}
  131. self.assertDictEqual(rbenv.absent(name), ret)
  132. name = '2.3.4'
  133. comt = 'Successfully removed ruby'
  134. ret = {'name': name, 'changes': {name: 'Uninstalled'},
  135. 'comment': comt, 'default': True, 'result': True}
  136. self.assertDictEqual(rbenv.absent(name), ret)
  137. comt = 'Failed to uninstall ruby'
  138. ret = {'name': name, 'changes': {}, 'comment': comt,
  139. 'default': True, 'result': False}
  140. self.assertDictEqual(rbenv.absent(name), ret)
  141. name = '2.4.1'
  142. comt = 'Failed to uninstall ruby'
  143. ret = {'name': name, 'changes': {}, 'comment': comt,
  144. 'default': False, 'result': False}
  145. self.assertDictEqual(rbenv.absent(name), ret)
  146. comt = 'Successfully removed ruby'
  147. ret = {'name': name, 'changes': {name: 'Uninstalled'},
  148. 'comment': comt, 'default': False, 'result': True}
  149. self.assertDictEqual(rbenv.absent(name), ret)
  150. # 'install_rbenv' function tests: 1
  151. def test_install_rbenv(self):
  152. '''
  153. Test to install rbenv if not installed.
  154. '''
  155. name = 'myqueue'
  156. ret = {'name': name,
  157. 'changes': {},
  158. 'result': True,
  159. 'comment': ''}
  160. mock_is = MagicMock(side_effect=[False, True, True, False, False])
  161. mock_i = MagicMock(side_effect=[False, True])
  162. with patch.dict(rbenv.__salt__,
  163. {'rbenv.is_installed': mock_is,
  164. 'rbenv.install': mock_i}):
  165. with patch.dict(rbenv.__opts__, {'test': True}):
  166. comt = 'Rbenv is set to be installed'
  167. ret.update({'comment': comt, 'result': None})
  168. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  169. comt = 'Rbenv is already installed'
  170. ret.update({'comment': comt, 'result': True})
  171. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  172. with patch.dict(rbenv.__opts__, {'test': False}):
  173. comt = 'Rbenv is already installed'
  174. ret.update({'comment': comt, 'result': True})
  175. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  176. comt = 'Rbenv failed to install'
  177. ret.update({'comment': comt, 'result': False})
  178. self.assertDictEqual(rbenv.install_rbenv(name), ret)
  179. comt = 'Rbenv installed'
  180. ret.update({'comment': comt, 'result': True})
  181. self.assertDictEqual(rbenv.install_rbenv(name), ret)