test_fileserver.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for the fileserver runner
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt libs
  9. import salt.loader
  10. import salt.runners.fileserver as fileserver
  11. import salt.utils.files
  12. # Import testing libs
  13. from tests.support.helpers import with_tempdir
  14. from tests.support.mixins import LoaderModuleMockMixin
  15. from tests.support.mock import MagicMock, patch
  16. from tests.support.unit import TestCase
  17. class DummyFS(object):
  18. """
  19. Dummy object to provide the attributes needed to run unit tests
  20. """
  21. def __init__(self, backends):
  22. self.backends = backends
  23. def keys(self):
  24. return ["{0}.envs".format(x) for x in self.backends]
  25. class FileserverTest(TestCase, LoaderModuleMockMixin):
  26. """
  27. Validate the cache runner
  28. """
  29. def setup_loader_modules(self):
  30. return {fileserver: {"__opts__": {"extension_modules": ""}}}
  31. def _make_file_lists_cache(self, cachedir, backends):
  32. """
  33. Create some dummy files to represent file list caches, as well as other
  34. files that aren't file list caches, so that we can confirm that *only*
  35. the cache files are touched. Create a dir for each configured backend,
  36. as well as for the roots backend (which is *not* configured as a
  37. backend in this test), so that we can ensure that its cache is left
  38. alone.
  39. """
  40. for back in backends:
  41. back_cachedir = os.path.join(cachedir, "file_lists", back)
  42. # Make file_lists cachedir
  43. os.makedirs(os.path.join(back_cachedir))
  44. # Touch a couple files
  45. for filename in ("base.p", "dev.p", "foo.txt"):
  46. with salt.utils.files.fopen(os.path.join(back_cachedir, filename), "w"):
  47. pass
  48. @with_tempdir()
  49. def test_clear_file_list_cache_vcs(self, cachedir):
  50. """
  51. Test that VCS backends are cleared irrespective of whether they are
  52. configured as gitfs/git, hgfs/hg, svnfs/svn.
  53. """
  54. # Mixture of VCS backends specified with and without "fs" at the end,
  55. # to confirm that the correct dirs are cleared.
  56. backends = ["gitfs", "hg", "svnfs"]
  57. opts = {
  58. "fileserver_backend": backends,
  59. "cachedir": cachedir,
  60. }
  61. mock_fs = DummyFS(backends)
  62. self._make_file_lists_cache(cachedir, backends + ["roots"])
  63. with patch.dict(fileserver.__opts__, opts), patch.object(
  64. salt.loader, "fileserver", MagicMock(return_value=mock_fs)
  65. ):
  66. cleared = fileserver.clear_file_list_cache()
  67. # Make sure the return data matches what you'd expect
  68. expected = {
  69. "gitfs": ["base", "dev"],
  70. "hg": ["base", "dev"],
  71. "svnfs": ["base", "dev"],
  72. }
  73. assert cleared == expected, cleared
  74. # Trust, but verify! Check that the correct files are actually gone
  75. assert not os.path.exists(
  76. os.path.join(cachedir, "file_lists", "gitfs", "base.p")
  77. )
  78. assert not os.path.exists(
  79. os.path.join(cachedir, "file_lists", "gitfs", "dev.p")
  80. )
  81. assert not os.path.exists(os.path.join(cachedir, "file_lists", "hg", "base.p"))
  82. assert not os.path.exists(
  83. os.path.join(cachedir, "file_lists", "gitfs", "dev.p")
  84. )
  85. assert not os.path.exists(os.path.join(cachedir, "file_lists", "hg", "base.p"))
  86. assert not os.path.exists(
  87. os.path.join(cachedir, "file_lists", "svnfs", "dev.p")
  88. )
  89. # These files *should* exist and shouldn't have been cleaned
  90. assert os.path.exists(os.path.join(cachedir, "file_lists", "gitfs", "foo.txt"))
  91. assert os.path.exists(os.path.join(cachedir, "file_lists", "hg", "foo.txt"))
  92. assert os.path.exists(os.path.join(cachedir, "file_lists", "svnfs", "foo.txt"))
  93. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "base.p"))
  94. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "dev.p"))
  95. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "foo.txt"))
  96. @with_tempdir()
  97. def test_clear_file_list_cache_vcs_limited(self, cachedir):
  98. """
  99. Test the arguments to limit what is cleared
  100. """
  101. # Mixture of VCS backends specified with and without "fs" at the end,
  102. # to confirm that the correct dirs are cleared.
  103. backends = ["gitfs", "hg", "svnfs"]
  104. opts = {
  105. "fileserver_backend": backends,
  106. "cachedir": cachedir,
  107. }
  108. mock_fs = DummyFS(backends)
  109. self._make_file_lists_cache(cachedir, backends + ["roots"])
  110. with patch.dict(fileserver.__opts__, opts), patch.object(
  111. salt.loader, "fileserver", MagicMock(return_value=mock_fs)
  112. ):
  113. cleared = fileserver.clear_file_list_cache(saltenv="base", backend="gitfs")
  114. expected = {"gitfs": ["base"]}
  115. assert cleared == expected, cleared
  116. # Trust, but verify! Check that the correct files are actually gone
  117. assert not os.path.exists(
  118. os.path.join(cachedir, "file_lists", "gitfs", "base.p")
  119. )
  120. # These files *should* exist and shouldn't have been cleaned
  121. assert os.path.exists(os.path.join(cachedir, "file_lists", "gitfs", "dev.p"))
  122. assert os.path.exists(os.path.join(cachedir, "file_lists", "gitfs", "foo.txt"))
  123. assert os.path.exists(os.path.join(cachedir, "file_lists", "hg", "base.p"))
  124. assert os.path.exists(os.path.join(cachedir, "file_lists", "hg", "dev.p"))
  125. assert os.path.exists(os.path.join(cachedir, "file_lists", "hg", "foo.txt"))
  126. assert os.path.exists(os.path.join(cachedir, "file_lists", "svnfs", "base.p"))
  127. assert os.path.exists(os.path.join(cachedir, "file_lists", "svnfs", "dev.p"))
  128. assert os.path.exists(os.path.join(cachedir, "file_lists", "svnfs", "foo.txt"))
  129. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "base.p"))
  130. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "dev.p"))
  131. assert os.path.exists(os.path.join(cachedir, "file_lists", "roots", "foo.txt"))