test_file_tree.py 6.0 KB

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