test_ext_grains.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration.loader.ext_grains
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Test Salt's loader regarding external grains
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. import os
  10. import time
  11. # Import Salt Testing libs
  12. from tests.support.runtime import RUNTIME_VARS
  13. from tests.support.case import ModuleCase
  14. from tests.support.unit import skipIf
  15. # Import salt libs
  16. import salt.config
  17. import salt.loader
  18. class LoaderGrainsTest(ModuleCase):
  19. '''
  20. Test the loader standard behavior with external grains
  21. '''
  22. #def setUp(self):
  23. # self.opts = minion_config(None)
  24. # self.opts['disable_modules'] = ['pillar']
  25. # self.opts['grains'] = grains(self.opts)
  26. def test_grains_overwrite(self):
  27. # Force a grains sync
  28. self.run_function('saltutil.sync_grains')
  29. # To avoid a race condition on Windows, we need to make sure the
  30. # `test_custom_grain2.py` file is present in the _grains directory
  31. # before trying to get the grains. This test may execute before the
  32. # minion has finished syncing down the files it needs.
  33. module = os.path.join(RUNTIME_VARS.TMP, 'rootdir', 'cache', 'files',
  34. 'base', '_grains', 'test_custom_grain2.py')
  35. tries = 0
  36. while not os.path.exists(module):
  37. tries += 1
  38. if tries > 60:
  39. break
  40. time.sleep(1)
  41. grains = self.run_function('grains.items')
  42. # Check that custom grains are overwritten
  43. self.assertEqual({'k2': 'v2'}, grains['a_custom'])
  44. @skipIf(True, "needs a way to reload minion after config change")
  45. class LoaderGrainsMergeTest(ModuleCase):
  46. '''
  47. Test the loader deep merge behavior with external grains
  48. '''
  49. def setUp(self):
  50. # XXX: This seems like it should become a unit test instead
  51. self.opts = salt.config.minion_config(None)
  52. self.opts['grains_deep_merge'] = True
  53. self.assertTrue(self.opts['grains_deep_merge'])
  54. self.opts['disable_modules'] = ['pillar']
  55. __grains__ = salt.loader.grains(self.opts)
  56. def test_grains_merge(self):
  57. __grain__ = self.run_function('grains.item', ['a_custom'])
  58. # Check that the grain is present
  59. self.assertIn('a_custom', __grain__)
  60. # Check that the grains are merged
  61. self.assertEqual({'k1': 'v1', 'k2': 'v2'}, __grain__['a_custom'])