Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cached osrelease_info grain type #55796

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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