Skip to content

Commit 2a8b579

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Introduce FakeImage class"
2 parents ff48f92 + 3f7c01c commit 2a8b579

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

openstackclient/tests/image/v2/fakes.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
import copy
1717
import mock
18+
import random
19+
import uuid
1820

21+
from openstackclient.common import utils as common_utils
1922
from openstackclient.tests import fakes
2023
from openstackclient.tests import utils
2124

@@ -167,3 +170,133 @@ def setUp(self):
167170
endpoint=fakes.AUTH_URL,
168171
token=fakes.AUTH_TOKEN,
169172
)
173+
174+
175+
class FakeImage(object):
176+
"""Fake one or more images.
177+
178+
TODO(xiexs): Currently, only image API v2 is supported by this class.
179+
"""
180+
181+
@staticmethod
182+
def create_one_image(attrs={}):
183+
"""Create a fake image.
184+
185+
:param Dictionary attrs:
186+
A dictionary with all attrbutes of image
187+
:retrun:
188+
A FakeResource object with id, name, owner, protected,
189+
visibility and tags attrs
190+
"""
191+
# Set default attribute
192+
image_info = {
193+
'id': 'image-id' + uuid.uuid4().hex,
194+
'name': 'image-name' + uuid.uuid4().hex,
195+
'owner': 'image-owner' + uuid.uuid4().hex,
196+
'protected': bool(random.choice([0, 1])),
197+
'visibility': random.choice(['public', 'private']),
198+
'tags': [uuid.uuid4().hex for r in range(random.randint(1, 5))],
199+
}
200+
201+
# Overwrite default attributes if there are some attributes set
202+
image_info.update(attrs)
203+
204+
image = fakes.FakeResource(
205+
None,
206+
image_info,
207+
loaded=True)
208+
return image
209+
210+
@staticmethod
211+
def create_images(attrs={}, count=2):
212+
"""Create multiple fake images.
213+
214+
:param Dictionary attrs:
215+
A dictionary with all attributes of image
216+
:param Integer count:
217+
The number of images to be faked
218+
:return:
219+
A list of FakeResource objects
220+
"""
221+
images = []
222+
for n in range(0, count):
223+
images.append(FakeImage.create_one_image(attrs))
224+
225+
return images
226+
227+
@staticmethod
228+
def get_images(images=None, count=2):
229+
"""Get an iterable MagicMock object with a list of faked images.
230+
231+
If images list is provided, then initialize the Mock object with the
232+
list. Otherwise create one.
233+
234+
:param List images:
235+
A list of FakeResource objects faking images
236+
:param Integer count:
237+
The number of images to be faked
238+
:return
239+
An iterable Mock object with side_effect set to a list of faked
240+
images
241+
"""
242+
if images is None:
243+
images = FakeImage.create_images(count)
244+
245+
return mock.MagicMock(side_effect=images)
246+
247+
@staticmethod
248+
def get_image_info(image=None):
249+
"""Get the image info from a faked image object.
250+
251+
:param image:
252+
A FakeResource objects faking image
253+
:return
254+
A dictionary which includes the faked image info as follows:
255+
{
256+
'id': image_id,
257+
'name': image_name,
258+
'owner': image_owner,
259+
'protected': image_protected,
260+
'visibility': image_visibility,
261+
'tags': image_tags
262+
}
263+
"""
264+
if image is not None:
265+
return image._info
266+
return {}
267+
268+
@staticmethod
269+
def get_image_columns(image=None):
270+
"""Get the image columns from a faked image object.
271+
272+
:param image:
273+
A FakeResource objects faking image
274+
:return
275+
A tuple which may include the following keys:
276+
('id', 'name', 'owner', 'protected', 'visibility', 'tags')
277+
"""
278+
if image is not None:
279+
return tuple(k for k in sorted(
280+
FakeImage.get_image_info(image).keys()))
281+
return tuple([])
282+
283+
@staticmethod
284+
def get_image_data(image=None):
285+
"""Get the image data from a faked image object.
286+
287+
:param image:
288+
A FakeResource objects faking image
289+
:return
290+
A tuple which may include the following values:
291+
('image-123', 'image-foo', 'admin', False, 'public', 'bar, baz')
292+
"""
293+
data_list = []
294+
if image is not None:
295+
for x in sorted(FakeImage.get_image_info(image).keys()):
296+
if x == 'tags':
297+
# The 'tags' should be format_list
298+
data_list.append(
299+
common_utils.format_list(getattr(image, x)))
300+
else:
301+
data_list.append(getattr(image, x))
302+
return tuple(data_list)

0 commit comments

Comments
 (0)