Skip to content

Commit

Permalink
Merge pull request saltstack#52989 from waynew/51853-fix-true-false-p…
Browse files Browse the repository at this point in the history
…aths

Fix true/false shell paths, zcbuildout tests
  • Loading branch information
waynew authored May 15, 2019
2 parents c086d77 + 7ab295c commit 596be7c
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 122 deletions.
39 changes: 18 additions & 21 deletions salt/modules/zcbuildout.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,20 @@ def by_level(self):
LOG = _Logger()


def _encode_string(string):
if isinstance(string, six.text_type):
string = string.encode('utf-8')
return string


def _encode_status(status):
status['out'] = _encode_string(status['out'])
status['outlog_by_level'] = _encode_string(status['outlog_by_level'])
if status['out'] is None:
status['out'] = None
else:
status['out'] = salt.utils.stringutils.to_unicode(status['out'])
status['outlog_by_level'] = salt.utils.stringutils.to_unicode(status['outlog_by_level'])
if status['logs']:
for i, data in enumerate(status['logs'][:]):
status['logs'][i] = (data[0], _encode_string(data[1]))
status['logs'][i] = (data[0], salt.utils.stringutils.to_unicode(data[1]))
for logger in 'error', 'warn', 'info', 'debug':
logs = status['logs_by_level'].get(logger, [])[:]
if logs:
for i, log in enumerate(logs):
status['logs_by_level'][logger][i] = _encode_string(log)
status['logs_by_level'][logger][i] = salt.utils.stringutils.to_unicode(log)
return status


Expand All @@ -222,7 +219,7 @@ def _set_status(m,
if out and isinstance(out, six.string_types):
outlog += HR
outlog += 'OUTPUT:\n'
outlog += '{0}\n'.format(_encode_string(out))
outlog += '{0}\n'.format(salt.utils.stringutils.to_unicode(out))
outlog += HR
if m['logs']:
outlog += HR
Expand All @@ -233,13 +230,13 @@ def _set_status(m,
outlog_by_level += HR
for level, msg in m['logs']:
outlog += '\n{0}: {1}\n'.format(level.upper(),
_encode_string(msg))
salt.utils.stringutils.to_unicode(msg))
for logger in 'error', 'warn', 'info', 'debug':
logs = m['logs_by_level'].get(logger, [])
if logs:
outlog_by_level += '\n{0}:\n'.format(logger.upper())
for idx, log in enumerate(logs[:]):
logs[idx] = _encode_string(log)
logs[idx] = salt.utils.stringutils.to_unicode(log)
outlog_by_level += '\n'.join(logs)
outlog_by_level += '\n'
outlog += HR
Expand Down Expand Up @@ -875,12 +872,12 @@ def _merge_statuses(statuses):
status = BASE_STATUS.copy()
status['status'] = None
status['merged_statuses'] = True
status['out'] = []
status['out'] = ''
for st in statuses:
if status['status'] is not False:
status['status'] = st['status']
out = st['out']
comment = _encode_string(st['comment'])
comment = salt.utils.stringutils.to_unicode(st['comment'])
logs = st['logs']
logs_by_level = st['logs_by_level']
outlog_by_level = st['outlog_by_level']
Expand All @@ -890,32 +887,32 @@ def _merge_statuses(statuses):
status['out'] = ''
status['out'] += '\n'
status['out'] += HR
out = _encode_string(out)
out = salt.utils.stringutils.to_unicode(out)
status['out'] += '{0}\n'.format(out)
status['out'] += HR
if comment:
if not status['comment']:
status['comment'] = ''
status['comment'] += '\n{0}\n'.format(
_encode_string(comment))
salt.utils.stringutils.to_unicode(comment))
if outlog:
if not status['outlog']:
status['outlog'] = ''
outlog = _encode_string(outlog)
outlog = salt.utils.stringutils.to_unicode(outlog)
status['outlog'] += '\n{0}'.format(HR)
status['outlog'] += outlog
if outlog_by_level:
if not status['outlog_by_level']:
status['outlog_by_level'] = ''
status['outlog_by_level'] += '\n{0}'.format(HR)
status['outlog_by_level'] += _encode_string(outlog_by_level)
status['outlog_by_level'] += salt.utils.stringutils.to_unicode(outlog_by_level)
status['logs'].extend([
(a[0], _encode_string(a[1])) for a in logs])
(a[0], salt.utils.stringutils.to_unicode(a[1])) for a in logs])
for log in logs_by_level:
if log not in status['logs_by_level']:
status['logs_by_level'][log] = []
status['logs_by_level'][log].extend(
[_encode_string(a) for a in logs_by_level[log]])
[salt.utils.stringutils.to_unicode(a) for a in logs_by_level[log]])
return _encode_status(status)


