test_ext_grains.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration.loader.ext_grains
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Test Salt's loader regarding external grains
  6. """
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import os
  9. import time
  10. import pytest
  11. import salt.config
  12. import salt.loader
  13. from tests.support.case import ModuleCase
  14. from tests.support.helpers import PYTEST_MIGRATION_SKIP_REASON
  15. from tests.support.runtests import RUNTIME_VARS
  16. from tests.support.unit import skipIf
  17. @pytest.mark.windows_whitelisted
  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. @pytest.mark.skip_on_windows(reason=PYTEST_MIGRATION_SKIP_REASON)
  27. @pytest.mark.slow_test(seconds=120) # Test takes >60 and <=120 seconds
  28. def test_grains_overwrite(self):
  29. # Force a grains sync
  30. self.run_function("saltutil.sync_grains")
  31. # To avoid a race condition on Windows, we need to make sure the
  32. # `test_custom_grain2.py` file is present in the _grains directory
  33. # before trying to get the grains. This test may execute before the
  34. # minion has finished syncing down the files it needs.
  35. module = os.path.join(
  36. RUNTIME_VARS.RUNTIME_CONFIGS["minion"]["cachedir"],
  37. "files",
  38. "base",
  39. "_grains",
  40. "custom_grain2.py",
  41. )
  42. tries = 0
  43. while not os.path.exists(module):
  44. tries += 1
  45. if tries > 60:
  46. pytest.fail(
  47. "Failed to found custom grains module in cache path {}".format(
  48. module
  49. )
  50. )
  51. break
  52. time.sleep(1)
  53. grains = self.run_function("grains.items")
  54. # Check that custom grains are overwritten
  55. self.assertEqual({"k2": "v2"}, grains["a_custom"])
  56. @skipIf(True, "needs a way to reload minion after config change")
  57. @pytest.mark.windows_whitelisted
  58. class LoaderGrainsMergeTest(ModuleCase):
  59. """
  60. Test the loader deep merge behavior with external grains
  61. """
  62. def setUp(self):
  63. # XXX: This seems like it should become a unit test instead
  64. self.opts = salt.config.minion_config(None)
  65. self.opts["grains_deep_merge"] = True
  66. self.assertTrue(self.opts["grains_deep_merge"])
  67. self.opts["disable_modules"] = ["pillar"]
  68. __grains__ = salt.loader.grains(self.opts)
  69. def test_grains_merge(self):
  70. __grain__ = self.run_function("grains.item", ["a_custom"])
  71. # Check that the grain is present
  72. self.assertIn("a_custom", __grain__)
  73. # Check that the grains are merged
  74. self.assertEqual({"k1": "v1", "k2": "v2"}, __grain__["a_custom"])