2012-09-18 00:53:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2012-12-11 10:23:37 +00:00
|
|
|
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
|
2013-09-16 16:24:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.integration.states.pip
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2012-09-18 00:53:05 +00:00
|
|
|
'''
|
|
|
|
|
2012-11-06 12:44:53 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2016-09-15 03:38:13 +00:00
|
|
|
import errno
|
2012-09-18 00:53:05 +00:00
|
|
|
import os
|
2013-08-27 13:54:00 +00:00
|
|
|
import pwd
|
|
|
|
import glob
|
2012-09-18 00:53:05 +00:00
|
|
|
import shutil
|
|
|
|
|
2013-06-27 12:45:55 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-02 16:09:47 +00:00
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.runtests import RUNTIME_VARS
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.helpers import (
|
2013-08-27 13:54:00 +00:00
|
|
|
destructiveTest,
|
2015-01-09 17:52:55 +00:00
|
|
|
requires_system_grains,
|
2017-04-04 17:57:27 +00:00
|
|
|
with_system_user,
|
|
|
|
skip_if_not_root
|
2013-08-27 13:54:00 +00:00
|
|
|
)
|
2012-09-18 00:53:05 +00:00
|
|
|
# Import salt libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2013-11-02 22:40:09 +00:00
|
|
|
import salt.utils
|
2014-01-11 19:19:29 +00:00
|
|
|
from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
|
2016-04-29 17:49:29 +00:00
|
|
|
from salt.exceptions import CommandExecutionError
|
2012-09-18 00:53:05 +00:00
|
|
|
|
2014-11-22 10:51:11 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import salt.ext.six as six
|
|
|
|
|
2012-09-18 00:53:05 +00:00
|
|
|
|
2017-05-22 16:47:54 +00:00
|
|
|
class VirtualEnv(object):
|
|
|
|
def __init__(self, test, venv_dir):
|
|
|
|
self.venv_dir = venv_dir
|
|
|
|
self.test = test
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
ret = self.test.run_function('virtualenv.create', [self.venv_dir])
|
|
|
|
self.test.assertEqual(ret['retcode'], 0)
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
if os.path.isdir(self.venv_dir):
|
|
|
|
shutil.rmtree(self.venv_dir)
|
|
|
|
|
|
|
|
|
2014-01-11 19:19:29 +00:00
|
|
|
@skipIf(salt.utils.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')
|
2017-04-03 16:04:09 +00:00
|
|
|
class PipStateTest(ModuleCase, SaltReturnAssertsMixin):
|
2012-09-18 00:53:05 +00:00
|
|
|
|
2017-05-22 16:47:54 +00:00
|
|
|
@skip_if_not_root
|
2016-05-12 20:54:47 +00:00
|
|
|
def test_pip_installed_removed(self):
|
|
|
|
'''
|
|
|
|
Tests installed and removed states
|
|
|
|
'''
|
2017-03-13 20:34:28 +00:00
|
|
|
name = 'pudb'
|
|
|
|
if name in self.run_function('pip.list'):
|
|
|
|
self.skipTest('{0} is already installed, uninstall to run this test'.format(name))
|
|
|
|
ret = self.run_state('pip.installed', name=name)
|
2016-05-12 20:54:47 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2017-03-13 20:34:28 +00:00
|
|
|
ret = self.run_state('pip.removed', name=name)
|
2016-05-12 20:54:47 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2017-05-22 16:47:54 +00:00
|
|
|
def test_pip_installed_removed_venv(self):
|
|
|
|
venv_dir = os.path.join(
|
|
|
|
RUNTIME_VARS.TMP, 'pip_installed_removed'
|
|
|
|
)
|
|
|
|
with VirtualEnv(self, venv_dir):
|
|
|
|
name = 'pudb'
|
|
|
|
ret = self.run_state('pip.installed', name=name, bin_env=venv_dir)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
ret = self.run_state('pip.removed', name=name, bin_env=venv_dir)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2012-09-23 17:16:11 +00:00
|
|
|
def test_pip_installed_errors(self):
|
2012-11-06 12:44:53 +00:00
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, 'pip-installed-errors'
|
2012-11-06 12:44:53 +00:00
|
|
|
)
|
2017-05-17 20:12:35 +00:00
|
|
|
orig_shell = os.environ.get('SHELL')
|
2012-09-23 17:16:11 +00:00
|
|
|
try:
|
|
|
|
# Since we don't have the virtualenv created, pip.installed will
|
2017-05-17 20:12:35 +00:00
|
|
|
# throw an error.
|
2013-08-14 11:29:34 +00:00
|
|
|
# Example error strings:
|
2017-03-06 17:12:14 +00:00
|
|
|
# * "Error installing 'pep8': /tmp/pip-installed-errors: not found"
|
|
|
|
# * "Error installing 'pep8': /bin/sh: 1: /tmp/pip-installed-errors: not found"
|
|
|
|
# * "Error installing 'pep8': /bin/bash: /tmp/pip-installed-errors: No such file or directory"
|
2013-08-14 11:29:34 +00:00
|
|
|
os.environ['SHELL'] = '/bin/sh'
|
2012-09-23 17:16:11 +00:00
|
|
|
ret = self.run_function('state.sls', mods='pip-installed-errors')
|
2012-12-07 16:59:24 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertSaltCommentRegexpMatches(
|
|
|
|
ret,
|
2017-03-06 17:12:14 +00:00
|
|
|
'Error installing \'pep8\':'
|
2012-12-07 16:59:24 +00:00
|
|
|
)
|
2012-09-23 17:16:11 +00:00
|
|
|
|
|
|
|
# We now create the missing virtualenv
|
|
|
|
ret = self.run_function('virtualenv.create', [venv_dir])
|
2012-11-06 12:44:53 +00:00
|
|
|
self.assertEqual(ret['retcode'], 0)
|
2012-09-23 17:16:11 +00:00
|
|
|
|
|
|
|
# The state should not have any issues running now
|
|
|
|
ret = self.run_function('state.sls', mods='pip-installed-errors')
|
2012-12-07 16:59:24 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-09-23 17:16:11 +00:00
|
|
|
finally:
|
2017-05-17 20:12:35 +00:00
|
|
|
if orig_shell is None:
|
|
|
|
# Didn't exist before, don't leave it there. This should never
|
|
|
|
# happen, but if it does, we don't want this test to affect
|
|
|
|
# others elsewhere in the suite.
|
|
|
|
os.environ.pop('SHELL')
|
|
|
|
else:
|
|
|
|
os.environ['SHELL'] = orig_shell
|
2012-09-23 17:16:11 +00:00
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2017-05-25 14:37:35 +00:00
|
|
|
@skipIf(six.PY3, 'Issue is specific to carbon module, which is PY2-only')
|
2015-01-09 17:52:55 +00:00
|
|
|
@requires_system_grains
|
|
|
|
def test_pip_installed_weird_install(self, grains=None):
|
2016-09-13 17:35:38 +00:00
|
|
|
# First, check to see if this is running on CentOS 5 or MacOS.
|
|
|
|
# If so, skip this test.
|
2015-01-09 17:52:55 +00:00
|
|
|
if grains['os'] in ('CentOS',) and grains['osrelease_info'][0] in (5,):
|
|
|
|
self.skipTest('This test does not run reliably on CentOS 5')
|
2016-09-13 17:35:38 +00:00
|
|
|
if grains['os'] in ('MacOS',):
|
|
|
|
self.skipTest('This test does not run reliably on MacOS')
|
2015-01-09 17:52:55 +00:00
|
|
|
|
2012-09-25 11:24:54 +00:00
|
|
|
ographite = '/opt/graphite'
|
|
|
|
if os.path.isdir(ographite):
|
|
|
|
self.skipTest(
|
|
|
|
'You already have \'{0}\'. This test would overwrite this '
|
|
|
|
'directory'.format(ographite)
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
os.makedirs(ographite)
|
2013-05-04 02:53:53 +00:00
|
|
|
except OSError as err:
|
2016-09-15 03:38:13 +00:00
|
|
|
if err.errno == errno.EACCES:
|
2012-09-25 11:24:54 +00:00
|
|
|
# Permission denied
|
|
|
|
self.skipTest(
|
|
|
|
'You don\'t have the required permissions to run this test'
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(ographite):
|
|
|
|
shutil.rmtree(ographite)
|
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
venv_dir = os.path.join(RUNTIME_VARS.TMP, 'pip-installed-weird-install')
|
2012-09-25 11:24:54 +00:00
|
|
|
try:
|
2016-09-15 03:38:13 +00:00
|
|
|
# We may be able to remove this, I had to add it because the custom
|
|
|
|
# modules from the test suite weren't available in the jinja
|
|
|
|
# context when running the call to state.sls that comes after.
|
|
|
|
self.run_function('saltutil.sync_modules')
|
2012-09-25 11:24:54 +00:00
|
|
|
# Since we don't have the virtualenv created, pip.installed will
|
|
|
|
# thrown and error.
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='pip-installed-weird-install'
|
|
|
|
)
|
2012-12-07 16:59:24 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-09-25 11:24:54 +00:00
|
|
|
|
2012-12-07 16:59:24 +00:00
|
|
|
# We cannot use assertInSaltComment here because we need to skip
|
|
|
|
# some of the state return parts
|
2014-11-22 10:51:11 +00:00
|
|
|
for key in six.iterkeys(ret):
|
2012-09-25 11:24:54 +00:00
|
|
|
self.assertTrue(ret[key]['result'])
|
2017-12-20 00:37:59 +00:00
|
|
|
if ret[key]['name'] != 'carbon < 1.1':
|
2012-09-25 11:24:54 +00:00
|
|
|
continue
|
|
|
|
self.assertEqual(
|
|
|
|
ret[key]['comment'],
|
2017-12-20 00:37:59 +00:00
|
|
|
'There was no error installing package \'carbon < 1.1\' '
|
2012-09-25 11:24:54 +00:00
|
|
|
'although it does not show when calling \'pip.freeze\'.'
|
|
|
|
)
|
2016-09-15 03:38:13 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Exception('Expected state did not run')
|
2012-09-25 11:24:54 +00:00
|
|
|
finally:
|
2017-12-20 00:37:59 +00:00
|
|
|
if os.path.isdir(ographite):
|
|
|
|
shutil.rmtree(ographite)
|
2012-09-25 11:24:54 +00:00
|
|
|
|
2012-09-23 17:16:11 +00:00
|
|
|
def test_issue_2028_pip_installed_state(self):
|
|
|
|
ret = self.run_function('state.sls', mods='issue-2028-pip-installed')
|
|
|
|
|
2012-11-06 12:44:53 +00:00
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, 'issue-2028-pip-installed'
|
2012-11-06 12:44:53 +00:00
|
|
|
)
|
2012-09-23 17:16:11 +00:00
|
|
|
|
|
|
|
try:
|
2012-12-07 16:59:24 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-09-23 17:16:11 +00:00
|
|
|
self.assertTrue(
|
2017-03-06 17:12:14 +00:00
|
|
|
os.path.isfile(os.path.join(venv_dir, 'bin', 'pep8'))
|
2012-09-23 17:16:11 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2012-09-27 11:06:09 +00:00
|
|
|
|
|
|
|
def test_issue_2087_missing_pip(self):
|
2012-11-06 12:44:53 +00:00
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, 'issue-2087-missing-pip'
|
2012-11-06 12:44:53 +00:00
|
|
|
)
|
|
|
|
|
2012-09-27 11:06:09 +00:00
|
|
|
try:
|
|
|
|
# Let's create the testing virtualenv
|
2012-12-07 16:59:24 +00:00
|
|
|
ret = self.run_function('virtualenv.create', [venv_dir])
|
|
|
|
self.assertEqual(ret['retcode'], 0)
|
2012-09-27 11:06:09 +00:00
|
|
|
|
|
|
|
# Let's remove the pip binary
|
|
|
|
pip_bin = os.path.join(venv_dir, 'bin', 'pip')
|
|
|
|
if not os.path.isfile(pip_bin):
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to find the pip binary to the test virtualenv'
|
|
|
|
)
|
|
|
|
os.remove(pip_bin)
|
|
|
|
|
|
|
|
# Let's run the state which should fail because pip is missing
|
|
|
|
ret = self.run_function('state.sls', mods='issue-2087-missing-pip')
|
2012-12-07 16:59:24 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
2013-08-18 04:46:33 +00:00
|
|
|
'Error installing \'pep8\': Could not find a `pip` binary',
|
|
|
|
ret
|
2012-09-27 11:06:09 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2013-06-25 07:57:26 +00:00
|
|
|
|
2013-07-05 22:00:14 +00:00
|
|
|
def test_issue_5940_multiple_pip_mirrors(self):
|
2016-04-29 17:49:29 +00:00
|
|
|
'''
|
|
|
|
Test multiple pip mirrors. This test only works with pip < 7.0.0
|
|
|
|
'''
|
2013-07-05 22:00:14 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-5940-multiple-pip-mirrors'
|
|
|
|
)
|
|
|
|
|
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, '5940-multiple-pip-mirrors'
|
2013-07-05 22:00:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(os.path.join(venv_dir, 'bin', 'pep8'))
|
|
|
|
)
|
2016-04-29 17:49:29 +00:00
|
|
|
except (AssertionError, CommandExecutionError):
|
|
|
|
pip_version = self.run_function('pip.version', [venv_dir])
|
|
|
|
if salt.utils.compare_versions(ver1=pip_version, oper='>=', ver2='7.0.0'):
|
|
|
|
self.skipTest('the --mirrors arg has been deprecated and removed in pip==7.0.0')
|
2013-07-05 22:00:14 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2013-08-27 13:54:00 +00:00
|
|
|
@destructiveTest
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2014-04-25 19:03:41 +00:00
|
|
|
@with_system_user('issue-6912', on_existing='delete', delete=True)
|
2013-08-27 13:54:00 +00:00
|
|
|
def test_issue_6912_wrong_owner(self, username):
|
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, '6912-wrong-owner'
|
2013-08-27 13:54:00 +00:00
|
|
|
)
|
2013-08-27 13:56:37 +00:00
|
|
|
# ----- Using runas ------------------------------------------------->
|
2013-08-27 13:54:00 +00:00
|
|
|
venv_create = self.run_function(
|
2015-01-07 18:56:57 +00:00
|
|
|
'virtualenv.create', [venv_dir], user=username
|
2013-08-27 13:54:00 +00:00
|
|
|
)
|
|
|
|
if venv_create['retcode'] > 0:
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to create testcase virtual environment: {0}'.format(
|
2013-11-02 22:40:09 +00:00
|
|
|
venv_create
|
2013-08-27 13:54:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-29 20:21:23 +00:00
|
|
|
# Using the package name.
|
2013-08-27 13:54:00 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
2015-01-07 21:18:28 +00:00
|
|
|
'pip.installed', name='pep8', user=username, bin_env=venv_dir
|
2013-08-27 13:54:00 +00:00
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
uinfo = pwd.getpwnam(username)
|
|
|
|
for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '*', '**', 'pep8*')):
|
|
|
|
for path in glob.glob(globmatch):
|
|
|
|
self.assertEqual(
|
|
|
|
uinfo.pw_uid, os.stat(path).st_uid
|
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2013-08-29 20:21:23 +00:00
|
|
|
|
|
|
|
# Using a requirements file
|
|
|
|
venv_create = self.run_function(
|
2015-01-07 21:18:28 +00:00
|
|
|
'virtualenv.create', [venv_dir], user=username
|
2013-08-29 20:21:23 +00:00
|
|
|
)
|
|
|
|
if venv_create['retcode'] > 0:
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to create testcase virtual environment: {0}'.format(
|
|
|
|
ret
|
|
|
|
)
|
|
|
|
)
|
|
|
|
req_filename = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP_STATE_TREE, 'issue-6912-requirements.txt'
|
2013-08-29 20:21:23 +00:00
|
|
|
)
|
2013-11-02 22:40:09 +00:00
|
|
|
with salt.utils.fopen(req_filename, 'wb') as reqf:
|
2017-03-06 17:12:14 +00:00
|
|
|
reqf.write(six.b('pep8'))
|
2013-08-29 20:21:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
2015-01-07 21:18:28 +00:00
|
|
|
'pip.installed', name='', user=username, bin_env=venv_dir,
|
2013-08-29 20:21:23 +00:00
|
|
|
requirements='salt://issue-6912-requirements.txt'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
uinfo = pwd.getpwnam(username)
|
|
|
|
for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '*', '**', 'pep8*')):
|
|
|
|
for path in glob.glob(globmatch):
|
|
|
|
self.assertEqual(
|
|
|
|
uinfo.pw_uid, os.stat(path).st_uid
|
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
os.unlink(req_filename)
|
2013-08-27 13:56:37 +00:00
|
|
|
# <---- Using runas --------------------------------------------------
|
|
|
|
|
|
|
|
# ----- Using user -------------------------------------------------->
|
|
|
|
venv_create = self.run_function(
|
2015-01-07 21:18:28 +00:00
|
|
|
'virtualenv.create', [venv_dir], user=username
|
2013-08-27 13:56:37 +00:00
|
|
|
)
|
|
|
|
if venv_create['retcode'] > 0:
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to create testcase virtual environment: {0}'.format(
|
|
|
|
ret
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-08-29 20:21:23 +00:00
|
|
|
# Using the package name
|
2013-08-27 13:56:37 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'pip.installed', name='pep8', user=username, bin_env=venv_dir
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
uinfo = pwd.getpwnam(username)
|
|
|
|
for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '*', '**', 'pep8*')):
|
|
|
|
for path in glob.glob(globmatch):
|
|
|
|
self.assertEqual(
|
|
|
|
uinfo.pw_uid, os.stat(path).st_uid
|
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2013-08-29 20:21:23 +00:00
|
|
|
|
|
|
|
# Using a requirements file
|
|
|
|
venv_create = self.run_function(
|
2015-01-07 21:18:28 +00:00
|
|
|
'virtualenv.create', [venv_dir], user=username
|
2013-08-29 20:21:23 +00:00
|
|
|
)
|
|
|
|
if venv_create['retcode'] > 0:
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to create testcase virtual environment: {0}'.format(
|
|
|
|
ret
|
|
|
|
)
|
|
|
|
)
|
|
|
|
req_filename = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP_STATE_TREE, 'issue-6912-requirements.txt'
|
2013-08-29 20:21:23 +00:00
|
|
|
)
|
2013-11-02 22:40:09 +00:00
|
|
|
with salt.utils.fopen(req_filename, 'wb') as reqf:
|
2017-03-06 17:12:14 +00:00
|
|
|
reqf.write(six.b('pep8'))
|
2013-08-29 20:21:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'pip.installed', name='', user=username, bin_env=venv_dir,
|
|
|
|
requirements='salt://issue-6912-requirements.txt'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
uinfo = pwd.getpwnam(username)
|
|
|
|
for globmatch in (os.path.join(venv_dir, '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '**', 'pep8*'),
|
|
|
|
os.path.join(venv_dir, '*', '*', '**', 'pep8*')):
|
|
|
|
for path in glob.glob(globmatch):
|
|
|
|
self.assertEqual(
|
|
|
|
uinfo.pw_uid, os.stat(path).st_uid
|
|
|
|
)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
os.unlink(req_filename)
|
2013-08-27 13:56:37 +00:00
|
|
|
# <---- Using user ---------------------------------------------------
|
2013-08-27 13:54:00 +00:00
|
|
|
|
2013-09-10 09:52:17 +00:00
|
|
|
def test_issue_6833_pip_upgrade_pip(self):
|
|
|
|
# Create the testing virtualenv
|
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, '6833-pip-upgrade-pip'
|
2013-09-10 09:52:17 +00:00
|
|
|
)
|
|
|
|
ret = self.run_function('virtualenv.create', [venv_dir])
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.assertEqual(ret['retcode'], 0)
|
|
|
|
self.assertIn(
|
|
|
|
'New python executable',
|
|
|
|
ret['stdout']
|
|
|
|
)
|
|
|
|
except AssertionError:
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(ret)
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Let's install a fixed version pip over whatever pip was
|
|
|
|
# previously installed
|
|
|
|
ret = self.run_function(
|
2017-05-22 16:52:32 +00:00
|
|
|
'pip.install', ['pip==8.0'], upgrade=True,
|
2013-09-10 09:52:17 +00:00
|
|
|
bin_env=venv_dir
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
self.assertEqual(ret['retcode'], 0)
|
|
|
|
self.assertIn(
|
|
|
|
'Successfully installed pip',
|
|
|
|
ret['stdout']
|
|
|
|
)
|
|
|
|
except AssertionError:
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(ret)
|
|
|
|
raise
|
|
|
|
|
2017-05-22 16:52:32 +00:00
|
|
|
# Let's make sure we have pip 8.0 installed
|
2013-09-10 09:52:17 +00:00
|
|
|
self.assertEqual(
|
2013-09-11 09:54:43 +00:00
|
|
|
self.run_function('pip.list', ['pip'], bin_env=venv_dir),
|
2017-05-22 16:52:32 +00:00
|
|
|
{'pip': '8.0.0'}
|
2013-09-10 09:52:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Now the actual pip upgrade pip test
|
|
|
|
ret = self.run_state(
|
2017-05-22 16:52:32 +00:00
|
|
|
'pip.installed', name='pip==8.0.1', upgrade=True,
|
2013-09-10 09:52:17 +00:00
|
|
|
bin_env=venv_dir
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertInSaltReturn(
|
|
|
|
'Installed',
|
|
|
|
ret,
|
2017-05-22 16:52:32 +00:00
|
|
|
['changes', 'pip==8.0.1']
|
2013-09-10 09:52:17 +00:00
|
|
|
)
|
|
|
|
except AssertionError:
|
|
|
|
import pprint
|
|
|
|
pprint.pprint(ret)
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2013-11-02 22:40:09 +00:00
|
|
|
def test_pip_installed_specific_env(self):
|
|
|
|
# Create the testing virtualenv
|
|
|
|
venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, 'pip-installed-specific-env'
|
2013-11-02 22:40:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Let's write a requirements file
|
|
|
|
requirements_file = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP_PRODENV_STATE_TREE, 'prod-env-requirements.txt'
|
2013-11-02 22:40:09 +00:00
|
|
|
)
|
|
|
|
with salt.utils.fopen(requirements_file, 'wb') as reqf:
|
2017-03-06 17:12:14 +00:00
|
|
|
reqf.write(six.b('pep8\n'))
|
2013-11-02 22:40:09 +00:00
|
|
|
|
|
|
|
try:
|
2015-01-05 22:20:18 +00:00
|
|
|
self.run_function('virtualenv.create', [venv_dir])
|
2013-11-02 22:40:09 +00:00
|
|
|
|
|
|
|
# The requirements file should not be found the base environment
|
|
|
|
ret = self.run_state(
|
|
|
|
'pip.installed', name='', bin_env=venv_dir,
|
|
|
|
requirements='salt://prod-env-requirements.txt'
|
|
|
|
)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
|
|
|
"'salt://prod-env-requirements.txt' not found", ret
|
|
|
|
)
|
|
|
|
|
|
|
|
# The requirements file must be found in the prod environment
|
|
|
|
ret = self.run_state(
|
2013-11-06 23:36:40 +00:00
|
|
|
'pip.installed', name='', bin_env=venv_dir, saltenv='prod',
|
2013-11-02 22:40:09 +00:00
|
|
|
requirements='salt://prod-env-requirements.txt'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
2015-01-05 22:36:45 +00:00
|
|
|
'Successfully processed requirements file '
|
|
|
|
'salt://prod-env-requirements.txt', ret
|
2013-11-02 22:40:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# We're using the base environment but we're passing the prod
|
|
|
|
# environment as an url arg to salt://
|
2013-11-06 23:36:40 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'pip.installed', name='', bin_env=venv_dir,
|
|
|
|
requirements='salt://prod-env-requirements.txt?saltenv=prod'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
2015-01-05 22:36:45 +00:00
|
|
|
'Requirements were already installed.',
|
|
|
|
ret
|
2013-11-06 23:36:40 +00:00
|
|
|
)
|
2013-11-02 22:40:09 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
if os.path.isfile(requirements_file):
|
|
|
|
os.unlink(requirements_file)
|
|
|
|
|
2015-04-05 00:00:08 +00:00
|
|
|
def test_22359_pip_installed_unless_does_not_trigger_warnings(self):
|
|
|
|
# This test case should be moved to a format_call unit test specific to
|
|
|
|
# the state internal keywords
|
|
|
|
venv_dir = venv_dir = os.path.join(
|
2017-04-03 16:04:09 +00:00
|
|
|
RUNTIME_VARS.TMP, 'pip-installed-unless'
|
2015-04-05 00:00:08 +00:00
|
|
|
)
|
|
|
|
venv_create = self.run_function('virtualenv.create', [venv_dir])
|
|
|
|
if venv_create['retcode'] > 0:
|
|
|
|
self.skipTest(
|
|
|
|
'Failed to create testcase virtual environment: {0}'.format(
|
|
|
|
venv_create
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'pip.installed', name='pep8', bin_env=venv_dir, unless='/bin/false'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2017-02-21 18:15:52 +00:00
|
|
|
self.assertNotIn('warnings', next(six.itervalues(ret)))
|
2015-04-05 00:00:08 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2018-02-28 07:16:54 +00:00
|
|
|
|
|
|
|
def test_46127_pip_env_vars(self):
|
|
|
|
'''
|
|
|
|
Test that checks if env_vars passed to pip.installed are also passed
|
|
|
|
to pip.freeze while checking for existing installations
|
|
|
|
'''
|
|
|
|
# This issue is most easily checked while installing carbon
|
|
|
|
# Much of the code here comes from the test_weird_install function above
|
|
|
|
ographite = '/opt/graphite'
|
|
|
|
if os.path.isdir(ographite):
|
|
|
|
self.skipTest(
|
|
|
|
'You already have \'{0}\'. This test would overwrite this '
|
|
|
|
'directory'.format(ographite)
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
os.makedirs(ographite)
|
|
|
|
except OSError as err:
|
|
|
|
if err.errno == errno.EACCES:
|
|
|
|
# Permission denied
|
|
|
|
self.skipTest(
|
|
|
|
'You don\'t have the required permissions to run this test'
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(ographite):
|
|
|
|
shutil.rmtree(ographite)
|
|
|
|
|
|
|
|
venv_dir = os.path.join(RUNTIME_VARS.TMP, 'issue-46127-pip-env-vars')
|
|
|
|
try:
|
|
|
|
# We may be able to remove this, I had to add it because the custom
|
|
|
|
# modules from the test suite weren't available in the jinja
|
|
|
|
# context when running the call to state.sls that comes after.
|
|
|
|
self.run_function('saltutil.sync_modules')
|
|
|
|
# Since we don't have the virtualenv created, pip.installed will
|
|
|
|
# thrown and error.
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-46127-pip-env-vars'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
for key in six.iterkeys(ret):
|
|
|
|
self.assertTrue(ret[key]['result'])
|
|
|
|
if ret[key]['name'] != 'carbon < 1.1':
|
|
|
|
continue
|
|
|
|
self.assertEqual(
|
|
|
|
ret[key]['comment'],
|
|
|
|
'All packages were successfully installed'
|
|
|
|
)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Exception('Expected state did not run')
|
|
|
|
# Run the state again. Now the already installed message should
|
|
|
|
# appear
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-46127-pip-env-vars'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
# We cannot use assertInSaltComment here because we need to skip
|
|
|
|
# some of the state return parts
|
|
|
|
for key in six.iterkeys(ret):
|
|
|
|
self.assertTrue(ret[key]['result'])
|
|
|
|
# As we are re-running the formula, some states will not be run
|
|
|
|
# and "name" may or may not be present, so we use .get() pattern
|
|
|
|
if ret[key].get('name', '') != 'carbon < 1.1':
|
|
|
|
continue
|
|
|
|
self.assertEqual(
|
|
|
|
ret[key]['comment'],
|
|
|
|
('Python package carbon < 1.1 was already installed\n'
|
|
|
|
'All packages were successfully installed'))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise Exception('Expected state did not run')
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(ographite):
|
|
|
|
shutil.rmtree(ographite)
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|