Manual merge of #24414 per thatch45

This commit is contained in:
Michael Steed 2015-06-06 07:47:52 -06:00
parent dccb83e1ca
commit 83a96367a4
34 changed files with 318 additions and 138 deletions

View file

@ -1215,6 +1215,6 @@ def ssh_version():
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
try:
return ret[1].split(',')[0].split('_')[1]
return ret[1].split(b',')[0].split(b'_')[1]
except IndexError:
return '2.0'

View file

@ -68,6 +68,7 @@ import os.path
import subprocess
# Import salt libs
import salt.utils
import salt.utils.cloud
import salt.utils.xmlutil
from salt.exceptions import SaltCloudSystemExit
@ -3252,7 +3253,8 @@ def add_host(kwargs=None, call=None):
p1 = subprocess.Popen(('echo', '-n'), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(('openssl', 's_client', '-connect', '{0}:443'.format(host_name)), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p3 = subprocess.Popen(('openssl', 'x509', '-noout', '-fingerprint', '-sha1'), stdin=p2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ssl_thumbprint = p3.stdout.read().split('=')[-1].strip()
out = salt.utils.to_str(p3.stdout.read())
ssl_thumbprint = out.split('=')[-1].strip()
log.debug('SSL thumbprint received from the host system: {0}'.format(ssl_thumbprint))
spec.sslThumbprint = ssl_thumbprint
except Exception as exc:

View file

@ -2346,8 +2346,9 @@ def get_id(opts, cache_minion_id=False):
try:
with salt.utils.fopen(id_cache) as idf:
name = idf.readline().strip()
if name.startswith(codecs.BOM): # Remove BOM if exists
name = name.replace(codecs.BOM, '', 1)
bname = salt.utils.to_bytes(name)
if bname.startswith(codecs.BOM): # Remove BOM if exists
name = salt.utils.to_str(bname.replace(codecs.BOM, '', 1))
if name:
log.debug('Using cached minion ID from {0}: {1}'.format(id_cache, name))
return name, False

View file

@ -56,7 +56,7 @@ def jobber_check(self):
rms.append(jid)
data = self.shells.value[jid]
stdout, stderr = data['proc'].communicate()
ret = json.loads(stdout, object_hook=salt.utils.decode_dict)['local']
ret = json.loads(salt.utils.to_str(stdout), object_hook=salt.utils.decode_dict)['local']
route = {'src': (self.stack.value.local.name, 'manor', 'jid_ret'),
'dst': (data['msg']['route']['src'][0], None, 'remote_cmd')}
ret['cmd'] = '_return'

View file

@ -199,7 +199,7 @@ def diff_mtime_map(map1, map2):
Is there a change to the mtime map? return a boolean
'''
# check if the mtimes are the same
if cmp(sorted(map1), sorted(map2)) != 0:
if sorted(map1) != sorted(map2):
#log.debug('diff_mtime_map: the maps are different')
return True

View file

@ -548,8 +548,8 @@ def _clean_stale(repo, local_refs=None):
# Rename heads to match the ref names from
# pygit2.Repository.listall_references()
remote_refs.append(
line.split()[-1].replace('refs/heads/',
'refs/remotes/origin/')
line.split()[-1].replace(b'refs/heads/',
b'refs/remotes/origin/')
)
except IndexError:
continue

View file

@ -434,9 +434,9 @@ def _run(cmd,
if rstrip:
if out is not None:
out = out.rstrip()
out = salt.utils.to_str(out).rstrip()
if err is not None:
err = err.rstrip()
err = salt.utils.to_str(err).rstrip()
ret['pid'] = proc.process.pid
ret['retcode'] = proc.process.returncode
ret['stdout'] = out

View file

@ -10,7 +10,6 @@ import random
# Import salt libs
import salt.utils
import salt.ext.six as six
from salt.ext.six.moves import range
@ -27,10 +26,11 @@ def __virtual__():
def _encode(string):
if isinstance(string, six.text_type):
string = string.encode('utf-8')
elif not string:
string = ''
try:
string = salt.utils.to_str(string)
except TypeError:
if not string:
string = ''
return "{0}".format(string)

View file

@ -122,7 +122,7 @@ def show_config(jail):
ret = {}
if subprocess.call(["jls", "-nq", "-j", jail]) == 0:
jls = subprocess.check_output(["jls", "-nq", "-j", jail]) # pylint: disable=minimum-python-version
jailopts = shlex.split(jls)
jailopts = shlex.split(salt.utils.to_str(jls))
for jailopt in jailopts:
if '=' not in jailopt:
ret[jailopt.strip().rstrip(";")] = '1'

View file

@ -3,10 +3,16 @@
A collection of hashing and encoding functions
'''
from __future__ import absolute_import
# Import python libs
import base64
import hashlib
import hmac
# Import third-party libs
import salt.utils
import salt.ext.six as six
def base64_encodestring(instr):
'''
@ -20,6 +26,10 @@ def base64_encodestring(instr):
salt '*' hashutil.base64_encodestring 'get salted'
'''
if six.PY3:
b = salt.utils.to_bytes(instr)
b64 = base64.encodebytes(b)
return salt.utils.to_str(b64)
return base64.encodestring(instr)
@ -35,6 +45,13 @@ def base64_decodestring(instr):
salt '*' hashutil.base64_decodestring 'Z2V0IHNhbHRlZA==\\n'
'''
if six.PY3:
b = salt.utils.to_bytes(instr)
data = base64.decodebytes(b)
try:
return salt.utils.to_str(data)
except UnicodeDecodeError:
return data
return base64.decodestring(instr)
@ -50,6 +67,9 @@ def md5_digest(instr):
salt '*' hashutil.md5_digest 'get salted'
'''
if six.PY3:
b = salt.utils.to_bytes(instr)
return hashlib.md5(b).hexdigest()
return hashlib.md5(instr).hexdigest()
@ -65,6 +85,9 @@ def sha256_digest(instr):
salt '*' hashutil.sha256_digest 'get salted'
'''
if six.PY3:
b = salt.utils.to_bytes(instr)
return hashlib.sha256(b).hexdigest()
return hashlib.sha256(instr).hexdigest()
@ -80,6 +103,9 @@ def sha512_digest(instr):
salt '*' hashutil.sha512_digest 'get salted'
'''
if six.PY3:
b = salt.utils.to_bytes(instr)
return hashlib.sha512(b).hexdigest()
return hashlib.sha512(instr).hexdigest()
@ -95,8 +121,16 @@ def hmac_signature(string, shared_secret, challenge_hmac):
.. code-block:: bash
salt '*' hashutil.hmac_signature 'get salted' 'shared secret' 'NS2BvKxFRk+rndAlFbCYIFNVkPtI/3KiIYQw4okNKU8='
salt '*' hashutil.hmac_signature 'get salted' 'shared secret' 'eBWf9bstXg+NiP5AOwppB5HMvZiYMPzEM9W5YMm/AmQ='
'''
hmac_hash = hmac.new(string, shared_secret, hashlib.sha256)
if six.PY3:
msg = salt.utils.to_bytes(string)
key = salt.utils.to_bytes(shared_secret)
challenge = salt.utils.to_bytes(challenge_hmac)
else:
msg = string
key = shared_secret
challenge = challenge_hmac
hmac_hash = hmac.new(key, msg, hashlib.sha256)
valid_hmac = base64.b64encode(hmac_hash.digest())
return valid_hmac == challenge_hmac
return valid_hmac == challenge

View file

@ -24,6 +24,7 @@ from subprocess import Popen, PIPE, STDOUT
# Import Salt Libs
from salt.modules.inspectlib.dbhandle import DBHandle
from salt.modules.inspectlib.exceptions import (InspectorSnapshotException)
import salt.utils
from salt.utils import fsutils
from salt.utils import reinit_crypto
@ -73,6 +74,7 @@ class Inspector(object):
pkg_name = None
pkg_configs = []
out = salt.utils.to_str(out)
for line in out.split(os.linesep):
line = line.strip()
if not line:
@ -99,6 +101,7 @@ class Inspector(object):
cfgs = list()
out, err = self._syscall("rpm", None, None, '-V', '--nodeps', '--nodigest',
'--nosignature', '--nomtime', '--nolinkto', pkg_name)
out = salt.utils.to_str(out)
for line in out.split(os.linesep):
line = line.strip()
if not line or line.find(" c ") < 0 or line.split(" ")[0].find("5") < 0:

View file

@ -66,7 +66,10 @@ def _available_services():
# the system provided plutil program to do the conversion
cmd = '/usr/bin/plutil -convert xml1 -o - -- "{0}"'.format(true_path)
plist_xml = __salt__['cmd.run_all'](cmd, python_shell=False)['stdout']
plist = plistlib.readPlistFromString(plist_xml)
if six.PY2:
plist = plistlib.readPlistFromString(plist_xml)
else:
plist = plistlib.readPlistFromBytes(salt.utils.to_bytes(plist_xml))
available_services[plist.Label.lower()] = {
'filename': filename,

View file

@ -14,6 +14,7 @@ from __future__ import absolute_import
import plistlib
import subprocess
import salt.utils
from salt.ext import six
PROFILER_BINARY = '/usr/sbin/system_profiler'
@ -41,7 +42,10 @@ def _call_system_profiler(datatype):
'-xml', datatype], stdout=subprocess.PIPE)
(sysprofresults, sysprof_stderr) = p.communicate(input=None)
plist = plistlib.readPlistFromString(sysprofresults)
if six.PY2:
plist = plistlib.readPlistFromString(sysprofresults)
else:
plist = plistlib.readPlistFromBytes(sysprofresults)
try:
apps = plist[0]['_items']

View file

@ -33,7 +33,7 @@ def _subprocess(cmd):
try:
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
ret = proc.communicate()[0].strip()
ret = utils.to_str(proc.communicate()[0]).strip()
retcode = proc.wait()
if ret:

View file

@ -173,15 +173,17 @@ def _libvirt_creds():
g_cmd = 'grep ^\\s*group /etc/libvirt/qemu.conf'
u_cmd = 'grep ^\\s*user /etc/libvirt/qemu.conf'
try:
group = subprocess.Popen(g_cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0].split('"')[1]
stdout = subprocess.Popen(g_cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
group = salt.utils.to_str(stdout).split('"')[1]
except IndexError:
group = 'root'
try:
user = subprocess.Popen(u_cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0].split('"')[1]
stdout = subprocess.Popen(u_cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
user = salt.utils.to_str(stdout).split('"')[1]
except IndexError:
user = 'root'
return {'user': user, 'group': group}
@ -908,10 +910,11 @@ def get_disks(vm_):
break
output = []
qemu_output = subprocess.Popen(['qemu-img', 'info',
disks[dev]['file']],
shell=False,
stdout=subprocess.PIPE).communicate()[0]
stdout = subprocess.Popen(
['qemu-img', 'info', disks[dev]['file']],
shell=False,
stdout=subprocess.PIPE).communicate()[0]
qemu_output = salt.utils.to_str(stdout)
snapshots = False
columns = None
lines = qemu_output.strip().split('\n')
@ -1359,9 +1362,10 @@ def migrate_non_shared(vm_, target, ssh=False):
cmd = _get_migrate_command() + ' --copy-storage-all ' + vm_\
+ _get_target(target, ssh)
return subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
stdout = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
return salt.utils.to_str(stdout)
def migrate_non_shared_inc(vm_, target, ssh=False):
@ -1377,9 +1381,10 @@ def migrate_non_shared_inc(vm_, target, ssh=False):
cmd = _get_migrate_command() + ' --copy-storage-inc ' + vm_\
+ _get_target(target, ssh)
return subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
stdout = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
return salt.utils.to_str(stdout)
def migrate(vm_, target, ssh=False):
@ -1395,9 +1400,10 @@ def migrate(vm_, target, ssh=False):
cmd = _get_migrate_command() + ' ' + vm_\
+ _get_target(target, ssh)
return subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
stdout = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE).communicate()[0]
return salt.utils.to_str(stdout)
def seed_non_shared_migrate(disks, force=False):

View file

@ -344,7 +344,7 @@ def master(master=None, connected=True):
log.error('Failed netstat')
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue

View file

@ -97,6 +97,7 @@ import sys
import salt.exceptions
import salt.ext.six as six
import salt.utils
HAS_VIRTUALENV = False
@ -177,14 +178,14 @@ def ext_pillar(minion_id, # pylint: disable=W0613
base_env = {}
proc = subprocess.Popen(['bash', '-c', 'env'], stdout=subprocess.PIPE)
for line in proc.stdout:
(key, _, value) = line.partition('=')
(key, _, value) = salt.utils.to_str(line).partition('=')
base_env[key] = value
command = ['bash', '-c', 'source {0} && env'.format(env_file)]
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in proc.stdout:
(key, _, value) = line.partition('=')
(key, _, value) = salt.utils.to_str(line).partition('=')
# only add a key if it is different or doesn't already exist
if key not in base_env or base_env[key] != value:
os.environ[key] = value.rstrip('\n')

View file

@ -249,7 +249,7 @@ class Script(Target):
self.tgt = tgt
self.tgt_type = tgt_type
inventory, error = subprocess.Popen([inventory_file], shell=True, stdout=subprocess.PIPE).communicate()
self.inventory = json.loads(inventory)
self.inventory = json.loads(salt.utils.to_str(inventory))
self.meta = self.inventory.get('_meta', {})
self.groups = dict()
self.hostvars = dict()

View file

@ -4,7 +4,7 @@ Some of the utils used by salt
'''
# Import python libs
from __future__ import absolute_import, print_function
from __future__ import absolute_import, division, print_function
import contextlib
import copy
import collections
@ -34,7 +34,7 @@ import string
import subprocess
# Import 3rd-party libs
import salt.ext.six as six
from salt.ext import six
# pylint: disable=import-error
from salt.ext.six.moves.urllib.parse import urlparse # pylint: disable=no-name-in-module
# pylint: disable=redefined-builtin
@ -267,7 +267,7 @@ def get_context(template, line, num_lines=5, marker=None):
# warning: jinja content may contain unicode strings
# instead of utf-8.
buf = [i.encode('UTF-8') if isinstance(i, six.text_type) else i for i in buf]
buf = [to_str(i) if isinstance(i, six.text_type) else i for i in buf]
return '---\n{0}\n---'.format('\n'.join(buf))
@ -593,8 +593,8 @@ def output_profile(pr, stats_path='/tmp/stats', stop=False, id_=None):
else:
log.info('PROFILING (dot): {0} generated'.format(fico))
log.trace('pyprof2calltree output:')
log.trace(pro.stdout.read().strip() +
pro.stderr.read().strip())
log.trace(to_str(pro.stdout.read()).strip() +
to_str(pro.stderr.read()).strip())
else:
log.info('You can run {0} for additional stats.'.format(cmd))
finally:
@ -1030,8 +1030,7 @@ def istextfile(fp_, blocksize=512):
If more than 30% of the chars in the block are non-text, or there
are NUL ('\x00') bytes in the block, assume this is a binary file.
'''
PY3 = sys.version_info[0] == 3 # pylint: disable=C0103
int2byte = (lambda x: bytes((x,))) if PY3 else chr
int2byte = (lambda x: bytes((x,))) if six.PY3 else chr
text_characters = (
b''.join(int2byte(i) for i in range(32, 127)) +
b'\n\r\t\f\b')
@ -1498,7 +1497,7 @@ def sanitize_win_path_string(winpath):
'''
intab = '<>:|?*'
outtab = '_' * len(intab)
trantab = string.maketrans(intab, outtab)
trantab = ''.maketrans(intab, outtab) if six.PY3 else string.maketrans(intab, outtab)
if isinstance(winpath, str):
winpath = winpath.translate(trantab)
elif isinstance(winpath, six.text_type):
@ -1936,7 +1935,8 @@ def date_cast(date):
if isinstance(date, six.string_types):
try:
if HAS_TIMELIB:
return timelib.strtodatetime(date)
# py3: yes, timelib.strtodatetime wants bytes, not str :/
return timelib.strtodatetime(to_bytes(date))
except ValueError:
pass
@ -2342,15 +2342,19 @@ def is_bin_str(data):
if not data:
return False
text_characters = ''.join(list(map(chr, list(range(32, 127)))) + list('\n\r\t\b'))
_null_trans = string.maketrans('', '')
# Get the non-text characters (maps a character to itself then
# use the 'remove' option to get rid of the text characters.)
text = data.translate(_null_trans, text_characters)
text_characters = ''.join([chr(x) for x in range(32, 127)] + list('\n\r\t\b'))
# Get the non-text characters (map each character to itself then use the
# 'remove' option to get rid of the text characters.)
if six.PY3:
trans = ''.maketrans('', '', text_characters)
nontext = data.translate(trans)
else:
trans = string.maketrans('', '')
nontext = data.translate(trans, text_characters)
# If more than 30% non-text characters, then
# this is considered a binary file
if len(text) / len(data) > 0.30:
if len(nontext) / len(data) > 0.30:
return True
return False
@ -2646,3 +2650,50 @@ def human_size_to_bytes(human_size):
size_num = int(match.group(1))
unit_multiplier = 1024 ** size_exp_map.get(match.group(2), 0)
return size_num * unit_multiplier
def to_str(s, encoding=None):
'''
Given str, bytes, bytearray, or unicode (py2), return str
'''
if isinstance(s, str):
return s
if six.PY3:
if isinstance(s, (bytes, bytearray)):
return s.decode(encoding or __salt_system_encoding__)
raise TypeError('expected str, bytes, or bytearray')
else:
if isinstance(s, bytearray):
return str(s)
if isinstance(s, unicode): # pylint: disable=incompatible-py3-code
return s.encode(encoding or __salt_system_encoding__)
raise TypeError('expected str, bytearray, or unicode')
def to_bytes(s, encoding=None):
'''
Given bytes, bytearray, str, or unicode (python 2), return bytes (str for
python 2)
'''
if six.PY3:
if isinstance(s, bytes):
return s
if isinstance(s, bytearray):
return bytes(s)
if isinstance(s, str):
return s.encode(encoding or __salt_system_encoding__)
raise TypeError('expected bytes, bytearray, or str')
else:
return to_str(s, encoding)
def to_unicode(s, encoding=None):
'''
Given str or unicode, return unicode (str for python 3)
'''
if six.PY3:
return to_str(s, encoding)
else:
if isinstance(s, str):
return s.decode(encoding or __salt_system_encoding__)
return unicode(s) # pylint: disable=incompatible-py3-code

View file

@ -210,7 +210,7 @@ class SaltEvent(object):
hash_type = getattr(hashlib, self.opts.get('hash_type', 'md5'))
# Only use the first 10 chars to keep longer hashes from exceeding the
# max socket path length.
id_hash = hash_type(self.opts.get('id', '')).hexdigest()[:10]
id_hash = hash_type(salt.utils.to_bytes(self.opts.get('id', ''))).hexdigest()[:10]
if node == 'master':
if self.opts.get('ipc_mode', '') == 'tcp':
puburi = 'tcp://127.0.0.1:{0}'.format(
@ -277,7 +277,7 @@ class SaltEvent(object):
self.sub = self.context.socket(zmq.SUB)
self.sub.connect(self.puburi)
self.poller.register(self.sub, zmq.POLLIN)
self.sub.setsockopt(zmq.SUBSCRIBE, '')
self.sub.setsockopt_string(zmq.SUBSCRIBE, u'')
self.sub.setsockopt(zmq.LINGER, 5000)
self.cpub = True
@ -661,7 +661,7 @@ class AsyncEventPublisher(object):
hash_type = getattr(hashlib, self.opts.get('hash_type', 'md5'))
# Only use the first 10 chars to keep longer hashes from exceeding the
# max socket path length.
id_hash = hash_type(self.opts.get('id', '')).hexdigest()[:10]
id_hash = hash_type(salt.utils.to_bytes(self.opts.get('id', ''))).hexdigest()[:10]
epub_sock_path = os.path.join(
self.opts['sock_dir'],
'minion_event_{0}_pub.ipc'.format(id_hash)

View file

@ -568,8 +568,8 @@ class ExecOption(Option):
log.error(
'Error running command: {0}\n\n{1}'.format(
command,
err))
return "{0}:\n{1}\n".format(command, out)
salt.utils.to_str(err)))
return "{0}:\n{1}\n".format(command, salt.utils.to_str(out))
except Exception as e:
log.error(

View file

@ -8,7 +8,7 @@ import hashlib
import os
import salt.utils
from salt.ext.six import string_types
from salt.ext import six
def gen_jid():
@ -22,7 +22,7 @@ def is_jid(jid):
'''
Returns True if the passed in value is a job id
'''
if not isinstance(jid, string_types):
if not isinstance(jid, six.string_types):
return False
if len(jid) != 20:
return False
@ -43,7 +43,7 @@ def jid_dir(jid, cachedir, sum_type):
'returner, this util function will be removed-- please use '
'the returner'
)
jid = str(jid)
jid = salt.utils.to_bytes(str(jid)) if six.PY3 else str(jid)
jhash = getattr(hashlib, sum_type)(jid).hexdigest()
return os.path.join(cachedir, 'jobs', jhash[:2], jhash[2:])

View file

@ -639,14 +639,16 @@ def linux_interfaces():
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
ifaces = _interfaces_ip("{0}\n{1}".format(cmd1, cmd2))
ifaces = _interfaces_ip("{0}\n{1}".format(
salt.utils.to_str(cmd1),
salt.utils.to_str(cmd2)))
elif ifconfig_path:
cmd = subprocess.Popen(
'{0} -a'.format(ifconfig_path),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
ifaces = _interfaces_ifconfig(cmd)
ifaces = _interfaces_ifconfig(salt.utils.to_str(cmd))
return ifaces
@ -1078,7 +1080,7 @@ def _sunos_remotes_on(port, which_end):
log.error('Failed netstat')
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue
@ -1124,7 +1126,7 @@ def _freebsd_remotes_on(port, which_end):
log.error('Failed "sockstat" with returncode = {0}'.format(ex.returncode))
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
chunks = line.split()
@ -1211,7 +1213,7 @@ def _windows_remotes_on(port, which_end):
log.error('Failed netstat')
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
if 'ESTABLISHED' not in line:
continue
@ -1259,7 +1261,7 @@ def remotes_on_local_tcp_port(port):
log.error('Failed "lsof" with returncode = {0}'.format(ex.returncode))
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
chunks = line.split()
if not chunks:
@ -1314,7 +1316,7 @@ def remotes_on_remote_tcp_port(port):
log.error('Failed "lsof" with returncode = {0}'.format(ex.returncode))
raise
lines = data.split('\n')
lines = salt.utils.to_str(data).split('\n')
for line in lines:
chunks = line.split()
if not chunks:

View file

@ -10,6 +10,7 @@ import subprocess
# Import Salt libs
from salt.exceptions import SaltInvocationError
import salt.utils
log = logging.getLogger(__name__)
@ -46,12 +47,13 @@ def version(context=None):
['systemctl', '--version'],
close_fds=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]
outstr = salt.utils.to_str(stdout)
try:
ret = int(stdout.splitlines()[0].split()[-1])
ret = int(outstr.splitlines()[0].split()[-1])
except (IndexError, ValueError):
log.error(
'Unable to determine systemd version from systemctl '
'--version, output follows:\n{0}'.format(stdout)
'--version, output follows:\n{0}'.format(outstr)
)
return None
else:

View file

@ -46,7 +46,7 @@ else:
import resource
# Import salt libs
from salt.utils import reinit_crypto
import salt.utils
from salt.ext.six import string_types
from salt.log.setup import LOG_LEVELS
@ -487,7 +487,7 @@ class Terminal(object):
# Close parent FDs
os.close(stdout_parent_fd)
os.close(stderr_parent_fd)
reinit_crypto()
salt.utils.reinit_crypto()
# ----- Make STDOUT the controlling PTY --------------------->
child_name = os.ttyname(stdout_child_fd)
@ -548,7 +548,7 @@ class Terminal(object):
# <---- Duplicate Descriptors --------------------------------
else:
# Parent. Close Child PTY's
reinit_crypto()
salt.utils.reinit_crypto()
os.close(stdout_child_fd)
os.close(stderr_child_fd)
@ -635,7 +635,9 @@ class Terminal(object):
if self.child_fde in rlist:
try:
stderr = self._translate_newlines(
os.read(self.child_fde, maxsize)
salt.utils.to_str(
os.read(self.child_fde, maxsize)
)
)
if not stderr:
@ -666,7 +668,9 @@ class Terminal(object):
if self.child_fd in rlist:
try:
stdout = self._translate_newlines(
os.read(self.child_fd, maxsize)
salt.utils.to_str(
os.read(self.child_fd, maxsize)
)
)
if not stdout:

View file

@ -327,7 +327,7 @@ class TestDaemon(object):
)
_, keygen_err = keygen_process.communicate()
if keygen_err:
print('ssh-keygen had errors: {0}'.format(keygen_err))
print('ssh-keygen had errors: {0}'.format(salt.utils.to_str(keygen_err)))
sshd_config_path = os.path.join(FILES, 'conf/_ssh/sshd_config')
shutil.copy(sshd_config_path, TMP_CONF_DIR)
auth_key_file = os.path.join(TMP_CONF_DIR, 'key_test.pub')
@ -370,7 +370,7 @@ class TestDaemon(object):
)
_, keygen_dsa_err = keygen_process_dsa.communicate()
if keygen_dsa_err:
print('ssh-keygen had errors: {0}'.format(keygen_dsa_err))
print('ssh-keygen had errors: {0}'.format(salt.utils.to_str(keygen_dsa_err)))
keygen_process_ecdsa = subprocess.Popen(
[keygen, '-t',
@ -390,7 +390,7 @@ class TestDaemon(object):
)
_, keygen_escda_err = keygen_process_ecdsa.communicate()
if keygen_escda_err:
print('ssh-keygen had errors: {0}'.format(keygen_escda_err))
print('ssh-keygen had errors: {0}'.format(salt.utils.to_str(keygen_escda_err)))
keygen_process_ed25519 = subprocess.Popen(
[keygen, '-t',
@ -410,7 +410,7 @@ class TestDaemon(object):
)
_, keygen_ed25519_err = keygen_process_ed25519.communicate()
if keygen_ed25519_err:
print('ssh-keygen had errors: {0}'.format(keygen_ed25519_err))
print('ssh-keygen had errors: {0}'.format(salt.utils.to_str(keygen_ed25519_err)))
with salt.utils.fopen(os.path.join(TMP_CONF_DIR, 'sshd_config'), 'a') as ssh_config:
ssh_config.write('AuthorizedKeysFile {0}\n'.format(auth_key_file))
@ -431,7 +431,7 @@ class TestDaemon(object):
)
_, sshd_err = self.sshd_process.communicate()
if sshd_err:
print('sshd had errors on startup: {0}'.format(sshd_err))
print('sshd had errors on startup: {0}'.format(salt.utils.to_str(sshd_err)))
else:
os.environ['SSH_DAEMON_RUNNING'] = 'True'
roster_path = os.path.join(FILES, 'conf/_ssh/roster')
@ -1044,7 +1044,7 @@ class AdaptedConfigurationTestCaseMixIn(object):
class SaltClientTestCaseMixIn(AdaptedConfigurationTestCaseMixIn):
_salt_client_config_file_name_ = 'master'
__slots__ = ('client', '_salt_client_config_file_name_')
__slots__ = ()
@property
def client(self):
@ -1296,7 +1296,7 @@ class ShellCaseCommonTestsMixIn(CheckShellBinaryNameAndVersionMixIn):
self.skipTest(
'Failed to get the output of \'git describe\'. '
'Error: {0!r}'.format(
err
salt.utils.to_str(err)
)
)

View file

@ -71,6 +71,7 @@ class SysModuleTest(integration.ModuleCase):
'container_resource.run',
'nspawn.stop',
'nspawn.restart',
'lowpkg.bin_pkg_info',
)
for fun in docs:

View file

@ -20,6 +20,7 @@ import optparse
import subprocess
# Import Salt libs
import salt.utils
try:
from salt.utils.nb_popen import NonBlockingPopen
except ImportError:
@ -454,7 +455,8 @@ def run(opts):
delete_vm(opts)
sys.exit(retcode)
if not stdout.strip():
outstr = salt.utils.to_str(stdout).strip()
if not outstr:
print('Failed to get the bootstrapped minion version(no output). Exit code: {0}'.format(retcode))
sys.stdout.flush()
if opts.clean and 'JENKINS_SALTCLOUD_VM_NAME' not in os.environ:
@ -462,7 +464,7 @@ def run(opts):
sys.exit(retcode)
try:
version_info = json.loads(stdout.strip())
version_info = json.loads(outstr)
bootstrap_minion_version = os.environ.get(
'SALT_MINION_BOOTSTRAP_RELEASE',
opts.bootstrap_salt_commit[:7]
@ -480,7 +482,7 @@ def run(opts):
else:
print('matches!')
except ValueError:
print('Failed to load any JSON from {0!r}'.format(stdout.strip()))
print('Failed to load any JSON from {0!r}'.format(outstr))
if opts.cloud_only:
# Run Cloud Provider tests preparation SLS
@ -516,11 +518,10 @@ def run(opts):
stdout, stderr = proc.communicate()
if stdout:
print(stdout)
sys.stdout.flush()
print(salt.utils.to_str(stdout))
if stderr:
print(stderr)
sys.stderr.flush()
print(salt.utils.to_str(stderr))
sys.stdout.flush()
retcode = proc.returncode
if retcode != 0:
@ -555,10 +556,9 @@ def run(opts):
if stdout:
# DO NOT print the state return here!
print('Cloud configuration files provisioned via pillar.')
sys.stdout.flush()
if stderr:
print(stderr)
sys.stderr.flush()
print(salt.utils.to_str(stderr))
sys.stdout.flush()
retcode = proc.returncode
if retcode != 0:
@ -592,11 +592,10 @@ def run(opts):
stdout, stderr = proc.communicate()
if stdout:
print(stdout)
sys.stdout.flush()
print(salt.utils.to_str(stdout))
if stderr:
print(stderr)
sys.stderr.flush()
print(salt.utils.to_str(stderr))
sys.stdout.flush()
retcode = proc.returncode
if retcode != 0:
@ -622,7 +621,7 @@ def run(opts):
stderr=subprocess.PIPE,
)
proc.communicate()
stdout, _ = proc.communicate()
retcode = proc.returncode
if retcode != 0:
@ -650,7 +649,7 @@ def run(opts):
sys.exit(retcode)
print('matches!')
except ValueError:
print('Failed to load any JSON from {0!r}'.format(stdout.strip()))
print('Failed to load any JSON from {0!r}'.format(salt.utils.to_str(stdout).strip()))
if opts.test_git_commit is not None:
time.sleep(1)
@ -696,7 +695,7 @@ def run(opts):
sys.exit(retcode)
print('matches!')
except ValueError:
print('Failed to load any JSON from {0!r}'.format(stdout.strip()))
print('Failed to load any JSON from {0!r}'.format(salt.utils.to_str(stdout).strip()))
# Run tests here
time.sleep(3)
@ -718,15 +717,15 @@ def run(opts):
)
stdout, stderr = proc.communicate()
if stdout:
print(stdout)
sys.stdout.flush()
outstr = salt.utils.to_str(stdout)
if outstr:
print(outstr)
if stderr:
print(stderr)
sys.stderr.flush()
print(salt.utils.to_str(stderr))
sys.stdout.flush()
try:
match = re.search(r'Test Suite Exit Code: (?P<exitcode>[\d]+)', stdout)
match = re.search(r'Test Suite Exit Code: (?P<exitcode>[\d]+)', outstr)
retcode = int(match.group('exitcode'))
except AttributeError:
# No regex matching
@ -737,7 +736,7 @@ def run(opts):
except TypeError:
# No output!?
retcode = 1
if stdout:
if outstr:
# Anything else, raise the exception
raise
@ -762,11 +761,10 @@ def run(opts):
stdout, stderr = proc.communicate()
if stdout:
print(stdout)
sys.stdout.flush()
print(salt.utils.to_str(stdout))
if stderr:
print(stderr)
sys.stderr.flush()
print(salt.utils.to_str(stderr))
sys.stdout.flush()
# Grab packages and log file (or just log file if build failed)
download_packages(opts)

