mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
Merge pull request #45885 from terminalmage/issue45845
Catch and report YAML errors in jinja load_yaml
This commit is contained in:
commit
a309933104
@ -79,7 +79,7 @@ with release Neon.
|
||||
|
||||
The functions have been moved as follows:
|
||||
|
||||
- ``salt.utils.appendproctitle``: use ``salt.utils.process.appendproctitle``
|
||||
- ``salt.utils.appendproctitle``: use ``salt.utils.process.appendproctitle``
|
||||
instead.
|
||||
- ``salt.utils.daemonize``: use ``salt.utils.process.daemonize`` instead.
|
||||
- ``salt.utils.daemonize_if``: use ``salt.utils.process.daemonize_if`` instead.
|
||||
@ -94,22 +94,22 @@ The functions have been moved as follows:
|
||||
- ``salt.utils.is_hex``: use ``salt.utils.stringutils.is_hex`` instead.
|
||||
- ``salt.utils.is_bin_str``: use ``salt.utils.stringutils.is_bin_str`` instead.
|
||||
- ``salt.utils.rand_string``: use ``salt.utils.stringutils.random`` instead.
|
||||
- ``salt.utils.contains_whitespace``: use
|
||||
- ``salt.utils.contains_whitespace``: use
|
||||
``salt.utils.stringutils.contains_whitespace`` instead.
|
||||
- ``salt.utils.build_whitespace_split_regex``: use
|
||||
- ``salt.utils.build_whitespace_split_regex``: use
|
||||
``salt.utils.stringutils.build_whitespace_split_regex`` instead.
|
||||
- ``salt.utils.expr_match``: use ``salt.utils.stringutils.expr_match`` instead.
|
||||
- ``salt.utils.check_whitelist_blacklist``: use
|
||||
- ``salt.utils.check_whitelist_blacklist``: use
|
||||
``salt.utils.stringutils.check_whitelist_blacklist`` instead.
|
||||
- ``salt.utils.check_include_exclude``: use
|
||||
- ``salt.utils.check_include_exclude``: use
|
||||
``salt.utils.stringutils.check_include_exclude`` instead.
|
||||
- ``salt.utils.print_cli``: use ``salt.utils.stringutils.print_cli`` instead.
|
||||
- ``salt.utils.clean_kwargs``: use ``salt.utils.args.clean_kwargs`` instead.
|
||||
- ``salt.utils.invalid_kwargs``: use ``salt.utils.args.invalid_kwargs``
|
||||
- ``salt.utils.invalid_kwargs``: use ``salt.utils.args.invalid_kwargs``
|
||||
instead.
|
||||
- ``salt.utils.shlex_split``: use ``salt.utils.args.shlex_split`` instead.
|
||||
- ``salt.utils.arg_lookup``: use ``salt.utils.args.arg_lookup`` instead.
|
||||
- ``salt.utils.argspec_report``: use ``salt.utils.args.argspec_report``
|
||||
- ``salt.utils.argspec_report``: use ``salt.utils.args.argspec_report``
|
||||
instead.
|
||||
- ``salt.utils.split_input``: use ``salt.utils.args.split_input`` instead.
|
||||
- ``salt.utils.test_mode``: use ``salt.utils.args.test_mode`` instead.
|
||||
@ -118,7 +118,7 @@ The functions have been moved as follows:
|
||||
- ``salt.utils.which_bin``: use ``salt.utils.path.which_bin`` instead.
|
||||
- ``salt.utils.path_join``: use ``salt.utils.path.join`` instead.
|
||||
- ``salt.utils.check_or_die``: use ``salt.utils.path.check_or_die`` instead.
|
||||
- ``salt.utils.sanitize_win_path_string``: use
|
||||
- ``salt.utils.sanitize_win_path_string``: use
|
||||
``salt.utils.path.sanitize_win_path`` instead.
|
||||
- ``salt.utils.rand_str``: use ``salt.utils.hashutils.random_hash`` instead.
|
||||
- ``salt.utils.get_hash``: use ``salt.utils.hashutils.get_hash`` instead.
|
||||
@ -128,9 +128,9 @@ The functions have been moved as follows:
|
||||
- ``salt.utils.is_darwin``: use ``salt.utils.platform.is_darwin`` instead.
|
||||
- ``salt.utils.is_sunos``: use ``salt.utils.platform.is_sunos`` instead.
|
||||
- ``salt.utils.is_smartos``: use ``salt.utils.platform.is_smartos`` instead.
|
||||
- ``salt.utils.is_smartos_globalzone``: use
|
||||
- ``salt.utils.is_smartos_globalzone``: use
|
||||
``salt.utils.platform.is_smartos_globalzone`` instead.
|
||||
- ``salt.utils.is_smartos_zone``: use ``salt.utils.platform.is_smartos_zone``
|
||||
- ``salt.utils.is_smartos_zone``: use ``salt.utils.platform.is_smartos_zone``
|
||||
instead.
|
||||
- ``salt.utils.is_freebsd``: use ``salt.utils.platform.is_freebsd`` instead.
|
||||
- ``salt.utils.is_netbsd``: use ``salt.utils.platform.is_netbsd`` instead.
|
||||
@ -147,55 +147,55 @@ The functions have been moved as follows:
|
||||
- ``salt.utils.is_bin_file``: use ``salt.utils.files.is_binary`` instead.
|
||||
- ``salt.utils.list_files``: use ``salt.utils.files.list_files`` instead.
|
||||
- ``salt.utils.safe_walk``: use ``salt.utils.files.safe_walk`` instead.
|
||||
- ``salt.utils.st_mode_to_octal``: use ``salt.utils.files.st_mode_to_octal``
|
||||
- ``salt.utils.st_mode_to_octal``: use ``salt.utils.files.st_mode_to_octal``
|
||||
instead.
|
||||
- ``salt.utils.normalize_mode``: use ``salt.utils.files.normalize_mode``
|
||||
- ``salt.utils.normalize_mode``: use ``salt.utils.files.normalize_mode``
|
||||
instead.
|
||||
- ``salt.utils.human_size_to_bytes``: use
|
||||
- ``salt.utils.human_size_to_bytes``: use
|
||||
``salt.utils.files.human_size_to_bytes`` instead.
|
||||
- ``salt.utils.backup_minion``: use ``salt.utils.files.backup_minion`` instead.
|
||||
- ``salt.utils.str_version_to_evr``: use ``salt.utils.pkg.rpm.version_to_evr``
|
||||
instead.
|
||||
- ``salt.utils.parse_docstring``: use ``salt.utils.doc.parse_docstring``
|
||||
- ``salt.utils.parse_docstring``: use ``salt.utils.doc.parse_docstring``
|
||||
instead.
|
||||
- ``salt.utils.compare_versions``: use ``salt.utils.versions.compare`` instead.
|
||||
- ``salt.utils.version_cmp``: use ``salt.utils.versions.version_cmp`` instead.
|
||||
- ``salt.utils.warn_until``: use ``salt.utils.versions.warn_until`` instead.
|
||||
- ``salt.utils.kwargs_warn_until``: use
|
||||
- ``salt.utils.kwargs_warn_until``: use
|
||||
``salt.utils.versions.kwargs_warn_until`` instead.
|
||||
- ``salt.utils.get_color_theme``: use ``salt.utils.color.get_color_theme``
|
||||
- ``salt.utils.get_color_theme``: use ``salt.utils.color.get_color_theme``
|
||||
instead.
|
||||
- ``salt.utils.get_colors``: use ``salt.utils.color.get_colors`` instead.
|
||||
- ``salt.utils.gen_state_tag``: use ``salt.utils.state.gen_tag`` instead.
|
||||
- ``salt.utils.search_onfail_requisites``: use
|
||||
- ``salt.utils.search_onfail_requisites``: use
|
||||
``salt.utils.state.search_onfail_requisites`` instead.
|
||||
- ``salt.utils.check_state_result``: use ``salt.utils.state.check_result``
|
||||
- ``salt.utils.check_state_result``: use ``salt.utils.state.check_result``
|
||||
instead.
|
||||
- ``salt.utils.get_user``: use ``salt.utils.user.get_user`` instead.
|
||||
- ``salt.utils.get_uid``: use ``salt.utils.user.get_uid`` instead.
|
||||
- ``salt.utils.get_specific_user``: use ``salt.utils.user.get_specific_user``
|
||||
- ``salt.utils.get_specific_user``: use ``salt.utils.user.get_specific_user``
|
||||
instead.
|
||||
- ``salt.utils.chugid``: use ``salt.utils.user.chugid`` instead.
|
||||
- ``salt.utils.chugid_and_umask``: use ``salt.utils.user.chugid_and_umask``
|
||||
- ``salt.utils.chugid_and_umask``: use ``salt.utils.user.chugid_and_umask``
|
||||
instead.
|
||||
- ``salt.utils.get_default_group``: use ``salt.utils.user.get_default_group``
|
||||
- ``salt.utils.get_default_group``: use ``salt.utils.user.get_default_group``
|
||||
instead.
|
||||
- ``salt.utils.get_group_list``: use ``salt.utils.user.get_group_list``
|
||||
- ``salt.utils.get_group_list``: use ``salt.utils.user.get_group_list``
|
||||
instead.
|
||||
- ``salt.utils.get_group_dict``: use ``salt.utils.user.get_group_dict``
|
||||
- ``salt.utils.get_group_dict``: use ``salt.utils.user.get_group_dict``
|
||||
instead.
|
||||
- ``salt.utils.get_gid_list``: use ``salt.utils.user.get_gid_list`` instead.
|
||||
- ``salt.utils.get_gid``: use ``salt.utils.user.get_gid`` instead.
|
||||
- ``salt.utils.enable_ctrl_logoff_handler``: use
|
||||
- ``salt.utils.enable_ctrl_logoff_handler``: use
|
||||
``salt.utils.win_functions.enable_ctrl_logoff_handler`` instead.
|
||||
- ``salt.utils.traverse_dict``: use ``salt.utils.data.traverse_dict`` instead.
|
||||
- ``salt.utils.traverse_dict_and_list``: use
|
||||
- ``salt.utils.traverse_dict_and_list``: use
|
||||
``salt.utils.data.traverse_dict_and_list`` instead.
|
||||
- ``salt.utils.filter_by``: use ``salt.utils.data.filter_by`` instead.
|
||||
- ``salt.utils.subdict_match``: use ``salt.utils.data.subdict_match`` instead.
|
||||
- ``salt.utils.substr_in_list``: use ``salt.utils.data.substr_in_list`` instead.
|
||||
- ``salt.utils.is_dictlist``: use ``salt.utils.data.is_dictlist``.
|
||||
- ``salt.utils.repack_dictlist``: use ``salt.utils.data.repack_dictlist``
|
||||
- ``salt.utils.repack_dictlist``: use ``salt.utils.data.repack_dictlist``
|
||||
instead.
|
||||
- ``salt.utils.compare_dicts``: use ``salt.utils.data.compare_dicts`` instead.
|
||||
- ``salt.utils.compare_lists``: use ``salt.utils.data.compare_lists`` instead.
|
||||
@ -208,33 +208,33 @@ The functions have been moved as follows:
|
||||
- ``salt.utils.isorted``: use ``salt.utils.data.sorted_ignorecase`` instead.
|
||||
- ``salt.utils.is_true``: use ``salt.utils.data.is_true`` instead.
|
||||
- ``salt.utils.mysql_to_dict``: use ``salt.utils.data.mysql_to_dict`` instead.
|
||||
- ``salt.utils.simple_types_filter``: use
|
||||
- ``salt.utils.simple_types_filter``: use
|
||||
``salt.utils.data.simple_types_filter`` instead.
|
||||
- ``salt.utils.ip_bracket``: use ``salt.utils.zeromq.ip_bracket`` instead.
|
||||
- ``salt.utils.gen_mac``: use ``salt.utils.network.gen_mac`` instead.
|
||||
- ``salt.utils.mac_str_to_bytes``: use ``salt.utils.network.mac_str_to_bytes``
|
||||
- ``salt.utils.mac_str_to_bytes``: use ``salt.utils.network.mac_str_to_bytes``
|
||||
instead.
|
||||
- ``salt.utils.refresh_dns``: use ``salt.utils.network.refresh_dns`` instead.
|
||||
- ``salt.utils.dns_check``: use ``salt.utils.network.dns_check`` instead.
|
||||
- ``salt.utils.get_context``: use ``salt.utils.templates.get_context`` instead.
|
||||
- ``salt.utils.get_master_key``: use ``salt.utils.master.get_master_key``
|
||||
- ``salt.utils.get_context``: use ``salt.utils.stringutils.get_context`` instead.
|
||||
- ``salt.utils.get_master_key``: use ``salt.utils.master.get_master_key``
|
||||
instead.
|
||||
- ``salt.utils.get_values_of_matching_keys``: use
|
||||
- ``salt.utils.get_values_of_matching_keys``: use
|
||||
``salt.utils.master.get_values_of_matching_keys`` instead.
|
||||
- ``salt.utils.date_cast``: use ``salt.utils.dateutils.date_cast`` instead.
|
||||
- ``salt.utils.date_format``: use ``salt.utils.dateutils.strftime`` instead.
|
||||
- ``salt.utils.total_seconds``: use ``salt.utils.dateutils.total_seconds``
|
||||
- ``salt.utils.total_seconds``: use ``salt.utils.dateutils.total_seconds``
|
||||
instead.
|
||||
- ``salt.utils.find_json``: use ``salt.utils.json.find_json`` instead.
|
||||
- ``salt.utils.import_json``: use ``salt.utils.json.import_json`` instead.
|
||||
- ``salt.utils.namespaced_function``: use
|
||||
- ``salt.utils.namespaced_function``: use
|
||||
``salt.utils.functools.namespaced_function`` instead.
|
||||
- ``salt.utils.alias_function``: use ``salt.utils.functools.alias_function``
|
||||
- ``salt.utils.alias_function``: use ``salt.utils.functools.alias_function``
|
||||
instead.
|
||||
- ``salt.utils.profile_func``: use ``salt.utils.profile.profile_func`` instead.
|
||||
- ``salt.utils.activate_profile``: use ``salt.utils.profile.activate_profile``
|
||||
- ``salt.utils.activate_profile``: use ``salt.utils.profile.activate_profile``
|
||||
instead.
|
||||
- ``salt.utils.output_profile``: use ``salt.utils.profile.output_profile``
|
||||
- ``salt.utils.output_profile``: use ``salt.utils.profile.output_profile``
|
||||
instead.
|
||||
|
||||
State and Execution Module Support for ``docker run`` Functionality
|
||||
|
@ -285,7 +285,7 @@ class SaltRenderError(SaltException):
|
||||
if self.line_num and self.buffer:
|
||||
# Avoid circular import
|
||||
import salt.utils.templates
|
||||
self.context = salt.utils.templates.get_context(
|
||||
self.context = salt.utils.stringutils.get_context(
|
||||
self.buffer,
|
||||
self.line_num,
|
||||
marker=marker
|
||||
|
@ -1740,15 +1740,15 @@ def dns_check(addr, port, safe=False, ipv6=None):
|
||||
def get_context(template, line, num_lines=5, marker=None):
|
||||
# Late import to avoid circular import.
|
||||
import salt.utils.versions
|
||||
import salt.utils.templates
|
||||
import salt.utils.stringutils
|
||||
salt.utils.versions.warn_until(
|
||||
'Neon',
|
||||
'Use of \'salt.utils.get_context\' detected. This function '
|
||||
'has been moved to \'salt.utils.templates.get_context\' as of '
|
||||
'has been moved to \'salt.utils.stringutils.get_context\' as of '
|
||||
'Salt Oxygen. This warning will be removed in Salt Neon.',
|
||||
stacklevel=3
|
||||
)
|
||||
return salt.utils.templates.get_context(template, line, num_lines, marker)
|
||||
return salt.utils.stringutils.get_context(template, line, num_lines, marker)
|
||||
|
||||
|
||||
def get_master_key(key_user, opts, skip_perm_errors=False):
|
||||
|
@ -850,6 +850,24 @@ class SerializerExtension(Extension, object):
|
||||
value = six.text_type(value)
|
||||
try:
|
||||
return salt.utils.data.decode(salt.utils.yaml.safe_load(value))
|
||||
except salt.utils.yaml.YAMLError as exc:
|
||||
msg = 'Encountered error loading yaml: '
|
||||
try:
|
||||
# Reported line is off by one, add 1 to correct it
|
||||
line = exc.problem_mark.line + 1
|
||||
buf = exc.problem_mark.buffer
|
||||
problem = exc.problem
|
||||
except AttributeError:
|
||||
# No context information available in the exception, fall back
|
||||
# to the stringified version of the exception.
|
||||
msg += six.text_type(exc)
|
||||
else:
|
||||
msg += '{0}\n'.format(problem)
|
||||
msg += salt.utils.stringutils.get_context(
|
||||
buf,
|
||||
line,
|
||||
marker=' <======================')
|
||||
raise TemplateRuntimeError(msg)
|
||||
except AttributeError:
|
||||
raise TemplateRuntimeError(
|
||||
'Unable to load yaml from {0}'.format(value))
|
||||
|
@ -430,3 +430,38 @@ def print_cli(msg, retries=10, step=0.01):
|
||||
else:
|
||||
raise
|
||||
break
|
||||
|
||||
|
||||
def get_context(template, line, num_lines=5, marker=None):
|
||||
'''
|
||||
Returns debugging context around a line in a given string
|
||||
|
||||
Returns:: string
|
||||
'''
|
||||
template_lines = template.splitlines()
|
||||
num_template_lines = len(template_lines)
|
||||
|
||||
# In test mode, a single line template would return a crazy line number like,
|
||||
# 357. Do this sanity check and if the given line is obviously wrong, just
|
||||
# return the entire template
|
||||
if line > num_template_lines:
|
||||
return template
|
||||
|
||||
context_start = max(0, line - num_lines - 1) # subt 1 for 0-based indexing
|
||||
context_end = min(num_template_lines, line + num_lines)
|
||||
error_line_in_context = line - context_start - 1 # subtr 1 for 0-based idx
|
||||
|
||||
buf = []
|
||||
if context_start > 0:
|
||||
buf.append('[...]')
|
||||
error_line_in_context += 1
|
||||
|
||||
buf.extend(template_lines[context_start:context_end])
|
||||
|
||||
if context_end < num_template_lines:
|
||||
buf.append('[...]')
|
||||
|
||||
if marker:
|
||||
buf[error_line_in_context] += marker
|
||||
|
||||
return '---\n{0}\n---'.format('\n'.join(buf))
|
||||
|
@ -95,41 +95,6 @@ class AliasedModule(object):
|
||||
return getattr(self.wrapped, name)
|
||||
|
||||
|
||||
def get_context(template, line, num_lines=5, marker=None):
|
||||
'''
|
||||
Returns debugging context around a line in a given string
|
||||
|
||||
Returns:: string
|
||||
'''
|
||||
template_lines = template.splitlines()
|
||||
num_template_lines = len(template_lines)
|
||||
|
||||
# in test, a single line template would return a crazy line number like,
|
||||
# 357. do this sanity check and if the given line is obviously wrong, just
|
||||
# return the entire template
|
||||
if line > num_template_lines:
|
||||
return template
|
||||
|
||||
context_start = max(0, line - num_lines - 1) # subt 1 for 0-based indexing
|
||||
context_end = min(num_template_lines, line + num_lines)
|
||||
error_line_in_context = line - context_start - 1 # subtr 1 for 0-based idx
|
||||
|
||||
buf = []
|
||||
if context_start > 0:
|
||||
buf.append('[...]')
|
||||
error_line_in_context += 1
|
||||
|
||||
buf.extend(template_lines[context_start:context_end])
|
||||
|
||||
if context_end < num_template_lines:
|
||||
buf.append('[...]')
|
||||
|
||||
if marker:
|
||||
buf[error_line_in_context] += marker
|
||||
|
||||
return '---\n{0}\n---'.format('\n'.join(buf))
|
||||
|
||||
|
||||
def wrap_tmpl_func(render_str):
|
||||
|
||||
def render_tmpl(tmplsrc,
|
||||
@ -315,7 +280,7 @@ def _get_jinja_error(trace, context=None):
|
||||
out = '\n{0}\n'.format(msg.splitlines()[0])
|
||||
with salt.utils.files.fopen(template_path) as fp_:
|
||||
template_contents = salt.utils.stringutils.to_unicode(fp_.read())
|
||||
out += get_context(
|
||||
out += salt.utils.stringutils.get_context(
|
||||
template_contents,
|
||||
line,
|
||||
marker=' <======================')
|
||||
@ -417,15 +382,6 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
|
||||
template = jinja_env.from_string(tmplstr)
|
||||
template.globals.update(decoded_context)
|
||||
output = template.render(**decoded_context)
|
||||
except jinja2.exceptions.TemplateSyntaxError as exc:
|
||||
trace = traceback.extract_tb(sys.exc_info()[2])
|
||||
line, out = _get_jinja_error(trace, context=decoded_context)
|
||||
if not line:
|
||||
tmplstr = ''
|
||||
raise SaltRenderError(
|
||||
'Jinja syntax error: {0}{1}'.format(exc, out),
|
||||
line,
|
||||
tmplstr)
|
||||
except jinja2.exceptions.UndefinedError as exc:
|
||||
trace = traceback.extract_tb(sys.exc_info()[2])
|
||||
out = _get_jinja_error(trace, context=decoded_context)[1]
|
||||
@ -436,6 +392,16 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None):
|
||||
'Jinja variable {0}{1}'.format(
|
||||
exc, out),
|
||||
buf=tmplstr)
|
||||
except (jinja2.exceptions.TemplateRuntimeError,
|
||||
jinja2.exceptions.TemplateSyntaxError) as exc:
|
||||
trace = traceback.extract_tb(sys.exc_info()[2])
|
||||
line, out = _get_jinja_error(trace, context=decoded_context)
|
||||
if not line:
|
||||
tmplstr = ''
|
||||
raise SaltRenderError(
|
||||
'Jinja syntax error: {0}{1}'.format(exc, out),
|
||||
line,
|
||||
tmplstr)
|
||||
except (SaltInvocationError, CommandExecutionError) as exc:
|
||||
trace = traceback.extract_tb(sys.exc_info()[2])
|
||||
line, out = _get_jinja_error(trace, context=decoded_context)
|
||||
|
@ -76,18 +76,25 @@ class SaltYamlSafeLoader(yaml.SafeLoader, object):
|
||||
|
||||
self.flatten_mapping(node)
|
||||
|
||||
context = 'while constructing a mapping'
|
||||
mapping = self.dictclass()
|
||||
for key_node, value_node in node.value:
|
||||
key = self.construct_object(key_node, deep=deep)
|
||||
try:
|
||||
hash(key)
|
||||
except TypeError:
|
||||
err = ('While constructing a mapping {0} found unacceptable '
|
||||
'key {1}').format(node.start_mark, key_node.start_mark)
|
||||
raise ConstructorError(err)
|
||||
raise ConstructorError(
|
||||
context,
|
||||
node.start_mark,
|
||||
"found unacceptable key {0}".format(key_node.value),
|
||||
key_node.start_mark)
|
||||
value = self.construct_object(value_node, deep=deep)
|
||||
if key in mapping:
|
||||
raise ConstructorError('Conflicting ID \'{0}\''.format(key))
|
||||
raise ConstructorError(
|
||||
context,
|
||||
node.start_mark,
|
||||
"found conflicting ID '{0}'".format(key),
|
||||
key_node.start_mark)
|
||||
mapping[key] = value
|
||||
return mapping
|
||||
|
||||
|
@ -34,11 +34,8 @@ from salt.utils.jinja import (
|
||||
ensure_sequence_filter
|
||||
)
|
||||
from salt.utils.odict import OrderedDict
|
||||
from salt.utils.templates import (
|
||||
get_context,
|
||||
JINJA,
|
||||
render_jinja_tmpl
|
||||
)
|
||||
from salt.utils.templates import JINJA, render_jinja_tmpl
|
||||
|
||||
# dateutils is needed so that the strftime jinja filter is loaded
|
||||
import salt.utils.dateutils # pylint: disable=unused-import
|
||||
import salt.utils.files
|
||||
@ -379,36 +376,6 @@ class TestGetTemplate(TestCase):
|
||||
result = salt.utils.stringutils.to_unicode(fp.read(), 'utf-8')
|
||||
self.assertEqual(salt.utils.stringutils.to_unicode('Assunção' + os.linesep), result)
|
||||
|
||||
def test_get_context_has_enough_context(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = get_context(template, 8)
|
||||
expected = '---\n[...]\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_at_top_of_file(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = get_context(template, 1)
|
||||
expected = '---\n1\n2\n3\n4\n5\n6\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_at_bottom_of_file(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = get_context(template, 15)
|
||||
expected = '---\n[...]\na\nb\nc\nd\ne\nf\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_2_context_lines(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = get_context(template, 8, num_lines=2)
|
||||
expected = '---\n[...]\n6\n7\n8\n9\na\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_with_marker(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = get_context(template, 8, num_lines=2, marker=' <---')
|
||||
expected = '---\n[...]\n6\n7\n8 <---\n9\na\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_render_with_syntax_error(self):
|
||||
template = 'hello\n\n{{ bad\n\nfoo'
|
||||
expected = r'.*---\nhello\n\n{{ bad\n\nfoo <======================\n---'
|
||||
|
@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import textwrap
|
||||
|
||||
# Import Salt libs
|
||||
from tests.support.mock import patch
|
||||
@ -108,3 +109,43 @@ class StringutilsTestCase(TestCase):
|
||||
'(?:[\\s]+)?$'
|
||||
ret = salt.utils.stringutils.build_whitespace_split_regex(' '.join(LOREM_IPSUM.split()[:5]))
|
||||
self.assertEqual(ret, expected_regex)
|
||||
|
||||
def test_get_context(self):
|
||||
expected_context = textwrap.dedent('''\
|
||||
---
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eget urna a arcu lacinia sagittis.
|
||||
Sed scelerisque, lacus eget malesuada vestibulum, justo diam facilisis tortor, in sodales dolor
|
||||
[...]
|
||||
---''')
|
||||
ret = salt.utils.stringutils.get_context(LOREM_IPSUM, 1, num_lines=1)
|
||||
self.assertEqual(ret, expected_context)
|
||||
|
||||
def test_get_context_has_enough_context(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = salt.utils.stringutils.get_context(template, 8)
|
||||
expected = '---\n[...]\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_at_top_of_file(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = salt.utils.stringutils.get_context(template, 1)
|
||||
expected = '---\n1\n2\n3\n4\n5\n6\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_at_bottom_of_file(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = salt.utils.stringutils.get_context(template, 15)
|
||||
expected = '---\n[...]\na\nb\nc\nd\ne\nf\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_2_context_lines(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = salt.utils.stringutils.get_context(template, 8, num_lines=2)
|
||||
expected = '---\n[...]\n6\n7\n8\n9\na\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
||||
def test_get_context_with_marker(self):
|
||||
template = '1\n2\n3\n4\n5\n6\n7\n8\n9\na\nb\nc\nd\ne\nf'
|
||||
context = salt.utils.stringutils.get_context(template, 8, num_lines=2, marker=' <---')
|
||||
expected = '---\n[...]\n6\n7\n8 <---\n9\na\n[...]\n---'
|
||||
self.assertEqual(expected, context)
|
||||
|
@ -1,25 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Tests for salt.utils.data
|
||||
'''
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import textwrap
|
||||
|
||||
# Import Salt libs
|
||||
import salt.utils.templates
|
||||
from tests.support.unit import TestCase, LOREM_IPSUM
|
||||
|
||||
|
||||
class TemplatesTestCase(TestCase):
|
||||
|
||||
def test_get_context(self):
|
||||
expected_context = textwrap.dedent('''\
|
||||
---
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eget urna a arcu lacinia sagittis.
|
||||
Sed scelerisque, lacus eget malesuada vestibulum, justo diam facilisis tortor, in sodales dolor
|
||||
[...]
|
||||
---''')
|
||||
ret = salt.utils.templates.get_context(LOREM_IPSUM, 1, num_lines=1)
|
||||
self.assertEqual(ret, expected_context)
|
Loading…
Reference in New Issue
Block a user