Introduce FakeImage class

Introduce FakeImage to improve the current image unittest framework
with following two advantages:
1. generate more than one faking images
2. all faking images generated by random

Change-Id: Ide326fa2a047ddeea478bef97000083617a0b744
Implements: blueprint improve-image-unittest-framework
This commit is contained in:
xiexs 2015-11-27 13:58:40 +08:00
parent bf95d74c2d
commit 3f7c01cae5

View File

@ -15,7 +15,10 @@
import copy
import mock
import random
import uuid
from openstackclient.common import utils as common_utils
from openstackclient.tests import fakes
from openstackclient.tests import utils
@ -167,3 +170,133 @@ class TestImagev2(utils.TestCommand):
endpoint=fakes.AUTH_URL,
token=fakes.AUTH_TOKEN,
)
class FakeImage(object):
"""Fake one or more images.
TODO(xiexs): Currently, only image API v2 is supported by this class.
"""
@staticmethod
def create_one_image(attrs={}):
"""Create a fake image.
:param Dictionary attrs:
A dictionary with all attrbutes of image
:retrun:
A FakeResource object with id, name, owner, protected,
visibility and tags attrs
"""
# Set default attribute
image_info = {
'id': 'image-id' + uuid.uuid4().hex,
'name': 'image-name' + uuid.uuid4().hex,
'owner': 'image-owner' + uuid.uuid4().hex,
'protected': bool(random.choice([0, 1])),
'visibility': random.choice(['public', 'private']),
'tags': [uuid.uuid4().hex for r in range(random.randint(1, 5))],
}
# Overwrite default attributes if there are some attributes set
image_info.update(attrs)
image = fakes.FakeResource(
None,
image_info,
loaded=True)
return image
@staticmethod
def create_images(attrs={}, count=2):
"""Create multiple fake images.
:param Dictionary attrs:
A dictionary with all attributes of image
:param Integer count:
The number of images to be faked
:return:
A list of FakeResource objects
"""
images = []
for n in range(0, count):
images.append(FakeImage.create_one_image(attrs))
return images
@staticmethod
def get_images(images=None, count=2):
"""Get an iterable MagicMock object with a list of faked images.
If images list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List images:
A list of FakeResource objects faking images
:param Integer count:
The number of images to be faked
:return
An iterable Mock object with side_effect set to a list of faked
images
"""
if images is None:
images = FakeImage.create_images(count)
return mock.MagicMock(side_effect=images)
@staticmethod
def get_image_info(image=None):
"""Get the image info from a faked image object.
:param image:
A FakeResource objects faking image
:return
A dictionary which includes the faked image info as follows:
{
'id': image_id,
'name': image_name,
'owner': image_owner,
'protected': image_protected,
'visibility': image_visibility,
'tags': image_tags
}
"""
if image is not None:
return image._info
return {}
@staticmethod
def get_image_columns(image=None):
"""Get the image columns from a faked image object.
:param image:
A FakeResource objects faking image
:return
A tuple which may include the following keys:
('id', 'name', 'owner', 'protected', 'visibility', 'tags')
"""
if image is not None:
return tuple(k for k in sorted(
FakeImage.get_image_info(image).keys()))
return tuple([])
@staticmethod
def get_image_data(image=None):
"""Get the image data from a faked image object.
:param image:
A FakeResource objects faking image
:return
A tuple which may include the following values:
('image-123', 'image-foo', 'admin', False, 'public', 'bar, baz')
"""
data_list = []
if image is not None:
for x in sorted(FakeImage.get_image_info(image).keys()):
if x == 'tags':
# The 'tags' should be format_list
data_list.append(
common_utils.format_list(getattr(image, x)))
else:
data_list.append(getattr(image, x))
return tuple(data_list)