View file

@ -222,4 +222,5 @@ class MasterACLTestCase(integration.ModuleCase):
if __name__ == '__main__':
from integration import run_tests
run_tests(LoadAuthTestCase, needs_daemon=False)
tests = [LoadAuthTestCase, MasterACLTestCase]
run_tests(*tests, needs_daemon=False)

View file

@ -12,7 +12,7 @@
# Import python libs
from __future__ import absolute_import
import logging
from cStringIO import StringIO
from salt.ext.six.moves import StringIO
# Import Salt Testing libs
from salttesting.case import TestCase
@ -94,17 +94,17 @@ class TestLog(TestCase):
try:
raise_exception_on_purpose()
except ZeroDivisionError as exc:
log.error('Exception raised on purpose caught: {0}'.format(exc),
log.error('Exception raised on purpose caught: ZeroDivisionError',
exc_info_on_loglevel=logging.DEBUG)
try:
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero\n',
'Exception raised on purpose caught: ZeroDivisionError',
stream1.getvalue()
)
self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero\n',
'Exception raised on purpose caught: ZeroDivisionError',
stream2.getvalue()
)
self.assertIn('Traceback (most recent call last)', stream2.getvalue())
@ -127,17 +127,17 @@ class TestLog(TestCase):
try:
raise_exception_on_purpose()
except ZeroDivisionError as exc:
log.error('Exception raised on purpose caught: {0}'.format(exc),
log.error('Exception raised on purpose caught: ZeroDivisionError',
exc_info_on_loglevel=logging.INFO)
try:
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero\n',
'Exception raised on purpose caught: ZeroDivisionError',
stream1.getvalue()
)
self.assertIn('Traceback (most recent call last)', stream1.getvalue())
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero\n',
'Exception raised on purpose caught: ZeroDivisionError',
stream2.getvalue()
)
self.assertIn('Traceback (most recent call last)', stream2.getvalue())
@ -160,17 +160,17 @@ class TestLog(TestCase):
try:
raise_exception_on_purpose()
except ZeroDivisionError as exc:
log.error('Exception raised on purpose caught: {0}'.format(exc),
log.error('Exception raised on purpose caught: ZeroDivisionError',
exc_info_on_loglevel=logging.DEBUG)
try:
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero',
'Exception raised on purpose caught: ZeroDivisionError',
stream1.getvalue()
)
self.assertNotIn('Traceback (most recent call last)', stream1.getvalue())
self.assertIn(
'Exception raised on purpose caught: integer division or modulo by zero',
'Exception raised on purpose caught: ZeroDivisionError',
stream2.getvalue()
)
self.assertNotIn('Traceback (most recent call last)', stream2.getvalue())

