test_fileserver.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the fileserver runner
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import contextlib
  7. import pytest
  8. import salt.utils.platform
  9. from tests.support.case import ShellCase
  10. from tests.support.helpers import slowTest
  11. from tests.support.unit import skipIf
  12. @pytest.mark.windows_whitelisted
  13. class FileserverTest(ShellCase):
  14. """
  15. Test the fileserver runner
  16. """
  17. @slowTest
  18. def test_dir_list(self):
  19. """
  20. fileserver.dir_list
  21. """
  22. ret = self.run_run_plus(fun="fileserver.dir_list")
  23. self.assertIsInstance(ret["return"], list)
  24. self.assertTrue("_modules" in ret["return"])
  25. # Backend submitted as a string
  26. ret = self.run_run_plus(fun="fileserver.dir_list", backend="roots")
  27. self.assertIsInstance(ret["return"], list)
  28. self.assertTrue("_modules" in ret["return"])
  29. # Backend submitted as a list
  30. ret = self.run_run_plus(fun="fileserver.dir_list", backend=["roots"])
  31. self.assertIsInstance(ret["return"], list)
  32. self.assertTrue("_modules" in ret["return"])
  33. @slowTest
  34. def test_empty_dir_list(self):
  35. """
  36. fileserver.empty_dir_list
  37. """
  38. ret = self.run_run_plus(fun="fileserver.empty_dir_list")
  39. self.assertIsInstance(ret["return"], list)
  40. self.assertEqual(ret["return"], [])
  41. # Backend submitted as a string
  42. ret = self.run_run_plus(fun="fileserver.empty_dir_list", backend="roots")
  43. self.assertIsInstance(ret["return"], list)
  44. self.assertEqual(ret["return"], [])
  45. # Backend submitted as a list
  46. ret = self.run_run_plus(fun="fileserver.empty_dir_list", backend=["roots"])
  47. self.assertIsInstance(ret["return"], list)
  48. self.assertEqual(ret["return"], [])
  49. @slowTest
  50. def test_envs(self):
  51. """
  52. fileserver.envs
  53. """
  54. ret = self.run_run_plus(fun="fileserver.envs")
  55. self.assertIsInstance(ret["return"], list)
  56. # Backend submitted as a string
  57. ret = self.run_run_plus(fun="fileserver.envs", backend="roots")
  58. self.assertIsInstance(ret["return"], list)
  59. # Backend submitted as a list
  60. ret = self.run_run_plus(fun="fileserver.envs", backend=["roots"])
  61. self.assertIsInstance(ret["return"], list)
  62. @slowTest
  63. def test_clear_file_list_cache(self):
  64. """
  65. fileserver.clear_file_list_cache
  66. If this test fails, then something may have changed in the test suite
  67. and we may have more than just "roots" configured in the fileserver
  68. backends. This assert will need to be updated accordingly.
  69. """
  70. saltenvs = sorted(self.run_run_plus(fun="fileserver.envs")["return"])
  71. @contextlib.contextmanager
  72. def gen_cache():
  73. """
  74. Create file_list cache so we have something to clear
  75. """
  76. for saltenv in saltenvs:
  77. self.run_run_plus(fun="fileserver.file_list", saltenv=saltenv)
  78. yield
  79. # Test with no arguments
  80. with gen_cache():
  81. ret = self.run_run_plus(fun="fileserver.clear_file_list_cache")
  82. ret["return"]["roots"].sort()
  83. self.assertEqual(ret["return"], {"roots": saltenvs})
  84. # Test with backend passed as string
  85. with gen_cache():
  86. ret = self.run_run_plus(
  87. fun="fileserver.clear_file_list_cache", backend="roots"
  88. )
  89. ret["return"]["roots"].sort()
  90. self.assertEqual(ret["return"], {"roots": saltenvs})
  91. # Test with backend passed as list
  92. with gen_cache():
  93. ret = self.run_run_plus(
  94. fun="fileserver.clear_file_list_cache", backend=["roots"]
  95. )
  96. ret["return"]["roots"].sort()
  97. self.assertEqual(ret["return"], {"roots": saltenvs})
  98. # Test with backend passed as string, but with a nonsense backend
  99. with gen_cache():
  100. ret = self.run_run_plus(
  101. fun="fileserver.clear_file_list_cache", backend="notarealbackend"
  102. )
  103. self.assertEqual(ret["return"], {})
  104. # Test with saltenv passed as string
  105. with gen_cache():
  106. ret = self.run_run_plus(
  107. fun="fileserver.clear_file_list_cache", saltenv="base"
  108. )
  109. self.assertEqual(ret["return"], {"roots": ["base"]})
  110. # Test with saltenv passed as list
  111. with gen_cache():
  112. ret = self.run_run_plus(
  113. fun="fileserver.clear_file_list_cache", saltenv=["base"]
  114. )
  115. self.assertEqual(ret["return"], {"roots": ["base"]})
  116. # Test with saltenv passed as string, but with a nonsense saltenv
  117. with gen_cache():
  118. ret = self.run_run_plus(
  119. fun="fileserver.clear_file_list_cache", saltenv="notarealsaltenv"
  120. )
  121. self.assertEqual(ret["return"], {})
  122. # Test with both backend and saltenv passed
  123. with gen_cache():
  124. ret = self.run_run_plus(
  125. fun="fileserver.clear_file_list_cache", backend="roots", saltenv="base"
  126. )
  127. self.assertEqual(ret["return"], {"roots": ["base"]})
  128. @slowTest
  129. def test_file_list(self):
  130. """
  131. fileserver.file_list
  132. """
  133. ret = self.run_run_plus(fun="fileserver.file_list")
  134. self.assertIsInstance(ret["return"], list)
  135. self.assertTrue("grail/scene33" in ret["return"])
  136. # Backend submitted as a string
  137. ret = self.run_run_plus(fun="fileserver.file_list", backend="roots")
  138. self.assertIsInstance(ret["return"], list)
  139. self.assertTrue("grail/scene33" in ret["return"])
  140. # Backend submitted as a list
  141. ret = self.run_run_plus(fun="fileserver.file_list", backend=["roots"])
  142. self.assertIsInstance(ret["return"], list)
  143. self.assertTrue("grail/scene33" in ret["return"])
  144. # Git doesn't handle symlinks in Windows. See the thread below:
  145. # http://stackoverflow.com/questions/5917249/git-symlinks-in-windows
  146. @skipIf(
  147. salt.utils.platform.is_windows(),
  148. "Git for Windows does not preserve symbolic links when cloning",
  149. )
  150. @slowTest
  151. def test_symlink_list(self):
  152. """
  153. fileserver.symlink_list
  154. """
  155. ret = self.run_run_plus(fun="fileserver.symlink_list")
  156. self.assertIsInstance(ret["return"], dict)
  157. self.assertTrue("dest_sym" in ret["return"])
  158. # Backend submitted as a string
  159. ret = self.run_run_plus(fun="fileserver.symlink_list", backend="roots")
  160. self.assertIsInstance(ret["return"], dict)
  161. self.assertTrue("dest_sym" in ret["return"])
  162. # Backend submitted as a list
  163. ret = self.run_run_plus(fun="fileserver.symlink_list", backend=["roots"])
  164. self.assertIsInstance(ret["return"], dict)
  165. self.assertTrue("dest_sym" in ret["return"])
  166. @slowTest
  167. def test_update(self):
  168. """
  169. fileserver.update
  170. """
  171. ret = self.run_run_plus(fun="fileserver.update")
  172. self.assertTrue(ret["return"])
  173. # Backend submitted as a string
  174. ret = self.run_run_plus(fun="fileserver.update", backend="roots")
  175. self.assertTrue(ret["return"])
  176. # Backend submitted as a list
  177. ret = self.run_run_plus(fun="fileserver.update", backend=["roots"])
  178. self.assertTrue(ret["return"])