2013-07-27 14:58:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.unit.states.pip_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2013-07-27 14:58:28 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-07-27 14:58:28 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.integration import SaltReturnAssertsMixIn
|
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
2013-07-27 14:58:28 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.states.pip_state as pip_state
|
2013-07-27 14:58:28 +00:00
|
|
|
|
2013-08-29 10:37:25 +00:00
|
|
|
# Import 3rd-party libs
|
2013-12-31 14:22:35 +00:00
|
|
|
try:
|
|
|
|
import pip
|
|
|
|
HAS_PIP = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PIP = False
|
2013-08-29 10:37:25 +00:00
|
|
|
|
2013-07-27 14:58:28 +00:00
|
|
|
|
2013-08-26 10:10:06 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2013-12-31 14:22:35 +00:00
|
|
|
@skipIf(not HAS_PIP,
|
|
|
|
'The \'pip\' library is not importable(installed system-wide)')
|
2017-02-19 15:35:30 +00:00
|
|
|
class PipStateTest(TestCase, SaltReturnAssertsMixIn, LoaderModuleMockMixin):
|
|
|
|
|
|
|
|
loader_module = pip_state
|
|
|
|
|
|
|
|
def loader_module_globals(self):
|
|
|
|
return {
|
|
|
|
'__env__': 'base',
|
|
|
|
'__opts__': {'test': False},
|
|
|
|
'__salt__': {'cmd.which_bin': lambda _: 'pip'}
|
|
|
|
}
|
2013-07-27 14:58:28 +00:00
|
|
|
|
2013-08-18 05:45:23 +00:00
|
|
|
def test_install_requirements_parsing(self):
|
2013-08-18 06:27:32 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.3'})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed('pep8=1.3.2')
|
2013-08-18 06:27:32 +00:00
|
|
|
self.assertSaltFalseReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'Invalid version specification in package pep8=1.3.2. '
|
|
|
|
'\'=\' is not supported, use \'==\' instead.',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
2013-08-18 05:45:23 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.3'})
|
2014-12-19 18:05:47 +00:00
|
|
|
pip_install = MagicMock(return_value={'retcode': 0})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
2014-12-19 18:05:47 +00:00
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed('pep8>=1.3.2')
|
2013-08-18 05:45:23 +00:00
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
2014-12-19 18:05:47 +00:00
|
|
|
'Python package pep8>=1.3.2 was already installed',
|
2013-08-18 05:45:23 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.3'})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed('pep8<1.3.2')
|
2013-08-18 05:45:23 +00:00
|
|
|
self.assertSaltNoneReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'Python package pep8<1.3.2 is set to be installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.2'})
|
2014-12-19 18:05:47 +00:00
|
|
|
pip_install = MagicMock(return_value={'retcode': 0})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
2014-12-19 18:05:47 +00:00
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed('pep8>1.3.1,<1.3.3')
|
2013-08-18 05:45:23 +00:00
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
2014-12-19 18:05:47 +00:00
|
|
|
'Python package pep8>1.3.1,<1.3.3 was already installed',
|
2013-08-18 05:45:23 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.1'})
|
2014-12-19 18:05:47 +00:00
|
|
|
pip_install = MagicMock(return_value={'retcode': 0})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
2014-12-19 18:05:47 +00:00
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed('pep8>1.3.1,<1.3.3')
|
2013-08-18 05:45:23 +00:00
|
|
|
self.assertSaltNoneReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
2014-12-19 18:05:47 +00:00
|
|
|
'Python package pep8>1.3.1,<1.3.3 is set to be installed',
|
2013-08-18 05:45:23 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
2013-08-18 06:27:32 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.1'})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed(
|
2013-08-18 06:27:32 +00:00
|
|
|
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting>=0.5.1'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'Python package git+https://github.com/saltstack/'
|
2013-08-26 08:54:26 +00:00
|
|
|
'salt-testing.git#egg=SaltTesting>=0.5.1 is set to be '
|
|
|
|
'installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.1'})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed(
|
2013-08-26 08:54:26 +00:00
|
|
|
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'Python package git+https://github.com/saltstack/'
|
2013-08-26 10:46:50 +00:00
|
|
|
'salt-testing.git#egg=SaltTesting is set to be '
|
2013-08-18 06:27:32 +00:00
|
|
|
'installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.1'})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': True}):
|
|
|
|
ret = pip_state.installed(
|
2013-08-18 06:27:32 +00:00
|
|
|
'https://pypi.python.org/packages/source/S/SaltTesting/'
|
|
|
|
'SaltTesting-0.5.0.tar.gz'
|
|
|
|
'#md5=e6760af92b7165f8be53b5763e40bc24'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'Python package https://pypi.python.org/packages/source/'
|
|
|
|
'S/SaltTesting/SaltTesting-0.5.0.tar.gz'
|
|
|
|
'#md5=e6760af92b7165f8be53b5763e40bc24 is set to be '
|
|
|
|
'installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
2013-08-18 05:45:23 +00:00
|
|
|
|
2013-08-18 07:03:41 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'SaltTesting': '0.5.0'})
|
|
|
|
pip_install = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
2013-11-27 12:59:56 +00:00
|
|
|
'stderr': '',
|
2013-08-18 07:03:41 +00:00
|
|
|
'stdout': 'Downloading/unpacking https://pypi.python.org/packages'
|
|
|
|
'/source/S/SaltTesting/SaltTesting-0.5.0.tar.gz\n '
|
|
|
|
'Downloading SaltTesting-0.5.0.tar.gz\n Running '
|
|
|
|
'setup.py egg_info for package from '
|
|
|
|
'https://pypi.python.org/packages/source/S/SaltTesting/'
|
|
|
|
'SaltTesting-0.5.0.tar.gz\n \nCleaning up...'
|
|
|
|
})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
ret = pip_state.installed(
|
2013-08-18 07:03:41 +00:00
|
|
|
'https://pypi.python.org/packages/source/S/SaltTesting/'
|
|
|
|
'SaltTesting-0.5.0.tar.gz'
|
|
|
|
'#md5=e6760af92b7165f8be53b5763e40bc24'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
2014-12-19 18:05:47 +00:00
|
|
|
self.assertInSaltComment('All packages were successfully installed',
|
2013-08-18 07:03:41 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
self.assertInSaltReturn(
|
|
|
|
'Installed',
|
|
|
|
{'test': ret},
|
2013-08-22 20:06:06 +00:00
|
|
|
('changes', 'https://pypi.python.org/packages/source/S/'
|
|
|
|
'SaltTesting/SaltTesting-0.5.0.tar.gz'
|
|
|
|
'#md5=e6760af92b7165f8be53b5763e40bc24==???')
|
2013-08-18 07:03:41 +00:00
|
|
|
)
|
|
|
|
|
2013-08-26 10:46:50 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
2014-12-19 18:05:47 +00:00
|
|
|
pip_list = MagicMock(return_value={'SaltTesting': '0.5.0'})
|
2013-08-26 10:46:50 +00:00
|
|
|
pip_install = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
2013-11-27 12:59:56 +00:00
|
|
|
'stderr': '',
|
2013-08-26 10:46:50 +00:00
|
|
|
'stdout': 'Cloned!'
|
|
|
|
})
|
2013-08-29 10:37:25 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': False}):
|
|
|
|
ret = pip_state.installed(
|
2013-08-26 10:46:50 +00:00
|
|
|
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
2014-12-19 18:05:47 +00:00
|
|
|
'successfully installed',
|
2013-08-26 10:46:50 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
2013-11-14 13:23:09 +00:00
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'pep8': '1.3.1'})
|
2013-11-27 12:59:56 +00:00
|
|
|
pip_install = MagicMock(return_value={'retcode': 0})
|
2013-11-14 13:23:09 +00:00
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': False}):
|
|
|
|
ret = pip_state.installed(
|
|
|
|
'arbitrary ID that should be ignored due to requirements specified',
|
|
|
|
requirements='/tmp/non-existing-requirements.txt'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
|
2013-09-09 19:53:52 +00:00
|
|
|
# Test VCS installations using git+git://
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
2014-12-19 18:05:47 +00:00
|
|
|
pip_list = MagicMock(return_value={'SaltTesting': '0.5.0'})
|
2013-09-09 19:53:52 +00:00
|
|
|
pip_install = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
2013-11-27 12:59:56 +00:00
|
|
|
'stderr': '',
|
2013-09-09 19:53:52 +00:00
|
|
|
'stdout': 'Cloned!'
|
|
|
|
})
|
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': False}):
|
|
|
|
ret = pip_state.installed(
|
|
|
|
'git+git://github.com/saltstack/salt-testing.git#egg=SaltTesting'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
2014-12-19 18:05:47 +00:00
|
|
|
'were successfully installed',
|
2013-09-09 19:53:52 +00:00
|
|
|
{'test': ret}
|
|
|
|
)
|
|
|
|
|
2013-08-29 10:37:25 +00:00
|
|
|
# Test VCS installations with version info like >= 0.1
|
2017-02-19 15:35:30 +00:00
|
|
|
with patch.object(pip, '__version__', MagicMock(side_effect=AttributeError(
|
|
|
|
'Faked missing __version__ attribute'))):
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={'SaltTesting': '0.5.0'})
|
|
|
|
pip_install = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stderr': '',
|
|
|
|
'stdout': 'Cloned!'
|
|
|
|
})
|
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
with patch.dict(pip_state.__opts__, {'test': False}):
|
|
|
|
ret = pip_state.installed(
|
|
|
|
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting>=0.5.0'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'were successfully installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|
2013-08-29 10:37:25 +00:00
|
|
|
|
2015-03-24 16:09:38 +00:00
|
|
|
def test_install_in_editable_mode(self):
|
|
|
|
'''
|
|
|
|
Check that `name` parameter containing bad characters is not parsed by
|
|
|
|
pip when package is being installed in editable mode.
|
|
|
|
For more information, see issue #21890.
|
|
|
|
'''
|
|
|
|
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
|
|
|
|
pip_list = MagicMock(return_value={})
|
|
|
|
pip_install = MagicMock(return_value={
|
|
|
|
'retcode': 0,
|
|
|
|
'stderr': '',
|
|
|
|
'stdout': 'Cloned!'
|
|
|
|
})
|
|
|
|
with patch.dict(pip_state.__salt__, {'cmd.run_all': mock,
|
|
|
|
'pip.list': pip_list,
|
|
|
|
'pip.install': pip_install}):
|
|
|
|
ret = pip_state.installed('state@name',
|
|
|
|
cwd='/path/to/project',
|
|
|
|
editable=['.'])
|
|
|
|
self.assertSaltTrueReturn({'test': ret})
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'successfully installed',
|
|
|
|
{'test': ret}
|
|
|
|
)
|