Skip to content

Commit f6556d9

Browse files
authored
Merge pull request #233 from hpe-container-platform-community/refactor_tests
refactor more tests
2 parents 25e0567 + cfa1a93 commit f6556d9

9 files changed

+436
-362
lines changed
File renamed without changes.

tests/license_mock_api_responses.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 .base_test import BaseTestCase, MockResponse
23+
24+
25+
def mockApiSetup():
26+
27+
BaseTestCase.registerHttpPostHandler(
28+
url="https://127.0.0.1:8080/api/v2/hpelicense",
29+
response=MockResponse(
30+
json_data={},
31+
status_code=201,
32+
headers={"location": "/api/v2/hpeclicense/1"},
33+
),
34+
)
35+
36+
BaseTestCase.registerHttpDeleteHandler(
37+
url="https://127.0.0.1:8080/api/v2/hpelicense/TEST_LICENSE_KEY/",
38+
response=MockResponse(json_data={}, status_code=200, headers=dict(),),
39+
)
40+
41+
BaseTestCase.registerHttpGetHandler(
42+
url="https://127.0.0.1:8080/api/v1/license",
43+
response=MockResponse(
44+
json_data={
45+
"_links": {"self": {"href": "/api/v1/license"}},
46+
"state": "unlicensed",
47+
"uuid": "3c831f6e-f76f-410d-977c-ed13b0c817d1",
48+
},
49+
status_code=200,
50+
headers=dict(),
51+
),
52+
)
53+
54+
BaseTestCase.registerHttpGetHandler(
55+
url="https://127.0.0.1:8080/api/v2/hpelicense",
56+
response=MockResponse(
57+
json_data={
58+
"_links": {"self": {"href": "/api/v2/hpelicense"}},
59+
"Licenses": [
60+
{
61+
"Label": "The License",
62+
"Feature": "HPE Machine Learning Ops",
63+
"Capacity": 240,
64+
"UnlimitedCapacity": False,
65+
"Start": 1566864000000,
66+
"StartDisplay": "2019-08-27T00:00:00Z",
67+
"Expiration": 1609286399000,
68+
"ExpirationDisplay": "2020-12-29T23:59:59Z",
69+
"LicenseKey": "TEST_LICENSE_KEY",
70+
"DeviceID": "1234 1234",
71+
"Evaluation": False,
72+
}
73+
],
74+
"Summaries": [
75+
{
76+
"Label": "HPE Container Platform",
77+
"UnlimitedCapacity": False,
78+
"TotalCapacity": 240,
79+
"UsedCapacity": 24,
80+
"AvailableCapacity": 216,
81+
"NextExpiration": 1609286399000,
82+
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
83+
"LatestExpiration": 1609286399000,
84+
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
85+
"Valid": True,
86+
"ValidationTime": 1594758782000,
87+
"RevalidateTime": 1609286400000,
88+
},
89+
{
90+
"Label": "HPE Machine Learning Ops",
91+
"UnlimitedCapacity": False,
92+
"TotalCapacity": 240,
93+
"UsedCapacity": 0,
94+
"AvailableCapacity": 240,
95+
"NextExpiration": 1609286399000,
96+
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
97+
"LatestExpiration": 1609286399000,
98+
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
99+
"Valid": True,
100+
"ValidationTime": 1594758782000,
101+
"RevalidateTime": 1609286400000,
102+
},
103+
],
104+
"Messages": [],
105+
"Valid": True,
106+
"Enabled": True,
107+
"ValidationTime": 1594758782000,
108+
"RevalidateTime": 1609286400000,
109+
},
110+
status_code=200,
111+
headers=dict(),
112+
),
113+
)

tests/license_test.py

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -25,94 +25,10 @@
2525
from mock import patch
2626

2727
from .base_test import BaseTestCase, MockResponse
28+
from .license_mock_api_responses import mockApiSetup
2829

