Merge pull request #49411 from terminalmage/issue49269-2018.3

Allow our custom yaml dumper to NamespacedDictWrapper objects (2018.3 branch)
This commit is contained in:
Nicole Thomas 2018-08-30 09:05:36 -04:00 committed by GitHub
commit a962efc009
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 7 deletions

View file

@ -1380,11 +1380,11 @@ class LazyLoader(salt.utils.lazy.LazyDict):
'''
if '__grains__' not in self.pack:
self.context_dict['grains'] = opts.get('grains', {})
self.pack['__grains__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'grains', override_name='grains')
self.pack['__grains__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'grains')
if '__pillar__' not in self.pack:
self.context_dict['pillar'] = opts.get('pillar', {})
self.pack['__pillar__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'pillar', override_name='pillar')
self.pack['__pillar__'] = salt.utils.context.NamespacedDictWrapper(self.context_dict, 'pillar')
mod_opts = {}
for key, val in list(opts.items()):

View file

@ -203,10 +203,14 @@ class NamespacedDictWrapper(collections.MutableMapping, dict):
self.pre_keys = (pre_keys,)
else:
self.pre_keys = pre_keys
if override_name:
self.__class__.__module__ = 'salt'
# __name__ can't be assigned a unicode
self.__class__.__name__ = str(override_name) # future lint: disable=blacklisted-function
if override_name is not None:
import salt.utils.versions
salt.utils.versions.warn_until(
'Neon',
'Overriding the class name is no longer supported. Please '
'remove the override_name argument before it is removed in '
'Salt Sodium.'
)
super(NamespacedDictWrapper, self).__init__(self._dict())
def _dict(self):

View file

@ -17,7 +17,7 @@ except ImportError:
import yaml # pylint: disable=blacklisted-import
import collections
import salt.utils.context
from salt.utils.odict import OrderedDict
try:
@ -77,6 +77,14 @@ SafeOrderedDumper.add_representer(
collections.defaultdict,
yaml.representer.SafeRepresenter.represent_dict
)
OrderedDumper.add_representer(
salt.utils.context.NamespacedDictWrapper,
yaml.representer.SafeRepresenter.represent_dict
)
SafeOrderedDumper.add_representer(
salt.utils.context.NamespacedDictWrapper,
yaml.representer.SafeRepresenter.represent_dict
)
OrderedDumper.add_representer(
'tag:yaml.org,2002:timestamp',

View file

@ -14,6 +14,7 @@ from tests.support.mixins import RUNTIME_VARS
# Import Salt libs
import salt.config
import salt.utils.yaml
from salt.output import display_output
from salt.ext import six
@ -82,6 +83,20 @@ class OutputReturnTest(ShellCase):
ret = self.run_call('test.ping --out=yaml')
self.assertEqual(ret, expected)
def test_output_yaml_namespaced_dict_wrapper(self):
'''
Tests the ability to dump a NamespacedDictWrapper instance, as used in
magic dunders like __grains__ and __pillar__
See https://github.com/saltstack/salt/issues/49269
'''
dumped_yaml = '\n'.join(self.run_call('grains.items --out=yaml'))
loaded_yaml = salt.utils.yaml.safe_load(dumped_yaml)
# We just want to check that the dumped YAML loades as a dict with a
# single top-level key, we don't care about the real contents.
assert isinstance(loaded_yaml, dict)
assert list(loaded_yaml) == ['local']
def test_output_unicodebad(self):
'''
Tests outputter reliability with utf8