1
0

test_gem.py 4.2 KB

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