Skip to content

Commit 79db64f

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Remove duplicate test utilities"
2 parents 4132ca1 + fb6dad4 commit 79db64f

File tree

3 files changed

+59
-143
lines changed

3 files changed

+59
-143
lines changed

openstackclient/tests/unit/common/test_module.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14-
#
1514

1615
"""Test module module"""
1716

1817
import sys
1918
from unittest import mock
2019

2120
from openstackclient.common import module as osc_module
22-
from openstackclient.tests.unit import fakes
2321
from openstackclient.tests.unit import utils
2422

2523

24+
class FakeModule:
25+
def __init__(self, name, version):
26+
self.name = name
27+
self.__version__ = version
28+
# Workaround for openstacksdk case
29+
self.version = mock.Mock()
30+
self.version.__version__ = version
31+
32+
2633
# NOTE(dtroyer): module_1 must match the version list filter (not --all)
2734
# currently == '*client*'
2835
module_name_1 = 'fakeclient'
@@ -45,11 +52,11 @@
4552

4653
MODULES = {
4754
'sys': sys,
48-
module_name_1: fakes.FakeModule(module_name_1, module_version_1),
49-
module_name_2: fakes.FakeModule(module_name_2, module_version_2),
50-
module_name_3: fakes.FakeModule(module_name_3, module_version_3),
51-
module_name_4: fakes.FakeModule(module_name_4, module_version_4),
52-
module_name_5: fakes.FakeModule(module_name_5, module_version_5),
55+
module_name_1: FakeModule(module_name_1, module_version_1),
56+
module_name_2: FakeModule(module_name_2, module_version_2),
57+
module_name_3: FakeModule(module_name_3, module_version_3),
58+
module_name_4: FakeModule(module_name_4, module_version_4),
59+
module_name_5: FakeModule(module_name_5, module_version_5),
5360
}
5461

5562

openstackclient/tests/unit/fakes.py

Lines changed: 35 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,43 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
# TODO(stephenfin): Remove the contents of this module in favour of the osc_lib
16+
# version once our min version is bumped to 4.3.0
17+
1518
import json
16-
import sys
1719
from unittest import mock
1820

1921
from keystoneauth1 import fixture
22+
from osc_lib.tests.fakes import (
23+
FakeApp,
24+
FakeClientManager as BaseFakeClientManager,
25+
FakeLog,
26+
FakeOptions,
27+
FakeResource as BaseFakeResource,
28+
FakeStdout,
29+
)
2030
import requests
2131

32+
__all__ = [
33+
'AUTH_TOKEN',
34+
'AUTH_URL',
35+
'INTERFACE',
36+
'PASSWORD',
37+
'PROJECT_NAME',
38+
'REGION_NAME',
39+
'TEST_RESPONSE_DICT',
40+
'TEST_RESPONSE_DICT_V3',
41+
'TEST_VERSIONS',
42+
'USERNAME',
43+
'VERSION',
44+
'FakeApp',
45+
'FakeClientManager',
46+
'FakeLog',
47+
'FakeOptions',
48+
'FakeResource',
49+
'FakeResponse',
50+
'FakeStdout',
51+
]
2252

2353
AUTH_TOKEN = "foobar"
2454
AUTH_URL = "http://0.0.0.0"
@@ -47,79 +77,15 @@
4777
TEST_VERSIONS = fixture.DiscoveryList(href=AUTH_URL)
4878

4979

50-
class FakeStdout:
51-
def __init__(self):
52-
self.content = []
53-
54-
def write(self, text):
55-
self.content.append(text)
56-
57-
def make_string(self):
58-
result = ''
59-
for line in self.content:
60-
result = result + line
61-
return result
62-
63-
64-
class FakeLog:
65-
def __init__(self):
66-
self.messages = {}
67-
68-
def debug(self, msg):
69-
self.messages['debug'] = msg
70-
71-
def info(self, msg):
72-
self.messages['info'] = msg
73-
74-
def warning(self, msg):
75-
self.messages['warning'] = msg
76-
77-
def error(self, msg):
78-
self.messages['error'] = msg
79-
80-
def critical(self, msg):
81-
self.messages['critical'] = msg
82-
83-
84-
class FakeApp:
85-
def __init__(self, _stdout, _log):
86-
self.stdout = _stdout
87-
self.client_manager = None
88-
self.api_version = {}
89-
self.stdin = sys.stdin
90-
self.stdout = _stdout or sys.stdout
91-
self.stderr = sys.stderr
92-
self.log = _log
93-
94-
95-
class FakeOptions:
96-
def __init__(self, **kwargs):
97-
self.os_beta_command = False
98-
99-
100-
class FakeClient:
101-
def __init__(self, **kwargs):
102-
self.endpoint = kwargs['endpoint']
103-
self.token = kwargs['token']
104-
105-
106-
class FakeClientManager:
80+
class FakeClientManager(BaseFakeClientManager):
10781
_api_version = {
10882
'image': '2',
10983
}
11084

