2015-10-31 19:43:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
integration.loader.ext_grains
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test Salt's loader regarding external grains
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
2017-12-15 18:14:18 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2017-02-24 18:47:39 +00:00
|
|
|
import os
|
|
|
|
import time
|
2015-10-31 19:43:17 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
|
|
|
from tests.support.paths import TMP
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2015-10-31 19:43:17 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2017-04-03 16:04:09 +00:00
|
|
|
import salt.config
|
|
|
|
import salt.loader
|
2015-10-31 19:43:17 +00:00
|
|
|
|
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class LoaderGrainsTest(ModuleCase):
|
2015-10-31 19:43:17 +00:00
|
|
|
'''
|
|
|
|
Test the loader standard behavior with external grains
|
|
|
|
'''
|
|
|
|
|
2017-02-23 18:30:51 +00:00
|
|
|
#def setUp(self):
|
|
|
|
# self.opts = minion_config(None)
|
|
|
|
# self.opts['disable_modules'] = ['pillar']
|
|
|
|
# self.opts['grains'] = grains(self.opts)
|
2015-10-31 19:43:17 +00:00
|
|
|
|
|
|
|
def test_grains_overwrite(self):
|
2017-06-01 17:47:22 +00:00
|
|
|
# Force a grains sync
|
|
|
|
self.run_function('saltutil.sync_grains')
|
2017-02-24 18:47:39 +00:00
|
|
|
# To avoid a race condition on Windows, we need to make sure the
|
|
|
|
# `test_custom_grain2.py` file is present in the _grains directory
|
2017-03-01 20:53:32 +00:00
|
|
|
# before trying to get the grains. This test may execute before the
|
|
|
|
# minion has finished syncing down the files it needs.
|
2017-04-03 16:04:09 +00:00
|
|
|
module = os.path.join(TMP, 'rootdir', 'cache', 'files',
|
2017-02-24 18:47:39 +00:00
|
|
|
'base', '_grains', 'test_custom_grain2.py')
|
|
|
|
tries = 0
|
|
|
|
while not os.path.exists(module):
|
|
|
|
tries += 1
|
|
|
|
if tries > 60:
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
2015-10-31 19:43:17 +00:00
|
|
|
grains = self.run_function('grains.items')
|
|
|
|
|
|
|
|
# Check that custom grains are overwritten
|
|
|
|
self.assertEqual({'k2': 'v2'}, grains['a_custom'])
|
|
|
|
|
|
|
|
|
2015-11-01 19:36:48 +00:00
|
|
|
@skipIf(True, "needs a way to reload minion after config change")
|
2017-04-03 16:04:09 +00:00
|
|
|
class LoaderGrainsMergeTest(ModuleCase):
|
2015-11-01 19:36:48 +00:00
|
|
|
'''
|
|
|
|
Test the loader deep merge behavior with external grains
|
|
|
|
'''
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-04-03 16:04:09 +00:00
|
|
|
# XXX: This seems like it should become a unit test instead
|
|
|
|
self.opts = salt.config.minion_config(None)
|
2015-11-01 19:36:48 +00:00
|
|
|
self.opts['grains_deep_merge'] = True
|
|
|
|
self.assertTrue(self.opts['grains_deep_merge'])
|
|
|
|
self.opts['disable_modules'] = ['pillar']
|
2017-04-03 16:04:09 +00:00
|
|
|
__grains__ = salt.loader.grains(self.opts)
|
2015-11-01 19:36:48 +00:00
|
|
|
|
|
|
|
def test_grains_merge(self):
|
|
|
|
__grain__ = self.run_function('grains.item', ['a_custom'])
|
|
|
|
|
|
|
|
# Check that the grain is present
|
|
|
|
self.assertIn('a_custom', __grain__)
|
|
|
|
# Check that the grains are merged
|
|
|
|
self.assertEqual({'k1': 'v1', 'k2': 'v2'}, __grain__['a_custom'])
|