test_npm.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 pytest
  11. # Import Salt Testing libs
  12. from tests.support.case import ModuleCase
  13. from tests.support.unit import skipIf
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. from tests.support.runtime 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. @pytest.mark.requires_network
  24. @pytest.mark.destructive_test
  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. @pytest.mark.requires_network
  36. @pytest.mark.destructive_test
  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. @pytest.mark.requires_network
  60. @pytest.mark.destructive_test
  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. @pytest.mark.destructive_test
  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)