test_bower.py 8.3 KB

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