Merge pull request #23136 from basepi/merge-forward-develop

Merge forward from 2015.2 to develop
This commit is contained in:
Justin Findlay 2015-04-28 12:00:23 -06:00
commit baca0bd5cf
8 changed files with 237 additions and 49 deletions

View File

@ -43,6 +43,16 @@ else:
import exceptions # pylint: disable=incompatible-py3-code
if not hasattr(ElementTree, 'ParseError'):
class ParseError(Exception):
'''
older versions of ElementTree do not have ParseError
'''
pass
ElementTree.ParseError = ParseError
def text_(s, encoding='latin-1', errors='strict'):
'''
If ``s`` is an instance of ``binary_type``, return

View File

@ -3858,8 +3858,30 @@ def manage_file(name,
if sfn:
__clean_tmp(sfn)
return ret
else:
# Only set the diff if the file contents is managed
else: # target file does not exist
contain_dir = os.path.dirname(name)
def _set_mode_and_make_dirs(name, dir_mode, mode, user, group):
# check for existence of windows drive letter
if salt.utils.is_windows():
drive, _ = os.path.splitdrive(name)
if drive and not os.path.exists(drive):
__clean_tmp(sfn)
return _error(ret,
'{0} drive not present'.format(drive))
if dir_mode is None and mode is not None:
# Add execute bit to each nonzero digit in the mode, if
# dir_mode was not specified. Otherwise, any
# directories created with makedirs_() below can't be
# listed via a shell.
mode_list = [x for x in str(mode)][-3:]
for idx in range(len(mode_list)):
if mode_list[idx] != '0':
mode_list[idx] = str(int(mode_list[idx]) | 1)
dir_mode = ''.join(mode_list)
makedirs_(name, user=user,
group=group, mode=dir_mode)
if source:
# It is a new file, set the diff accordingly
ret['changes']['diff'] = 'New file'
@ -3881,43 +3903,18 @@ def manage_file(name,
dl_sum)
ret['result'] = False
return ret
if not os.path.isdir(os.path.dirname(name)):
if not os.path.isdir(contain_dir):
if makedirs:
# check for existence of windows drive letter
if salt.utils.is_windows():
drive, _ = os.path.splitdrive(name)
if drive and not os.path.exists(drive):
__clean_tmp(sfn)
return _error(ret,
'{0} drive not present'.format(drive))
if dir_mode is None and mode is not None:
# Add execute bit to each nonzero digit in the mode, if
# dir_mode was not specified. Otherwise, any
# directories created with makedirs_() below can't be
# listed via a shell.
mode_list = [x for x in str(mode)][-3:]
for idx in range(len(mode_list)):
if mode_list[idx] != '0':
mode_list[idx] = str(int(mode_list[idx]) | 1)
dir_mode = ''.join(mode_list)
makedirs_(name, user=user, group=group, mode=dir_mode)
_set_mode_and_make_dirs(name, dir_mode, mode, user, group)
else:
__clean_tmp(sfn)
# No changes actually made
ret['changes'].pop('diff', None)
return _error(ret, 'Parent directory not present')
else:
if not os.path.isdir(os.path.dirname(name)):
else: # source != True
if not os.path.isdir(contain_dir):
if makedirs:
# check for existence of windows drive letter
if salt.utils.is_windows():
drive, _ = os.path.splitdrive(name)
if drive and not os.path.exists(drive):
__clean_tmp(sfn)
return _error(ret,
'{0} drive not present'.format(drive))
makedirs_(name, user=user, group=group,
mode=dir_mode or mode)
_set_mode_and_make_dirs(name, dir_mode, mode, user, group)
else:
__clean_tmp(sfn)
# No changes actually made

View File

@ -88,9 +88,9 @@ def commit():
conn.cu.commit(confirm=True)
ret['out'] = True
ret['message'] = 'Commit Successful.'
except Exception as e:
except Exception as exception:
ret['out'] = False
ret['message'] = 'Pre-commit check succeeded but actual commit failed with "{0}"'.format(e.message)
ret['message'] = 'Pre-commit check succeeded but actual commit failed with "{0}"'.format(exception)
else:
ret['out'] = False
ret['message'] = 'Pre-commit check failed.'

View File

@ -50,7 +50,10 @@ def _get_proc_cmdline(proc):
It's backward compatible with < 2.0 versions of psutil.
'''
return proc.cmdline() if PSUTIL2 else proc.cmdline
try:
return proc.cmdline() if PSUTIL2 else proc.cmdline
except (psutil.NoSuchProcess, psutil.AccessDenied):
return ''
def _get_proc_create_time(proc):
@ -59,7 +62,10 @@ def _get_proc_create_time(proc):
It's backward compatible with < 2.0 versions of psutil.
'''
return proc.create_time() if PSUTIL2 else proc.create_time
try:
return proc.create_time() if PSUTIL2 else proc.create_time
except (psutil.NoSuchProcess, psutil.AccessDenied):
return None
def _get_proc_name(proc):
@ -68,12 +74,10 @@ def _get_proc_name(proc):
It's backward compatible with < 2.0 versions of psutil.
'''
ret = []
try:
ret = proc.name() if PSUTIL2 else proc.name
return proc.name() if PSUTIL2 else proc.name
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass
return ret
return []
def _get_proc_status(proc):
@ -82,7 +86,10 @@ def _get_proc_status(proc):
It's backward compatible with < 2.0 versions of psutil.
'''
return proc.status() if PSUTIL2 else proc.status
try:
return proc.status() if PSUTIL2 else proc.status
except (psutil.NoSuchProcess, psutil.AccessDenied):
return None
def _get_proc_username(proc):
@ -91,7 +98,10 @@ def _get_proc_username(proc):
It's backward compatible with < 2.0 versions of psutil.
'''
return proc.username() if PSUTIL2 else proc.username
try:
return proc.username() if PSUTIL2 else proc.username
except (psutil.NoSuchProcess, psutil.AccessDenied):
return None
def _get_proc_pid(proc):
@ -141,7 +151,7 @@ def top(num_processes=5, interval=3):
if num_processes and idx >= num_processes:
break
if len(_get_proc_cmdline(process)) == 0:
cmdline = [_get_proc_name(process)]
cmdline = _get_proc_name(process)
else:
cmdline = _get_proc_cmdline(process)
info = {'cmd': cmdline,

View File

@ -52,7 +52,7 @@ from salt.modules.file import (check_hash, # pylint: disable=W0611
directory_exists, get_managed, mkdir, makedirs_, makedirs_perms,
check_managed, check_managed_changes, check_perms, remove, source_list,
touch, append, contains, contains_regex, contains_regex_multiline,
contains_glob, find, psed, get_sum, _get_bkroot,
contains_glob, find, psed, get_sum, _get_bkroot, _mkstemp_copy,
get_hash, manage_file, file_exists, get_diff, list_backups,
__clean_tmp, check_file_meta, _binary_replace, restore_backup,
access, copy, readdir, rmdir, truncate, replace, delete_backup,
@ -84,8 +84,8 @@ def __virtual__():
global access, copy, readdir, rmdir, truncate, replace, search
global _binary_replace, _get_bkroot, list_backups, restore_backup
global blockreplace, prepend, seek_read, seek_write, rename, lstat
global path_exists_glob
global write, pardir, join
global path_exists_glob, _mkstemp_copy
replace = _namespaced_function(replace, globals())
search = _namespaced_function(search, globals())
@ -138,6 +138,7 @@ def __virtual__():
write = _namespaced_function(write, globals())
pardir = _namespaced_function(pardir, globals())
join = _namespaced_function(join, globals())
_mkstemp_copy = _namespaced_function(_mkstemp_copy, globals())
return __virtualname__
return False

View File

@ -2448,10 +2448,21 @@ def get_encodings():
return a list of string encodings to try
'''
encodings = []
loc = locale.getdefaultlocale()[-1]
if loc:
encodings.append(loc)
encodings.append(sys.getdefaultencoding())
try:
loc_enc = locale.getdefaultlocale()[-1]
except (ValueError, IndexError): # system locale is nonstandard or malformed
loc_enc = None
if loc_enc:
encodings.append(loc_enc)
try:
enc = sys.getdefaultencoding()
except ValueError: # system encoding is nonstandard or malformed
enc = None
if enc:
encodings.append(enc)
encodings.extend(['utf-8', 'latin-1'])
return encodings

View File

@ -0,0 +1,78 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.modules import rest_package
# Globals
rest_package.__opts__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class RestPkgTestCase(TestCase):
'''
Test cases for salt.modules.rest_package
'''
def test_list_pkgs(self):
'''
Test for list pkgs
'''
with patch.dict(rest_package.__opts__, {'proxyobject': MagicMock()}):
self.assertTrue(rest_package.list_pkgs())
def test_install(self):
'''
Test for install
'''
with patch.dict(rest_package.__opts__, {'proxyobject': MagicMock()}):
self.assertTrue(rest_package.install())
def test_remove(self):
'''
Test for remove
'''
with patch.dict(rest_package.__opts__, {'proxyobject': MagicMock()}):
self.assertTrue(rest_package.remove())
def test_version(self):
'''
Test to return a string representing the package version or
an empty string if not installed.
'''
with patch.dict(rest_package.__opts__, {'proxyobject': MagicMock()}):
self.assertTrue(rest_package.version('A'))
def test_installed(self):
'''
Test for installed
'''
with patch.dict(rest_package.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_package.__opts__['proxyobject'],
'package_status',
MagicMock(return_value={'ret': 'ret'})):
self.assertEqual(rest_package.installed('name'), 'ret')
self.assertTrue(rest_package.installed('name'))
self.assertFalse(rest_package.installed('name', version='v'))
if __name__ == '__main__':
from integration import run_tests
run_tests(RestPkgTestCase, needs_daemon=False)

View File

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
from salt.modules import rest_service
# Globals
rest_service.__opts__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class RestSvcTestCase(TestCase):
'''
Test cases for salt.modules.rest_service
'''
def test_start(self):
'''
Test to start the specified service
'''
with patch.dict(rest_service.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_service.__opts__['proxyobject'],
'service_start', MagicMock(return_value=True)):
self.assertTrue(rest_service.start('name'))
def test_stop(self):
'''
Test to stop the specified service
'''
with patch.dict(rest_service.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_service.__opts__['proxyobject'],
'service_stop', MagicMock(return_value=True)):
self.assertTrue(rest_service.stop('name'))
def test_restart(self):
'''
Test to restart the named service
'''
with patch.dict(rest_service.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_service.__opts__['proxyobject'],
'service_restart', MagicMock(return_value=True)):
self.assertTrue(rest_service.restart('name'))
def test_status(self):
'''
Test to return the status for a service, returns a bool whether
the service is running.
'''
with patch.dict(rest_service.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_service.__opts__['proxyobject'],
'service_status', MagicMock(return_value=True)):
self.assertTrue(rest_service.status('name'))
def test_list_(self):
'''
Test for list services.
'''
with patch.dict(rest_service.__opts__, {'proxyobject': MagicMock()}):
with patch.object(rest_service.__opts__['proxyobject'],
'service_list_', MagicMock(return_value=True)):
self.assertTrue(rest_service.list_())
if __name__ == '__main__':
from integration import run_tests
run_tests(RestSvcTestCase, needs_daemon=False)