29-
BaseTestCase.registerHttpPostHandler(
30-
url="https://127.0.0.1:8080/api/v2/hpelicense",
31-
response=MockResponse(
32-
json_data={},
33-
status_code=201,
34-
headers={"location": "/api/v2/hpeclicense/1"},
35-
),
36-
)
37-
38-
BaseTestCase.registerHttpDeleteHandler(
39-
url="https://127.0.0.1:8080/api/v2/hpelicense/TEST_LICENSE_KEY/",
40-
response=MockResponse(json_data={}, status_code=200, headers=dict(),),
41-
)
42-
43-
BaseTestCase.registerHttpGetHandler(
44-
url="https://127.0.0.1:8080/api/v1/license",
45-
response=MockResponse(
46-
json_data={
47-
"_links": {"self": {"href": "/api/v1/license"}},
48-
"state": "unlicensed",
49-
"uuid": "3c831f6e-f76f-410d-977c-ed13b0c817d1",
50-
},
51-
status_code=200,
52-
headers=dict(),
53-
),
54-
)
55-
56-
BaseTestCase.registerHttpGetHandler(
57-
url="https://127.0.0.1:8080/api/v2/hpelicense",
58-
response=MockResponse(
59-
json_data={
60-
"_links": {"self": {"href": "/api/v2/hpelicense"}},
61-
"Licenses": [
62-
{
63-
"Label": "The License",
64-
"Feature": "HPE Machine Learning Ops",
65-
"Capacity": 240,
66-
"UnlimitedCapacity": False,
67-
"Start": 1566864000000,
68-
"StartDisplay": "2019-08-27T00:00:00Z",
69-
"Expiration": 1609286399000,
70-
"ExpirationDisplay": "2020-12-29T23:59:59Z",
71-
"LicenseKey": "TEST_LICENSE_KEY",
72-
"DeviceID": "1234 1234",
73-
"Evaluation": False,
74-
}
75-
],
76-
"Summaries": [
77-
{
78-
"Label": "HPE Container Platform",
79-
"UnlimitedCapacity": False,
80-
"TotalCapacity": 240,
81-
"UsedCapacity": 24,
82-
"AvailableCapacity": 216,
83-
"NextExpiration": 1609286399000,
84-
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
85-
"LatestExpiration": 1609286399000,
86-
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
87-
"Valid": True,
88-
"ValidationTime": 1594758782000,
89-
"RevalidateTime": 1609286400000,
90-
},
91-
{
92-
"Label": "HPE Machine Learning Ops",
93-
"UnlimitedCapacity": False,
94-
"TotalCapacity": 240,
95-
"UsedCapacity": 0,
96-
"AvailableCapacity": 240,
97-
"NextExpiration": 1609286399000,
98-
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
99-
"LatestExpiration": 1609286399000,
100-
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
101-
"Valid": True,
102-
"ValidationTime": 1594758782000,
103-
"RevalidateTime": 1609286400000,
104-
},
105-
],
106-
"Messages": [],
107-
"Valid": True,
108-
"Enabled": True,
109-
"ValidationTime": 1594758782000,
110-
"RevalidateTime": 1609286400000,
111-
},
112-
status_code=200,
113-
headers=dict(),
114-
),
115-
)
30+
# setup the mock data
31+
mockApiSetup()
11632

11733

11834
class TestCLI(BaseTestCase):

tests/lock_mock_api_responses.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 .base_test import BaseTestCase, MockResponse
23+
24+
25+
def mockApiSetup():
26+
27+
BaseTestCase.registerHttpGetHandler(
28+
url="https://127.0.0.1:8080/api/v1/lock",
29+
response=MockResponse(
30+
json_data={
31+
"_links": {"self": {"href": "/api/v1/lock"}},
32+
"locked": False,
33+
"_embedded": {"internal_locks": [], "external_locks": []},
34+
},
35+
status_code=200,
36+
headers=dict(),
37+
),
38+
)
39+
40+
BaseTestCase.registerHttpPostHandler(
41+
url="https://127.0.0.1:8080/api/v1/lock",
42+
response=MockResponse(
43+
json_data={},
44+
status_code=201,
45+
headers={"Location": "/test_location/1"},
46+
),
47+
)
48+
49+
BaseTestCase.registerHttpDeleteHandler(
50+
url="https://127.0.0.1:8080/api/v1/lock/1",
51+
response=MockResponse(json_data={}, status_code=201, headers=dict(),),
52+
)

tests/lock_test.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,15 @@
2525
from mock import patch
2626

2727
from .base_test import BaseTestCase, MockResponse, mocked_login_post
28+
from .lock_mock_api_responses import mockApiSetup
2829