View file

@ -5,7 +5,6 @@
# Import python libs
from __future__ import absolute_import
from StringIO import StringIO
# Import Salt Testing libs
from salttesting import TestCase, skipIf
@ -16,6 +15,7 @@ ensure_in_syspath('../../')
# Import Salt libs
from salt.modules import cron
from salt.ext.six.moves import StringIO
STUB_USER = 'root'
STUB_PATH = '/tmp'
@ -41,6 +41,7 @@ def get_crontab(*args, **kw):
def set_crontab(val):
CRONTAB.seek(0)
CRONTAB.truncate(0)
CRONTAB.write(val)
@ -475,7 +476,7 @@ class CronTestCase(TestCase):
self.assertEqual(
get_crontab(),
inc_tests[idx], (
"idx {0}\n '{1}'\n != \n'{2}'\n\n\n"
"idx {0}\n'{1}'\n != \n'{2}'\n\n\n"
"{1!r} != {2!r}"
).format(
idx, get_crontab(), inc_tests[idx]))

View file

@ -2,11 +2,14 @@
# Import python libs
from __future__ import absolute_import
import os
# Import salt testing libs
from salttesting.case import ModuleCase
from salttesting.mixins import RUNTIME_VARS
# Import Salt libs
import salt.config
import salt.loader
@ -16,10 +19,11 @@ class HashutilTestCase(ModuleCase):
the_string_md5 = '2aacf29e92feaf528fb738bcf9d647ac'
the_string_sha256 = 'd49859ccbc854fa68d800b5734efc70d72383e6479d545468bc300263164ff33'
the_string_sha512 = 'a8c174a7941c64a068e686812a2fafd7624c840fde800f5965fbeca675f2f6e37061ffe41e17728c919bdea290eab7a21e13c04ae71661955a87f2e0e04bb045'
the_string_hmac = 'NS2BvKxFRk+rndAlFbCYIFNVkPtI/3KiIYQw4okNKU8='
the_string_hmac = 'eBWf9bstXg+NiP5AOwppB5HMvZiYMPzEM9W5YMm/AmQ='
def setUp(self):
self.hashutil = salt.loader.raw_mod(self.minion_opts, 'hashutil', None)
minion_opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
self.hashutil = salt.loader.raw_mod(minion_opts, 'hashutil', None)
def test_base64_encodestring(self):
ret = self.hashutil['hashutil.base64_encodestring'](self.the_string)

