Skip to content

Commit a40be1c

Browse files
committed
more test refactoring
Signed-off-by: Chris Snow <[email protected]>
1 parent cfa1a93 commit a40be1c

File tree

5 files changed

+54
-65
lines changed

5 files changed

+54
-65
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/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_test 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+
)

tests/client_test.py

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,11 @@
2828

2929
from hpecp import ContainerPlatformClient, ContainerPlatformClientException
3030

31+
from .base_test import BaseTestCase, MockResponse
32+
from .client_mock_api_responses import mockApiSetup
3133

32-
class MockResponse:
33-
def __init__(
34-
self,
35-
json_data,
36-
status_code,
37-
headers,
38-
raise_for_status_flag=False,
39-
text_data="",
40-
):
41-
self.json_data = json_data
42-
self.text = text_data
43-
self.status_code = status_code
44-
self.raise_for_status_flag = raise_for_status_flag
45-
self.headers = headers
46-
47-
def raise_for_status(self):
48-
if self.raise_for_status_flag:
49-
self.text = "some error occurred"
50-
raise requests.exceptions.HTTPError()
51-
else:
52-
return
53-
54-
def json(self):
55-
return self.json_data
34+
# setup the mock data
35+
mockApiSetup()
5636

5737

5838
class TestCreateFromProperties(TestCase):
@@ -170,23 +150,8 @@ def test_create_from_env_var_factory_method_with_falses(self):
170150
self.assertEqual(client.warn_ssl, False)
171151

172152

173-
class TestAuth(TestCase):
174-
175-
# pylint: disable=no-method-argument
176-
def mocked_requests_post(*args, **kwargs):
177-
if args[0] == "http://127.0.0.1:8080/api/v1/login":
178-
return MockResponse(
179-
json_data={},
180-
status_code=200,
181-
headers={
182-
"location": (
183-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
184-
)
185-
},
186-
)
187-
raise RuntimeError("Unhandle POST request: " + args[0])
188-
189-
@patch("requests.post", side_effect=mocked_requests_post)
153+
class TestAuth(BaseTestCase):
154+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
190155
def test_create_session(self, mock_post):
191156

192157
client = ContainerPlatformClient(
@@ -199,7 +164,7 @@ def test_create_session(self, mock_post):
199164

200165
self.assertIsInstance(client.create_session(), ContainerPlatformClient)
201166

202-
@patch("requests.post", side_effect=mocked_requests_post)
167+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
203168
def test_create_session_chained(self, mock_post):
204169

205170
client = ContainerPlatformClient(
@@ -212,20 +177,7 @@ def test_create_session_chained(self, mock_post):
212177

213178
self.assertIsInstance(client, ContainerPlatformClient)
214179

215-
def mocked_requests_post_ssl(*args, **kwargs):
216-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
217-
return MockResponse(
218-
json_data={},
219-
status_code=200,
220-
headers={
221-
"location": (
222-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
223-
)
224-
},
225-
)
226-
raise RuntimeError("Unhandle POST request: " + args[0])
227-
228-
@patch("requests.post", side_effect=mocked_requests_post_ssl)
180+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
229181
def test_auth_ssl(self, mock_post):
230182

231183
client = ContainerPlatformClient(

tests/role_test.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727

2828
from hpecp import ContainerPlatformClient
2929

30-
from .base_test import (
31-
BaseTestCase,
32-
MockResponse,
33-
get_client,
34-
session_mock_response,
35-
)
30+
from .base_test import BaseTestCase, MockResponse, get_client
3631
from .role_mock_api_responses import mockApiSetup
3732

3833
# setup the mock data

0 commit comments

Comments
 (0)