1
0

test_npm.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, print_function, unicode_literals
  9. import os
  10. # Import salt libs
  11. import salt.utils.path
  12. import salt.utils.platform
  13. from salt.utils.versions import LooseVersion
  14. # Import Salt Testing libs
  15. from tests.support.case import ModuleCase
  16. from tests.support.helpers import destructiveTest, requires_network, slowTest
  17. from tests.support.mixins import SaltReturnAssertsMixin
  18. from tests.support.runtests import RUNTIME_VARS
  19. from tests.support.unit import skipIf
  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. @slowTest
  26. def test_npm_installed_removed(self):
  27. """
  28. Basic test to determine if NPM module was successfully installed and
  29. removed.
  30. """
  31. ret = self.run_state(
  32. "npm.installed", name="pm2@2.10.4", registry="http://registry.npmjs.org/"
  33. )
  34. self.assertSaltTrueReturn(ret)
  35. ret = self.run_state("npm.removed", name="pm2")
  36. self.assertSaltTrueReturn(ret)
  37. @skipIf(salt.utils.platform.is_darwin(), "TODO this test hangs on mac.")
  38. @requires_network()
  39. @destructiveTest
  40. @slowTest
  41. def test_npm_install_url_referenced_package(self):
  42. """
  43. Determine if URL-referenced NPM module can be successfully installed.
  44. """
  45. npm_version = self.run_function("cmd.run", ["npm -v"])
  46. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  47. user = os.environ.get("SUDO_USER", "root")
  48. npm_dir = os.path.join(RUNTIME_VARS.TMP, "git-install-npm")
  49. self.run_state("file.directory", name=npm_dir, user=user, dir_mode="755")
  50. else:
  51. user = None
  52. npm_dir = None
  53. ret = self.run_state(
  54. "npm.installed",
  55. name="request/request#v2.81.1",
  56. runas=user,
  57. dir=npm_dir,
  58. registry="http://registry.npmjs.org/",
  59. )
  60. self.assertSaltTrueReturn(ret)
  61. ret = self.run_state(
  62. "npm.removed",
  63. name="git://github.com/request/request",
  64. runas=user,
  65. dir=npm_dir,
  66. )
  67. self.assertSaltTrueReturn(ret)
  68. if npm_dir is not None:
  69. self.run_state("file.absent", name=npm_dir)
  70. @requires_network()
  71. @destructiveTest
  72. @slowTest
  73. def test_npm_installed_pkgs(self):
  74. """
  75. Basic test to determine if NPM module successfully installs multiple
  76. packages.
  77. """
  78. ret = self.run_state(
  79. "npm.installed",
  80. name="unused",
  81. pkgs=["pm2@2.10.4", "grunt@1.0.2"],
  82. registry="http://registry.npmjs.org/",
  83. )
  84. self.assertSaltTrueReturn(ret)
  85. @destructiveTest
  86. @slowTest
  87. def test_npm_cache_clean(self):
  88. """
  89. Basic test to determine if NPM successfully cleans its cached packages.
  90. """
  91. npm_version = self.run_function("cmd.run", ["npm -v"])
  92. if LooseVersion(npm_version) >= LooseVersion(MAX_NPM_VERSION):
  93. self.skipTest("Skip with npm >= 5.0.0 until #41770 is fixed")
  94. ret = self.run_state("npm.cache_cleaned", name="unused", force=True)
  95. self.assertSaltTrueReturn(ret)