test_npm.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 textwrap
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.utils.json
  17. import salt.modules.npm as npm
  18. from salt.exceptions import CommandExecutionError
  19. class NpmTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. Test cases for salt.modules.npm
  22. '''
  23. def setup_loader_modules(self):
  24. patcher = patch('salt.modules.npm._check_valid_version',
  25. MagicMock(return_value=True))
  26. patcher.start()
  27. self.addCleanup(patcher.stop)
  28. return {npm: {}}
  29. # 'install' function tests: 4
  30. def test_install(self):
  31. '''
  32. Test if it installs an NPM package.
  33. '''
  34. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  35. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  36. self.assertRaises(CommandExecutionError, npm.install,
  37. 'coffee-script')
  38. # This is at least somewhat closer to the actual output format.
  39. mock_json_out = textwrap.dedent('''\
  40. [
  41. {
  42. "salt": "SALT"
  43. }
  44. ]''')
  45. # Successful run, expected output format
  46. mock = MagicMock(return_value={'retcode': 0, 'stderr': '',
  47. 'stdout': mock_json_out})
  48. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  49. self.assertEqual(npm.install('coffee-script'),
  50. [{u'salt': u'SALT'}])
  51. mock_json_out_extra = textwrap.dedent('''\
  52. Compilation output here
  53. [bcrypt] Success: "/tmp/node_modules/bcrypt/foo" is installed via remote"
  54. [grpc] Success: "/usr/lib/node_modules/@foo/bar" is installed via remote"
  55. [
  56. {
  57. "from" : "express@",
  58. "name" : "express",
  59. "dependencies" : {
  60. "escape-html" : {
  61. "from" : "escape-html@~1.0.3",
  62. "dependencies" : {},
  63. "version" : "1.0.3"
  64. }
  65. },
  66. "version" : "4.16.3"
  67. }
  68. ]''')
  69. extra_expected = [{u'dependencies':
  70. {u'escape-html': {
  71. u'dependencies': {},
  72. u'from': u'escape-html@~1.0.3',
  73. u'version': u'1.0.3'}
  74. },
  75. u'from': u'express@',
  76. u'name': u'express',
  77. u'version': u'4.16.3'}]
  78. # Successful run, expected output format with additional leading text
  79. mock = MagicMock(return_value={'retcode': 0, 'stderr': '',
  80. 'stdout': mock_json_out_extra})
  81. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  82. self.assertEqual(npm.install('coffee-script'), extra_expected)
  83. # Successful run, unexpected output format
  84. mock = MagicMock(return_value={'retcode': 0, 'stderr': '',
  85. 'stdout': 'SALT'})
  86. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  87. mock_err = MagicMock(side_effect=ValueError())
  88. # When JSON isn't successfully parsed, return should equal input
  89. with patch.object(salt.utils.json, 'loads', mock_err):
  90. self.assertEqual(npm.install('coffee-script'), 'SALT')
  91. # 'uninstall' function tests: 1
  92. def test_uninstall(self):
  93. '''
  94. Test if it uninstalls an NPM package.
  95. '''
  96. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  97. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  98. self.assertFalse(npm.uninstall('coffee-script'))
  99. mock = MagicMock(return_value={'retcode': 0, 'stderr': ''})
  100. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  101. self.assertTrue(npm.uninstall('coffee-script'))
  102. # 'list_' function tests: 1
  103. def test_list(self):
  104. '''
  105. Test if it list installed NPM packages.
  106. '''
  107. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  108. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  109. self.assertRaises(CommandExecutionError, npm.list_, 'coffee-script')
  110. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  111. 'stdout': '{"salt": ["SALT"]}'})
  112. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  113. mock_err = MagicMock(return_value={'dependencies': 'SALT'})
  114. with patch.object(salt.utils.json, 'loads', mock_err):
  115. self.assertEqual(npm.list_('coffee-script'), 'SALT')
  116. # 'cache_clean' function tests: 1
  117. def test_cache_clean(self):
  118. '''
  119. Test if it cleans the cached NPM packages.
  120. '''
  121. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  122. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  123. self.assertFalse(npm.cache_clean())
  124. mock = MagicMock(return_value={'retcode': 0})
  125. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  126. self.assertTrue(npm.cache_clean())
  127. mock = MagicMock(return_value={'retcode': 0})
  128. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  129. self.assertTrue(npm.cache_clean('coffee-script'))
  130. # 'cache_list' function tests: 1
  131. def test_cache_list(self):
  132. '''
  133. Test if it lists the NPM cache.
  134. '''
  135. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  136. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  137. self.assertRaises(CommandExecutionError, npm.cache_list)
  138. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  139. 'stdout': ['~/.npm']})
  140. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  141. self.assertEqual(npm.cache_list(), ['~/.npm'])
  142. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  143. 'stdout': ''})
  144. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  145. self.assertEqual(npm.cache_list('coffee-script'), '')
  146. # 'cache_path' function tests: 1
  147. def test_cache_path(self):
  148. '''
  149. Test if it prints the NPM cache path.
  150. '''
  151. mock = MagicMock(return_value={'retcode': 1, 'stderr': 'error'})
  152. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  153. self.assertEqual(npm.cache_path(), 'error')
  154. mock = MagicMock(return_value={'retcode': 0, 'stderr': 'error',
  155. 'stdout': '/User/salt/.npm'})
  156. with patch.dict(npm.__salt__, {'cmd.run_all': mock}):
  157. self.assertEqual(npm.cache_path(), '/User/salt/.npm')