Skip to content

Commit 185412f

Browse files
tang-chenTerryHowe
authored andcommitted
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
1 parent 823ba77 commit 185412f

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

  • openstackclient/tests/network/v2

openstackclient/tests/network/v2/fakes.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,84 @@ def get_networks(networks=None, count=2):
140140
if networks is None:
141141
networks = FakeNetwork.create_networks(count)
142142
return mock.MagicMock(side_effect=networks)
143+
144+
145+
class FakeRouter(object):
146+
"""Fake one or more routers."""
147+
148+
@staticmethod
149+
def create_one_router(attrs={}, methods={}):
150+
"""Create a fake router.
151+
152+
:param Dictionary attrs:
153+
A dictionary with all attributes
154+
:param Dictionary methods:
155+
A dictionary with all methods
156+
:return:
157+
A FakeResource object, with id, name, admin_state_up,
158+
status, tenant_id
159+
"""
160+
# Set default attributes.
161+
router_attrs = {
162+
'id': 'router-id-' + uuid.uuid4().hex,
163+
'name': 'router-name-' + uuid.uuid4().hex,
164+
'status': 'ACTIVE',
165+
'admin_state_up': True,
166+
'distributed': False,
167+
'ha': False,
168+
'tenant_id': 'project-id-' + uuid.uuid4().hex,
169+
'routes': [],
170+
'external_gateway_info': {},
171+
}
172+
173+
# Overwrite default attributes.
174+
router_attrs.update(attrs)
175+
176+
# Set default methods.
177+
router_methods = {}
178+
179+
# Overwrite default methods.
180+
router_methods.update(methods)
181+
182+
router = fakes.FakeResource(info=copy.deepcopy(router_attrs),
183+
methods=copy.deepcopy(router_methods),
184+
loaded=True)
185+
return router
186+
187+
@staticmethod
188+
def create_routers(attrs={}, methods={}, count=2):
189+
"""Create multiple fake routers.
190+
191+
:param Dictionary attrs:
192+
A dictionary with all attributes
193+
:param Dictionary methods:
194+
A dictionary with all methods
195+
:param int count:
196+
The number of routers to fake
197+
:return:
198+
A list of FakeResource objects faking the routers
199+
"""
200+
routers = []
201+
for i in range(0, count):
202+
routers.append(FakeRouter.create_one_router(attrs, methods))
203+
204+
return routers
205+
206+
@staticmethod
207+
def get_routers(routers=None, count=2):
208+
"""Get an iterable MagicMock object with a list of faked routers.
209+
210+
If routers list is provided, then initialize the Mock object with the
211+
list. Otherwise create one.
212+
213+
:param List routers:
214+
A list of FakeResource objects faking routers
215+
:param int count:
216+
The number of routers to fake
217+
:return:
218+
An iterable Mock object with side_effect set to a list of faked
219+
routers
220+
"""
221+
if routers is None:
222+
routers = FakeRouter.create_routers(count)
223+
return mock.MagicMock(side_effect=routers)

0 commit comments

Comments
 (0)