test_git.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Erik Johnson <erik@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import
  7. import logging
  8. import os
  9. # Import Salt Testing Libs
  10. from tests.support.helpers import with_tempdir
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.unit import TestCase
  13. from tests.support.mock import (
  14. Mock,
  15. MagicMock,
  16. patch,
  17. DEFAULT,
  18. )
  19. # Import Salt Libs
  20. import salt.states.git as git_state # Don't potentially shadow GitPython
  21. log = logging.getLogger(__name__)
  22. class GitTestCase(TestCase, LoaderModuleMockMixin):
  23. '''
  24. Test cases for salt.states.git
  25. '''
  26. def setup_loader_modules(self):
  27. return {
  28. git_state: {
  29. '__env__': 'base',
  30. '__opts__': {'test': False},
  31. '__salt__': {},
  32. }
  33. }
  34. @with_tempdir()
  35. def test_latest_no_diff_for_bare_repo(self, target):
  36. '''
  37. This test ensures that we don't attempt to diff when cloning a repo
  38. using either bare=True or mirror=True.
  39. '''
  40. name = 'https://foo.com/bar/baz.git'
  41. gitdir = os.path.join(target, 'refs')
  42. isdir_mock = MagicMock(
  43. side_effect=lambda path: DEFAULT if path != gitdir else True)
  44. branches = ['foo', 'bar', 'baz']
  45. tags = ['v1.1.0', 'v.1.1.1', 'v1.2.0']
  46. local_head = 'b9ef06ab6b7524eb7c27d740dbbd5109c6d75ee4'
  47. remote_head = 'eef672c1ec9b8e613905dbcd22a4612e31162807'
  48. git_diff = Mock()
  49. dunder_salt = {
  50. 'git.current_branch': MagicMock(return_value=branches[0]),
  51. 'git.config_get_regexp': MagicMock(return_value={}),
  52. 'git.diff': git_diff,
  53. 'git.fetch': MagicMock(return_value={}),
  54. 'git.is_worktree': MagicMock(return_value=False),
  55. 'git.list_branches': MagicMock(return_value=branches),
  56. 'git.list_tags': MagicMock(return_value=tags),
  57. 'git.remote_refs': MagicMock(return_value={'HEAD': remote_head}),
  58. 'git.remotes': MagicMock(return_value={
  59. 'origin': {'fetch': name, 'push': name},
  60. }),
  61. 'git.rev_parse': MagicMock(side_effect=git_state.CommandExecutionError()),
  62. 'git.revision': MagicMock(return_value=local_head),
  63. 'git.version': MagicMock(return_value='1.8.3.1'),
  64. }
  65. with patch('os.path.isdir', isdir_mock), \
  66. patch.dict(git_state.__salt__, dunder_salt):
  67. result = git_state.latest(
  68. name=name,
  69. target=target,
  70. mirror=True, # mirror=True implies bare=True
  71. )
  72. assert result['result'] is True, result
  73. git_diff.assert_not_called()