test_file_tree.py 6.1 KB

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