test_gem.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import MagicMock, patch
  8. # Late import so mock can do its job
  9. import salt.states.gem as gem
  10. class TestGemState(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {gem: {'__opts__': {'test': False}}}
  13. def test_installed(self):
  14. gems = {'foo': ['1.0'], 'bar': ['2.0']}
  15. gem_list = MagicMock(return_value=gems)
  16. gem_install_succeeds = MagicMock(return_value=True)
  17. gem_install_fails = MagicMock(return_value=False)
  18. with patch.dict(gem.__salt__, {'gem.list': gem_list}):
  19. with patch.dict(gem.__salt__,
  20. {'gem.install': gem_install_succeeds}):
  21. ret = gem.installed('foo')
  22. self.assertEqual(True, ret['result'])
  23. ret = gem.installed('quux')
  24. self.assertEqual(True, ret['result'])
  25. gem_install_succeeds.assert_called_once_with(
  26. 'quux', pre_releases=False, ruby=None, runas=None,
  27. version=None, proxy=None, rdoc=False, source=None,
  28. ri=False, gem_bin=None
  29. )
  30. with patch.dict(gem.__salt__,
  31. {'gem.install': gem_install_fails}):
  32. ret = gem.installed('quux')
  33. self.assertEqual(False, ret['result'])
  34. gem_install_fails.assert_called_once_with(
  35. 'quux', pre_releases=False, ruby=None, runas=None,
  36. version=None, proxy=None, rdoc=False, source=None,
  37. ri=False, gem_bin=None
  38. )
  39. def test_installed_version(self):
  40. gems = {'foo': ['1.0'], 'bar': ['2.0']}
  41. gem_list = MagicMock(return_value=gems)
  42. gem_install_succeeds = MagicMock(return_value=True)
  43. with patch.dict(gem.__salt__, {'gem.list': gem_list}):
  44. with patch.dict(gem.__salt__,
  45. {'gem.install': gem_install_succeeds}):
  46. ret = gem.installed('foo', version='>= 1.0')
  47. self.assertEqual(True, ret['result'])
  48. self.assertEqual('Installed Gem meets version requirements.',
  49. ret['comment'])
  50. def test_removed(self):
  51. gems = ['foo', 'bar']
  52. gem_list = MagicMock(return_value=gems)
  53. gem_uninstall_succeeds = MagicMock(return_value=True)
  54. gem_uninstall_fails = MagicMock(return_value=False)
  55. with patch.dict(gem.__salt__, {'gem.list': gem_list}):
  56. with patch.dict(gem.__salt__,
  57. {'gem.uninstall': gem_uninstall_succeeds}):
  58. ret = gem.removed('quux')
  59. self.assertEqual(True, ret['result'])
  60. ret = gem.removed('foo')
  61. self.assertEqual(True, ret['result'])
  62. gem_uninstall_succeeds.assert_called_once_with(
  63. 'foo', None, runas=None, gem_bin=None)
  64. with patch.dict(gem.__salt__,
  65. {'gem.uninstall': gem_uninstall_fails}):
  66. ret = gem.removed('bar')
  67. self.assertEqual(False, ret['result'])
  68. gem_uninstall_fails.assert_called_once_with(
  69. 'bar', None, runas=None, gem_bin=None)
  70. def test_sources_add(self):
  71. gem_sources = ['http://foo', 'http://bar']
  72. gem_sources_list = MagicMock(return_value=gem_sources)
  73. gem_sources_add_succeeds = MagicMock(return_value=True)
  74. gem_sources_add_fails = MagicMock(return_value=False)
  75. with patch.dict(gem.__salt__, {'gem.sources_list': gem_sources_list}):
  76. with patch.dict(gem.__salt__, {'gem.sources_add': gem_sources_add_succeeds}):
  77. ret = gem.sources_add('http://foo')
  78. self.assertEqual(True, ret['result'])
  79. ret = gem.sources_add('http://fui')
  80. self.assertEqual(True, ret['result'])
  81. gem_sources_add_succeeds.assert_called_once_with(
  82. source_uri='http://fui', ruby=None, runas=None)
  83. with patch.dict(gem.__salt__, {'gem.sources_add': gem_sources_add_fails}):
  84. ret = gem.sources_add('http://fui')
  85. self.assertEqual(False, ret['result'])
  86. gem_sources_add_fails.assert_called_once_with(
  87. source_uri='http://fui', ruby=None, runas=None)
  88. def test_sources_remove(self):
  89. gem_sources = ['http://foo', 'http://bar']
  90. gem_sources_list = MagicMock(return_value=gem_sources)
  91. gem_sources_remove_succeeds = MagicMock(return_value=True)
  92. gem_sources_remove_fails = MagicMock(return_value=False)
  93. with patch.dict(gem.__salt__, {'gem.sources_list': gem_sources_list}):
  94. with patch.dict(gem.__salt__, {'gem.sources_remove': gem_sources_remove_succeeds}):
  95. ret = gem.sources_remove('http://fui')
  96. self.assertEqual(True, ret['result'])
  97. ret = gem.sources_remove('http://foo')
  98. self.assertEqual(True, ret['result'])
  99. gem_sources_remove_succeeds.assert_called_once_with(
  100. source_uri='http://foo', ruby=None, runas=None)
  101. with patch.dict(gem.__salt__, {'gem.sources_remove': gem_sources_remove_fails}):
  102. ret = gem.sources_remove('http://bar')
  103. self.assertEqual(False, ret['result'])
  104. gem_sources_remove_fails.assert_called_once_with(
  105. source_uri='http://bar', ruby=None, runas=None)