2015-01-08 08:44:51 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-01-21 02:15:43 +00:00
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2015-01-08 08:44:51 +00:00
|
|
|
# Import Salt Testing libs
|
2017-03-31 12:42:52 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import (
|
2015-01-08 08:44:51 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt libs
|
2016-07-19 22:41:00 +00:00
|
|
|
import salt.ext.six as six
|
2015-01-08 08:44:51 +00:00
|
|
|
from salt.utils.odict import OrderedDict
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.pillar as pillarmod
|
2015-01-08 08:44:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
pillar_value_1 = dict(a=1, b='very secret')
|
|
|
|
|
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
class PillarModuleTestCase(TestCase, LoaderModuleMockMixin):
|
|
|
|
|
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {pillarmod: {}}
|
2015-01-08 08:44:51 +00:00
|
|
|
|
|
|
|
def test_obfuscate_inner_recursion(self):
|
|
|
|
self.assertEqual(
|
2015-01-08 16:41:49 +00:00
|
|
|
pillarmod._obfuscate_inner(dict(a=[1, 2],
|
|
|
|
b=dict(pwd='secret', deeper=('a', 1)))),
|
2015-01-08 08:44:51 +00:00
|
|
|
dict(a=['<int>', '<int>'],
|
|
|
|
b=dict(pwd='<str>', deeper=('<str>', '<int>')))
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_obfuscate_inner_more_types(self):
|
2015-01-08 16:41:49 +00:00
|
|
|
self.assertEqual(pillarmod._obfuscate_inner(OrderedDict([('key', 'value')])),
|
2015-01-08 08:44:51 +00:00
|
|
|
OrderedDict([('key', '<str>')]))
|
|
|
|
|
2015-01-08 16:41:49 +00:00
|
|
|
self.assertEqual(pillarmod._obfuscate_inner(set((1, 2))),
|
2015-01-08 08:44:51 +00:00
|
|
|
set(['<int>']))
|
|
|
|
|
2015-01-08 16:41:49 +00:00
|
|
|
self.assertEqual(pillarmod._obfuscate_inner((1, 2)),
|
2015-01-08 08:44:51 +00:00
|
|
|
('<int>', '<int>'))
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
@patch('salt.modules.pillar.items', MagicMock(return_value=pillar_value_1))
|
|
|
|
def test_obfuscate(self):
|
|
|
|
self.assertEqual(pillarmod.obfuscate(),
|
|
|
|
dict(a='<int>', b='<str>'))
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
@patch('salt.modules.pillar.items', MagicMock(return_value=pillar_value_1))
|
|
|
|
def test_ls(self):
|
2016-07-19 22:41:00 +00:00
|
|
|
if six.PY3:
|
|
|
|
self.assertCountEqual(pillarmod.ls(), ['a', 'b'])
|
|
|
|
else:
|
|
|
|
self.assertEqual(pillarmod.ls(), ['a', 'b'])
|
2015-01-08 08:44:51 +00:00
|
|
|
|
2017-04-06 17:22:50 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-01-05 22:12:57 +00:00
|
|
|
def test_pillar_get_default_merge(self):
|
2017-04-06 17:22:50 +00:00
|
|
|
defaults = {'int': 1,
|
|
|
|
'string': 'foo',
|
|
|
|
'list': ['foo'],
|
|
|
|
'dict': {'foo': 'bar', 'subkey': {'foo': 'bar'}}}
|
2017-01-05 22:12:57 +00:00
|
|
|
|
2017-04-11 18:16:15 +00:00
|
|
|
pillar_mock = {'int': 2,
|
|
|
|
'string': 'bar',
|
|
|
|
'list': ['bar', 'baz'],
|
|
|
|
'dict': {'baz': 'qux', 'subkey': {'baz': 'qux'}}}
|
2017-04-06 17:22:50 +00:00
|
|
|
|
|
|
|
# Test that we raise a KeyError when pillar_raise_on_missing is True
|
|
|
|
with patch.dict(pillarmod.__opts__, {'pillar_raise_on_missing': True}):
|
|
|
|
self.assertRaises(KeyError, pillarmod.get, 'missing')
|
|
|
|
# Test that we return an empty string when it is not
|
2017-04-11 18:16:15 +00:00
|
|
|
with patch.dict(pillarmod.__opts__, {}):
|
|
|
|
self.assertEqual(pillarmod.get('missing'), '')
|
|
|
|
|
|
|
|
with patch.dict(pillarmod.__pilar__, pillar_mock):
|
|
|
|
# Test with no default passed (it should be KeyError) and merge=True.
|
|
|
|
# The merge should be skipped and the value returned from __pillar__
|
|
|
|
# should be returned.
|
|
|
|
for item in pillarmod.__pillar__:
|
2017-04-06 17:22:50 +00:00
|
|
|
self.assertEqual(
|
2017-04-11 18:16:15 +00:00
|
|
|
pillarmod.get(item, merge=True),
|
2017-04-06 17:22:50 +00:00
|
|
|
pillarmod.__pillar__[item]
|
|
|
|
)
|
|
|
|
|
2017-04-11 18:16:15 +00:00
|
|
|
# Test merging when the type of the default value is not the same as
|
|
|
|
# what was returned. Merging should be skipped and the value returned
|
|
|
|
# from __pillar__ should be returned.
|
|
|
|
for default_type in defaults:
|
|
|
|
for data_type in ('dict', 'list'):
|
|
|
|
if default_type == data_type:
|
|
|
|
continue
|
|
|
|
self.assertEqual(
|
|
|
|
pillarmod.get(item, default=defaults[default_type], merge=True),
|
|
|
|
pillarmod.__pillar__[item]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test recursive dict merging
|
|
|
|
self.assertEqual(
|
|
|
|
pillarmod.get('dict', default=defaults['dict'], merge=True),
|
|
|
|
{'foo': 'bar', 'baz': 'qux', 'subkey': {'foo': 'bar', 'baz': 'qux'}}
|
|
|
|
)
|
2017-01-05 22:12:57 +00:00
|
|
|
|
2017-04-11 18:16:15 +00:00
|
|
|
# Test list merging
|
|
|
|
self.assertEqual(
|
|
|
|
pillarmod.get('list', default=defaults['list'], merge=True),
|
|
|
|
['foo', 'bar', 'baz']
|
|
|
|
)
|
2017-01-05 22:12:57 +00:00
|
|
|
|
2017-01-05 22:09:35 +00:00
|
|
|
def test_pillar_get_default_merge_regression_38558(self):
|
|
|
|
"""Test for pillar.get(key=..., default=..., merge=True)
|
|
|
|
|
|
|
|
Do not update the ``default`` value when using ``merge=True``.
|
|
|
|
|
|
|
|
See: https://github.com/saltstack/salt/issues/38558
|
|
|
|
"""
|
2017-03-31 12:42:52 +00:00
|
|
|
with patch.dict(pillarmod.__pillar__, {'l1': {'l2': {'l3': 42}}}):
|
2017-01-05 22:09:35 +00:00
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
res = pillarmod.get(key='l1')
|
|
|
|
self.assertEqual({'l2': {'l3': 42}}, res)
|
2017-01-05 22:09:35 +00:00
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
default = {'l2': {'l3': 43}}
|
2017-01-05 22:09:35 +00:00
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
res = pillarmod.get(key='l1', default=default)
|
|
|
|
self.assertEqual({'l2': {'l3': 42}}, res)
|
|
|
|
self.assertEqual({'l2': {'l3': 43}}, default)
|
2017-01-05 22:09:35 +00:00
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
res = pillarmod.get(key='l1', default=default, merge=True)
|
|
|
|
self.assertEqual({'l2': {'l3': 42}}, res)
|
|
|
|
self.assertEqual({'l2': {'l3': 43}}, default)
|
2017-01-05 22:09:35 +00:00
|
|
|
|
2017-02-01 22:07:31 +00:00
|
|
|
def test_pillar_get_default_merge_regression_39062(self):
|
|
|
|
'''
|
|
|
|
Confirm that we do not raise an exception if default is None and
|
|
|
|
merge=True.
|
|
|
|
|
|
|
|
See https://github.com/saltstack/salt/issues/39062 for more info.
|
|
|
|
'''
|
2017-03-31 12:42:52 +00:00
|
|
|
with patch.dict(pillarmod.__pillar__, {'foo': 'bar'}):
|
2017-02-01 22:07:31 +00:00
|
|
|
|
2017-03-31 12:42:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
pillarmod.get(key='foo', default=None, merge=True),
|
|
|
|
'bar',
|
|
|
|
)
|