salt/tests/unit/states/gem_test.py

70 lines
2.7 KiB
Python
Raw Normal View History

import sys
import os
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
Skip the rvm and gem module/state tests when mock is missing The mock python module is required for the gem and rvm tests. If it is not available, the tests should be gracefully skipped. From python runtests.py -u: test__check_and_install_ruby (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test__check_ruby (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test__check_rvm (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_gemset_present (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_installed (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_installed (unit.states.gem_test.TestGemState) ... skipped 'mock python module is unavailable' test_removed (unit.states.gem_test.TestGemState) ... skipped 'mock python module is unavailable' test__rvm (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test__rvm_do (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_gemset_list (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_gemset_list_all (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_install (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_list (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test__gem (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable' test_list (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable' test_sources_list (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable'
2012-06-30 19:29:48 +00:00
from saltunittest import TestCase, TestLoader, TextTestRunner, skipIf
try:
from mock import MagicMock, patch
has_mock = True
except ImportError:
has_mock = False
import salt.states.gem as gem
gem.__salt__ = {}
gem.__opts__ = {'test': False}
Skip the rvm and gem module/state tests when mock is missing The mock python module is required for the gem and rvm tests. If it is not available, the tests should be gracefully skipped. From python runtests.py -u: test__check_and_install_ruby (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test__check_ruby (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test__check_rvm (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_gemset_present (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_installed (unit.states.rvm_test.TestRvmState) ... skipped 'mock python module is unavailable' test_installed (unit.states.gem_test.TestGemState) ... skipped 'mock python module is unavailable' test_removed (unit.states.gem_test.TestGemState) ... skipped 'mock python module is unavailable' test__rvm (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test__rvm_do (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_gemset_list (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_gemset_list_all (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_install (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test_list (unit.modules.rvm_test.TestRvmModule) ... skipped 'mock python module is unavailable' test__gem (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable' test_list (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable' test_sources_list (unit.modules.gem_test.TestGemModule) ... skipped 'mock python module is unavailable'
2012-06-30 19:29:48 +00:00
@skipIf(has_mock is False, "mock python module is unavailable")
class TestGemState(TestCase):
def test_installed(self):
gems = {'foo' : ['1.0'], 'bar' : ['2.0']}
gem_list = MagicMock(return_value=gems)
gem_install_succeeds = MagicMock(return_value=True)
gem_install_fails = MagicMock(return_value=False)
with patch.dict(gem.__salt__, {'gem.list': gem_list}):
with patch.dict(gem.__salt__,
{'gem.install': gem_install_succeeds}):
ret = gem.installed('foo')
self.assertEqual(True, ret['result'])
ret = gem.installed('quux')
self.assertEqual(True, ret['result'])
gem_install_succeeds.assert_called_once_with(
'quux', ruby=None, runas=None, version=None, rdoc=False, ri=False)
with patch.dict(gem.__salt__,
{'gem.install': gem_install_fails}):
ret = gem.installed('quux')
self.assertEqual(False, ret['result'])
gem_install_fails.assert_called_once_with(
'quux', ruby=None, runas=None, version=None, rdoc=False, ri=False)
def test_removed(self):
gems = ['foo', 'bar']
gem_list = MagicMock(return_value=gems)
gem_uninstall_succeeds = MagicMock(return_value=True)
gem_uninstall_fails = MagicMock(return_value=False)
with patch.dict(gem.__salt__, {'gem.list': gem_list}):
with patch.dict(gem.__salt__,
{'gem.uninstall': gem_uninstall_succeeds}):
ret = gem.removed('quux')
self.assertEqual(True, ret['result'])
ret = gem.removed('foo')
self.assertEqual(True, ret['result'])
gem_uninstall_succeeds.assert_called_once_with(
'foo', None, runas=None)
with patch.dict(gem.__salt__,
{'gem.uninstall': gem_uninstall_fails}):
ret = gem.removed('bar')
self.assertEqual(False, ret['result'])
gem_uninstall_fails.assert_called_once_with(
'bar', None, runas=None)
if __name__ == "__main__":
loader = TestLoader()
tests = loader.loadTestsFromTestCase(TestGemState)
TextTestRunner(verbosity=1).run(tests)