Skip to content

Commit 8b70f31

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add FakeObject classes to fakes.py, update unit tests in identity V2."
2 parents 05e818c + 000c253 commit 8b70f31

5 files changed

Lines changed: 422 additions & 275 deletions

File tree

openstackclient/tests/identity/v2_0/fakes.py

Lines changed: 244 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
'publicurl': endpoint_publicurl,
111111
'service_id': endpoint_service_id,
112112
}
113+
SERVICE_NAME = 'service-name-' + uuid.uuid4().hex
114+
SERVICE_ID = 'service-id-' + uuid.uuid4().hex
113115

114116

115117
def fake_auth_ref(fake_token, fake_service=None):
@@ -123,12 +125,12 @@ def fake_auth_ref(fake_token, fake_service=None):
123125
# Create a service catalog
124126
if fake_service:
125127
service = token.add_service(
126-
fake_service['type'],
127-
fake_service['name'],
128+
fake_service.type,
129+
fake_service.name,
128130
)
129131
# TODO(dtroyer): Add an 'id' element to KSA's _Service fixure
130-
service['id'] = fake_service['id']
131-
for e in fake_service['endpoints']:
132+
service['id'] = fake_service.id
133+
for e in fake_service.endpoints:
132134
# KSA's _Service fixture copies publicURL to internalURL and
133135
# adminURL if they do not exist. Soooo helpful...
134136
internal = e.get('internalURL', None)
@@ -224,3 +226,241 @@ def create_one_extension(attrs=None):
224226
info=copy.deepcopy(extension_info),
225227
loaded=True)
226228
return extension
229+
230+
231+
class FakeCatalog(object):
232+
"""Fake one or more catalog."""
233+
234+
@staticmethod
235+
def create_catalog(attrs=None):
236+
"""Create a fake catalog.
237+
238+
:param Dictionary attrs:
239+
A dictionary with all attributes
240+
:return:
241+
A FakeResource object with id, name, type and so on.
242+
"""
243+
attrs = attrs or {}
244+
245+
# Set default attributes.
246+
catalog_info = {
247+
'id': SERVICE_ID,
248+
'type': 'compute',
249+
'name': 'supernova',
250+
'endpoints': [
251+
{
252+
'region': 'one',
253+
'publicURL': 'https://public.one.example.com',
254+
'internalURL': 'https://internal.one.example.com',
255+
'adminURL': 'https://admin.one.example.com',
256+
},
257+
{
258+
'region': 'two',
259+
'publicURL': 'https://public.two.example.com',
260+
'internalURL': 'https://internal.two.example.com',
261+
'adminURL': 'https://admin.two.example.com',
262+
},
263+
{
264+
'region': None,
265+
'publicURL': 'https://public.none.example.com',
266+
'internalURL': 'https://internal.none.example.com',
267+
'adminURL': 'https://admin.none.example.com',
268+
},
269+
],
270+
}
271+
# Overwrite default attributes.
272+
catalog_info.update(attrs)
273+
274+
catalog = fakes.FakeResource(
275+
info=copy.deepcopy(catalog_info),
276+
loaded=True)
277+
278+
return catalog
279+
280+
281+
class FakeProject(object):
282+
"""Fake one or more project."""
283+
284+
@staticmethod
285+
def create_one_project(attrs=None):
286+
"""Create a fake project.
287+
288+
:param Dictionary attrs:
289+
A dictionary with all attributes
290+
:return:
291+
A FakeResource object, with id, name, and so on
292+
"""
293+
294+
attrs = attrs or {}
295+
296+
# set default attributes.
297+
project_info = {
298+
'id': 'project-id' + uuid.uuid4().hex,
299+
'name': 'project-name' + uuid.uuid4().hex,
300+
'description': 'project_description',
301+
'enabled': True,
302+
}
303+
project_info.update(attrs)
304+
305+
project = fakes.FakeResource(info=copy.deepcopy(project_info),
306+
loaded=True)
307+
return project
308+
309+
@staticmethod
310+
def create_projects(attrs=None, count=2):
311+
"""Create multiple fake projects.
312+
313+
:param Dictionary attrs:
314+
A dictionary with all attributes
315+
:param int count:
316+
The number of projects to fake
317+
:return:
318+
A list of FakeResource objects faking the projects
319+
"""
320+
projects = []
321+
for i in range(0, count):
322+
projects.append(FakeProject.create_one_project(attrs))
323+
324+
return projects
325+
326+
327+
class FakeEndpoint(object):
328+
"""Fake one or more endpoint."""
329+
330+
@staticmethod
331+
def create_one_endpoint(attrs=None):
332+
"""Create a fake agent.
333+
334+
:param Dictionary attrs:
335+
A dictionary with all attributes
336+
:return:
337+
A FakeResource object, with id, name, region, and so on
338+
"""
339+
340+
attrs = attrs or {}
341+
342+
# set default attributes.
343+
endpoint_info = {
344+
'service_name': SERVICE_NAME,
345+
'adminurl': 'http://endpoint_adminurl',
346+
'region': 'endpoint_region',
347+
'internalurl': 'http://endpoint_internalurl',
348+
'service_type': 'service_type',
349+
'id': 'endpoint-id-' + uuid.uuid4().hex,
350+
'publicurl': 'http://endpoint_publicurl',
351+
'service_id': SERVICE_ID,
352+
353+
}
354+
endpoint_info.update(attrs)
355+
356+
endpoint = fakes.FakeResource(info=copy.deepcopy(endpoint_info),
357+
loaded=True)
358+
return endpoint
359+
360+
@staticmethod
361+
def create_endpoints(attrs=None, count=2):
362+
"""Create multiple fake endpoints.
363+
364+
:param Dictionary attrs:
365+
A dictionary with all attributes
366+
:param int count:
367+
The number of endpoints to fake
368+
:return:
369+
A list of FakeResource objects faking the endpoints
370+
"""
371+
endpoints = []
372+
for i in range(0, count):
373+
endpoints.append(FakeEndpoint.create_one_endpoint(attrs))
374+
375+
return endpoints
376+
377+
378+
class FakeService(object):
379+
"""Fake one or more service."""
380+
381+
@staticmethod
382+
def create_one_service(attrs=None):
383+
"""Create a fake service.
384+
385+
:param Dictionary attrs:
386+
A dictionary with all attributes
387+
:return:
388+
A FakeResource object, with id, name, type, and so on
389+
"""
390+
391+
attrs = attrs or {}
392+
393+
# set default attributes.
394+
service_info = {
395+
'id': SERVICE_ID,
396+
'name': SERVICE_NAME,
397+
'description': 'service_description',
398+
'type': 'service_type',
399+
400+
}
401+
service_info.update(attrs)
402+
403+
service = fakes.FakeResource(info=copy.deepcopy(service_info),
404+
loaded=True)
405+
return service
406+
407+
@staticmethod
408+
def create_services(attrs=None, count=2):
409+
"""Create multiple fake services.
410+
411+
:param Dictionary attrs:
412+
A dictionary with all attributes
413+
:param int count:
414+
The number of services to fake
415+
:return:
416+
A list of FakeResource objects faking the services
417+
"""
418+
services = []
419+
for i in range(0, count):
420+
services.append(FakeService.create_one_service(attrs))
421+
422+
return services
423+
424+
425+
class FakeRole(object):
426+
"""Fake one or more role."""
427+
428+
@staticmethod
429+
def create_one_role(attrs=None):
430+
"""Create a fake role.
431+
432+
:param Dictionary attrs:
433+
A dictionary with all attributes
434+
:return:
435+
A FakeResource object, with id, name, and so on
436+
"""
437+
438+
attrs = attrs or {}
439+
440+
# set default attributes.
441+
role_info = {
442+
'id': 'role-id' + uuid.uuid4().hex,
443+
'name': 'role-name' + uuid.uuid4().hex,
444+
}
445+
role_info.update(attrs)
446+
447+
role = fakes.FakeResource(info=copy.deepcopy(role_info),
448+
loaded=True)
449+
return role
450+
451+
@staticmethod
452+
def create_roles(attrs=None, count=2):
453+
"""Create multiple fake roles.
454+
455+
:param Dictionary attrs:
456+
A dictionary with all attributes
457+
:param int count:
458+
The number of roles to fake
459+
:return:
460+
A list of FakeResource objects faking the roles
461+
"""
462+
roles = []
463+
for i in range(0, count):
464+
roles.append(FakeRole.create_one_role(attrs))
465+
466+
return roles

