1
0

test_git_pillar.py 2.7 KB

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