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

This commit is contained in:
Jenkins
2015-12-11 20:19:17 +00:00
committed by Gerrit Code Review

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)