29-
30-
def mocked_requests_get(*args, **kwargs):
31-
if args[0] == "https://127.0.0.1:8080/api/v1/lock":
32-
return MockResponse(
33-
json_data={
34-
"_links": {"self": {"href": "/api/v1/lock"}},
35-
"locked": False,
36-
"_embedded": {"internal_locks": [], "external_locks": []},
37-
},
38-
status_code=200,
39-
headers=dict(),
40-
)
41-
raise RuntimeError("Unhandle GET request: " + args[0])
30+
# setup the mock data
31+
mockApiSetup()
4232

4333

4434
class TestCLIList(BaseTestCase):
45-
@patch("requests.post", side_effect=mocked_login_post)
46-
@patch("requests.get", side_effect=mocked_requests_get)
35+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
36+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
4737
def test_list_yaml(self, mock_post, mock_get):
4838

4939
try:
@@ -73,8 +63,8 @@ def test_list_yaml(self, mock_post, mock_get):
7363
if six.PY2:
7464
self.assertEqual(stderr, expected_stderr)
7565

76-
@patch("requests.post", side_effect=mocked_login_post)
77-
@patch("requests.get", side_effect=mocked_requests_get)
66+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
67+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
7868
def test_list_json(self, mock_post, mock_get):
7969

8070
self.maxDiff = None
@@ -103,8 +93,8 @@ def test_list_json(self, mock_post, mock_get):
10393
expected_stderr = ""
10494
self.assertEqual(stderr, expected_stderr)
10595

106-
@patch("requests.post", side_effect=mocked_login_post)
107-
@patch("requests.get", side_effect=mocked_requests_get)
96+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
97+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
10898
def test_list_output_parameter_invalid(self, mock_post, mock_get):
10999

110100
self.maxDiff = None
@@ -126,13 +116,8 @@ def test_list_output_parameter_invalid(self, mock_post, mock_get):
126116

127117

128118
class TestCLIDelete(BaseTestCase):
129-
def mocked_requests_delete(*args, **kwargs):
130-
if args[0] == "https://127.0.0.1:8080/api/v1/lock/1":
131-
return MockResponse(json_data={}, status_code=201, headers=dict(),)
132-
raise RuntimeError("Unhandle DELETE request: " + args[0])
133-
134-
@patch("requests.post", side_effect=mocked_login_post)
135-
@patch("requests.delete", side_effect=mocked_requests_delete)
119+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
120+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
136121
def test_delete(self, mock_post, mock_delete):
137122

138123
try:
@@ -154,9 +139,9 @@ def test_delete(self, mock_post, mock_delete):
154139
if six.PY2:
155140
self.assertEqual(stderr, expected_stderr)
156141

157-
@patch("requests.get", side_effect=mocked_requests_get)
158-
@patch("requests.post", side_effect=mocked_login_post)
159-
@patch("requests.delete", side_effect=mocked_requests_delete)
142+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
143+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
144+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
160145
def test_delete_all(self, mock_get, mock_post, mock_delete):
161146

162147
try:
@@ -195,8 +180,8 @@ def mocked_requests_get_locked(*args, **kwargs):
195180
raise RuntimeError("Unhandle GET request: " + args[0])
196181

197182
@patch("requests.get", side_effect=mocked_requests_get_locked)
198-
@patch("requests.post", side_effect=mocked_login_post)
199-
@patch("requests.delete", side_effect=mocked_requests_delete)
183+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
184+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
200185
def test_delete_all_timeout(self, mock_get, mock_post, mock_delete):
201186

202187
with self.assertRaises(SystemExit) as cm:
@@ -219,26 +204,7 @@ def test_delete_all_timeout(self, mock_get, mock_post, mock_delete):
219204

220205

221206
class TestCLICreate(BaseTestCase):
222-
def mocked_requests_post(*args, **kwargs):
223-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
224-
return MockResponse(
225-
json_data={},
226-
status_code=200,
227-
headers={
228-
"location": (
229-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
230-
)
231-
},
232-
)
233-
elif args[0] == "https://127.0.0.1:8080/api/v1/lock":
234-
return MockResponse(
235-
json_data={},
236-
status_code=201,
237-
headers={"Location": "/test_location/1"},
238-
)
239-
raise RuntimeError("Unhandle GET request: " + args[0])
240-
241-
@patch("requests.post", side_effect=mocked_requests_post)
207+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
242208
def test_create(self, mock_post):
243209

244210
try:

0 commit comments

Comments
 (0)