1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-26 09:47:58 +00:00

Add testing for exception transport.

This commit is contained in:
Martin Hostettler 2016-11-08 23:19:06 +01:00
parent e14406fdbf
commit bd3a4a2f92
2 changed files with 78 additions and 3 deletions

View file

@ -122,7 +122,8 @@ class RepositoryServer: # pragma: no cover
'load_key',
'break_lock',
'get_free_nonce',
'commit_nonce_reservation'
'commit_nonce_reservation',
'inject_exception',
)
def __init__(self, restrict_to_paths, append_only):
@ -286,6 +287,27 @@ def open(self, path, create=False, lock_wait=None, lock=True, exclusive=None, ap
self.repository.__enter__() # clean exit handled by serve() method
return self.repository.id
def inject_exception(self, kind):
kind = kind.decode()
s1 = 'test string'
s2 = 'test string2'
if kind == 'DoesNotExist':
raise Repository.DoesNotExist(s1)
elif kind == 'AlreadyExists':
raise Repository.AlreadyExists(s1)
elif kind == 'CheckNeeded':
raise Repository.CheckNeeded(s1)
elif kind == 'IntegrityError':
raise IntegrityError(s1)
elif kind == 'PathNotAllowed':
raise PathNotAllowed()
elif kind == 'ObjectNotFound':
raise Repository.ObjectNotFound(s1, s2)
elif kind == 'InvalidRPCMethod':
raise InvalidRPCMethod(s1)
elif kind == 'divide':
0 // 0
class SleepingBandwidthLimiter:
def __init__(self, limit):

View file

@ -12,7 +12,7 @@
from ..helpers import Location
from ..helpers import IntegrityError
from ..locking import Lock, LockFailed
from ..remote import RemoteRepository, InvalidRPCMethod, ConnectionClosedWithHint, handle_remote_line
from ..remote import RemoteRepository, InvalidRPCMethod, PathNotAllowed, ConnectionClosedWithHint, handle_remote_line
from ..repository import Repository, LoggedIO, MAGIC, MAX_DATA_SIZE, TAG_DELETE
from . import BaseTestCase
from .hashindex import H
@ -647,7 +647,60 @@ def open(self, create=False):
exclusive=True, create=create)
def test_invalid_rpc(self):
self.assert_raises(InvalidRPCMethod, lambda: self.repository.call('__init__', None))
self.assert_raises(InvalidRPCMethod, lambda: self.repository.call('__init__', {}))
def test_rpc_exception_transport(self):
s1 = 'test string'
try:
self.repository.call('inject_exception', {'kind': 'DoesNotExist'})
except Repository.DoesNotExist as e:
assert len(e.args) == 1
assert e.args[0] == self.repository.location.orig
try:
self.repository.call('inject_exception', {'kind': 'AlreadyExists'})
except Repository.AlreadyExists as e:
assert len(e.args) == 1
assert e.args[0] == self.repository.location.orig
try:
self.repository.call('inject_exception', {'kind': 'CheckNeeded'})
except Repository.CheckNeeded as e:
assert len(e.args) == 1
assert e.args[0] == self.repository.location.orig
try:
self.repository.call('inject_exception', {'kind': 'IntegrityError'})
except IntegrityError as e:
assert len(e.args) == 1
assert e.args[0] == s1
try:
self.repository.call('inject_exception', {'kind': 'PathNotAllowed'})
except PathNotAllowed as e:
assert len(e.args) == 0
try:
self.repository.call('inject_exception', {'kind': 'ObjectNotFound'})
except Repository.ObjectNotFound as e:
assert len(e.args) == 2
assert e.args[0] == s1
assert e.args[1] == self.repository.location.orig
try:
self.repository.call('inject_exception', {'kind': 'InvalidRPCMethod'})
except InvalidRPCMethod as e:
assert len(e.args) == 1
assert e.args[0] == s1
try:
self.repository.call('inject_exception', {'kind': 'divide'})
except RemoteRepository.RPCError as e:
assert e.unpacked
assert e.get_message() == 'ZeroDivisionError: integer division or modulo by zero\n'
assert e.exception_class == 'ZeroDivisionError'
assert len(e.exception_full) > 0
def test_ssh_cmd(self):
assert self.repository.ssh_cmd(Location('example.com:foo')) == ['ssh', 'example.com']