test_npm.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.npm as npm
  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 NpmTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.npm
  17. """
  18. def setup_loader_modules(self):
  19. return {npm: {"__opts__": {"test": False}}}
  20. # 'installed' function tests: 1
  21. def test_installed(self):
  22. """
  23. Test to verify that the given package is installed
  24. and is at the correct version.
  25. """
  26. name = "coffee-script"
  27. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  28. mock_err = MagicMock(side_effect=CommandExecutionError)
  29. mock_dict = MagicMock(return_value={name: {"version": "1.2"}})
  30. with patch.dict(npm.__salt__, {"npm.list": mock_err}):
  31. comt = "Error looking up 'coffee-script': "
  32. ret.update({"comment": comt})
  33. self.assertDictEqual(npm.installed(name), ret)
  34. with patch.dict(npm.__salt__, {"npm.list": mock_dict, "npm.install": mock_err}):
  35. with patch.dict(npm.__opts__, {"test": True}):
  36. comt = "Package(s) 'coffee-script' " "satisfied by coffee-script@1.2"
  37. ret.update({"comment": comt, "result": True})
  38. self.assertDictEqual(npm.installed(name), ret)
  39. with patch.dict(npm.__opts__, {"test": False}):
  40. comt = "Package(s) 'coffee-script' " "satisfied by coffee-script@1.2"
  41. ret.update({"comment": comt, "result": True})
  42. self.assertDictEqual(npm.installed(name), ret)
  43. comt = "Error installing 'n, p, m': "
  44. ret.update({"comment": comt, "result": False})
  45. self.assertDictEqual(npm.installed(name, "npm"), ret)
  46. with patch.dict(npm.__salt__, {"npm.install": mock_dict}):
  47. comt = "Package(s) 'n, p, m' successfully installed"
  48. ret.update(
  49. {
  50. "comment": comt,
  51. "result": True,
  52. "changes": {"new": ["n", "p", "m"], "old": []},
  53. }
  54. )
  55. self.assertDictEqual(npm.installed(name, "npm"), ret)
  56. # 'removed' function tests: 1
  57. def test_removed(self):
  58. """
  59. Test to verify that the given package is not installed.
  60. """
  61. name = "coffee-script"
  62. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  63. mock_err = MagicMock(
  64. side_effect=[CommandExecutionError, {}, {name: ""}, {name: ""}]
  65. )
  66. mock_t = MagicMock(return_value=True)
  67. with patch.dict(npm.__salt__, {"npm.list": mock_err, "npm.uninstall": mock_t}):
  68. comt = "Error uninstalling 'coffee-script': "
  69. ret.update({"comment": comt})
  70. self.assertDictEqual(npm.removed(name), ret)
  71. comt = "Package 'coffee-script' is not installed"
  72. ret.update({"comment": comt, "result": True})
  73. self.assertDictEqual(npm.removed(name), ret)
  74. with patch.dict(npm.__opts__, {"test": True}):
  75. comt = "Package 'coffee-script' is set to be removed"
  76. ret.update({"comment": comt, "result": None})
  77. self.assertDictEqual(npm.removed(name), ret)
  78. with patch.dict(npm.__opts__, {"test": False}):
  79. comt = "Package 'coffee-script' was successfully removed"
  80. ret.update(
  81. {"comment": comt, "result": True, "changes": {name: "Removed"}}
  82. )
  83. self.assertDictEqual(npm.removed(name), ret)
  84. # 'bootstrap' function tests: 1
  85. def test_bootstrap(self):
  86. """
  87. Test to bootstraps a node.js application.
  88. """
  89. name = "coffee-script"
  90. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  91. mock_err = MagicMock(side_effect=[CommandExecutionError, False, True])
  92. with patch.dict(npm.__salt__, {"npm.install": mock_err}):
  93. comt = "Error Bootstrapping 'coffee-script': "
  94. ret.update({"comment": comt})
  95. self.assertDictEqual(npm.bootstrap(name), ret)
  96. comt = "Directory is already bootstrapped"
  97. ret.update({"comment": comt, "result": True})
  98. self.assertDictEqual(npm.bootstrap(name), ret)
  99. comt = "Directory was successfully bootstrapped"
  100. ret.update(
  101. {"comment": comt, "result": True, "changes": {name: "Bootstrapped"}}
  102. )
  103. self.assertDictEqual(npm.bootstrap(name), ret)
  104. # 'bootstrap' function tests: 1
  105. def test_cache_cleaned(self):
  106. """
  107. Test to verify that the npm cache is cleaned.
  108. """
  109. name = "coffee-script"
  110. pkg_ret = {"name": name, "result": False, "comment": "", "changes": {}}
  111. ret = {"name": None, "result": False, "comment": "", "changes": {}}
  112. mock_list = MagicMock(return_value=["~/.npm", "~/.npm/{0}/".format(name)])
  113. mock_cache_clean_success = MagicMock(return_value=True)
  114. mock_cache_clean_failure = MagicMock(return_value=False)
  115. mock_err = MagicMock(side_effect=CommandExecutionError)
  116. with patch.dict(npm.__salt__, {"npm.cache_list": mock_err}):
  117. comt = "Error looking up cached packages: "
  118. ret.update({"comment": comt})
  119. self.assertDictEqual(npm.cache_cleaned(), ret)
  120. with patch.dict(npm.__salt__, {"npm.cache_list": mock_err}):
  121. comt = "Error looking up cached {0}: ".format(name)
  122. pkg_ret.update({"comment": comt})
  123. self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)
  124. mock_data = {"npm.cache_list": mock_list, "npm.cache_clean": MagicMock()}
  125. with patch.dict(npm.__salt__, mock_data):
  126. non_cached_pkg = "salt"
  127. comt = "Package {0} is not in the cache".format(non_cached_pkg)
  128. pkg_ret.update({"name": non_cached_pkg, "result": True, "comment": comt})
  129. self.assertDictEqual(npm.cache_cleaned(non_cached_pkg), pkg_ret)
  130. pkg_ret.update({"name": name})
  131. with patch.dict(npm.__opts__, {"test": True}):
  132. comt = "Cached packages set to be removed"
  133. ret.update({"result": None, "comment": comt})
  134. self.assertDictEqual(npm.cache_cleaned(), ret)
  135. with patch.dict(npm.__opts__, {"test": True}):
  136. comt = "Cached {0} set to be removed".format(name)
  137. pkg_ret.update({"result": None, "comment": comt})
  138. self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)
  139. with patch.dict(npm.__opts__, {"test": False}):
  140. comt = "Cached packages successfully removed"
  141. ret.update(
  142. {"result": True, "comment": comt, "changes": {"cache": "Removed"}}
  143. )
  144. self.assertDictEqual(npm.cache_cleaned(), ret)
  145. with patch.dict(npm.__opts__, {"test": False}):
  146. comt = "Cached {0} successfully removed".format(name)
  147. pkg_ret.update(
  148. {"result": True, "comment": comt, "changes": {name: "Removed"}}
  149. )
  150. self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)
  151. mock_data = {
  152. "npm.cache_list": mock_list,
  153. "npm.cache_clean": MagicMock(return_value=False),
  154. }
  155. with patch.dict(npm.__salt__, mock_data):
  156. with patch.dict(npm.__opts__, {"test": False}):
  157. comt = "Error cleaning cached packages"
  158. ret.update({"result": False, "comment": comt})
  159. ret["changes"] = {}
  160. self.assertDictEqual(npm.cache_cleaned(), ret)
  161. with patch.dict(npm.__opts__, {"test": False}):
  162. comt = "Error cleaning cached {0}".format(name)
  163. pkg_ret.update({"result": False, "comment": comt})
  164. pkg_ret["changes"] = {}
  165. self.assertDictEqual(npm.cache_cleaned(name), pkg_ret)