Merge pull request #26617 from terminalmage/issue9592

Fix Windows failures in pip module due to raw string formatting
This commit is contained in:
Colton Myers 2015-08-27 13:24:53 -06:00
commit c3a6280f8c
4 changed files with 346 additions and 268 deletions

View file

@ -11,8 +11,8 @@ Salt now uses a portable python. As a result the entire pip module is now
functional on the salt installation itself. You can pip install dependencies
for your custom modules. You can even upgrade salt itself using pip. For this
to work properly, you must specify the Current Working Directory (``cwd``) and
the Pip Binary (``bin_env``) salt should use. The variable ``pip_bin`` can
be either a virtualenv path or the path to the pip binary itself.
the Pip Binary (``bin_env``) salt should use. The variable ``pip_bin`` can be
either a virtualenv path or the path to the pip binary itself.
For example, the following command will list all software installed using pip
to your current salt environment:
@ -223,10 +223,8 @@ def _process_requirements(requirements, cmd, saltenv, user, no_chown):
)
if not cached_requirements:
ret = {'result': False,
'comment': 'pip requirements file {0!r} not found'.format(
requirement
)
}
'comment': 'pip requirements file \'{0}\' not found'
.format(requirement)}
return None, ret
requirement = cached_requirements
@ -236,12 +234,12 @@ def _process_requirements(requirements, cmd, saltenv, user, no_chown):
treq = salt.utils.mkstemp()
shutil.copyfile(requirement, treq)
logger.debug(
'Changing ownership of requirements file {0!r} to '
'user {1!r}'.format(treq, user)
'Changing ownership of requirements file \'{0}\' to '
'user \'{1}\''.format(treq, user)
)
__salt__['file.chown'](treq, user, None)
cleanup_requirements.append(treq)
cmd.append('--requirement={0!r}'.format(treq or requirement))
cmd.extend(['--requirement', treq or requirement])
return cleanup_requirements, None
@ -296,92 +294,122 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
pkgs
Comma separated list of packages to install
requirements
Path to requirements
bin_env
Path to pip bin or path to virtualenv. If doing a system install,
and want to use a specific pip bin (pip-2.7, pip-2.6, etc..) just
specify the pip bin you want.
If installing into a virtualenv, just use the path to the virtualenv
(/home/code/path/to/virtualenv/)
.. note::
If installing into a virtualenv, just use the path to the
virtualenv (e.g. ``/home/code/path/to/virtualenv/``)
env
Deprecated, use bin_env now
use_wheel
Prefer wheel archives (requires pip>=1.4)
no_use_wheel
Force to not use wheel archives (requires pip>=1.4)
log
Log file where a complete (maximum verbosity) record will be kept
proxy
Specify a proxy in the form
user:passwd@proxy.server:port. Note that the
user:password@ is optional and required only if you
are behind an authenticated proxy. If you provide
user@proxy.server:port then you will be prompted for a
password.
Specify a proxy in the form ``user:passwd@proxy.server:port``. Note
that the ``user:password@`` is optional and required only if you are
behind an authenticated proxy. If you provide
``user@proxy.server:port`` then you will be prompted for a password.
timeout
Set the socket timeout (default 15 seconds)
editable
install something editable (i.e.
git+https://github.com/worldcompany/djangoembed.git#egg=djangoembed)
install something editable (e.g.
``git+https://github.com/worldcompany/djangoembed.git#egg=djangoembed``)
find_links
URL to look for packages at
URL to search for packages
index_url
Base URL of Python Package Index
extra_index_url
Extra URLs of package indexes to use in addition to ``index_url``
no_index
Ignore package index
mirrors
Specific mirror URL(s) to query (automatically adds --use-mirrors)
build
Unpack packages into ``build`` dir
target
Install packages into ``target`` dir
download
Download packages into ``download`` instead of installing them
download_cache
Cache downloaded packages in ``download_cache`` dir
source
Check out ``editable`` packages into ``source`` dir
upgrade
Upgrade all packages to the newest available version
force_reinstall
When upgrading, reinstall all packages even if they are already
up-to-date.
ignore_installed
Ignore the installed packages (reinstalling instead)
exists_action
Default action when a path already exists: (s)witch, (i)gnore, (w)ipe,
(b)ackup
no_deps
Ignore package dependencies
no_install
Download and unpack all packages, but don't actually install them
no_download
Don't download any packages, just install the ones
already downloaded (completes an install run with
--no-install)
Don't download any packages, just install the ones already downloaded
(completes an install run with ``--no-install``)
install_options
Extra arguments to be supplied to the setup.py install
command (use like --install-option="--install-
scripts=/usr/local/bin"). Use multiple --install-
option options to pass multiple options to setup.py
install. If you are using an option with a directory
path, be sure to use absolute path.
Extra arguments to be supplied to the setup.py install command (e.g.
like ``--install-option='--install-scripts=/usr/local/bin'``). Use
multiple --install-option options to pass multiple options to setup.py
install. If you are using an option with a directory path, be sure to
use absolute path.
global_options
Extra global options to be supplied to the setup.py call before the
install command.
user
The user under which to run pip
no_chown
When user is given, do not attempt to copy and chown
a requirements file
When user is given, do not attempt to copy and chown a requirements
file
cwd
Current working directory to run pip from
activate
Activates the virtual environment, if given via bin_env,
before running install.
Activates the virtual environment, if given via bin_env, before running
install.
.. deprecated:: 2014.7.2
If `bin_env` is given, pip will already be sourced from that
@ -389,18 +417,27 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
pre_releases
Include pre-releases in the available versions
cert
Provide a path to an alternate CA bundle
allow_all_external
Allow the installation of all externally hosted files
allow_external
Allow the installation of externally hosted files (comma separated list)
Allow the installation of externally hosted files (comma separated
list)
allow_unverified
Allow the installation of insecure and unverifiable files (comma separated list)
Allow the installation of insecure and unverifiable files (comma
separated list)
process_dependency_links
Enable the processing of dependency links
use_vt
Use VT terminal emulation (see ouptut while installing)
env_vars
Set environment variables that some builds will depend on. For example,
a Python C-module may have a Makefile that needs INCLUDE_PATH set to
@ -458,9 +495,13 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
cmd = [pip_bin, 'install']
cleanup_requirements, error = _process_requirements(requirements=requirements, cmd=cmd,
saltenv=saltenv, user=user,
no_chown=no_chown)
cleanup_requirements, error = _process_requirements(
requirements=requirements,
cmd=cmd,
saltenv=saltenv,
user=user,
no_chown=no_chown)
if error:
return error
@ -495,21 +536,26 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
# TODO make this check if writeable
os.path.exists(log)
except IOError:
raise IOError('{0!r} is not writeable'.format(log))
raise IOError('\'{0}\' is not writeable'.format(log))
cmd.append('--log={0}'.format(log))
cmd.extend(['--log', log])
if proxy:
cmd.append('--proxy={0!r}'.format(proxy))
cmd.extend(['--proxy', proxy])
if timeout:
try:
if isinstance(timeout, float):
# Catch floating point input, exception will be caught in
# exception class below.
raise ValueError('Timeout cannot be a float')
int(timeout)
except ValueError:
raise ValueError(
'{0!r} is not a valid integer base 10.'.format(timeout)
'\'{0}\' is not a valid timeout, must be an integer'
.format(timeout)
)
cmd.append('--timeout={0}'.format(timeout))
cmd.extend(['--timeout', timeout])
if find_links:
if isinstance(find_links, string_types):
@ -518,9 +564,9 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
for link in find_links:
if not (salt.utils.valid_url(link, VALID_PROTOS) or os.path.exists(link)):
raise CommandExecutionError(
'{0!r} must be a valid URL or path'.format(link)
'\'{0}\' is not a valid URL or path'.format(link)
)
cmd.append('--find-links={0}'.format(link))
cmd.extend(['--find-links', link])
if no_index and (index_url or extra_index_url):
raise CommandExecutionError(
@ -531,16 +577,16 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
if index_url:
if not salt.utils.valid_url(index_url, VALID_PROTOS):
raise CommandExecutionError(
'{0!r} must be a valid URL'.format(index_url)
'\'{0}\' is not a valid URL'.format(index_url)
)
cmd.append('--index-url={0!r}'.format(index_url))
cmd.extend(['--index-url', index_url])
if extra_index_url:
if not salt.utils.valid_url(extra_index_url, VALID_PROTOS):
raise CommandExecutionError(
'{0!r} must be a valid URL'.format(extra_index_url)
'\'{0}\' is not a valid URL'.format(extra_index_url)
)
cmd.append('--extra-index-url={0!r}'.format(extra_index_url))
cmd.extend(['--extra-index-url', extra_index_url])
if no_index:
cmd.append('--no-index')
@ -553,24 +599,24 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
for mirror in mirrors:
if not mirror.startswith('http://'):
raise CommandExecutionError(
'{0!r} must be a valid URL'.format(mirror)
'\'{0}\' is not a valid URL'.format(mirror)
)
cmd.append('--mirrors={0}'.format(mirror))
cmd.extend(['--mirrors', mirror])
if build:
cmd.append('--build={0}'.format(build))
cmd.extend(['--build', build])
if target:
cmd.append('--target={0}'.format(target))
cmd.extend(['--target', target])
if download:
cmd.append('--download={0}'.format(download))
cmd.extend(['--download', download])
if download_cache:
cmd.append('--download-cache={0}'.format(download_cache))
cmd.extend(['--download-cache', download_cache])
if source:
cmd.append('--source={0}'.format(source))
cmd.extend(['--source', source])
if upgrade:
cmd.append('--upgrade')
@ -584,11 +630,10 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
if exists_action:
if exists_action.lower() not in ('s', 'i', 'w', 'b'):
raise CommandExecutionError(
'The `exists_action`(`--exists-action`) pip option only '
'allows one of (s, i, w, b) to be passed. The {0!r} value '
'is not valid.'.format(exists_action)
'The exists_action pip option only supports the values '
's, i, w, and b. \'{0}\' is not valid.'.format(exists_action)
)
cmd.append('--exists-action={0}'.format(exists_action))
cmd.extend(['--exists-action', exists_action])
if no_deps:
cmd.append('--no-deps')
@ -608,21 +653,21 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
cmd.append('--pre')
if cert:
cmd.append('--cert={0}'.format(cert))
cmd.append(['--cert', cert])
if global_options:
if isinstance(global_options, string_types):
global_options = [go.strip() for go in global_options.split(',')]
for opt in global_options:
cmd.append('--global-option={0!r}'.format(opt))
cmd.extend(['--global-option', opt])
if install_options:
if isinstance(install_options, string_types):
install_options = [io.strip() for io in install_options.split(',')]
for opt in install_options:
cmd.append('--install-option={0!r}'.format(opt))
cmd.extend(['--install-option', opt])
if pkgs:
if isinstance(pkgs, string_types):
@ -632,14 +677,7 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
# they would survive the previous line (in the pip.installed state).
# Put the commas back in while making sure the names are contained in
# quotes, this allows for proper version spec passing salt>=0.17.0
if salt.utils.is_windows():
cmd.extend(
['{0}'.format(p.replace(';', ',')) for p in pkgs]
)
else:
cmd.extend(
['{0!r}'.format(p.replace(';', ',')) for p in pkgs]
)
cmd.extend(['{0}'.format(p.replace(';', ',')) for p in pkgs])
if editable:
egg_match = re.compile(r'(?:#|#.*?&)egg=([^&]*)')
@ -656,7 +694,7 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
raise CommandExecutionError(
'You must specify an egg for this editable'
)
cmd.append('--editable={0}'.format(entry))
cmd.extend(['--editable', entry])
if allow_all_external:
cmd.append('--allow-all-external')
@ -670,7 +708,8 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
if allow_unverified:
if isinstance(allow_unverified, string_types):
allow_unverified = [p.strip() for p in allow_unverified.split(',')]
allow_unverified = \
[p.strip() for p in allow_unverified.split(',')]
for pkg in allow_unverified:
cmd.append('--allow-unverified {0}'.format(pkg))
@ -685,7 +724,9 @@ def install(pkgs=None, # pylint: disable=R0912,R0913,R0914
cmd_kwargs = dict(cwd=cwd, saltenv=saltenv, use_vt=use_vt, runas=user)
if bin_env and os.path.isdir(bin_env):
cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}
return __salt__['cmd.run_all'](' '.join(cmd), python_shell=False, **cmd_kwargs)
return __salt__['cmd.run_all'](cmd,
python_shell=False,
**cmd_kwargs)
finally:
for requirement in cleanup_requirements:
try:
@ -780,21 +821,26 @@ def uninstall(pkgs=None,
# TODO make this check if writeable
os.path.exists(log)
except IOError:
raise IOError('{0!r} is not writeable'.format(log))
raise IOError('\'{0}\' is not writeable'.format(log))
cmd.append('--log={0}'.format(log))
cmd.extend(['--log', log])
if proxy:
cmd.append('--proxy={0!r}'.format(proxy))
cmd.extend(['--proxy', proxy])
if timeout:
try:
if isinstance(timeout, float):
# Catch floating point input, exception will be caught in
# exception class below.
raise ValueError('Timeout cannot be a float')
int(timeout)
except ValueError:
raise ValueError(
'{0!r} is not a valid integer base 10.'.format(timeout)
'\'{0}\' is not a valid timeout, must be an integer'
.format(timeout)
)
cmd.append('--timeout={0}'.format(timeout))
cmd.extend(['--timeout', timeout])
if pkgs:
if isinstance(pkgs, string_types):
@ -816,7 +862,7 @@ def uninstall(pkgs=None,
cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}
try:
return __salt__['cmd.run_all'](' '.join(cmd), **cmd_kwargs)
return __salt__['cmd.run_all'](cmd, **cmd_kwargs)
finally:
for requirement in cleanup_requirements:
try:
@ -856,7 +902,7 @@ def freeze(bin_env=None,
cmd_kwargs = dict(runas=user, cwd=cwd, use_vt=use_vt, python_shell=False)
if bin_env and os.path.isdir(bin_env):
cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}
result = __salt__['cmd.run_all'](' '.join(cmd), **cmd_kwargs)
result = __salt__['cmd.run_all'](cmd, **cmd_kwargs)
if result['retcode'] > 0:
raise CommandExecutionError(result['stderr'])
@ -891,7 +937,7 @@ def list_(prefix=None,
if not prefix or prefix in ('p', 'pi', 'pip'):
packages['pip'] = version(bin_env)
result = __salt__['cmd.run_all'](' '.join(cmd), **cmd_kwargs)
result = __salt__['cmd.run_all'](cmd, **cmd_kwargs)
if result['retcode'] > 0:
raise CommandExecutionError(result['stderr'])
@ -910,7 +956,7 @@ def list_(prefix=None,
name = line.split('==')[0]
version_ = line.split('==')[1]
else:
logger.error('Can\'t parse line {0!r}'.format(line))
logger.error('Can\'t parse line \'{0}\''.format(line))
continue
if prefix:
@ -959,24 +1005,24 @@ def list_upgrades(bin_env=None,
'''
pip_bin = _get_pip_bin(bin_env)
cmd = [pip_bin, "list", "--outdated"]
cmd = [pip_bin, 'list', '--outdated']
cmd_kwargs = dict(cwd=cwd, runas=user)
if bin_env and os.path.isdir(bin_env):
cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}
result = __salt__['cmd.run_all'](' '.join(cmd), **cmd_kwargs)
result = __salt__['cmd.run_all'](cmd, **cmd_kwargs)
if result['retcode'] > 0:
logger.error(result['stderr'])
raise CommandExecutionError(result['stderr'])
packages = {}
for line in result['stdout'].splitlines():
match = re.search(r"(\S*)\s+\(.*Latest:\s+(.*)\)", line)
match = re.search(r'(\S*)\s+\(.*Latest:\s+(.*)\)', line)
if match:
name, version_ = match.groups()
else:
logger.error('Can\'t parse line {0!r}'.format(line))
logger.error('Can\'t parse line \'{0}\''.format(line))
continue
packages[name] = version_
return packages
@ -1029,13 +1075,13 @@ def upgrade(bin_env=None,
old = list_(bin_env=bin_env, user=user, cwd=cwd)
cmd = [pip_bin, "install", "-U"]
cmd = [pip_bin, 'install', '-U']
cmd_kwargs = dict(cwd=cwd, use_vt=use_vt)
if bin_env and os.path.isdir(bin_env):
cmd_kwargs['env'] = {'VIRTUAL_ENV': bin_env}
errors = False
for pkg in list_upgrades(bin_env=bin_env, user=user, cwd=cwd):
result = __salt__['cmd.run_all'](' '.join(cmd+[pkg]), **cmd_kwargs)
result = __salt__['cmd.run_all'](cmd + [pkg], **cmd_kwargs)
if result['retcode'] != 0:
errors = True
if 'stderr' in result:

View file

@ -474,7 +474,13 @@ def installed(name,
bin_env = env
# If pkgs is present, ignore name
if not pkgs:
if pkgs:
if not isinstance(pkgs, list):
return {'name': name,
'result': False,
'changes': {},
'comment': 'pkgs argument must be formatted as a list'}
else:
pkgs = [name]
# Assumption: If `pkg` is not an `string`, it's a `collections.OrderedDict`

View file

@ -5,6 +5,7 @@ from __future__ import absolute_import
import subprocess
import threading
import salt.exceptions
from salt.ext import six
class TimedProc(object):
@ -13,7 +14,6 @@ class TimedProc(object):
'''
def __init__(self, args, **kwargs):
self.command = args
self.stdin = kwargs.pop('stdin', None)
if self.stdin is not None:
# Translate a newline submitted as '\n' on the CLI to an actual
@ -22,7 +22,18 @@ class TimedProc(object):
kwargs['stdin'] = subprocess.PIPE
self.with_communicate = kwargs.pop('with_communicate', True)
self.process = subprocess.Popen(args, **kwargs)
try:
self.process = subprocess.Popen(args, **kwargs)
except TypeError:
str_args = []
for arg in args:
if not isinstance(arg, six.string_types):
str_args.append(str(arg))
else:
str_args.append(arg)
args = str_args
self.process = subprocess.Popen(args, **kwargs)
self.command = args
def wait(self, timeout=None):
'''

View file

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Import python libs
import os
# Import Salt Testing libs
from salttesting import skipIf, TestCase
@ -22,7 +23,8 @@ class PipTestCase(TestCase):
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\''
expected_cmd = ['pip', 'install', '--requirement',
'requirements.txt']
mock.assert_called_once_with(
expected_cmd,
saltenv='base',
@ -32,7 +34,7 @@ class PipTestCase(TestCase):
python_shell=False,
)
def test_install_editable_withough_egg_fails(self):
def test_install_editable_without_egg_fails(self):
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
self.assertRaises(
@ -40,7 +42,6 @@ class PipTestCase(TestCase):
pip.install,
editable='git+https://github.com/saltstack/salt-testing.git'
)
#mock.assert_called_once_with('', cwd=None, use_vt=False)
def test_install_multiple_editable(self):
editables = [
@ -48,14 +49,16 @@ class PipTestCase(TestCase):
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
]
expected = ['pip', 'install']
for item in editables:
expected.extend(['--editable', item])
# Passing editables as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(editable=editables)
mock.assert_called_once_with(
'pip install '
'--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
'--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -68,9 +71,7 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(editable=','.join(editables))
mock.assert_called_once_with(
'pip install '
'--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
'--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -79,24 +80,22 @@ class PipTestCase(TestCase):
)
def test_install_multiple_pkgs_and_editables(self):
pkgs = [
'pep8',
'salt'
]
pkgs = ['pep8', 'salt']
editables = [
'git+https://github.com/jek/blinker.git#egg=Blinker',
'git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting'
]
expected = ['pip', 'install'] + pkgs
for item in editables:
expected.extend(['--editable', item])
# Passing editables as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(pkgs=pkgs, editable=editables)
mock.assert_called_once_with(
'pip install \'pep8\' \'salt\' '
'--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
'--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -109,9 +108,7 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(pkgs=','.join(pkgs), editable=','.join(editables))
mock.assert_called_once_with(
'pip install \'pep8\' \'salt\' '
'--editable=git+https://github.com/jek/blinker.git#egg=Blinker '
'--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -119,13 +116,12 @@ class PipTestCase(TestCase):
python_shell=False,
)
# As a single string
# As single string (just use the first element from pkgs and editables)
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(pkgs=pkgs[0], editable=editables[0])
mock.assert_called_once_with(
'pip install \'pep8\' '
'--editable=git+https://github.com/jek/blinker.git#egg=Blinker',
['pip', 'install', pkgs[0], '--editable', editables[0]],
saltenv='base',
runas=None,
cwd=None,
@ -140,15 +136,16 @@ class PipTestCase(TestCase):
'http://pypi.crate.io'
]
expected = ['pip', 'install', '--use-mirrors']
for item in mirrors:
expected.extend(['--mirrors', item])
# Passing mirrors as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(mirrors=mirrors)
mock.assert_called_once_with(
'pip install --use-mirrors '
'--mirrors=http://g.pypi.python.org '
'--mirrors=http://c.pypi.python.org '
'--mirrors=http://pypi.crate.io',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -161,10 +158,7 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(mirrors=','.join(mirrors))
mock.assert_called_once_with(
'pip install --use-mirrors '
'--mirrors=http://g.pypi.python.org '
'--mirrors=http://c.pypi.python.org '
'--mirrors=http://pypi.crate.io',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -172,13 +166,12 @@ class PipTestCase(TestCase):
python_shell=False,
)
# As a single string
# As single string (just use the first element from mirrors)
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(mirrors=mirrors[0])
mock.assert_called_once_with(
'pip install --use-mirrors '
'--mirrors=http://g.pypi.python.org',
['pip', 'install', '--use-mirrors', '--mirrors', mirrors[0]],
saltenv='base',
runas=None,
cwd=None,
@ -192,16 +185,19 @@ class PipTestCase(TestCase):
'http://c.pypi.python.org',
'http://pypi.crate.io'
]
pkg = 'pep8'
expected = ['pip', 'install']
for item in find_links:
expected.extend(['--find-links', item])
expected.append(pkg)
# Passing mirrors as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', find_links=find_links)
pip.install(pkg, find_links=find_links)
mock.assert_called_once_with(
'pip install '
'--find-links=http://g.pypi.python.org '
'--find-links=http://c.pypi.python.org '
'--find-links=http://pypi.crate.io \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -212,12 +208,9 @@ class PipTestCase(TestCase):
# Passing mirrors as a comma separated list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', find_links=','.join(find_links))
pip.install(pkg, find_links=','.join(find_links))
mock.assert_called_once_with(
'pip install '
'--find-links=http://g.pypi.python.org '
'--find-links=http://c.pypi.python.org '
'--find-links=http://pypi.crate.io \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -225,12 +218,12 @@ class PipTestCase(TestCase):
python_shell=False,
)
# Passing mirrors as a single string entry
# As single string (just use the first element from find_links)
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', find_links=find_links[0])
pip.install(pkg, find_links=find_links[0])
mock.assert_called_once_with(
'pip install --find-links=http://g.pypi.python.org \'pep8\'',
['pip', 'install', '--find-links', find_links[0], pkg],
saltenv='base',
runas=None,
cwd=None,
@ -244,25 +237,16 @@ class PipTestCase(TestCase):
self.assertRaises(
CommandExecutionError,
pip.install,
'\'pep8\'',
'\'' + pkg + '\'',
find_links='sftp://pypi.crate.io'
)
# Valid protos work?
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(
'pep8', find_links=[
'ftp://g.pypi.python.org',
'http://c.pypi.python.org',
'https://pypi.crate.io'
]
)
pip.install(pkg, find_links=find_links)
mock.assert_called_once_with(
'pip install '
'--find-links=ftp://g.pypi.python.org '
'--find-links=http://c.pypi.python.org '
'--find-links=https://pypi.crate.io \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -277,7 +261,6 @@ class PipTestCase(TestCase):
CommandExecutionError,
pip.install, no_index=True, index_url='http://foo.tld'
)
#mock.assert_called_once_with('', cwd=None)
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
@ -285,7 +268,6 @@ class PipTestCase(TestCase):
CommandExecutionError,
pip.install, no_index=True, extra_index_url='http://foo.tld'
)
#mock.assert_called_once_with('', cwd=None)
@patch('salt.modules.pip._get_cached_requirements')
def test_install_failed_cached_requirements(self, get_cached_requirements):
@ -300,9 +282,9 @@ class PipTestCase(TestCase):
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\''
expected = ['pip', 'install', '--requirement', 'my_cached_reqs']
mock.assert_called_once_with(
expected_cmd,
expected,
saltenv='base',
runas=None,
cwd=None,
@ -315,15 +297,17 @@ class PipTestCase(TestCase):
mock_path.is_file.return_value = True
mock_path.isdir.return_value = True
pkg = 'mock'
venv_path = '/test_env'
def join(*args):
return '/'.join(args)
mock_path.join = join
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('mock', bin_env='/test_env')
pip.install(pkg, bin_env=venv_path)
mock.assert_called_once_with(
'/test_env/bin/pip install '
'\'mock\'',
[os.path.join(venv_path, 'bin', 'pip'), 'install', pkg],
env={'VIRTUAL_ENV': '/test_env'},
saltenv='base',
runas=None,
@ -334,11 +318,13 @@ class PipTestCase(TestCase):
@patch('os.path')
def test_install_log_argument_in_resulting_command(self, mock_path):
pkg = 'pep8'
log_path = '/tmp/pip-install.log'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', log='/tmp/pip-install.log')
pip.install(pkg, log=log_path)
mock.assert_called_once_with(
'pip install --log=/tmp/pip-install.log \'pep8\'',
['pip', 'install', '--log', log_path, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -353,17 +339,19 @@ class PipTestCase(TestCase):
self.assertRaises(
IOError,
pip.install,
'pep8',
log='/tmp/pip-install.log'
pkg,
log=log_path
)
def test_install_timeout_argument_in_resulting_command(self):
# Passing an int
pkg = 'pep8'
expected_prefix = ['pip', 'install', '--timeout']
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', timeout=10)
pip.install(pkg, timeout=10)
mock.assert_called_once_with(
'pip install --timeout=10 \'pep8\'',
expected_prefix + [10, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -374,9 +362,9 @@ class PipTestCase(TestCase):
# Passing an int as a string
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', timeout='10')
pip.install(pkg, timeout='10')
mock.assert_called_once_with(
'pip install --timeout=10 \'pep8\'',
expected_prefix + ['10', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -390,16 +378,18 @@ class PipTestCase(TestCase):
self.assertRaises(
ValueError,
pip.install,
'pep8',
pkg,
timeout='a'
)
def test_install_index_url_argument_in_resulting_command(self):
pkg = 'pep8'
index_url = 'http://foo.tld'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', index_url='http://foo.tld')
pip.install(pkg, index_url=index_url)
mock.assert_called_once_with(
'pip install --index-url=\'http://foo.tld\' \'pep8\'',
['pip', 'install', '--index-url', index_url, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -408,11 +398,13 @@ class PipTestCase(TestCase):
)
def test_install_extra_index_url_argument_in_resulting_command(self):
pkg = 'pep8'
extra_index_url = 'http://foo.tld'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', extra_index_url='http://foo.tld')
pip.install(pkg, extra_index_url=extra_index_url)
mock.assert_called_once_with(
'pip install --extra-index-url=\'http://foo.tld\' \'pep8\'',
['pip', 'install', '--extra-index-url', extra_index_url, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -421,11 +413,12 @@ class PipTestCase(TestCase):
)
def test_install_no_index_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', no_index=True)
pip.install(pkg, no_index=True)
mock.assert_called_once_with(
'pip install --no-index \'pep8\'',
['pip', 'install', '--no-index', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -434,11 +427,13 @@ class PipTestCase(TestCase):
)
def test_install_build_argument_in_resulting_command(self):
pkg = 'pep8'
build = '/tmp/foo'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', build='/tmp/foo')
pip.install(pkg, build=build)
mock.assert_called_once_with(
'pip install --build=/tmp/foo \'pep8\'',
['pip', 'install', '--build', build, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -447,11 +442,13 @@ class PipTestCase(TestCase):
)
def test_install_target_argument_in_resulting_command(self):
pkg = 'pep8'
target = '/tmp/foo'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', target='/tmp/foo')
pip.install(pkg, target=target)
mock.assert_called_once_with(
'pip install --target=/tmp/foo \'pep8\'',
['pip', 'install', '--target', target, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -460,11 +457,13 @@ class PipTestCase(TestCase):
)
def test_install_download_argument_in_resulting_command(self):
pkg = 'pep8'
download = '/tmp/foo'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', download='/tmp/foo')
pip.install(pkg, download=download)
mock.assert_called_once_with(
'pip install --download=/tmp/foo \'pep8\'',
['pip', 'install', '--download', download, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -473,11 +472,12 @@ class PipTestCase(TestCase):
)
def test_install_no_download_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', no_download=True)
pip.install(pkg, no_download=True)
mock.assert_called_once_with(
'pip install --no-download \'pep8\'',
['pip', 'install', '--no-download', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -486,11 +486,13 @@ class PipTestCase(TestCase):
)
def test_install_download_cache_argument_in_resulting_command(self):
pkg = 'pep8'
download_cache = '/tmp/foo'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', download_cache='/tmp/foo')
pip.install(pkg, download_cache='/tmp/foo')
mock.assert_called_once_with(
'pip install --download-cache=/tmp/foo \'pep8\'',
['pip', 'install', '--download-cache', download_cache, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -499,11 +501,13 @@ class PipTestCase(TestCase):
)
def test_install_source_argument_in_resulting_command(self):
pkg = 'pep8'
source = '/tmp/foo'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', source='/tmp/foo')
pip.install(pkg, source=source)
mock.assert_called_once_with(
'pip install --source=/tmp/foo \'pep8\'',
['pip', 'install', '--source', source, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -512,12 +516,13 @@ class PipTestCase(TestCase):
)
def test_install_exists_action_argument_in_resulting_command(self):
pkg = 'pep8'
for action in ('s', 'i', 'w', 'b'):
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', exists_action=action)
mock.assert_called_once_with(
'pip install --exists-action={0} \'pep8\''.format(action),
['pip', 'install', '--exists-action', action, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -531,7 +536,7 @@ class PipTestCase(TestCase):
self.assertRaises(
CommandExecutionError,
pip.install,
'pep8',
pkg,
exists_action='d'
)
@ -540,15 +545,19 @@ class PipTestCase(TestCase):
'--exec-prefix=/foo/bar',
'--install-scripts=/foo/bar/bin'
]
pkg = 'pep8'
expected = ['pip', 'install']
for item in install_options:
expected.extend(['--install-option', item])
expected.append(pkg)
# Passing options as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', install_options=install_options)
pip.install(pkg, install_options=install_options)
mock.assert_called_once_with(
'pip install '
'--install-option=\'--exec-prefix=/foo/bar\' '
'--install-option=\'--install-scripts=/foo/bar/bin\' \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -559,11 +568,9 @@ class PipTestCase(TestCase):
# Passing mirrors as a comma separated list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', install_options=','.join(install_options))
pip.install(pkg, install_options=','.join(install_options))
mock.assert_called_once_with(
'pip install '
'--install-option=\'--exec-prefix=/foo/bar\' '
'--install-option=\'--install-scripts=/foo/bar/bin\' \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -574,10 +581,10 @@ class PipTestCase(TestCase):
# Passing mirrors as a single string entry
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', install_options=install_options[0])
pip.install(pkg, install_options=install_options[0])
mock.assert_called_once_with(
'pip install --install-option=\'--exec-prefix=/foo/bar\' '
'\'pep8\'',
['pip', 'install', '--install-option',
install_options[0], pkg],
saltenv='base',
runas=None,
cwd=None,
@ -590,15 +597,19 @@ class PipTestCase(TestCase):
'--quiet',
'--no-user-cfg'
]
pkg = 'pep8'
expected = ['pip', 'install']
for item in global_options:
expected.extend(['--global-option', item])
expected.append(pkg)
# Passing options as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', global_options=global_options)
pip.install(pkg, global_options=global_options)
mock.assert_called_once_with(
'pip install '
'--global-option=\'--quiet\' '
'--global-option=\'--no-user-cfg\' \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -609,11 +620,9 @@ class PipTestCase(TestCase):
# Passing mirrors as a comma separated list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', global_options=','.join(global_options))
pip.install(pkg, global_options=','.join(global_options))
mock.assert_called_once_with(
'pip install '
'--global-option=\'--quiet\' '
'--global-option=\'--no-user-cfg\' \'pep8\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -624,9 +633,9 @@ class PipTestCase(TestCase):
# Passing mirrors as a single string entry
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', global_options=global_options[0])
pip.install(pkg, global_options=global_options[0])
mock.assert_called_once_with(
'pip install --global-option=\'--quiet\' \'pep8\'',
['pip', 'install', '--global-option', global_options[0], pkg],
saltenv='base',
runas=None,
cwd=None,
@ -635,11 +644,12 @@ class PipTestCase(TestCase):
)
def test_install_upgrade_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', upgrade=True)
pip.install(pkg, upgrade=True)
mock.assert_called_once_with(
'pip install --upgrade \'pep8\'',
['pip', 'install', '--upgrade', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -648,11 +658,12 @@ class PipTestCase(TestCase):
)
def test_install_force_reinstall_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', force_reinstall=True)
pip.install(pkg, force_reinstall=True)
mock.assert_called_once_with(
'pip install --force-reinstall \'pep8\'',
['pip', 'install', '--force-reinstall', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -661,11 +672,12 @@ class PipTestCase(TestCase):
)
def test_install_ignore_installed_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', ignore_installed=True)
pip.install(pkg, ignore_installed=True)
mock.assert_called_once_with(
'pip install --ignore-installed \'pep8\'',
['pip', 'install', '--ignore-installed', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -674,11 +686,12 @@ class PipTestCase(TestCase):
)
def test_install_no_deps_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', no_deps=True)
pip.install(pkg, no_deps=True)
mock.assert_called_once_with(
'pip install --no-deps \'pep8\'',
['pip', 'install', '--no-deps', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -687,11 +700,12 @@ class PipTestCase(TestCase):
)
def test_install_no_install_argument_in_resulting_command(self):
pkg = 'pep8'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', no_install=True)
pip.install(pkg, no_install=True)
mock.assert_called_once_with(
'pip install --no-install \'pep8\'',
['pip', 'install', '--no-install', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -700,12 +714,13 @@ class PipTestCase(TestCase):
)
def test_install_proxy_argument_in_resulting_command(self):
pkg = 'pep8'
proxy = 'salt-user:salt-passwd@salt-proxy:3128'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install('pep8', proxy='salt-user:salt-passwd@salt-proxy:3128')
pip.install(pkg, proxy=proxy)
mock.assert_called_once_with(
'pip install '
'--proxy=\'salt-user:salt-passwd@salt-proxy:3128\' \'pep8\'',
['pip', 'install', '--proxy', proxy, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -715,21 +730,24 @@ class PipTestCase(TestCase):
@patch('salt.modules.pip._get_cached_requirements')
def test_install_multiple_requirements_arguments_in_resulting_command(self, get_cached_requirements):
get_cached_requirements.side_effect = [
cached_reqs = [
'my_cached_reqs-1', 'my_cached_reqs-2'
]
get_cached_requirements.side_effect = cached_reqs
requirements = [
'salt://requirements-1.txt', 'salt://requirements-2.txt'
]
expected = ['pip', 'install']
for item in cached_reqs:
expected.extend(['--requirement', item])
# Passing option as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(requirements=requirements)
mock.assert_called_once_with(
'pip install '
'--requirement=\'my_cached_reqs-1\' '
'--requirement=\'my_cached_reqs-2\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -738,16 +756,12 @@ class PipTestCase(TestCase):
)
# Passing option as a comma separated list
get_cached_requirements.side_effect = [
'my_cached_reqs-1', 'my_cached_reqs-2'
]
get_cached_requirements.side_effect = cached_reqs
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(requirements=','.join(requirements))
mock.assert_called_once_with(
'pip install '
'--requirement=\'my_cached_reqs-1\' '
'--requirement=\'my_cached_reqs-2\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -756,12 +770,12 @@ class PipTestCase(TestCase):
)
# Passing option as a single string entry
get_cached_requirements.side_effect = ['my_cached_reqs-1']
get_cached_requirements.side_effect = [cached_reqs[0]]
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(requirements=requirements[0])
mock.assert_called_once_with(
'pip install --requirement=\'my_cached_reqs-1\'',
['pip', 'install', '--requirement', cached_reqs[0]],
saltenv='base',
runas=None,
cwd=None,
@ -771,21 +785,24 @@ class PipTestCase(TestCase):
@patch('salt.modules.pip._get_cached_requirements')
def test_uninstall_multiple_requirements_arguments_in_resulting_command(self, get_cached_requirements):
get_cached_requirements.side_effect = [
cached_reqs = [
'my_cached_reqs-1', 'my_cached_reqs-2'
]
get_cached_requirements.side_effect = cached_reqs
requirements = [
'salt://requirements-1.txt', 'salt://requirements-2.txt'
]
expected = ['pip', 'uninstall', '-y']
for item in cached_reqs:
expected.extend(['--requirement', item])
# Passing option as a list
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall(requirements=requirements)
mock.assert_called_once_with(
'pip uninstall -y '
'--requirement=\'my_cached_reqs-1\' '
'--requirement=\'my_cached_reqs-2\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -794,16 +811,12 @@ class PipTestCase(TestCase):
)
# Passing option as a comma separated list
get_cached_requirements.side_effect = [
'my_cached_reqs-1', 'my_cached_reqs-2'
]
get_cached_requirements.side_effect = cached_reqs
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall(requirements=','.join(requirements))
mock.assert_called_once_with(
'pip uninstall -y '
'--requirement=\'my_cached_reqs-1\' '
'--requirement=\'my_cached_reqs-2\'',
expected,
saltenv='base',
runas=None,
cwd=None,
@ -812,12 +825,12 @@ class PipTestCase(TestCase):
)
# Passing option as a single string entry
get_cached_requirements.side_effect = ['my_cached_reqs-1']
get_cached_requirements.side_effect = [cached_reqs[0]]
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall(requirements=requirements[0])
mock.assert_called_once_with(
'pip uninstall -y --requirement=\'my_cached_reqs-1\'',
['pip', 'uninstall', '-y', '--requirement', cached_reqs[0]],
saltenv='base',
runas=None,
cwd=None,
@ -826,14 +839,13 @@ class PipTestCase(TestCase):
)
def test_uninstall_proxy_argument_in_resulting_command(self):
pkg = 'pep8'
proxy = 'salt-user:salt-passwd@salt-proxy:3128'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall(
'pep8', proxy='salt-user:salt-passwd@salt-proxy:3128'
)
pip.uninstall(pkg, proxy=proxy)
mock.assert_called_once_with(
'pip uninstall -y '
'--proxy=\'salt-user:salt-passwd@salt-proxy:3128\' pep8',
['pip', 'uninstall', '-y', '--proxy', proxy, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -843,11 +855,13 @@ class PipTestCase(TestCase):
@patch('os.path')
def test_uninstall_log_argument_in_resulting_command(self, mock_path):
pkg = 'pep8'
log_path = '/tmp/pip-install.log'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall('pep8', log='/tmp/pip-install.log')
pip.uninstall(pkg, log=log_path)
mock.assert_called_once_with(
'pip uninstall -y --log=/tmp/pip-install.log pep8',
['pip', 'uninstall', '-y', '--log', log_path, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -862,17 +876,19 @@ class PipTestCase(TestCase):
self.assertRaises(
IOError,
pip.uninstall,
'pep8',
log='/tmp/pip-install.log'
pkg,
log=log_path
)
def test_uninstall_timeout_argument_in_resulting_command(self):
pkg = 'pep8'
expected_prefix = ['pip', 'uninstall', '-y', '--timeout']
# Passing an int
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall('pep8', timeout=10)
pip.uninstall(pkg, timeout=10)
mock.assert_called_once_with(
'pip uninstall -y --timeout=10 pep8',
expected_prefix + [10, pkg],
saltenv='base',
runas=None,
cwd=None,
@ -883,9 +899,9 @@ class PipTestCase(TestCase):
# Passing an int as a string
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall('pep8', timeout='10')
pip.uninstall(pkg, timeout='10')
mock.assert_called_once_with(
'pip uninstall -y --timeout=10 pep8',
expected_prefix + ['10', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -899,7 +915,7 @@ class PipTestCase(TestCase):
self.assertRaises(
ValueError,
pip.uninstall,
'pep8',
pkg,
timeout='a'
)
@ -920,7 +936,7 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
ret = pip.freeze()
mock.assert_called_once_with(
'pip freeze',
['pip', 'freeze'],
runas=None,
cwd=None,
use_vt=False,
@ -951,7 +967,7 @@ class PipTestCase(TestCase):
MagicMock(return_value=mock_version)):
ret = pip.list_()
mock.assert_called_with(
'pip freeze',
['pip', 'freeze'],
runas=None,
cwd=None,
python_shell=False,
@ -994,7 +1010,7 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
ret = pip.list_(prefix='bb')
mock.assert_called_with(
'pip freeze',
['pip', 'freeze'],
runas=None,
cwd=None,
python_shell=False,
@ -1007,6 +1023,7 @@ class PipTestCase(TestCase):
)
def test_install_pre_argument_in_resulting_command(self):
pkg = 'pep8'
# Lower than 1.4 versions don't end-up with `--pre` in the resulting
# output
mock = MagicMock(side_effect=[
@ -1016,11 +1033,9 @@ class PipTestCase(TestCase):
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
with patch('salt.modules.pip.version',
MagicMock(return_value='1.3')):
pip.install(
'pep8', pre_releases=True
)
pip.install(pkg, pre_releases=True)
mock.assert_called_with(
'pip install \'pep8\'',
['pip', 'install', pkg],
saltenv='base',
runas=None,
cwd=None,
@ -1034,9 +1049,9 @@ class PipTestCase(TestCase):
'cmd.run_all': mock_run_all}):
with patch('salt.modules.pip._get_pip_bin',
MagicMock(return_value='pip')):
pip.install('pep8', pre_releases=True)
pip.install(pkg, pre_releases=True)
mock_run_all.assert_called_with(
'pip install --pre \'pep8\'',
['pip', 'install', '--pre', pkg],
saltenv='base',
runas=None,
cwd=None,