1
0

test_npm.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.utils.path
  18. import salt.utils.platform
  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. @skipIf(salt.utils.platform.is_darwin(), 'TODO this test hangs on mac.')
  35. @requires_network()
  36. @destructiveTest
  37. def test_npm_install_url_referenced_package(self):
  38. '''
  39. Determine if URL-referenced NPM module can be successfully installed.
  40. '''
  41. npm_version = self.run_function('cmd.run', ['npm -v'])
  42. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  43. user = os.environ.get('SUDO_USER', 'root')
  44. npm_dir = os.path.join(RUNTIME_VARS.TMP, 'git-install-npm')
  45. self.run_state('file.directory', name=npm_dir, user=user, dir_mode='755')
  46. else:
  47. user = None
  48. npm_dir = None
  49. ret = self.run_state('npm.installed',
  50. name='request/request#v2.81.1',
  51. runas=user,
  52. dir=npm_dir,
  53. registry="http://registry.npmjs.org/")
  54. self.assertSaltTrueReturn(ret)
  55. ret = self.run_state('npm.removed', name='git://github.com/request/request', runas=user, dir=npm_dir)
  56. self.assertSaltTrueReturn(ret)
  57. if npm_dir is not None:
  58. self.run_state('file.absent', name=npm_dir)
  59. @requires_network()
  60. @destructiveTest
  61. def test_npm_installed_pkgs(self):
  62. '''
  63. Basic test to determine if NPM module successfully installs multiple
  64. packages.
  65. '''
  66. ret = self.run_state('npm.installed', name='unused', pkgs=['pm2@2.10.4', 'grunt@1.0.2'], registry="http://registry.npmjs.org/")
  67. self.assertSaltTrueReturn(ret)
  68. @destructiveTest
  69. def test_npm_cache_clean(self):
  70. '''
  71. Basic test to determine if NPM successfully cleans its cached packages.
  72. '''
  73. npm_version = self.run_function('cmd.run', ['npm -v'])
  74. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  75. self.skipTest('Skip with npm >= 5.0.0 until #41770 is fixed')
  76. ret = self.run_state('npm.cache_cleaned', name='unused', force=True)
  77. self.assertSaltTrueReturn(ret)