test_npm.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Erik Johnson (erik@saltstack.com)
  4. tests.integration.states.npm
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import os
  9. import pytest
  10. import salt.utils.path
  11. import salt.utils.platform
  12. from salt.utils.versions import LooseVersion
  13. from tests.support.case import ModuleCase
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. from tests.support.runtests import RUNTIME_VARS
  16. from tests.support.unit import skipIf
  17. MAX_NPM_VERSION = "5.0.0"
  18. @skipIf(salt.utils.path.which("npm") is None, "npm not installed")
  19. class NpmStateTest(ModuleCase, SaltReturnAssertsMixin):
  20. @pytest.mark.requires_network
  21. @pytest.mark.destructive_test
  22. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  23. def test_npm_installed_removed(self):
  24. """
  25. Basic test to determine if NPM module was successfully installed and
  26. removed.
  27. """
  28. ret = self.run_state(
  29. "npm.installed", name="pm2@2.10.4", registry="http://registry.npmjs.org/"
  30. )
  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. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  38. def test_npm_install_url_referenced_package(self):
  39. """
  40. Determine if URL-referenced NPM module can be successfully installed.
  41. """
  42. npm_version = self.run_function("cmd.run", ["npm -v"])
  43. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  44. user = os.environ.get("SUDO_USER", "root")
  45. npm_dir = os.path.join(RUNTIME_VARS.TMP, "git-install-npm")
  46. self.run_state("file.directory", name=npm_dir, user=user, dir_mode="755")
  47. else:
  48. user = None
  49. npm_dir = None
  50. ret = self.run_state(
  51. "npm.installed",
  52. name="request/request#v2.81.1",
  53. runas=user,
  54. dir=npm_dir,
  55. registry="http://registry.npmjs.org/",
  56. )
  57. self.assertSaltTrueReturn(ret)
  58. ret = self.run_state(
  59. "npm.removed",
  60. name="git://github.com/request/request",
  61. runas=user,
  62. dir=npm_dir,
  63. )
  64. self.assertSaltTrueReturn(ret)
  65. if npm_dir is not None:
  66. self.run_state("file.absent", name=npm_dir)
  67. @pytest.mark.requires_network
  68. @pytest.mark.destructive_test
  69. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  70. def test_npm_installed_pkgs(self):
  71. """
  72. Basic test to determine if NPM module successfully installs multiple
  73. packages.
  74. """
  75. ret = self.run_state(
  76. "npm.installed",
  77. name="unused",
  78. pkgs=["pm2@2.10.4", "grunt@1.0.2"],
  79. registry="http://registry.npmjs.org/",
  80. )
  81. self.assertSaltTrueReturn(ret)
  82. @pytest.mark.destructive_test
  83. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  84. def test_npm_cache_clean(self):
  85. """
  86. Basic test to determine if NPM successfully cleans its cached packages.
  87. """
  88. npm_version = self.run_function("cmd.run", ["npm -v"])
  89. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  90. self.skipTest("Skip with npm >= 5.0.0 until #41770 is fixed")
  91. ret = self.run_state("npm.cache_cleaned", name="unused", force=True)
  92. self.assertSaltTrueReturn(ret)