Move caching of requirements to helper function and add unit tests.

This commit is contained in:
Daniel Watkins 2013-05-07 10:12:43 +01:00
parent d06a706a36
commit 7e7bf244c7
2 changed files with 38 additions and 15 deletions

View File

@ -43,6 +43,26 @@ def _get_pip_bin(bin_env):
return bin_env
def _get_cached_requirements(requirements):
"""Get the location of a cached requirements file; caching if necessary."""
cached_requirements = __salt__['cp.is_cached'](
requirements, __env__
)
if not cached_requirements:
# It's not cached, let's cache it.
cached_requirements = __salt__['cp.cache_file'](
requirements, __env__
)
# Check if the master version has changed.
if __salt__['cp.hash_file'](requirements, __env__) != \
__salt__['cp.hash_file'](cached_requirements, __env__):
cached_requirements = __salt__['cp.cache_file'](
requirements, __env__
)
return cached_requirements
def install(pkgs=None,
requirements=None,
env=None,
@ -189,20 +209,7 @@ def install(pkgs=None,
treq = None
if requirements:
if requirements.startswith('salt://'):
cached_requirements = __salt__['cp.is_cached'](
requirements, __env__
)
if not cached_requirements:
# It's not cached, let's cache it.
cached_requirements = __salt__['cp.cache_file'](
requirements, __env__
)
# Check if the master version has changed.
if __salt__['cp.hash_file'](requirements, __env__) != \
__salt__['cp.hash_file'](cached_requirements, __env__):
cached_requirements = __salt__['cp.cache_file'](
requirements, __env__
)
cached_requirements = _get_cached_requirements(requirements)
if not cached_requirements:
return {
'result': False,
@ -212,7 +219,6 @@ def install(pkgs=None,
)
)
}
requirements = cached_requirements
if runas:

View File

@ -11,9 +11,26 @@ pip.__salt__ = {"cmd.which_bin":lambda _:"pip"}
@skipIf(has_mock is False, "mock python module is unavailable")
class PipTestCase(TestCase):
def test_fix4361(self):
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all':mock}):
pip.install(requirements="requirements.txt")
expected_cmd = 'pip install --requirement "requirements.txt" '
mock.assert_called_once_with(expected_cmd, runas=None, cwd=None)
@patch('salt.modules.pip._get_cached_requirements')
def test_failed_cached_requirements(self, get_cached_requirements):
get_cached_requirements.return_value = False
ret = pip.install(requirements='salt://my_test_reqs')
self.assertEqual(False, ret['result'])
self.assertIn('my_test_reqs', ret['comment'])
@patch('salt.modules.pip._get_cached_requirements')
def test_cached_requirements_used(self, get_cached_requirements):
get_cached_requirements.return_value = 'my_cached_reqs'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(requirements="salt://requirements.txt")
expected_cmd = 'pip install --requirement "my_cached_reqs" '
mock.assert_called_once_with(expected_cmd, runas=None, cwd=None)