Skip to content

Commit

Permalink
Merge pull request #49408 from terminalmage/issue49269
Browse files Browse the repository at this point in the history
Allow our custom yaml dumper to NamespacedDictWrapper objects
  • Loading branch information
Mike Place authored Aug 29, 2018
2 parents 5746fc8 + d02ec34 commit 24faa5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions salt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,11 +1357,11 @@ def __prep_mod_opts(self, opts):
'''
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()):
Expand Down
9 changes: 9 additions & 0 deletions salt/utils/yamldumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import yaml
import collections
import salt.utils.context
from salt.utils.odict import OrderedDict

try:
Expand Down Expand Up @@ -54,6 +55,14 @@ def represent_ordereddict(dumper, data):
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
)

if HAS_IOFLO:
OrderedDumper.add_representer(odict, represent_ordereddict)
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/output/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import absolute_import
import os
import traceback
import yaml

# Import Salt Testing Libs
from tests.support.case import ShellCase
Expand Down Expand Up @@ -81,6 +82,20 @@ def test_output_yaml(self):
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 = yaml.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
Expand Down

0 comments on commit 24faa5e

Please sign in to comment.