test_parse.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks.credential import parse as module
  4. def test_hash_adapter_is_always_equal():
  5. assert module.Hash_adapter({1: 2}) == module.Hash_adapter({3: 4})
  6. def test_hash_adapter_alwaysh_hashes_the_same():
  7. assert hash(module.Hash_adapter({1: 2})) == hash(module.Hash_adapter({3: 4}))
  8. def test_cache_ignoring_unhashable_arguments_caches_arguments_after_first_call():
  9. hashable = 3
  10. unhashable = {1, 2}
  11. calls = 0
  12. @module.cache_ignoring_unhashable_arguments
  13. def function(first, second, third):
  14. nonlocal calls
  15. calls += 1
  16. assert first == hashable
  17. assert second == unhashable
  18. assert third == unhashable
  19. return first
  20. assert function(hashable, unhashable, third=unhashable) == hashable
  21. assert calls == 1
  22. assert function(hashable, unhashable, third=unhashable) == hashable
  23. assert calls == 1
  24. def test_resolve_credential_passes_through_string_without_credential():
  25. module.resolve_credential.cache_clear()
  26. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  27. assert module.resolve_credential('{no credentials here}', config={}) == '{no credentials here}'
  28. def test_resolve_credential_passes_through_none():
  29. module.resolve_credential.cache_clear()
  30. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  31. assert module.resolve_credential(None, config={}) is None
  32. @pytest.mark.parametrize('invalid_value', ('{credential}', '{credential }', '{credential systemd}'))
  33. def test_resolve_credential_with_invalid_credential_raises(invalid_value):
  34. module.resolve_credential.cache_clear()
  35. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
  36. with pytest.raises(ValueError):
  37. module.resolve_credential(invalid_value, config={})
  38. def test_resolve_credential_with_valid_credential_loads_credential():
  39. module.resolve_credential.cache_clear()
  40. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  41. 'load_credential',
  42. {},
  43. 'systemd',
  44. ('mycredential',),
  45. ).and_return('result').once()
  46. assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'
  47. def test_resolve_credential_with_valid_credential_and_quoted_parameters_loads_credential():
  48. module.resolve_credential.cache_clear()
  49. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  50. 'load_credential',
  51. {},
  52. 'systemd',
  53. ('my credential',),
  54. ).and_return('result').once()
  55. assert module.resolve_credential('{credential systemd "my credential"}', config={}) == 'result'
  56. def test_resolve_credential_caches_credential_after_first_call():
  57. module.resolve_credential.cache_clear()
  58. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  59. 'load_credential',
  60. {},
  61. 'systemd',
  62. ('mycredential',),
  63. ).and_return('result').once()
  64. assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'
  65. assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'