Skip to content

Commit 407e164

Browse files
author
Huanxuan Ao
committed
Refactor image v1 unit tests with FakeImage class
Add FakeImage class, and refactor the unit tests with it in image v1. Change-Id: I9024ca5eca5c604e7588c1d905562bf6838309f1 Implements: bp improve-image-unittest-framework
1 parent 762f2f2 commit 407e164

2 files changed

Lines changed: 185 additions & 95 deletions

File tree

openstackclient/tests/unit/image/v1/fakes.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# under the License.
1414
#
1515

16+
import copy
1617
import mock
18+
import uuid
1719

1820
from openstackclient.tests.unit import fakes
1921
from openstackclient.tests.unit import utils
@@ -74,3 +76,45 @@ def setUp(self):
7476
endpoint=fakes.AUTH_URL,
7577
token=fakes.AUTH_TOKEN,
7678
)
79+
80+
81+
class FakeImage(object):
82+
"""Fake one or more images."""
83+
84+
@staticmethod
85+
def create_one_image(attrs=None):
86+
"""Create a fake image.
87+
88+
:param Dictionary attrs:
89+
A dictionary with all attrbutes of image
90+
:return:
91+
A FakeResource object with id, name, owner, protected,
92+
visibility and tags attrs
93+
"""
94+
attrs = attrs or {}
95+
96+
# Set default attribute
97+
image_info = {
98+
'id': str(uuid.uuid4()),
99+
'name': 'image-name' + uuid.uuid4().hex,
100+
'owner': 'image-owner' + uuid.uuid4().hex,
101+
'container_format': '',
102+
'disk_format': '',
103+
'min_disk': 0,
104+
'min_ram': 0,
105+
'is_public': True,
106+
'protected': False,
107+
'properties': {
108+
'Alpha': 'a',
109+
'Beta': 'b',
110+
'Gamma': 'g'},
111+
}
112+
113+
# Overwrite default attributes if there are some attributes set
114+
image_info.update(attrs)
115+
116+
image = fakes.FakeResource(
117+
info=copy.deepcopy(image_info),
118+
loaded=True)
119+
120+
return image

0 commit comments

Comments
 (0)