openstackclient/tests/identity/v2_0/test_catalog.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,14 @@
2020

2121
class TestCatalog(utils.TestCommand):
2222

23-
fake_service = {
24-
'id': 'qwertyuiop',
25-
'type': 'compute',
26-
'name': 'supernova',
27-
'endpoints': [
28-
{
29-
'region': 'one',
30-
'publicURL': 'https://public.one.example.com',
31-
'internalURL': 'https://internal.one.example.com',
32-
'adminURL': 'https://admin.one.example.com',
33-
},
34-
{
35-
'region': 'two',
36-
'publicURL': 'https://public.two.example.com',
37-
'internalURL': 'https://internal.two.example.com',
38-
'adminURL': 'https://admin.two.example.com',
39-
},
40-
{
41-
'region': None,
42-
'publicURL': 'https://public.none.example.com',
43-
'internalURL': 'https://internal.none.example.com',
44-
'adminURL': 'https://admin.none.example.com',
45-
},
46-
],
47-
}
23+
service_catalog = identity_fakes.FakeCatalog.create_catalog()
4824

4925
def setUp(self):
5026
super(TestCatalog, self).setUp()
5127

5228
self.sc_mock = mock.MagicMock()
5329
self.sc_mock.service_catalog.catalog.return_value = [
54-
self.fake_service,
30+
self.service_catalog,
5531
]
5632

