1
0

test_gitfs.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. '''
  3. These only test the provider selection and verification logic, they do not init
  4. any remotes.
  5. '''
  6. # Import python libs
  7. from __future__ import absolute_import, unicode_literals, print_function
  8. # Import Salt Testing libs
  9. from tests.support.unit import skipIf, TestCase
  10. from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
  11. # Import salt libs
  12. import salt.utils.gitfs
  13. from salt.exceptions import FileserverConfigError
  14. # GLOBALS
  15. OPTS = {'cachedir': '/tmp/gitfs-test-cache'}
  16. @skipIf(NO_MOCK, NO_MOCK_REASON)
  17. class TestGitFSProvider(TestCase):
  18. def test_provider_case_insensitive(self):
  19. '''
  20. Ensure that both lowercase and non-lowercase values are supported
  21. '''
  22. provider = 'GitPython'
  23. for role_name, role_class in (
  24. ('gitfs', salt.utils.gitfs.GitFS),
  25. ('git_pillar', salt.utils.gitfs.GitPillar),
  26. ('winrepo', salt.utils.gitfs.WinRepo)):
  27. key = '{0}_provider'.format(role_name)
  28. with patch.object(role_class, 'verify_gitpython',
  29. MagicMock(return_value=True)):
  30. with patch.object(role_class, 'verify_pygit2',
  31. MagicMock(return_value=False)):
  32. args = [OPTS, {}]
  33. kwargs = {'init_remotes': False}
  34. if role_name == 'winrepo':
  35. kwargs['cache_root'] = '/tmp/winrepo-dir'
  36. with patch.dict(OPTS, {key: provider}):
  37. # Try to create an instance with uppercase letters in
  38. # provider name. If it fails then a
  39. # FileserverConfigError will be raised, so no assert is
  40. # necessary.
  41. role_class(*args, **kwargs)
  42. # Now try to instantiate an instance with all lowercase
  43. # letters. Again, no need for an assert here.
  44. role_class(*args, **kwargs)
  45. def test_valid_provider(self):
  46. '''
  47. Ensure that an invalid provider is not accepted, raising a
  48. FileserverConfigError.
  49. '''
  50. def _get_mock(verify, provider):
  51. '''
  52. Return a MagicMock with the desired return value
  53. '''
  54. return MagicMock(return_value=verify.endswith(provider))
  55. for role_name, role_class in (
  56. ('gitfs', salt.utils.gitfs.GitFS),
  57. ('git_pillar', salt.utils.gitfs.GitPillar),
  58. ('winrepo', salt.utils.gitfs.WinRepo)):
  59. key = '{0}_provider'.format(role_name)
  60. for provider in salt.utils.gitfs.GIT_PROVIDERS:
  61. verify = 'verify_gitpython'
  62. mock1 = _get_mock(verify, provider)
  63. with patch.object(role_class, verify, mock1):
  64. verify = 'verify_pygit2'
  65. mock2 = _get_mock(verify, provider)
  66. with patch.object(role_class, verify, mock2):
  67. args = [OPTS, {}]
  68. kwargs = {'init_remotes': False}
  69. if role_name == 'winrepo':
  70. kwargs['cache_root'] = '/tmp/winrepo-dir'
  71. with patch.dict(OPTS, {key: provider}):
  72. role_class(*args, **kwargs)
  73. with patch.dict(OPTS, {key: 'foo'}):
  74. # Set the provider name to a known invalid provider
  75. # and make sure it raises an exception.
  76. self.assertRaises(
  77. FileserverConfigError,
  78. role_class,
  79. *args,
  80. **kwargs)