PageRenderTime 1732ms CodeModel.GetById 31ms RepoModel.GetById 10ms app.codeStats 0ms

/tests/integration/fileserver/roots_test.py

https://gitlab.com/ricardo.hernandez/salt
Python | 181 lines | 140 code | 24 blank | 17 comment | 12 complexity | f65e9e9619dab5c34071cea110c19196 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Mike Place <mp@saltstack.com>`
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. import os
  8. # Import Salt Testing libs
  9. from salttesting import skipIf
  10. from salttesting.helpers import ensure_in_syspath
  11. from salttesting.mock import patch, NO_MOCK, NO_MOCK_REASON
  12. ensure_in_syspath('../..')
  13. # Import salt libs
  14. import integration
  15. from salt.fileserver import roots
  16. from salt import fileclient
  17. roots.__opts__ = {}
  18. @skipIf(NO_MOCK, NO_MOCK_REASON)
  19. class RootsTest(integration.ModuleCase):
  20. def setUp(self):
  21. if integration.TMP_STATE_TREE not in self.master_opts['file_roots']['base']:
  22. # We need to setup the file roots
  23. self.master_opts['file_roots']['base'] = [os.path.join(integration.FILES, 'file', 'base')]
  24. def test_file_list(self):
  25. with patch.dict(roots.__opts__, {'cachedir': self.master_opts['cachedir'],
  26. 'file_roots': self.master_opts['file_roots'],
  27. 'fileserver_ignoresymlinks': False,
  28. 'fileserver_followsymlinks': False,
  29. 'file_ignore_regex': False,
  30. 'file_ignore_glob': False}):
  31. ret = roots.file_list({'saltenv': 'base'})
  32. self.assertIn('testfile', ret)
  33. def test_find_file(self):
  34. with patch.dict(roots.__opts__, {'file_roots': self.master_opts['file_roots'],
  35. 'fileserver_ignoresymlinks': False,
  36. 'fileserver_followsymlinks': False,
  37. 'file_ignore_regex': False,
  38. 'file_ignore_glob': False}):
  39. ret = roots.find_file('testfile')
  40. self.assertEqual('testfile', ret['rel'])
  41. full_path_to_file = os.path.join(integration.FILES, 'file', 'base', 'testfile')
  42. self.assertEqual(full_path_to_file, ret['path'])
  43. def test_serve_file(self):
  44. with patch.dict(roots.__opts__, {'file_roots': self.master_opts['file_roots'],
  45. 'fileserver_ignoresymlinks': False,
  46. 'fileserver_followsymlinks': False,
  47. 'file_ignore_regex': False,
  48. 'file_ignore_glob': False,
  49. 'file_buffer_size': 262144}):
  50. load = {'saltenv': 'base',
  51. 'path': os.path.join(integration.FILES, 'file', 'base', 'testfile'),
  52. 'loc': 0
  53. }
  54. fnd = {'path': os.path.join(integration.FILES, 'file', 'base', 'testfile'),
  55. 'rel': 'testfile'}
  56. ret = roots.serve_file(load, fnd)
  57. self.assertDictEqual(
  58. ret,
  59. {'data': 'Scene 24\n\n \n OLD MAN: Ah, hee he he ha!\n '
  60. 'ARTHUR: And this enchanter of whom you speak, he '
  61. 'has seen the grail?\n OLD MAN: Ha ha he he he '
  62. 'he!\n ARTHUR: Where does he live? Old man, where '
  63. 'does he live?\n OLD MAN: He knows of a cave, a '
  64. 'cave which no man has entered.\n ARTHUR: And the '
  65. 'Grail... The Grail is there?\n OLD MAN: Very much '
  66. 'danger, for beyond the cave lies the Gorge\n '
  67. 'of Eternal Peril, which no man has ever crossed.\n '
  68. 'ARTHUR: But the Grail! Where is the Grail!?\n '
  69. 'OLD MAN: Seek you the Bridge of Death.\n ARTHUR: '
  70. 'The Bridge of Death, which leads to the Grail?\n '
  71. 'OLD MAN: Hee hee ha ha!\n\n',
  72. 'dest': 'testfile'})
  73. @skipIf(True, "Update test not yet implemented")
  74. def test_update(self):
  75. pass
  76. def test_file_hash(self):
  77. with patch.dict(roots.__opts__, {'file_roots': self.master_opts['file_roots'],
  78. 'fileserver_ignoresymlinks': False,
  79. 'fileserver_followsymlinks': False,
  80. 'file_ignore_regex': False,
  81. 'file_ignore_glob': False,
  82. 'hash_type': self.master_opts['hash_type'],
  83. 'cachedir': self.master_opts['cachedir']}):
  84. load = {
  85. 'saltenv': 'base',
  86. 'path': os.path.join(integration.FILES, 'file', 'base', 'testfile'),
  87. }
  88. fnd = {
  89. 'path': os.path.join(integration.FILES, 'file', 'base', 'testfile'),
  90. 'rel': 'testfile'
  91. }
  92. ret = roots.file_hash(load, fnd)
  93. self.assertDictEqual(ret, {'hsum': '98aa509006628302ce38ce521a7f805f', 'hash_type': 'md5'})
  94. def test_file_list_emptydirs(self):
  95. if integration.TMP_STATE_TREE not in self.master_opts['file_roots']['base']:
  96. self.skipTest('This test fails when using tests/runtests.py. salt-runtests will be available soon.')
  97. empty_dir = os.path.join(integration.TMP_STATE_TREE, 'empty_dir')
  98. if not os.path.isdir(empty_dir):
  99. # There's no use creating the empty-directory ourselves at this
  100. # point, the minions have already synced, it wouldn't get pushed to
  101. # them
  102. self.skipTest('This test fails when using tests/runtests.py. salt-runtests will be available soon.')
  103. with patch.dict(roots.__opts__, {'cachedir': self.master_opts['cachedir'],
  104. 'file_roots': self.master_opts['file_roots'],
  105. 'fileserver_ignoresymlinks': False,
  106. 'fileserver_followsymlinks': False,
  107. 'file_ignore_regex': False,
  108. 'file_ignore_glob': False}):
  109. ret = roots.file_list_emptydirs({'saltenv': 'base'})
  110. self.assertIn('empty_dir', ret)
  111. def test_dir_list(self):
  112. empty_dir = os.path.join(integration.TMP_STATE_TREE, 'empty_dir')
  113. if integration.TMP_STATE_TREE not in self.master_opts['file_roots']['base']:
  114. self.skipTest('This test fails when using tests/runtests.py. salt-runtests will be available soon.')
  115. empty_dir = os.path.join(integration.TMP_STATE_TREE, 'empty_dir')
  116. if not os.path.isdir(empty_dir):
  117. # There's no use creating the empty-directory ourselves at this
  118. # point, the minions have already synced, it wouldn't get pushed to
  119. # them
  120. self.skipTest('This test fails when using tests/runtests.py. salt-runtests will be available soon.')
  121. with patch.dict(roots.__opts__, {'cachedir': self.master_opts['cachedir'],
  122. 'file_roots': self.master_opts['file_roots'],
  123. 'fileserver_ignoresymlinks': False,
  124. 'fileserver_followsymlinks': False,
  125. 'file_ignore_regex': False,
  126. 'file_ignore_glob': False}):
  127. ret = roots.dir_list({'saltenv': 'base'})
  128. self.assertIn('empty_dir', ret)
  129. def test_symlink_list(self):
  130. with patch.dict(roots.__opts__, {'file_roots': self.master_opts['file_roots'],
  131. 'fileserver_ignoresymlinks': False,
  132. 'fileserver_followsymlinks': False,
  133. 'file_ignore_regex': False,
  134. 'file_ignore_glob': False}):
  135. ret = roots.symlink_list({'saltenv': 'base'})
  136. self.assertDictEqual(ret, {'dest_sym': 'source_sym'})
  137. class RootsLimitTraversalTest(integration.ModuleCase):
  138. # @destructiveTest
  139. def test_limit_traversal(self):
  140. '''
  141. 1) Set up a deep directory structure
  142. 2) Enable the configuration option for 'limit_directory_traversal'
  143. 3) Ensure that we can find SLS files in a directory so long as there is an SLS file in a directory above.
  144. 4) Ensure that we cannot find an SLS file in a directory that does not have an SLS file in a directory above.
  145. '''
  146. file_client_opts = self.get_config('master', from_scratch=True)
  147. file_client_opts['fileserver_limit_traversal'] = True
  148. ret = fileclient.Client(file_client_opts).list_states('base')
  149. self.assertIn('test_deep.test', ret)
  150. self.assertIn('test_deep.a.test', ret)
  151. self.assertNotIn('test_deep.b.2.test', ret)
  152. if __name__ == '__main__':
  153. from integration import run_tests
  154. run_tests(RootsTest, RootsLimitTraversalTest)