Router: Add class FakeRouter to test "router xxx" command

A unit test class similar to FakeServer, which is able to fake one
or more routers. It will be used by the router CRUD patches.

Change-Id: I9b87c6c95282902c3a829da51229a35d4265a1e4
Implements: blueprint neutron-client
Partial-bug: #1519503
This commit is contained in:
Tang Chen 2015-12-12 17:19:10 +08:00 committed by Terry Howe
parent 823ba770e0
commit 185412f28c
1 changed files with 81 additions and 0 deletions

View File

@ -140,3 +140,84 @@ class FakeNetwork(object):
if networks is None:
networks = FakeNetwork.create_networks(count)
return mock.MagicMock(side_effect=networks)
class FakeRouter(object):
"""Fake one or more routers."""
@staticmethod
def create_one_router(attrs={}, methods={}):
"""Create a fake router.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, admin_state_up,
status, tenant_id
"""
# Set default attributes.
router_attrs = {
'id': 'router-id-' + uuid.uuid4().hex,
'name': 'router-name-' + uuid.uuid4().hex,
'status': 'ACTIVE',
'admin_state_up': True,
'distributed': False,
'ha': False,
'tenant_id': 'project-id-' + uuid.uuid4().hex,
'routes': [],
'external_gateway_info': {},
}
# Overwrite default attributes.
router_attrs.update(attrs)
# Set default methods.
router_methods = {}
# Overwrite default methods.
router_methods.update(methods)
router = fakes.FakeResource(info=copy.deepcopy(router_attrs),
methods=copy.deepcopy(router_methods),
loaded=True)
return router
@staticmethod
def create_routers(attrs={}, methods={}, count=2):
"""Create multiple fake routers.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of routers to fake
:return:
A list of FakeResource objects faking the routers
"""
routers = []
for i in range(0, count):
routers.append(FakeRouter.create_one_router(attrs, methods))
return routers
@staticmethod
def get_routers(routers=None, count=2):
"""Get an iterable MagicMock object with a list of faked routers.
If routers list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List routers:
A list of FakeResource objects faking routers
:param int count:
The number of routers to fake
:return:
An iterable Mock object with side_effect set to a list of faked
routers
"""
if routers is None:
routers = FakeRouter.create_routers(count)
return mock.MagicMock(side_effect=routers)