1
0

test_gitfs.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 TestCase
  10. from tests.support.mock import MagicMock, patch
  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. class TestGitFSProvider(TestCase):
  17. def test_provider_case_insensitive(self):
  18. '''
  19. Ensure that both lowercase and non-lowercase values are supported
  20. '''
  21. provider = 'GitPython'
  22. for role_name, role_class in (
  23. ('gitfs', salt.utils.gitfs.GitFS),
  24. ('git_pillar', salt.utils.gitfs.GitPillar),
  25. ('winrepo', salt.utils.gitfs.WinRepo)):
  26. key = '{0}_provider'.format(role_name)
  27. with patch.object(role_class, 'verify_gitpython',
  28. MagicMock(return_value=True)):
  29. with patch.object(role_class, 'verify_pygit2',
  30. MagicMock(return_value=False)):
  31. args = [OPTS, {}]
  32. kwargs = {'init_remotes': False}
  33. if role_name == 'winrepo':
  34. kwargs['cache_root'] = '/tmp/winrepo-dir'
  35. with patch.dict(OPTS, {key: provider}):
  36. # Try to create an instance with uppercase letters in
  37. # provider name. If it fails then a
  38. # FileserverConfigError will be raised, so no assert is
  39. # necessary.
  40. role_class(*args, **kwargs)
  41. # Now try to instantiate an instance with all lowercase
  42. # letters. Again, no need for an assert here.
  43. role_class(*args, **kwargs)
  44. def test_valid_provider(self):
  45. '''
  46. Ensure that an invalid provider is not accepted, raising a
  47. FileserverConfigError.
  48. '''
  49. def _get_mock(verify, provider):
  50. '''
  51. Return a MagicMock with the desired return value
  52. '''
  53. return MagicMock(return_value=verify.endswith(provider))
  54. for role_name, role_class in (
  55. ('gitfs', salt.utils.gitfs.GitFS),
  56. ('git_pillar', salt.utils.gitfs.GitPillar),
  57. ('winrepo', salt.utils.gitfs.WinRepo)):
  58. key = '{0}_provider'.format(role_name)
  59. for provider in salt.utils.gitfs.GIT_PROVIDERS:
  60. verify = 'verify_gitpython'
  61. mock1 = _get_mock(verify, provider)
  62. with patch.object(role_class, verify, mock1):
  63. verify = 'verify_pygit2'
  64. mock2 = _get_mock(verify, provider)
  65. with patch.object(role_class, verify, mock2):
  66. args = [OPTS, {}]
  67. kwargs = {'init_remotes': False}
  68. if role_name == 'winrepo':
  69. kwargs['cache_root'] = '/tmp/winrepo-dir'
  70. with patch.dict(OPTS, {key: provider}):
  71. role_class(*args, **kwargs)
  72. with patch.dict(OPTS, {key: 'foo'}):
  73. # Set the provider name to a known invalid provider
  74. # and make sure it raises an exception.
  75. self.assertRaises(
  76. FileserverConfigError,
  77. role_class,
  78. *args,
  79. **kwargs)