Skip to content

Commit

Permalink
Merge pull request #55796 from srg91/fix-cached-osrelease_info-grain-…
Browse files Browse the repository at this point in the history
…type

Fix cached osrelease_info grain type
  • Loading branch information
dwoz authored Jan 13, 2020
2 parents cbfc3ce + 2fc84f4 commit 224fb88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion salt/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,17 @@ def grain_funcs(opts, proxy=None):
return ret


def _format_cached_grains(cached_grains):
"""
Returns cached grains with fixed types, like tuples.
"""
if cached_grains.get('osrelease_info'):
osrelease_info = cached_grains['osrelease_info']
if isinstance(osrelease_info, list):
cached_grains['osrelease_info'] = tuple(osrelease_info)
return cached_grains


def _load_cached_grains(opts, cfn):
'''
Returns the grains cached in cfn, or None if the cache is too old or is
Expand Down Expand Up @@ -720,7 +731,7 @@ def _load_cached_grains(opts, cfn):
log.debug('Cached grains are empty, cache might be corrupted. Refreshing.')
return None

return cached_grains
return _format_cached_grains(cached_grains)
except (IOError, OSError):
return None

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,3 +1353,31 @@ def test_load_source_file(self):
basename = os.path.basename(filename)
expected = 'lazyloadertest.py' if six.PY3 else 'lazyloadertest.pyc'
assert basename == expected, basename


class LoaderLoadCachedGrainsTest(TestCase):
'''
Test how the loader works with cached grains
'''

@classmethod
def setUpClass(cls):
cls.opts = salt.config.minion_config(None)
if not os.path.isdir(RUNTIME_VARS.TMP):
os.makedirs(RUNTIME_VARS.TMP)

def setUp(self):
self.cache_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
self.addCleanup(shutil.rmtree, self.cache_dir, ignore_errors=True)

self.opts['cachedir'] = self.cache_dir
self.opts['grains_cache'] = True
self.opts['grains'] = salt.loader.grains(self.opts)

def test_osrelease_info_has_correct_type(self):
'''
Make sure osrelease_info is tuple after caching
'''
grains = salt.loader.grains(self.opts)
osrelease_info = grains['osrelease_info']
assert isinstance(osrelease_info, tuple), osrelease_info

0 comments on commit 224fb88

Please sign in to comment.