1
0

test_npm.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Erik Johnson (erik@saltstack.com)
  4. tests.integration.states.npm
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, unicode_literals, print_function
  9. import os
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.unit import skipIf
  13. from tests.support.helpers import destructiveTest, requires_network
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. from tests.support.runtests import RUNTIME_VARS
  16. # Import salt libs
  17. import salt.modules.cmdmod as cmd
  18. import salt.utils.path
  19. from salt.utils.versions import LooseVersion
  20. MAX_NPM_VERSION = '5.0.0'
  21. @skipIf(salt.utils.path.which('npm') is None, 'npm not installed')
  22. class NpmStateTest(ModuleCase, SaltReturnAssertsMixin):
  23. @requires_network()
  24. @destructiveTest
  25. def test_npm_installed_removed(self):
  26. '''
  27. Basic test to determine if NPM module was successfully installed and
  28. removed.
  29. '''
  30. ret = self.run_state('npm.installed', name='pm2@2.10.4', registry="http://registry.npmjs.org/")
  31. self.assertSaltTrueReturn(ret)
  32. ret = self.run_state('npm.removed', name='pm2')
  33. self.assertSaltTrueReturn(ret)
  34. @requires_network()
  35. @destructiveTest
  36. def test_npm_install_url_referenced_package(self):
  37. '''
  38. Determine if URL-referenced NPM module can be successfully installed.
  39. '''
  40. if LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION):
  41. user = os.environ.get('SUDO_USER', 'root')
  42. npm_dir = os.path.join(RUNTIME_VARS.TMP, 'git-install-npm')
  43. self.run_state('file.directory', name=npm_dir, user=user, dir_mode='755')
  44. else:
  45. user = None
  46. npm_dir = None
  47. ret = self.run_state('npm.installed',
  48. name='request/request#v2.81.1',
  49. runas=user,
  50. dir=npm_dir,
  51. registry="http://registry.npmjs.org/")
  52. self.assertSaltTrueReturn(ret)
  53. ret = self.run_state('npm.removed', name='git://github.com/request/request', runas=user, dir=npm_dir)
  54. self.assertSaltTrueReturn(ret)
  55. if npm_dir is not None:
  56. self.run_state('file.absent', name=npm_dir)
  57. @requires_network()
  58. @destructiveTest
  59. def test_npm_installed_pkgs(self):
  60. '''
  61. Basic test to determine if NPM module successfully installs multiple
  62. packages.
  63. '''
  64. ret = self.run_state('npm.installed', name='unused', pkgs=['pm2@2.10.4', 'grunt@1.0.2'], registry="http://registry.npmjs.org/")
  65. self.assertSaltTrueReturn(ret)
  66. @skipIf(salt.utils.path.which('npm') and LooseVersion(cmd.run('npm -v')) >= LooseVersion(MAX_NPM_VERSION),
  67. 'Skip with npm >= 5.0.0 until #41770 is fixed')
  68. @destructiveTest
  69. def test_npm_cache_clean(self):
  70. '''
  71. Basic test to determine if NPM successfully cleans its cached packages.
  72. '''
  73. ret = self.run_state('npm.cache_cleaned', name='unused', force=True)
  74. self.assertSaltTrueReturn(ret)