Merge pull request #39852 from rallytime/bp-39651

Back-port #39651 to 2016.11
This commit is contained in:
Nicole Thomas 2017-03-06 14:18:33 -07:00 committed by GitHub
commit 8ecc719f90
5 changed files with 15 additions and 7 deletions

View file

@ -143,8 +143,8 @@ class Cache(object):
added by the driver itself.
:return:
Return a python object fetched from the cache or None if the given
path or key not found.
Return a python object fetched from the cache or an empty dict if
the given path or key not found.
:raises SaltCacheError:
Raises an exception if cache driver detected an error accessing data

View file

@ -104,7 +104,7 @@ def fetch(bank, key):
try:
_, value = api.kv.get(c_key)
if value is None:
return value
return {}
return __context__['serial'].loads(value['Value'])
except Exception as exc:
raise SaltCacheError(

View file

@ -61,7 +61,7 @@ def fetch(bank, key, cachedir):
key_file = os.path.join(cachedir, os.path.normpath(bank), '{0}.p'.format(key))
if not os.path.isfile(key_file):
log.debug('Cache file "%s" does not exist', key_file)
return None
return {}
try:
with salt.utils.fopen(key_file, 'rb') as fh_:
return __context__['serial'].load(fh_)

View file

@ -134,6 +134,14 @@ class MasterPillarUtil(object):
if not salt.utils.verify.valid_id(self.opts, minion_id):
continue
mdata = self.cache.fetch('minions/{0}'.format(minion_id), 'data')
if not isinstance(mdata, dict):
log.warning(
'cache.fetch should always return a dict. ReturnedType: {0}, MinionId: {1}'.format(
type(mdata).__name__,
minion_id
)
)
continue
if 'grains' in mdata:
grains[minion_id] = mdata['grains']
if 'pillar' in mdata:

View file

@ -100,10 +100,10 @@ class LocalFSTest(TestCase):
@patch('os.path.isfile', MagicMock(return_value=False))
def test_fetch_return_when_cache_file_does_not_exist(self):
'''
Tests that the fetch function returns None when the cache key file doesn't
exist.
Tests that the fetch function returns an empty dic when the cache key file
doesn't exist.
'''
self.assertIsNone(localfs.fetch(bank='', key='', cachedir=''))
self.assertEqual(localfs.fetch(bank='', key='', cachedir=''), {})
@patch('os.path.isfile', MagicMock(return_value=True))
@patch('salt.utils.fopen', MagicMock(side_effect=IOError))