test_gem.py 4.9 KB

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