2016-04-07 16:27:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
Unit tests for salt.utils.yamlloader.SaltYamlSafeLoader
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2017-08-08 21:33:42 +00:00
|
|
|
import textwrap
|
2016-04-07 16:27:20 +00:00
|
|
|
|
|
|
|
# Import Salt Libs
|
|
|
|
from yaml.constructor import ConstructorError
|
|
|
|
from salt.utils.yamlloader import SaltYamlSafeLoader
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
2016-04-07 16:27:20 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON, mock_open
|
2016-04-07 16:27:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class YamlLoaderTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
TestCase for salt.utils.yamlloader module
|
|
|
|
'''
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _render_yaml(data):
|
|
|
|
'''
|
|
|
|
Takes a YAML string, puts it into a mock file, passes that to the YAML
|
|
|
|
SaltYamlSafeLoader and then returns the rendered/parsed YAML data
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with patch('salt.utils.files.fopen', mock_open(read_data=data)) as mocked_file:
|
|
|
|
with salt.utils.files.fopen(mocked_file) as mocked_stream:
|
2016-04-07 16:27:20 +00:00
|
|
|
return SaltYamlSafeLoader(mocked_stream).get_data()
|
|
|
|
|
|
|
|
def test_yaml_basics(self):
|
|
|
|
'''
|
|
|
|
Test parsing an ordinary path
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1:
|
|
|
|
- alpha
|
|
|
|
- beta''')),
|
2016-04-07 16:27:20 +00:00
|
|
|
{'p1': ['alpha', 'beta']}
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_yaml_merge(self):
|
|
|
|
'''
|
|
|
|
Test YAML anchors
|
|
|
|
'''
|
|
|
|
# Simple merge test
|
|
|
|
self.assertEqual(
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1: &p1
|
|
|
|
v1: alpha
|
|
|
|
p2:
|
|
|
|
<<: *p1
|
|
|
|
v2: beta''')),
|
2016-04-07 16:27:20 +00:00
|
|
|
{'p1': {'v1': 'alpha'}, 'p2': {'v1': 'alpha', 'v2': 'beta'}}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that keys/nodes are overwritten
|
|
|
|
self.assertEqual(
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1: &p1
|
|
|
|
v1: alpha
|
|
|
|
p2:
|
|
|
|
<<: *p1
|
|
|
|
v1: new_alpha''')),
|
2016-04-07 16:27:20 +00:00
|
|
|
{'p1': {'v1': 'alpha'}, 'p2': {'v1': 'new_alpha'}}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test merging of lists
|
|
|
|
self.assertEqual(
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1: &p1
|
|
|
|
v1: &v1
|
|
|
|
- t1
|
|
|
|
- t2
|
|
|
|
p2:
|
|
|
|
v2: *v1''')),
|
2016-04-07 16:27:20 +00:00
|
|
|
{"p2": {"v2": ["t1", "t2"]}, "p1": {"v1": ["t1", "t2"]}}
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_yaml_duplicates(self):
|
|
|
|
'''
|
|
|
|
Test that duplicates still throw an error
|
|
|
|
'''
|
|
|
|
with self.assertRaises(ConstructorError):
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1: alpha
|
|
|
|
p1: beta'''))
|
2016-04-07 16:27:20 +00:00
|
|
|
|
|
|
|
with self.assertRaises(ConstructorError):
|
2017-08-08 21:33:42 +00:00
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
p1: &p1
|
|
|
|
v1: alpha
|
|
|
|
p2:
|
|
|
|
<<: *p1
|
|
|
|
v2: beta
|
|
|
|
v2: betabeta'''))
|
2017-08-08 21:48:43 +00:00
|
|
|
|
|
|
|
def test_yaml_with_unicode_literals(self):
|
|
|
|
'''
|
|
|
|
Test proper loading of unicode literals
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
|
|
|
self._render_yaml(textwrap.dedent('''\
|
|
|
|
foo:
|
|
|
|
a: Д
|
|
|
|
b: {'a': u'\\u0414'}''')),
|
|
|
|
{'foo': {'a': u'\u0414', 'b': {'a': u'\u0414'}}}
|
|
|
|
)
|