1
0

test_file_tree.py 6.2 KB

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