1
0

test_ext_grains.py 2.7 KB

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