test_gem.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for Ruby Gem module
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import pytest
  7. import salt.utils.path
  8. from salt.ext.tornado.httpclient import HTTPClient
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import destructiveTest, slowTest
  11. from tests.support.unit import skipIf
  12. def check_status():
  13. """
  14. Check the status of the rubygems source
  15. """
  16. try:
  17. return HTTPClient().fetch("https://rubygems.org").code == 200
  18. except Exception: # pylint: disable=broad-except
  19. return False
  20. @destructiveTest
  21. @skipIf(not salt.utils.path.which("gem"), "Gem is not available")
  22. @pytest.mark.windows_whitelisted
  23. class GemModuleTest(ModuleCase):
  24. """
  25. Validate gem module
  26. """
  27. def setUp(self):
  28. if check_status() is False:
  29. self.skipTest("External resource 'https://rubygems.org' is not available")
  30. self.GEM = "tidy"
  31. self.GEM_VER = "1.1.2"
  32. self.OLD_GEM = "brass"
  33. self.OLD_VERSION = "1.0.0"
  34. self.NEW_VERSION = "1.2.1"
  35. self.GEM_LIST = [self.GEM, self.OLD_GEM]
  36. for name in (
  37. "GEM",
  38. "GEM_VER",
  39. "OLD_GEM",
  40. "OLD_VERSION",
  41. "NEW_VERSION",
  42. "GEM_LIST",
  43. ):
  44. self.addCleanup(delattr, self, name)
  45. def uninstall_gem():
  46. # Remove gem if it is already installed
  47. if self.run_function("gem.list", [self.GEM]):
  48. self.run_function("gem.uninstall", [self.GEM])
  49. self.addCleanup(uninstall_gem)
  50. @slowTest
  51. def test_install_uninstall(self):
  52. """
  53. gem.install
  54. gem.uninstall
  55. """
  56. self.run_function("gem.install", [self.GEM])
  57. gem_list = self.run_function("gem.list", [self.GEM])
  58. self.assertIn(self.GEM, gem_list)
  59. self.run_function("gem.uninstall", [self.GEM])
  60. self.assertFalse(self.run_function("gem.list", [self.GEM]))
  61. @slowTest
  62. def test_install_version(self):
  63. """
  64. gem.install rake version=11.1.2
  65. """
  66. self.run_function("gem.install", [self.GEM], version=self.GEM_VER)
  67. gem_list = self.run_function("gem.list", [self.GEM])
  68. self.assertIn(self.GEM, gem_list)
  69. self.assertIn(self.GEM_VER, gem_list[self.GEM])
  70. self.run_function("gem.uninstall", [self.GEM])
  71. self.assertFalse(self.run_function("gem.list", [self.GEM]))
  72. @slowTest
  73. def test_list(self):
  74. """
  75. gem.list
  76. """
  77. self.run_function("gem.install", [" ".join(self.GEM_LIST)])
  78. all_ret = self.run_function("gem.list")
  79. for gem in self.GEM_LIST:
  80. self.assertIn(gem, all_ret)
  81. single_ret = self.run_function("gem.list", [self.GEM])
  82. self.assertIn(self.GEM, single_ret)
  83. self.run_function("gem.uninstall", [" ".join(self.GEM_LIST)])
  84. @slowTest
  85. def test_list_upgrades(self):
  86. """
  87. gem.list_upgrades
  88. """
  89. # install outdated gem
  90. self.run_function("gem.install", [self.OLD_GEM], version=self.OLD_VERSION)
  91. ret = self.run_function("gem.list_upgrades")
  92. self.assertIn(self.OLD_GEM, ret)
  93. self.run_function("gem.uninstall", [self.OLD_GEM])
  94. @slowTest
  95. def test_sources_add_remove(self):
  96. """
  97. gem.sources_add
  98. gem.sources_remove
  99. """
  100. source = "http://production.cf.rubygems.org"
  101. self.run_function("gem.sources_add", [source])
  102. sources_list = self.run_function("gem.sources_list")
  103. self.assertIn(source, sources_list)
  104. self.run_function("gem.sources_remove", [source])
  105. sources_list = self.run_function("gem.sources_list")
  106. self.assertNotIn(source, sources_list)
  107. @slowTest
  108. def test_update(self):
  109. """
  110. gem.update
  111. """
  112. self.run_function("gem.install", [self.OLD_GEM], version=self.OLD_VERSION)
  113. gem_list = self.run_function("gem.list", [self.OLD_GEM])
  114. self.assertEqual({self.OLD_GEM: [self.OLD_VERSION]}, gem_list)
  115. self.run_function("gem.update", [self.OLD_GEM])
  116. gem_list = self.run_function("gem.list", [self.OLD_GEM])
  117. self.assertEqual({self.OLD_GEM: [self.NEW_VERSION, self.OLD_VERSION]}, gem_list)
  118. self.run_function("gem.uninstall", [self.OLD_GEM])
  119. self.assertFalse(self.run_function("gem.list", [self.OLD_GEM]))
  120. @slowTest
  121. def test_update_system(self):
  122. """
  123. gem.update_system
  124. """
  125. ret = self.run_function("gem.update_system")
  126. self.assertTrue(ret)