11185
def __init__(self):
112-
self.compute = None
113-
self.identity = None
114-
self.image = None
115-
self.object_store = None
116-
self.volume = None
117-
self.network = None
118-
self.sdk_connection = mock.Mock()
86+
super().__init__()
11987

120-
self.session = None
121-
self.auth_ref = None
122-
self.auth_plugin_name = None
88+
self.sdk_connection = mock.Mock()
12389

12490
self.network_endpoint_enabled = True
12591
self.compute_endpoint_enabled = True
@@ -158,64 +124,7 @@ def is_volume_endpoint_enabled(self, client=None):
158124
return self.volume_endpoint_enabled
159125

160126

161-
class FakeModule:
162-
def __init__(self, name, version):
163-
self.name = name
164-
self.__version__ = version
165-
# Workaround for openstacksdk case
166-
self.version = mock.Mock()
167-
self.version.__version__ = version
168-
169-
170-
class FakeResource:
171-
def __init__(self, manager=None, info=None, loaded=False, methods=None):
172-
"""Set attributes and methods for a resource.
173-
174-
:param manager:
175-
The resource manager
176-
:param Dictionary info:
177-
A dictionary with all attributes
178-
:param bool loaded:
179-
True if the resource is loaded in memory
180-
:param Dictionary methods:
181-
A dictionary with all methods
182-
"""
183-
info = info or {}
184-
methods = methods or {}
185-
186-
self.__name__ = type(self).__name__
187-
self.manager = manager
188-
self._info = info
189-
self._add_details(info)
190-
self._add_methods(methods)
191-
self._loaded = loaded
192-
193-
def _add_details(self, info):
194-
for k, v in info.items():
195-
setattr(self, k, v)
196-
197-
def _add_methods(self, methods):
198-
"""Fake methods with MagicMock objects.
199-
200-
For each <@key, @value> pairs in methods, add an callable MagicMock
201-
object named @key as an attribute, and set the mock's return_value to
202-
@value. When users access the attribute with (), @value will be
203-
returned, which looks like a function call.
204-
"""
205-
for name, ret in methods.items():
206-
method = mock.Mock(return_value=ret)
207-
setattr(self, name, method)
208-
209-
def __repr__(self):
210-
reprkeys = sorted(
211-
k for k in self.__dict__.keys() if k[0] != '_' and k != 'manager'
212-
)
213-
info = ", ".join(f"{k}={getattr(self, k)}" for k in reprkeys)
214-
return f"<{self.__class__.__name__} {info}>"
215-
216-
def keys(self):
217-
return self._info.keys()
218-
127+
class FakeResource(BaseFakeResource):
219128
def to_dict(self):
220129
return self._info
221130

@@ -247,11 +156,3 @@ def __init__(
247156
self._content = json.dumps(data)
248157
if not isinstance(self._content, bytes):
249158
self._content = self._content.encode()
250-
251-
252-
class FakeModel(dict):
253-
def __getattr__(self, key):
254-
try:
255-
return self[key]
256-
except KeyError:
257-
raise AttributeError(key)

openstackclient/tests/unit/identity/v3/fakes.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ class TestIdentityv3(
693693
): ...
694694

695695

696+
class FakeModel(dict):
697+
def __getattr__(self, key):
698+
try:
699+
return self[key]
700+
except KeyError:
701+
raise AttributeError(key)
702+
703+
696704
# We don't use FakeClientMixin since we want a different fake legacy client
697705
class TestFederatedIdentity(utils.TestCommand):
698706
def setUp(self):
@@ -1075,7 +1083,7 @@ def create_one_endpoint_filter(attrs=None):
10751083
# Overwrite default attributes if there are some attributes set
10761084
endpoint_filter_info.update(attrs)
10771085

1078-
endpoint_filter = fakes.FakeModel(copy.deepcopy(endpoint_filter_info))
1086+
endpoint_filter = FakeModel(copy.deepcopy(endpoint_filter_info))
10791087

10801088
return endpoint_filter
10811089

@@ -1133,7 +1141,7 @@ def create_one_endpointgroup_filter(attrs=None):
11331141
# Overwrite default attributes if there are some attributes set
11341142
endpointgroup_filter_info.update(attrs)
11351143

1136-
endpointgroup_filter = fakes.FakeModel(
1144+
endpointgroup_filter = FakeModel(
11371145
copy.deepcopy(endpointgroup_filter_info)
11381146
)
11391147

0 commit comments

Comments
 (0)