1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import pytest
- from flexmock import flexmock
- from borgmatic.hooks.credential import parse as module
- def test_hash_adapter_is_always_equal():
- assert module.Hash_adapter({1: 2}) == module.Hash_adapter({3: 4})
- def test_hash_adapter_alwaysh_hashes_the_same():
- assert hash(module.Hash_adapter({1: 2})) == hash(module.Hash_adapter({3: 4}))
- def test_cache_ignoring_unhashable_arguments_caches_arguments_after_first_call():
- hashable = 3
- unhashable = {1, 2}
- calls = 0
- @module.cache_ignoring_unhashable_arguments
- def function(first, second, third):
- nonlocal calls
- calls += 1
- assert first == hashable
- assert second == unhashable
- assert third == unhashable
- return first
- assert function(hashable, unhashable, third=unhashable) == hashable
- assert calls == 1
- assert function(hashable, unhashable, third=unhashable) == hashable
- assert calls == 1
- def test_resolve_credential_passes_through_string_without_credential():
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
- assert module.resolve_credential('{no credentials here}', config={}) == '{no credentials here}'
- def test_resolve_credential_passes_through_none():
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
- assert module.resolve_credential(None, config={}) is None
- @pytest.mark.parametrize('invalid_value', ('{credential}', '{credential }', '{credential systemd}'))
- def test_resolve_credential_with_invalid_credential_raises(invalid_value):
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').never()
- with pytest.raises(ValueError):
- module.resolve_credential(invalid_value, config={})
- def test_resolve_credential_with_valid_credential_loads_credential():
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- 'load_credential',
- {},
- 'systemd',
- ('mycredential',),
- ).and_return('result').once()
- assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'
- def test_resolve_credential_with_valid_credential_and_quoted_parameters_loads_credential():
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- 'load_credential',
- {},
- 'systemd',
- ('my credential',),
- ).and_return('result').once()
- assert module.resolve_credential('{credential systemd "my credential"}', config={}) == 'result'
- def test_resolve_credential_caches_credential_after_first_call():
- module.resolve_credential.cache_clear()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- 'load_credential',
- {},
- 'systemd',
- ('mycredential',),
- ).and_return('result').once()
- assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'
- assert module.resolve_credential('{credential systemd mycredential}', config={}) == 'result'
|