View file

@ -216,9 +216,9 @@ class FileTestCase(TestCase):
' SALTshould go')
ret.update({'comment': comt, 'result': False})
self.assertDictEqual(filestate.symlink
(name, target, user=user,
group=group, backupname='SALT',
force=True), ret)
(name, target, user=user,
group=group, backupname='SALT',
force=True), ret)
with patch.object(os.path, 'isfile', mock_t):
comt = ('File exists where the symlink {0} should be'

View file

@ -18,10 +18,10 @@ from salttesting.mock import (
ensure_in_syspath('../../')
# Import Salt libs
from salt.utils.odict import OrderedDict
from salt import utils
from salt.utils import args
from salt.utils.odict import OrderedDict
from salt.exceptions import (SaltInvocationError, SaltSystemExit, CommandNotFoundError)
from salt import utils
# Import Python libraries
import os
@ -32,6 +32,7 @@ from collections import namedtuple
# Import 3rd-party libs
import salt.ext.six as six
from salt.ext.six.moves import range
try:
import timelib # pylint: disable=import-error,unused-import
HAS_TIMELIB = True
@ -324,6 +325,11 @@ class UtilsTestCase(TestCase):
self.assertDictEqual(utils.clean_kwargs(__foo_bar='gwar'), {})
self.assertDictEqual(utils.clean_kwargs(foo_bar='gwar'), {'foo_bar': 'gwar'})
def test_sanitize_win_path_string(self):
p = '\\windows\\system'
self.assertEqual(utils.sanitize_win_path_string('\\windows\\system'), '\\windows\\system')
self.assertEqual(utils.sanitize_win_path_string('\\bo:g|us\\p?at*h>'), '\\bo_g_us\\p_at_h_')
def test_check_state_result(self):
self.assertFalse(utils.check_state_result(None), "Failed to handle None as an invalid data type.")
self.assertFalse(utils.check_state_result([]), "Failed to handle an invalid data type.")
@ -629,7 +635,12 @@ class UtilsTestCase(TestCase):
# To to ensure safe exit if str passed doesn't evaluate to True
self.assertFalse(utils.is_bin_str(''))
# TODO: Test binary detection
nontext = 3 * (''.join([chr(x) for x in range(1, 32) if x not in (8, 9, 10, 12, 13)]))
almost_bin_str = '{0}{1}'.format(LORUM_IPSUM[:100], nontext[:42])
self.assertFalse(utils.is_bin_str(almost_bin_str))
bin_str = almost_bin_str + '\x01'
self.assertTrue(utils.is_bin_str(bin_str))
def test_repack_dict(self):
list_of_one_element_dicts = [{'dict_key_1': 'dict_val_1'},
@ -728,6 +739,57 @@ class UtilsTestCase(TestCase):
# Test invalid version arg
self.assertRaises(RuntimeError, utils.kwargs_warn_until, {}, [])
def test_to_str(self):
for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
self.assertRaises(TypeError, utils.to_str, x)
if six.PY3:
self.assertEqual(utils.to_str('plugh'), 'plugh')
self.assertEqual(utils.to_str('áéíóúý'), 'áéíóúý')
un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string
ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
self.assertEqual(utils.to_str(ut, 'utf-8'), un)
self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), un)
else:
self.assertEqual(utils.to_str('plugh'), 'plugh')
self.assertEqual(utils.to_str(u'áéíóúý'), 'áéíóúý')
un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'
ut = '\xe4\xb8\xad\xe5\x9b\xbd\xe8\xaa\x9e (\xe7\xb9\x81\xe4\xbd\x93)'
self.assertEqual(utils.to_str(un, 'utf-8'), ut)
self.assertEqual(utils.to_str(bytearray(ut), 'utf-8'), ut)
def test_to_bytes(self):
for x in (123, (1, 2, 3), [1, 2, 3], {1: 23}, None):
self.assertRaises(TypeError, utils.to_bytes, x)
if six.PY3:
self.assertEqual(utils.to_bytes('xyzzy'), b'xyzzy')
ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string
self.assertEqual(utils.to_bytes(ut), ut)
self.assertEqual(utils.to_bytes(bytearray(ut)), ut)
self.assertEqual(utils.to_bytes(un, 'utf-8'), ut)
else:
self.assertEqual(utils.to_bytes('xyzzy'), 'xyzzy')
ut = ''.join([chr(x) for x in (0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29)])
un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string
self.assertEqual(utils.to_bytes(ut), ut)
self.assertEqual(utils.to_bytes(bytearray(ut)), ut)
self.assertEqual(utils.to_bytes(un, 'utf-8'), ut)
def test_to_unicode(self):
if six.PY3:
self.assertEqual(utils.to_unicode('plugh'), 'plugh')
self.assertEqual(utils.to_unicode('áéíóúý'), 'áéíóúý')
un = '\u4e2d\u56fd\u8a9e (\u7e41\u4f53)' # pylint: disable=anomalous-unicode-escape-in-string
ut = bytes((0xe4, 0xb8, 0xad, 0xe5, 0x9b, 0xbd, 0xe8, 0xaa, 0x9e, 0x20, 0x28, 0xe7, 0xb9, 0x81, 0xe4, 0xbd, 0x93, 0x29))
self.assertEqual(utils.to_unicode(ut, 'utf-8'), un)
self.assertEqual(utils.to_unicode(bytearray(ut), 'utf-8'), un)
else:
self.assertEqual(utils.to_unicode('xyzzy', 'utf-8'), u'xyzzy')
ut = '\xe4\xb8\xad\xe5\x9b\xbd\xe8\xaa\x9e (\xe7\xb9\x81\xe4\xbd\x93)'
un = u'\u4e2d\u56fd\u8a9e (\u7e41\u4f53)'
self.assertEqual(utils.to_unicode(ut, 'utf-8'), un)
if __name__ == '__main__':
from integration import run_tests
run_tests(UtilsTestCase, needs_daemon=False)