Skip to content

Commit 39da82b

Browse files
authored
Merge pull request #234 from hpe-container-platform-community/refactor_tests
more test refactoring
2 parents f6556d9 + 047476e commit 39da82b

27 files changed

+167
-166
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ RUN sudo apt-get update \
1010

1111
# FIXME: Python 3.9 returns errors with pip
1212
RUN echo "Installing python modules" \
13-
&& for v in 2 3 3.5 3.6 3.7 3.8; do python${v} -m pip install -U pylint pytest mock nose flake8-docstrings flake8-per-file-ignores==0.8.1; done \
13+
&& for v in 2 3 3.5 3.6 3.7 3.8; do python${v} -m pip install -U pylint pytest mock nose flake8-docstrings flake8-per-file-ignores==0.8.1 isort; done \
1414
&& for v in 3 3.5 3.6 3.7 3.8; do python${v} -m pip install -U black; done \
1515
&& sudo ln -s /home/theia/.local/bin//black /bin/ \
1616
&& for v in 2 3 3.5 3.6 3.7 3.8; do python${v} -m pip install -r /tmp/requirements.txt; done

pre_push_verifications.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
set -e
44

5-
isort --check-only bin/*.py hpecp/**.py tests/*.py
6-
5+
isort tests/*.py
6+
isort hpecp/**.py
7+
isort bin/*.py
78

89
black bin/ tests/ hpecp/
910

tests/base_test.py renamed to tests/base.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@
3737
from io import StringIO
3838

3939

40-
def session_mock_response():
41-
return MockResponse(
42-
json_data={},
43-
status_code=200,
44-
headers={
45-
"location": "/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
46-
},
47-
)
48-
49-
5040
class MockResponse:
5141
def __init__(
5242
self,
@@ -90,26 +80,12 @@ def get_client():
9080
return client
9181

9282

93-
# pylint: disable=no-method-argument
94-
def mocked_login_post(*args, **kwargs):
95-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
96-
return MockResponse(
97-
json_data={},
98-
status_code=200,
99-
headers={
100-
"location": (
101-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
102-
)
103-
},
104-
)
105-
raise RuntimeError("Unhandle POST request: " + args[0])
106-
107-
10883
@six.add_metaclass(abc.ABCMeta)
10984
class BaseTestCase(unittest.TestCase):
11085

11186
_http_get_handlers = {}
11287
_http_post_handlers = {}
88+
_http_put_handlers = {}
11389
_http_delete_handlers = {}
11490

11591
@classmethod
@@ -128,6 +104,22 @@ def httpPostHandlers(cls, *args, **kwargs):
128104
else:
129105
return handler
130106

107+
@classmethod
108+
def httpPutHandlers(cls, *args, **kwargs):
109+
try:
110+
handler = BaseTestCase._http_put_handlers[args[0]]
111+
except KeyError:
112+
raise Exception(
113+
"Handler not found for PUT '{}'.\nDid you register a handler with BaseTestCase.registerHttpPutHandler?".format(
114+
args[0]
115+
)
116+
)
117+
118+
if isinstance(handler, Exception):
119+
raise handler
120+
else:
121+
return handler
122+
131123
@classmethod
132124
def httpGetHandlers(cls, *args, **kwargs):
133125
try:
@@ -164,6 +156,10 @@ def httpDeleteHandlers(cls, *args, **kwargs):
164156
def registerHttpPostHandler(cls, url, response):
165157
BaseTestCase._http_post_handlers[url] = response
166158

159+
@classmethod
160+
def registerHttpPutHandler(cls, url, response):
161+
BaseTestCase._http_put_handlers[url] = response
162+
167163
@classmethod
168164
def registerHttpGetHandler(cls, url, response):
169165
BaseTestCase._http_get_handlers[url] = response
@@ -177,6 +173,7 @@ def setUpClass(cls):
177173

178174
_http_get_handlers = {}
179175
_http_post_handlers = {}
176+
_http_put_handlers = {}
180177
_http_delete_handlers = {}
181178

182179
# Register the login handler

tests/catalog_mock_api_responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from hpecp.exceptions import APIItemNotFoundException
2323

24-
from .base_test import BaseTestCase, MockResponse
24+
from .base import BaseTestCase, MockResponse
2525

2626

2727
def mockApiSetup():

tests/catalog_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from hpecp.base_resource import ResourceList
3030
from hpecp.exceptions import APIItemNotFoundException
3131

32-
from .base_test import BaseTestCase, MockResponse, get_client
32+
from .base import BaseTestCase, MockResponse, get_client
3333
from .catalog_mock_api_responses import mockApiSetup
3434

3535
# setup the mock data

tests/cli_mock_api_responses.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# (C) Copyright [2020] Hewlett Packard Enterprise Development LP
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a
4+
# copy of this software and associated documentation files (the "Software"),
5+
# to deal in the Software without restriction, including without limitation
6+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
# and/or sell copies of the Software, and to permit persons to whom the
8+
# Software is furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included
11+
# in all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19+
# OTHER DEALINGS IN THE SOFTWARE.
20+
21+
22+
from hpecp.exceptions import APIItemNotFoundException
23+
24+
from .base import BaseTestCase, MockResponse
25+
26+
27+
def mockApiSetup():
28+
BaseTestCase.registerHttpGetHandler(
29+
url="https://127.0.0.1:8080/some/url",
30+
response=MockResponse(
31+
json_data={"foo": "bar"},
32+
text_data='{"foo":"bar"}',
33+
status_code=200,
34+
headers=dict(),
35+
),
36+
)
37+
38+
BaseTestCase.registerHttpPostHandler(
39+
url="https://127.0.0.1:8080/some/url",
40+
response=MockResponse(
41+
text_data={"mock_data": True},
42+
json_data={},
43+
status_code=200,
44+
headers={},
45+
),
46+
)
47+
48+
BaseTestCase.registerHttpPutHandler(
49+
url="https://127.0.0.1:8080/some/url",
50+
response=MockResponse(
51+
text_data={"mock_data": True},
52+
json_data={},
53+
status_code=200,
54+
headers={},
55+
),
56+
)

tests/cli_test.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
from hpecp.cli import base
3232
from hpecp.gateway import Gateway
3333

34-
from .base_test import BaseTestCase, MockResponse
35-
from .base_test import session_mock_response as base_login_post_response
34+
from .base import BaseTestCase, MockResponse
35+
from .cli_mock_api_responses import mockApiSetup
36+
37+
# setup the mock data
38+
mockApiSetup()
39+
3640

3741
if six.PY2:
3842
# from io import BytesIO as StringIO # noqa: F811
@@ -204,7 +208,7 @@ def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):
204208

205209

206210
class TestBaseProxy(BaseTestCase):
207-
@patch("requests.post", side_effect=base_login_post_response)
211+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
208212
def test_list_with_invalid_column(self, mock_post):
209213

210214
with self.assertRaises(SystemExit) as cm:
@@ -223,7 +227,7 @@ def test_list_with_invalid_column(self, mock_post):
223227

224228
self.assertEqual(cm.exception.code, 1)
225229

226-
@patch("requests.post", side_effect=base_login_post_response)
230+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
227231
def test_list_with_invalid_columns_list(self, mock_post):
228232

229233
with self.assertRaises(SystemExit) as cm:
@@ -242,7 +246,7 @@ def test_list_with_invalid_columns_list(self, mock_post):
242246

243247
self.assertEqual(cm.exception.code, 1)
244248

245-
@patch("requests.post", side_effect=base_login_post_response)
249+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
246250
def test_list_with_invalid_output_param(self, mock_post):
247251

248252
with self.assertRaises(SystemExit) as cm:
@@ -289,21 +293,6 @@ def test_hpe_config_file_var(self):
289293

290294

291295
class TestCLIHttpClient(BaseTestCase):
292-
def mocked_requests_post(*args, **kwargs):
293-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
294-
return base_login_post_response()
295-
raise RuntimeError("Unhandle POST request: " + args[0])
296-
297-
def mocked_requests_get(*args, **kwargs):
298-
if args[0] == "https://127.0.0.1:8080/some/url":
299-
return MockResponse(
300-
json_data={"foo": "bar"},
301-
text_data='{"foo":"bar"}',
302-
status_code=200,
303-
headers=dict(),
304-
)
305-
raise RuntimeError("Unhandle GET request: " + args[0])
306-
307296
def mocked_requests_failed_login(*args, **kwargs):
308297
if args[0] == "https://127.0.0.1:8080/api/v1/login":
309298
return MockResponse(
@@ -315,7 +304,7 @@ def mocked_requests_failed_login(*args, **kwargs):
315304
)
316305
raise RuntimeError("Unhandle POST request: " + args[0])
317306

318-
@patch("requests.get", side_effect=mocked_requests_get)
307+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
319308
@patch("requests.post", side_effect=mocked_requests_failed_login)
320309
def test_get_failed_login(self, mock_get, mock_post):
321310

@@ -338,8 +327,8 @@ def test_get_failed_login(self, mock_get, mock_post):
338327
"Expected: `{}` Actual: `{}`".format(expected_err, actual_err),
339328
)
340329

341-
@patch("requests.get", side_effect=mocked_requests_get)
342-
@patch("requests.post", side_effect=mocked_requests_post)
330+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
331+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
343332
def test_get(self, mock_get, mock_post):
344333

345334
hpecp = self.cli.CLI()
@@ -358,7 +347,7 @@ def mocked_requests_delete(*args, **kwargs):
358347
raise RuntimeError("Unhandle DELETE request: " + args[0])
359348

360349
@patch("requests.delete", side_effect=mocked_requests_delete)
361-
@patch("requests.post", side_effect=mocked_requests_post)
350+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
362351
def test_delete(self, mock_delete, mock_post):
363352

364353
hpecp = self.cli.CLI()
@@ -367,20 +356,9 @@ def test_delete(self, mock_delete, mock_post):
367356
self.assertEqual(self.out.getvalue(), "")
368357

369358
def test_post(self):
370-
def mocked_requests_post(*args, **kwargs):
371-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
372-
return base_login_post_response()
373-
if args[0] == "https://127.0.0.1:8080/some/url":
374-
return MockResponse(
375-
text_data={"mock_data": True},
376-
json_data={},
377-
status_code=200,
378-
headers={},
379-
)
380-
raise RuntimeError("Unhandle POST request: " + args[0])
381359

382360
with patch("requests.post") as mock_requests:
383-
mock_requests.side_effect = mocked_requests_post
361+
mock_requests.side_effect = BaseTestCase.httpPostHandlers
384362

385363
with tempfile.NamedTemporaryFile() as json_file:
386364
json_file.write(json.dumps({"abc": "def"}).encode("utf-8"))
@@ -415,20 +393,11 @@ def mocked_requests_post(*args, **kwargs):
415393
if six.PY2:
416394
self.assertEqual(stderr, expected_stderr)
417395

418-
@patch("requests.post", side_effect=mocked_requests_post)
396+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
419397
def test_put(self, mock_post):
420-
def mocked_requests_put(*args, **kwargs):
421-
if args[0] == "https://127.0.0.1:8080/some/url":
422-
return MockResponse(
423-
text_data={"mock_data": True},
424-
json_data={},
425-
status_code=200,
426-
headers={},
427-
)
428-
raise RuntimeError("Unhandle PUT request: " + args[0])
429398

430399
with patch("requests.put") as mock_requests:
431-
mock_requests.side_effect = mocked_requests_put
400+
mock_requests.side_effect = BaseTestCase.httpPutHandlers
432401

433402
with tempfile.NamedTemporaryFile() as json_file:
434403
json_file.write(json.dumps({"abc": "def"}).encode("utf-8"))

tests/client_mock_api_responses.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# (C) Copyright [2020] Hewlett Packard Enterprise Development LP
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a
4+
# copy of this software and associated documentation files (the "Software"),
5+
# to deal in the Software without restriction, including without limitation
6+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
# and/or sell copies of the Software, and to permit persons to whom the
8+
# Software is furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included
11+
# in all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19+
# OTHER DEALINGS IN THE SOFTWARE.
20+
21+
22+
from hpecp.exceptions import APIItemNotFoundException
23+
24+
from .base import BaseTestCase, MockResponse
25+
26+
27+
def mockApiSetup():
28+
29+
# Mock http (not https) session
30+
BaseTestCase.registerHttpPostHandler(
31+
"http://127.0.0.1:8080/api/v1/login",
32+
MockResponse(
33+
json_data={},
34+
status_code=200,
35+
headers={
36+
"location": (
37+
"/api/v1/session/df1bfacb-ssl-false-xxxx-c8f57d8f3c72"
38+
)
39+
},
40+
),
41+
)

0 commit comments

Comments
 (0)