test_bower.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Alexander Pyatkin <asp@thexyz.net>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  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.bower as bower
  16. from salt.exceptions import CommandExecutionError
  17. class BowerTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.states.bower
  20. '''
  21. def setup_loader_modules(self):
  22. return {bower: {'__opts__': {'test': False}}}
  23. def test_removed_not_installed(self):
  24. '''
  25. Test if it returns True when specified package is not installed
  26. '''
  27. mock = MagicMock(return_value={'underscore': {}})
  28. with patch.dict(bower.__salt__, {'bower.list': mock}):
  29. ret = bower.removed('jquery', '/path/to/project')
  30. expected = {'name': 'jquery',
  31. 'result': True,
  32. 'comment': 'Package \'jquery\' is not installed',
  33. 'changes': {}}
  34. self.assertEqual(ret, expected)
  35. def test_removed_with_error(self):
  36. '''
  37. Test if returns False when list packages fails
  38. '''
  39. mock = MagicMock(side_effect=CommandExecutionError)
  40. with patch.dict(bower.__salt__, {'bower.list': mock}):
  41. ret = bower.removed('underscore', '/path/to/project')
  42. expected = {'name': 'underscore',
  43. 'result': False,
  44. 'comment': 'Error removing \'underscore\': ',
  45. 'changes': {}}
  46. self.assertEqual(ret, expected)
  47. def test_removed_existing(self):
  48. '''
  49. Test if it returns True when specified package is installed and
  50. uninstall succeeds
  51. '''
  52. mock_list = MagicMock(return_value={'underscore': {}})
  53. mock_uninstall = MagicMock(return_value=True)
  54. with patch.dict(bower.__salt__, {'bower.list': mock_list,
  55. 'bower.uninstall': mock_uninstall}):
  56. ret = bower.removed('underscore', '/path/to/project')
  57. expected = {'name': 'underscore',
  58. 'result': True,
  59. 'comment':
  60. 'Package \'underscore\' was successfully removed',
  61. 'changes': {'underscore': 'Removed'}}
  62. self.assertEqual(ret, expected)
  63. def test_removed_existing_with_error(self):
  64. '''
  65. Test if it returns False when specified package is installed and
  66. uninstall fails
  67. '''
  68. mock_list = MagicMock(return_value={'underscore': {}})
  69. mock_uninstall = MagicMock(side_effect=CommandExecutionError)
  70. with patch.dict(bower.__salt__, {'bower.list': mock_list,
  71. 'bower.uninstall': mock_uninstall}):
  72. ret = bower.removed('underscore', '/path/to/project')
  73. expected = {'name': 'underscore',
  74. 'result': False,
  75. 'comment':
  76. 'Error removing \'underscore\': ',
  77. 'changes': {}}
  78. self.assertEqual(ret, expected)
  79. def test_bootstrap_with_error(self):
  80. '''
  81. Test if it return False when install packages fails
  82. '''
  83. mock = MagicMock(side_effect=CommandExecutionError)
  84. with patch.dict(bower.__salt__, {'bower.install': mock}):
  85. ret = bower.bootstrap('/path/to/project')
  86. expected = {'name': '/path/to/project',
  87. 'result': False,
  88. 'comment':
  89. 'Error bootstrapping \'/path/to/project\': ',
  90. 'changes': {}}
  91. self.assertEqual(ret, expected)
  92. def test_bootstrap_not_needed(self):
  93. '''
  94. Test if it returns True when there is nothing to install
  95. '''
  96. mock = MagicMock(return_value=False)
  97. with patch.dict(bower.__salt__, {'bower.install': mock}):
  98. ret = bower.bootstrap('/path/to/project')
  99. expected = {'name': '/path/to/project',
  100. 'result': True,
  101. 'comment':
  102. 'Directory is already bootstrapped',
  103. 'changes': {}}
  104. self.assertEqual(ret, expected)
  105. def test_bootstrap_success(self):
  106. '''
  107. Test if it returns True when install packages succeeds
  108. '''
  109. mock = MagicMock(return_value=True)
  110. with patch.dict(bower.__salt__, {'bower.install': mock}):
  111. ret = bower.bootstrap('/path/to/project')
  112. expected = {'name': '/path/to/project',
  113. 'result': True,
  114. 'comment':
  115. 'Directory was successfully bootstrapped',
  116. 'changes': {'/path/to/project': 'Bootstrapped'}}
  117. self.assertEqual(ret, expected)
  118. def test_installed_with_error(self):
  119. '''
  120. Test if it returns False when list packages fails
  121. '''
  122. mock = MagicMock(side_effect=CommandExecutionError)
  123. with patch.dict(bower.__salt__, {'bower.list': mock}):
  124. ret = bower.installed('underscore', '/path/to/project')
  125. expected = {'name': 'underscore',
  126. 'result': False,
  127. 'comment': 'Error looking up \'underscore\': ',
  128. 'changes': {}}
  129. self.assertEqual(ret, expected)
  130. def test_installed_not_needed(self):
  131. '''
  132. Test if it returns True when there is nothing to install
  133. '''
  134. mock = MagicMock(return_value={
  135. 'underscore': {
  136. 'pkgMeta': {'version': '1.7.0'}},
  137. 'jquery': {
  138. 'pkgMeta': {'version': '2.0.0'}}})
  139. with patch.dict(bower.__salt__, {'bower.list': mock}):
  140. ret = bower.installed('test', '/path/to/project',
  141. ['underscore', 'jquery#2.0.0'])
  142. expected = {'name': 'test',
  143. 'result': True,
  144. 'comment':
  145. ('Package(s) \'underscore, jquery#2.0.0\''
  146. ' satisfied by underscore#1.7.0, jquery#2.0.0'),
  147. 'changes': {}}
  148. self.assertEqual(ret, expected)
  149. def test_installed_new_with_exc(self):
  150. '''
  151. Test if it returns False when install packages fails (exception)
  152. '''
  153. mock_list = MagicMock(return_value={})
  154. mock_install = MagicMock(side_effect=CommandExecutionError)
  155. with patch.dict(bower.__salt__, {'bower.list': mock_list,
  156. 'bower.install': mock_install}):
  157. ret = bower.installed('underscore', '/path/to/project')
  158. expected = {'name': 'underscore',
  159. 'result': False,
  160. 'comment': 'Error installing \'underscore\': ',
  161. 'changes': {}}
  162. self.assertEqual(ret, expected)
  163. def test_installed_new_with_error(self):
  164. '''
  165. Test if returns False when install packages fails (bower error)
  166. '''
  167. mock_list = MagicMock(return_value={})
  168. mock_install = MagicMock(return_value=False)
  169. with patch.dict(bower.__salt__, {'bower.list': mock_list,
  170. 'bower.install': mock_install}):
  171. ret = bower.installed('underscore', '/path/to/project')
  172. expected = {'name': 'underscore',
  173. 'result': False,
  174. 'comment':
  175. 'Could not install package(s) \'underscore\'',
  176. 'changes': {}}
  177. self.assertEqual(ret, expected)
  178. def test_installed_success(self):
  179. '''
  180. Test if it returns True when install succeeds
  181. '''
  182. mock_list = MagicMock(return_value={})
  183. mock_install = MagicMock(return_value=True)
  184. with patch.dict(bower.__salt__, {'bower.list': mock_list,
  185. 'bower.install': mock_install}):
  186. ret = bower.installed('underscore', '/path/to/project')
  187. expected = {'name': 'underscore',
  188. 'result': True,
  189. 'comment':
  190. 'Package(s) \'underscore\' successfully installed',
  191. 'changes': {'new': ['underscore'], 'old': []}}
  192. self.assertEqual(ret, expected)