salt/tests/unit/states/gem_test.py
Pedro Algarvio 222be7b446 Make sure every test module is runnable by __main__.
* Double to single quotes
2013-06-28 00:12:56 +01:00

91 lines
3.0 KiB
Python

# Import python libs
import os
import sys
# Import salt libs
try:
import integration
except ImportError:
if __name__ == '__main__':
sys.path.insert(
0, os.path.abspath(
os.path.join(
os.path.dirname(__file__), '../../'
)
)
)
import integration
# Import Salt Testing libs
from salttesting import TestCase, skipIf
# Import external libs
try:
from mock import MagicMock, patch
has_mock = True
except ImportError:
has_mock = False
# Late import so mock can do it's job
import salt.states.gem as gem
gem.__salt__ = {}
gem.__opts__ = {'test': False}
@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__':
from integration import run_tests
run_tests(TestGemState, needs_daemon=False)