Expand Down
8 changes: 4 additions & 4 deletions salt/utils/stringutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def to_bytes(s, encoding=None, errors='strict'):
# raised, otherwise we would have already returned (or raised some
# other exception).
raise exc # pylint: disable=raising-bad-type
raise TypeError('expected bytes, bytearray, or str')
raise TypeError('expected str, bytes, or bytearray not {}'.format(type(s)))
else:
return to_str(s, encoding, errors)

Expand Down Expand Up @@ -115,7 +115,7 @@ def _normalize(s):
# raised, otherwise we would have already returned (or raised some
# other exception).
raise exc # pylint: disable=raising-bad-type
raise TypeError('expected str, bytearray, or unicode')
raise TypeError('expected str, bytes, or bytearray not {}'.format(type(s)))


def to_unicode(s, encoding=None, errors='strict', normalize=False):
Expand All @@ -140,7 +140,7 @@ def _normalize(s):
return _normalize(s)
elif isinstance(s, (bytes, bytearray)):
return _normalize(to_str(s, encoding, errors))
raise TypeError('expected str, bytes, or bytearray')
raise TypeError('expected str, bytes, or bytearray not {}'.format(type(s)))
else:
# This needs to be str and not six.string_types, since if the string is
# already a unicode type, it does not need to be decoded (and doing so
Expand All @@ -158,7 +158,7 @@ def _normalize(s):
# raised, otherwise we would have already returned (or raised some
# other exception).
raise exc # pylint: disable=raising-bad-type
raise TypeError('expected str or bytearray')
raise TypeError('expected str, bytes, or bytearray not {}'.format(type(s)))


@jinja_filter('str_to_num') # Remove this for Neon
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/files/file/base/issue-35384.sls
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ cmd_run_unless_multiple:
cmd.run:
- name: echo "hello"
- unless:
- /bin/true
- /bin/false
- "$(which true)"
- "$(which false)"
2 changes: 1 addition & 1 deletion tests/integration/files/file/base/nested-orch/inner.sls
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ cmd.run:
salt.function:
- tgt: minion
- arg:
- /bin/false
- "$(which false)"
- failhard: True
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ B:

C:
cmd.run:
- name: /bin/false
- name: "$(which false)"

D:
cmd.run:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/modules/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,9 @@ def test_requisites_require_any(self):
'result': True,
'changes': True,
},
'cmd_|-C_|-/bin/false_|-run': {
'cmd_|-C_|-$(which false)_|-run': {
'__run_num__': 1,
'comment': 'Command "/bin/false" run',
'comment': 'Command "$(which false)" run',
'result': False,
'changes': True,
},
Expand Down
27 changes: 14 additions & 13 deletions tests/integration/states/test_alternatives.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.unit import skipIf
from tests.support.helpers import destructiveTest
from tests.support.mixins import SaltReturnAssertsMixin
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import skipIf

NO_ALTERNATIVES = False
if not os.path.exists('/etc/alternatives'):
Expand All @@ -22,38 +23,38 @@
class AlterantivesStateTest(ModuleCase, SaltReturnAssertsMixin):
@destructiveTest
def test_install_set_and_remove(self):
ret = self.run_state('alternatives.set', name='alt-test', path='/bin/true')
ret = self.run_state('alternatives.set', name='alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH)
self.assertSaltFalseReturn(ret)

ret = self.run_state('alternatives.install', name='alt-test',
link='/usr/local/bin/alt-test', path='/bin/true', priority=50)
link='/usr/local/bin/alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH, priority=50)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, '/bin/true', keys=['path'])
self.assertSaltStateChangesEqual(ret, RUNTIME_VARS.SHELL_TRUE_PATH, keys=['path'])

ret = self.run_state('alternatives.install', name='alt-test',
link='/usr/local/bin/alt-test', path='/bin/true', priority=50)
link='/usr/local/bin/alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH, priority=50)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, {})

ret = self.run_state('alternatives.install', name='alt-test',
link='/usr/local/bin/alt-test', path='/bin/false', priority=90)
link='/usr/local/bin/alt-test', path=RUNTIME_VARS.SHELL_FALSE_PATH, priority=90)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, '/bin/false', keys=['path'])
self.assertSaltStateChangesEqual(ret, RUNTIME_VARS.SHELL_FALSE_PATH, keys=['path'])

