1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
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'
|