5733
self.auth_mock = mock.MagicMock()
@@ -77,7 +53,7 @@ def setUp(self):
7753
def test_catalog_list(self):
7854
auth_ref = identity_fakes.fake_auth_ref(
7955
identity_fakes.TOKEN,
80-
fake_service=self.fake_service,
56+
fake_service=self.service_catalog,
8157
)
8258
self.ar_mock = mock.PropertyMock(return_value=auth_ref)
8359
type(self.app.client_manager).auth_ref = self.ar_mock
@@ -108,7 +84,7 @@ def test_catalog_list(self):
10884
self.assertEqual(datalist, tuple(data))
10985

11086
def test_catalog_list_with_endpoint_url(self):
111-
fake_service = {
87+
attr = {
11288
'id': 'qwertyuiop',
11389
'type': 'compute',
11490
'name': 'supernova',
@@ -124,9 +100,10 @@ def test_catalog_list_with_endpoint_url(self):
124100
},
125101
],
126102
}
103+
service_catalog = identity_fakes.FakeCatalog.create_catalog(attr)
127104
auth_ref = identity_fakes.fake_auth_ref(
128105
identity_fakes.TOKEN,
129-
fake_service=fake_service,
106+
fake_service=service_catalog,
130107
)
131108
self.ar_mock = mock.PropertyMock(return_value=auth_ref)
132109
type(self.app.client_manager).auth_ref = self.ar_mock
@@ -162,7 +139,7 @@ def setUp(self):
162139
def test_catalog_show(self):
163140
auth_ref = identity_fakes.fake_auth_ref(
164141
identity_fakes.UNSCOPED_TOKEN,
165-
fake_service=self.fake_service,
142+
fake_service=self.service_catalog,
166143
)
167144
self.ar_mock = mock.PropertyMock(return_value=auth_ref)
168145
type(self.app.client_manager).auth_ref = self.ar_mock
@@ -192,7 +169,7 @@ def test_catalog_show(self):
192169
'<none>\n publicURL: https://public.none.example.com\n '
193170
'internalURL: https://internal.none.example.com\n '
194171
'adminURL: https://admin.none.example.com\n',
195-
'qwertyuiop',
172+
self.service_catalog.id,
196173
'supernova',
197174
'compute',
198175
)

0 commit comments

Comments
 (0)