Skip to content

Commit 0148a6f

Browse files
committed
refactor more tests
1 parent aeb4646 commit 0148a6f

8 files changed

+116
-165
lines changed

pre_push_verifications.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
set -e
44

5-
isort --check-only tests/*.py bin/*.py hpecp/**.py
5+
isort --check-only tests/*.py bin/*.py hpecp/b*.py hpecp/c*.py hpecp/g*.py hpecp/l*.py hpecp/r*.py hpecp/t*.py. hpecp/u*.py
6+
67

78
black bin/ tests/ hpecp/
89

910
#flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
1011
flake8 --exclude hpecp/role.py --docstring-convention numpy bin/ hpecp/
1112

12-
flake8 --ignore=D,E501 tests/catalog_test.py tests/base_test.py tests/cli_test.py tests/client_test.py tests/config_test.py tests/gateway_test.py
13+
flake8 --ignore=D,E501 tests/*.py
1314

1415
tox -e py35 -- tests/
1516

tests/base_resource_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
import unittest
2222

23-
from mock import MagicMock
24-
2523
from hpecp.base_resource import (
2624
AbstractResourceController,
2725
AbstractWaitableResourceController,

tests/gateway_test.py

-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030

3131
from .base_test import BaseTestCase
3232

33-
# if six.PY2:
34-
# from io import BytesIO as StringIO
35-
# else:
36-
# from io import StringIO
37-
3833

3934
class MockResponse:
4035
def __init__(

tests/k8s_cluster_test.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1919
# OTHER DEALINGS IN THE SOFTWARE.
2020

21-
import base64
2221
import json
23-
import os
24-
import sys
25-
import tempfile
26-
from io import StringIO
27-
from textwrap import dedent
2822
from unittest import TestCase
2923

3024
import requests
31-
import six
3225
from mock import patch
3326

3427
from hpecp import (
@@ -44,10 +37,10 @@
4437

4538
from .base_test import BaseTestCase, session_mock_response
4639

47-
if six.PY2:
48-
from io import BytesIO as StringIO # noqa: F811
49-
else:
50-
from io import StringIO
40+
# if six.PY2:
41+
# from io import BytesIO as StringIO # noqa: F811
42+
# else:
43+
# from io import StringIO
5144

5245

5346
class MockResponse:

tests/k8s_worker_test.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import json
2-
import os
3-
import sys
41
import tempfile
5-
from unittest import TestCase
62

73
import requests
8-
from mock import MagicMock, patch
4+
from mock import patch
95

106
from hpecp import ContainerPlatformClient
117
from hpecp.cli import base
@@ -14,10 +10,10 @@
1410

1511
from .base_test import BaseTestCase, session_mock_response
1612

17-
try:
18-
from imp import reload
19-
except Exception:
20-
from importlib import reload
13+
# try:
14+
# from imp import reload
15+
# except Exception:
16+
# from importlib import reload
2117

2218

2319
class MockResponse:
@@ -680,7 +676,7 @@ def test_wwithout_ephemeral_storage_prints_err(self, mock_k8sworker):
680676

681677
with patch.object(
682678
K8sWorkerController, "set_storage", return_value=None,
683-
) as mock_k8s_worker:
679+
):
684680

685681
with self.assertRaises(SystemExit) as cm:
686682
try:
@@ -714,7 +710,7 @@ def test_with_exception(self, mock_post, mock_k8sworker):
714710
K8sWorkerController,
715711
"set_storage",
716712
side_effect=Exception("TEST_EXCEPTION"),
717-
) as mock_k8s_worker:
713+
):
718714

719715
with self.assertRaises(SystemExit) as cm:
720716
try:

tests/license_test.py

+101-122
Original file line numberDiff line numberDiff line change
@@ -20,117 +20,104 @@
2020

2121

2222
import json
23-
import os
24-
import sys
25-
import tempfile
26-
import unittest
27-
from textwrap import dedent
2823

29-
import requests
3024
import six
31-
import yaml
3225
from mock import patch
3326

34-
from hpecp import ContainerPlatformClient
35-
from hpecp.base_resource import ResourceList
36-
from hpecp.exceptions import APIItemNotFoundException
37-
38-
from .base_test import BaseTestCase, MockResponse, mocked_login_post
39-
40-
41-
class TestCLI(BaseTestCase):
42-
def mocked_login_post(*args, **kwargs):
43-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
44-
return MockResponse(
45-
json_data={},
46-
status_code=200,
47-
headers={
48-
"location": (
49-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
50-
)
51-
},
52-
)
53-
if args[0] == "https://127.0.0.1:8080/api/v2/hpelicense":
54-
return MockResponse(
55-
json_data={},
56-
status_code=201,
57-
headers={"location": "/api/v2/hpeclicense/1"},
58-
)
59-
raise RuntimeError("Unhandle POST request: " + args[0])
60-
61-
def mocked_requests_get(*args, **kwargs):
62-
if args[0] == "https://127.0.0.1:8080/api/v1/license":
63-
return MockResponse(
64-
json_data={
65-
"_links": {"self": {"href": "/api/v1/license"}},
66-
"state": "unlicensed",
67-
"uuid": "3c831f6e-f76f-410d-977c-ed13b0c817d1",
27+
from .base_test import BaseTestCase, MockResponse
28+
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,
6890
},
69-
status_code=200,
70-
headers=dict(),
71-
)
72-
if args[0] == "https://127.0.0.1:8080/api/v2/hpelicense":
73-
return MockResponse(
74-
json_data={
75-
"_links": {"self": {"href": "/api/v2/hpelicense"}},
76-
"Licenses": [
77-
{
78-
"Label": "The License",
79-
"Feature": "HPE Machine Learning Ops",
80-
"Capacity": 240,
81-
"UnlimitedCapacity": False,
82-
"Start": 1566864000000,
83-
"StartDisplay": "2019-08-27T00:00:00Z",
84-
"Expiration": 1609286399000,
85-
"ExpirationDisplay": "2020-12-29T23:59:59Z",
86-
"LicenseKey": "TEST_LICENSE_KEY",
87-
"DeviceID": "1234 1234",
88-
"Evaluation": False,
89-
}
90-
],
91-
"Summaries": [
92-
{
93-
"Label": "HPE Container Platform",
94-
"UnlimitedCapacity": False,
95-
"TotalCapacity": 240,
96-
"UsedCapacity": 24,
97-
"AvailableCapacity": 216,
98-
"NextExpiration": 1609286399000,
99-
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
100-
"LatestExpiration": 1609286399000,
101-
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
102-
"Valid": True,
103-
"ValidationTime": 1594758782000,
104-
"RevalidateTime": 1609286400000,
105-
},
106-
{
107-
"Label": "HPE Machine Learning Ops",
108-
"UnlimitedCapacity": False,
109-
"TotalCapacity": 240,
110-
"UsedCapacity": 0,
111-
"AvailableCapacity": 240,
112-
"NextExpiration": 1609286399000,
113-
"NextExpirationDisplay": "2020-12-29T23:59:59Z",
114-
"LatestExpiration": 1609286399000,
115-
"LatestExpirationDisplay": "2020-12-29T23:59:59Z",
116-
"Valid": True,
117-
"ValidationTime": 1594758782000,
118-
"RevalidateTime": 1609286400000,
119-
},
120-
],
121-
"Messages": [],
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",
122101
"Valid": True,
123-
"Enabled": True,
124102
"ValidationTime": 1594758782000,
125103
"RevalidateTime": 1609286400000,
126104
},
127-
status_code=200,
128-
headers=dict(),
129-
)
130-
raise RuntimeError("Unhandle GET request: " + args[0])
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+
)
116+
131117

132-
@patch("requests.post", side_effect=mocked_login_post)
133-
@patch("requests.get", side_effect=mocked_requests_get)
118+
class TestCLI(BaseTestCase):
119+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
120+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
134121
def test_list(self, mock_post, mock_get):
135122

136123
try:
@@ -198,8 +185,8 @@ def test_list(self, mock_post, mock_get):
198185
if six.PY2:
199186
self.assertEqual(stderr, expected_stderr)
200187

201-
@patch("requests.post", side_effect=mocked_login_post)
202-
@patch("requests.get", side_effect=mocked_requests_get)
188+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
189+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
203190
def test_list_license_key_only(self, mock_post, mock_get):
204191

205192
try:
@@ -221,8 +208,8 @@ def test_list_license_key_only(self, mock_post, mock_get):
221208
if six.PY2:
222209
self.assertEqual(stderr, expected_stderr)
223210

224-
@patch("requests.post", side_effect=mocked_login_post)
225-
@patch("requests.get", side_effect=mocked_requests_get)
211+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
212+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
226213
def test_list_output_json(self, mock_post, mock_get):
227214

228215
self.maxDiff = None
@@ -302,8 +289,8 @@ def test_list_output_json(self, mock_post, mock_get):
302289
if six.PY2:
303290
self.assertEqual(stderr, expected_stderr)
304291

305-
@patch("requests.post", side_effect=mocked_login_post)
306-
@patch("requests.get", side_effect=mocked_requests_get)
292+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
293+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
307294
def test_platform_id(self, mock_post, mock_get):
308295

309296
try:
@@ -325,8 +312,8 @@ def test_platform_id(self, mock_post, mock_get):
325312
if six.PY2:
326313
self.assertEqual(stderr, expected_stderr)
327314

328-
@patch("requests.post", side_effect=mocked_login_post)
329-
@patch("requests.get", side_effect=mocked_requests_get)
315+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
316+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
330317
def test_register(self, mock_post, mock_get):
331318

332319
try:
@@ -348,16 +335,8 @@ def test_register(self, mock_post, mock_get):
348335
if six.PY2:
349336
self.assertEqual(stderr, expected_stderr)
350337

351-
def mocked_requests_delete(*args, **kwargs):
352-
if (
353-
args[0]
354-
== "https://127.0.0.1:8080/api/v2/hpelicense/TEST_LICENSE_KEY/"
355-
):
356-
return MockResponse(json_data={}, status_code=200, headers=dict(),)
357-
raise RuntimeError("Unhandle DELETE request: " + args[0])
358-
359-
@patch("requests.post", side_effect=mocked_login_post)
360-
@patch("requests.delete", side_effect=mocked_requests_delete)
338+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
339+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
361340
def test_delete(self, mock_post, mock_delete):
362341

363342
with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}):
@@ -376,9 +355,9 @@ def test_delete(self, mock_post, mock_delete):
376355
if six.PY2:
377356
self.assertEqual(stderr, expected_stderr)
378357

379-
@patch("requests.post", side_effect=mocked_login_post)
380-
@patch("requests.delete", side_effect=mocked_requests_delete)
381-
@patch("requests.get", side_effect=mocked_requests_get)
358+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
359+
@patch("requests.delete", side_effect=BaseTestCase.httpDeleteHandlers)
360+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
382361
def test_delete_all(self, mock_post, mock_delete, mock_get):
383362

384363
with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}):

0 commit comments

Comments
 (0)