test_file_tree.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """
  2. test for pillar file_tree.py
  3. """
  4. import os
  5. import shutil
  6. import tempfile
  7. import salt.pillar.file_tree as file_tree
  8. import salt.utils.files
  9. import salt.utils.stringutils
  10. from tests.support.helpers import TstSuiteLoggingHandler
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.unit import TestCase
  15. MINION_ID = "test-host"
  16. NODEGROUP_PATH = os.path.join("nodegroups", "test-group", "files")
  17. HOST_PATH = os.path.join("hosts", MINION_ID, "files")
  18. BASE_PILLAR_CONTENT = {"files": {"hostfile": b"base", "groupfile": b"base"}}
  19. DEV_PILLAR_CONTENT = {
  20. "files": {
  21. "hostfile": b"base",
  22. "groupfile": b"dev2",
  23. "hostfile1": b"dev1",
  24. "groupfile1": b"dev1",
  25. "hostfile2": b"dev2",
  26. }
  27. }
  28. PARENT_PILLAR_CONTENT = {
  29. "files": {"hostfile": b"base", "groupfile": b"base", "hostfile2": b"dev2"}
  30. }
  31. FILE_DATA = {
  32. os.path.join("base", HOST_PATH, "hostfile"): "base",
  33. os.path.join("dev1", HOST_PATH, "hostfile1"): "dev1",
  34. os.path.join("dev2", HOST_PATH, "hostfile2"): "dev2",
  35. os.path.join("base", NODEGROUP_PATH, "groupfile"): "base",
  36. os.path.join("dev1", NODEGROUP_PATH, "groupfile1"): "dev1",
  37. os.path.join("dev2", NODEGROUP_PATH, "groupfile"): "dev2", # test merging
  38. }
  39. _CHECK_MINIONS_RETURN = {"minions": [MINION_ID], "missing": []}
  40. class FileTreePillarTestCase(TestCase, LoaderModuleMockMixin):
  41. "test file_tree pillar"
  42. maxDiff = None
  43. def setup_loader_modules(self):
  44. self.tmpdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  45. self.addCleanup(shutil.rmtree, self.tmpdir)
  46. cachedir = os.path.join(self.tmpdir, "cachedir")
  47. os.makedirs(os.path.join(cachedir, "file_tree"))
  48. self.pillar_path = self._create_pillar_files()
  49. return {
  50. file_tree: {
  51. "__opts__": {
  52. "cachedir": cachedir,
  53. "pillar_roots": {
  54. "base": [os.path.join(self.pillar_path, "base")],
  55. "dev": [
  56. os.path.join(self.pillar_path, "base"),
  57. os.path.join(self.pillar_path, "dev1"),
  58. os.path.join(self.pillar_path, "dev2"),
  59. ],
  60. "parent": [
  61. os.path.join(self.pillar_path, "base", "sub1"),
  62. os.path.join(self.pillar_path, "dev2", "sub"),
  63. os.path.join(self.pillar_path, "base", "sub2"),
  64. ],
  65. },
  66. "pillarenv": "base",
  67. "nodegroups": {"test-group": [MINION_ID]},
  68. "optimization_order": [0, 1, 2],
  69. "file_buffer_size": 262144,
  70. "file_roots": {"base": "", "dev": "", "parent": ""},
  71. "extension_modules": "",
  72. "renderer": "yaml_jinja",
  73. "renderer_blacklist": [],
  74. "renderer_whitelist": [],
  75. }
  76. }
  77. }
  78. def _create_pillar_files(self):
  79. "create files in tempdir"
  80. pillar_path = os.path.join(self.tmpdir, "file_tree")
  81. for filename in FILE_DATA:
  82. filepath = os.path.join(pillar_path, filename)
  83. os.makedirs(os.path.dirname(filepath))
  84. with salt.utils.files.fopen(filepath, "w") as data_file:
  85. data_file.write(salt.utils.stringutils.to_str(FILE_DATA[filename]))
  86. return pillar_path
  87. def test_absolute_path(self):
  88. "check file tree is imported correctly with an absolute path"
  89. absolute_path = os.path.join(self.pillar_path, "base")
  90. with patch(
  91. "salt.utils.minions.CkMinions.check_minions",
  92. MagicMock(return_value=_CHECK_MINIONS_RETURN),
  93. ):
  94. mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  95. self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  96. with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  97. mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  98. self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  99. def test_relative_path(self):
  100. "check file tree is imported correctly with a relative path"
  101. with patch(
  102. "salt.utils.minions.CkMinions.check_minions",
  103. MagicMock(return_value=_CHECK_MINIONS_RETURN),
  104. ):
  105. mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  106. self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  107. with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  108. mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  109. self.assertEqual(DEV_PILLAR_CONTENT, mypillar)
  110. def test_parent_path(self):
  111. "check if file tree is merged correctly with a .. path"
  112. with patch(
  113. "salt.utils.minions.CkMinions.check_minions",
  114. MagicMock(return_value=_CHECK_MINIONS_RETURN),
  115. ):
  116. with patch.dict(file_tree.__opts__, {"pillarenv": "parent"}):
  117. mypillar = file_tree.ext_pillar(MINION_ID, None, "..")
  118. self.assertEqual(PARENT_PILLAR_CONTENT, mypillar)
  119. def test_no_pillarenv(self):
  120. "confirm that file_tree yells when pillarenv is missing for a relative path"
  121. with patch(
  122. "salt.utils.minions.CkMinions.check_minions",
  123. MagicMock(return_value=_CHECK_MINIONS_RETURN),
  124. ):
  125. with patch.dict(file_tree.__opts__, {"pillarenv": None}):
  126. with TstSuiteLoggingHandler() as handler:
  127. mypillar = file_tree.ext_pillar(MINION_ID, None, ".")
  128. self.assertEqual({}, mypillar)
  129. for message in handler.messages:
  130. if (
  131. message.startswith("ERROR:")
  132. and "pillarenv is not set" in message
  133. ):
  134. break
  135. else:
  136. raise AssertionError("Did not find error message")
  137. def test_file_tree_bytes(self):
  138. """
  139. test file_tree pillar returns bytes
  140. """
  141. absolute_path = os.path.join(self.pillar_path, "base")
  142. with patch(
  143. "salt.utils.minions.CkMinions.check_minions",
  144. MagicMock(return_value=_CHECK_MINIONS_RETURN),
  145. ):
  146. mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  147. self.assertEqual(BASE_PILLAR_CONTENT, mypillar)
  148. with patch.dict(file_tree.__opts__, {"pillarenv": "dev"}):
  149. mypillar = file_tree.ext_pillar(MINION_ID, None, absolute_path)
  150. self.assertEqual(mypillar["files"]["groupfile"], b"base")