mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #50064 from rallytime/bp-50022
Back-port #50022 to 2018.3
This commit is contained in:
commit
f3fe4b67f7
2 changed files with 71 additions and 2 deletions
|
@ -334,7 +334,7 @@ def _check_key_type(key_str, key_type=None):
|
|||
value = __salt__['pillar.get'](key_str, None)
|
||||
if value is None:
|
||||
return None
|
||||
elif type(value) is not key_type and key_type is not None:
|
||||
elif key_type is not None and not isinstance(value, key_type):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
@ -415,7 +415,7 @@ def check_pillar(name,
|
|||
checks[int] = integer
|
||||
# those should be str:
|
||||
string = _if_str_then_list(string)
|
||||
checks[str] = string
|
||||
checks[six.string_types] = string
|
||||
# those should be list:
|
||||
listing = _if_str_then_list(listing)
|
||||
checks[list] = listing
|
||||
|
|
|
@ -19,6 +19,8 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
from salt.exceptions import SaltInvocationError
|
||||
import salt.states.test as test
|
||||
from salt.utils.odict import OrderedDict
|
||||
from salt.ext import six
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
|
@ -310,6 +312,48 @@ class TestTestCase(TestCase, LoaderModuleMockMixin):
|
|||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertEqual(test.check_pillar('salt', present='my_pillar'), ret)
|
||||
|
||||
def test_check_pillar_string(self):
|
||||
'''
|
||||
Test to ensure the check_pillar function
|
||||
works properly with the 'key_type' checks,
|
||||
using the string key_type.
|
||||
'''
|
||||
ret = {
|
||||
'name': 'salt',
|
||||
'changes': {},
|
||||
'result': True,
|
||||
'comment': ''
|
||||
}
|
||||
pillar_return = 'I am a pillar.'
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertEqual(test.check_pillar('salt', string='my_pillar'), ret)
|
||||
# With unicode (py2) or str (py3) strings
|
||||
pillar_return = six.text_type('I am a pillar.')
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertEqual(test.check_pillar('salt', string='my_pillar'), ret)
|
||||
# With a dict
|
||||
pillar_return = {'this': 'dictionary'}
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
|
||||
# With a list
|
||||
pillar_return = ['I am a pillar.']
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
|
||||
# With a boolean
|
||||
pillar_return = True
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
|
||||
# With an int
|
||||
pillar_return = 1
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', string='my_pillar')['result'])
|
||||
|
||||
def test_check_pillar_dictionary(self):
|
||||
'''
|
||||
Test to ensure the check_pillar function
|
||||
|
@ -326,3 +370,28 @@ class TestTestCase(TestCase, LoaderModuleMockMixin):
|
|||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertEqual(test.check_pillar('salt', dictionary='my_pillar'), ret)
|
||||
# With an ordered dict
|
||||
pillar_return = OrderedDict({'this': 'dictionary'})
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertEqual(test.check_pillar('salt', dictionary='my_pillar'), ret)
|
||||
# With a string
|
||||
pillar_return = 'I am a pillar.'
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
|
||||
# With a list
|
||||
pillar_return = ['I am a pillar.']
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
|
||||
# With a boolean
|
||||
pillar_return = True
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
|
||||
# With an int
|
||||
pillar_return = 1
|
||||
pillar_mock = MagicMock(return_value=pillar_return)
|
||||
with patch.dict(test.__salt__, {'pillar.get': pillar_mock}):
|
||||
self.assertFalse(test.check_pillar('salt', dictionary='my_pillar')['result'])
|
||||
|
|
Loading…
Add table
Reference in a new issue