ret = self.run_state('alternatives.set', name='alt-test', path='/bin/false')
ret = self.run_state('alternatives.set', name='alt-test', path=RUNTIME_VARS.SHELL_FALSE_PATH)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, {})

ret = self.run_state('alternatives.set', name='alt-test', path='/bin/true')
ret = self.run_state('alternatives.set', name='alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, '/bin/true', keys=['path'])
self.assertSaltStateChangesEqual(ret, RUNTIME_VARS.SHELL_TRUE_PATH, keys=['path'])

ret = self.run_state('alternatives.set', name='alt-test', path='/bin/true')
ret = self.run_state('alternatives.set', name='alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH)
self.assertSaltTrueReturn(ret)
self.assertSaltStateChangesEqual(ret, {})

ret = self.run_state('alternatives.remove', name='alt-test', path='/bin/true')
ret = self.run_state('alternatives.remove', name='alt-test', path=RUNTIME_VARS.SHELL_TRUE_PATH)
self.assertSaltTrueReturn(ret)

ret = self.run_state('alternatives.remove', name='alt-test', path='/bin/false')
ret = self.run_state('alternatives.remove', name='alt-test', path=RUNTIME_VARS.SHELL_FALSE_PATH)
self.assertSaltTrueReturn(ret)
9 changes: 5 additions & 4 deletions tests/integration/states/test_docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import tempfile

# Import Salt Testing Libs
from tests.support.unit import skipIf
from tests.support.case import ModuleCase
from tests.support.docker import with_network, random_name
from tests.support.paths import FILES, TMP
from tests.support.helpers import destructiveTest, with_tempdir
from tests.support.mixins import SaltReturnAssertsMixin
from tests.support.paths import FILES, TMP
from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import skipIf

# Import Salt Libs
import salt.utils.files
Expand Down Expand Up @@ -1099,7 +1100,7 @@ def test_run_failhard(self, name):
'docker_container.run',
name=name,
image=self.image,
command='/bin/false',
command=RUNTIME_VARS.SHELL_FALSE_PATH,
failhard=True)
self.assertSaltFalseReturn(ret)
ret = ret[next(iter(ret))]
Expand All @@ -1115,7 +1116,7 @@ def test_run_failhard(self, name):
'docker_container.run',
name=name,
image=self.image,
command='/bin/false',
command=RUNTIME_VARS.SHELL_FALSE_PATH,
failhard=False)
self.assertSaltTrueReturn(ret)
ret = ret[next(iter(ret))]
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/states/test_pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def test_22359_pip_installed_unless_does_not_trigger_warnings(self):
)
)

false_cmd = '/bin/false'
false_cmd = RUNTIME_VARS.SHELL_FALSE_PATH
if salt.utils.platform.is_windows():
false_cmd = 'exit 1 >nul'
try:
Expand Down
5 changes: 4 additions & 1 deletion tests/support/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import multiprocessing

import salt.utils.json
import salt.utils.path

# Import tests support libs
import tests.support.paths as paths
Expand Down Expand Up @@ -219,6 +220,8 @@ def __setattr__(self, name, value):
TMP_PILLAR_TREE=paths.TMP_PILLAR_TREE,
TMP_PRODENV_STATE_TREE=paths.TMP_PRODENV_STATE_TREE,
RUNNING_TESTS_USER=RUNNING_TESTS_USER,
RUNTIME_CONFIGS={}
RUNTIME_CONFIGS={},
SHELL_TRUE_PATH=salt.utils.path.which('true'),
SHELL_FALSE_PATH=salt.utils.path.which('false'),
)
# <---- Tests Runtime Variables --------------------------------------------------------------------------------------
Loading

0 comments on commit 596be7c

Please sign in to comment.