1
0

test_git_pillar.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. unit tests for the git_pillar runner
  3. """
  4. import errno
  5. import logging
  6. import tempfile
  7. import salt.runners.git_pillar as git_pillar
  8. import salt.utils.files
  9. import salt.utils.gitfs
  10. from tests.support.gitfs import _OPTS
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import patch
  13. from tests.support.runtests import RUNTIME_VARS
  14. from tests.support.unit import TestCase
  15. log = logging.getLogger(__name__)
  16. class GitPillarTest(TestCase, LoaderModuleMockMixin):
  17. """
  18. Validate the git_pillar runner
  19. """
  20. @classmethod
  21. def setUpClass(cls):
  22. cls.tmp_cachedir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
  23. @classmethod
  24. def tearDownClass(cls):
  25. try:
  26. salt.utils.files.rm_rf(cls.tmp_cachedir)
  27. except OSError as exc:
  28. if exc.errno == errno.EACCES:
  29. log.error("Access error removing file %s", cls.tmp_cachedir)
  30. elif exc.errno != errno.EEXIST:
  31. raise
  32. def setup_loader_modules(self):
  33. opts = _OPTS.copy()
  34. opts["cachedir"] = self.tmp_cachedir
  35. opts["verified_git_pillar_provider"] = "gitfoo"
  36. opts["ext_pillar"] = [
  37. {
  38. "git": [
  39. "master https://someurl/some",
  40. {"dev https://otherurl/other": [{"name": "somename"}]},
  41. ]
  42. }
  43. ]
  44. return {git_pillar: {"__opts__": opts}}
  45. def test_update(self):
  46. """
  47. test git_pillar.update
  48. """
  49. class MockGitProvider(
  50. salt.utils.gitfs.GitProvider
  51. ): # pylint: disable=abstract-method
  52. def init_remote(self):
  53. new = False
  54. self.repo = True
  55. return new
  56. def fetch(self):
  57. return True
  58. def clear_lock(self, lock_type="update"):
  59. pass # return success, failed
  60. git_providers = {"gitfoo": MockGitProvider}
  61. repo1 = {"master https://someurl/some": True}
  62. repo2 = {"dev https://otherurl/other": True}
  63. all_repos = {
  64. "master https://someurl/some": True,
  65. "dev https://otherurl/other": True,
  66. }
  67. with patch.object(salt.utils.gitfs, "GIT_PROVIDERS", git_providers):
  68. self.assertEqual(git_pillar.update(), all_repos)
  69. self.assertEqual(git_pillar.update(branch="master"), repo1)
  70. self.assertEqual(git_pillar.update(branch="dev"), repo2)
  71. self.assertEqual(git_pillar.update(repo="somename"), repo2)