Skip to content

Commit 47abd82

Browse files
committed
refactor k8s cluster tests
Signed-off-by: Chris Snow <[email protected]>
1 parent 0148a6f commit 47abd82

6 files changed

+808
-887
lines changed

pre_push_verifications.sh

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

33
set -e
44

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
5+
isort --check-only bin/*.py hpecp/**.py tests/*.py
66

77

88
black bin/ tests/ hpecp/
99

1010
#flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
11-
flake8 --exclude hpecp/role.py --docstring-convention numpy bin/ hpecp/
11+
flake8 --docstring-convention numpy bin/ hpecp/
1212

13-
flake8 --ignore=D,E501 tests/*.py
13+
flake8 --ignore=D,E501 tests/*.py # don't verify documentation in tests
1414

1515
tox -e py35 -- tests/
1616

tests/base_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def httpPostHandlers(cls, *args, **kwargs):
118118
handler = BaseTestCase._http_post_handlers[args[0]]
119119
except KeyError:
120120
raise Exception(
121-
"Handler not found for POST {}.\nDid you register a handler with BaseTestCase.registerHttpGetHandler?".format(
121+
"Handler not found for POST '{}'.\nDid you register a handler with BaseTestCase.registerHttpPostHandler?".format(
122122
args[0]
123123
)
124124
)
@@ -134,7 +134,7 @@ def httpGetHandlers(cls, *args, **kwargs):
134134
handler = BaseTestCase._http_get_handlers[args[0]]
135135
except KeyError:
136136
raise Exception(
137-
"Handler not found for GET {}.\nDid you register a handler with BaseTestCase.registerHttpGetHandler?".format(
137+
"Handler not found for GET '{}'.\nDid you register a handler with BaseTestCase.registerHttpGetHandler?".format(
138138
args[0]
139139
)
140140
)
@@ -150,7 +150,7 @@ def httpDeleteHandlers(cls, *args, **kwargs):
150150
handler = BaseTestCase._http_delete_handlers[args[0]]
151151
except KeyError:
152152
raise Exception(
153-
"Handler not found for DELETE {}.\nDid you register a handler with BaseTestCase.registerHttpDeleteHandler?".format(
153+
"Handler not found for DELETE '{}'.\nDid you register a handler with BaseTestCase.registerHttpDeleteHandler?".format(
154154
args[0]
155155
)
156156
)

tests/cli_test.py

-7
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ def test_autocomplete_bash(self):
150150

151151
class TestCLIConfig(TestCase):
152152
def setUp(self):
153-
# try:
154-
# reload
155-
# except NameError:
156-
# # Python 3
157-
# from imp import reload
158-
159153
sys.path.insert(0, os.path.abspath("../../"))
160154

161155
def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):
@@ -290,7 +284,6 @@ def test_hpe_config_file_var(self):
290284

291285
# reload cli module with mock env
292286
reload(cli)
293-
# reload(hpecp.cli.base)
294287

295288
self.assertEqual(dummy_filepath, get_config_file())
296289

tests/k8s_cluster_mock_api.py

+281
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
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+
# Response for cluster list all
28+
BaseTestCase.registerHttpGetHandler(
29+
url="https://127.0.0.1:8080/api/v2/k8scluster",
30+
response=MockResponse(
31+
json_data={
32+
"_links": {"self": {"href": "/api/v2/k8scluster"}},
33+
"_embedded": {
34+
"k8sclusters": [
35+
{
36+
"_links": {
37+
"self": {"href": "/api/v2/k8scluster/20"}
38+
},
39+
"label": {
40+
"name": "def",
41+
"description": "my cluster",
42+
},
43+
"k8s_version": "1.17.0",
44+
"pod_network_range": "10.192.0.0/12",
45+
"service_network_range": "10.96.0.0/12",
46+
"pod_dns_domain": "cluster.local",
47+
"created_by_user_id": "/api/v1/user/5",
48+
"created_by_user_name": "admin",
49+
"created_time": 1588260014,
50+
"k8shosts_config": [
51+
{
52+
"node": "/api/v2/worker/k8shost/4",
53+
"role": "worker",
54+
},
55+
{
56+
"node": "/api/v2/worker/k8shost/5",
57+
"role": "master",
58+
},
59+
],
60+
"status": "ready",
61+
"status_message": "really ready",
62+
"api_endpoint_access": "api:1234",
63+
"dashboard_endpoint_access": "dashboard:1234",
64+
"admin_kube_config": "xyz==",
65+
"dashboard_token": "abc==",
66+
"persistent_storage": {"nimble_csi": False},
67+
}
68+
]
69+
},
70+
},
71+
status_code=200,
72+
headers={},
73+
),
74+
)
75+
76+
# Response for a cluster that doesn't exist
77+
BaseTestCase.registerHttpGetHandler(
78+
url="https://127.0.0.1:8080/api/v2/k8scluster/999",
79+
response=MockResponse(
80+
json_data={},
81+
status_code=404,
82+
raise_for_status_flag=True,
83+
headers={},
84+
),
85+
)
86+
87+
# Response for a cluster that doesn't exist
88+
BaseTestCase.registerHttpGetHandler(
89+
url="https://127.0.0.1:8080/api/v2/k8scluster/999?setup_log=true",
90+
response=MockResponse(
91+
json_data={},
92+
status_code=404,
93+
raise_for_status_flag=True,
94+
headers={},
95+
),
96+
)
97+
98+
# Response for a cluster that does exist
99+
BaseTestCase.registerHttpGetHandler(
100+
url="https://127.0.0.1:8080/api/v2/k8scluster/123",
101+
response=MockResponse(
102+
json_data={
103+
"_links": {"self": {"href": "/api/v2/k8scluster/123"}},
104+
"label": {"name": "def", "description": "my cluster"},
105+
"k8s_version": "1.17.0",
106+
"pod_network_range": "10.192.0.0/12",
107+
"service_network_range": "10.96.0.0/12",
108+
"pod_dns_domain": "cluster.local",
109+
"created_by_user_id": "/api/v1/user/5",
110+
"created_by_user_name": "admin",
111+
"created_time": 1588260014,
112+
"k8shosts_config": [
113+
{"node": "/api/v2/worker/k8shost/4", "role": "worker"},
114+
{"node": "/api/v2/worker/k8shost/5", "role": "master"},
115+
],
116+
"status": "ready",
117+
"status_message": "really ready",
118+
"api_endpoint_access": "api:1234",
119+
"dashboard_endpoint_access": "test_dashboard_url",
120+
"admin_kube_config": "test_admin_kube_config",
121+
"dashboard_token": "YWJjCg==",
122+
"persistent_storage": {"nimble_csi": False},
123+
},
124+
status_code=200,
125+
headers={},
126+
),
127+
)
128+
129+
BaseTestCase.registerHttpGetHandler(
130+
url="https://127.0.0.1:8080/api/v2/k8scluster/123?setup_log=true",
131+
response=MockResponse(
132+
json_data={
133+
"_links": {"self": {"href": "/api/v2/k8scluster/123"}},
134+
"label": {"name": "def", "description": "my cluster"},
135+
"k8s_version": "1.17.0",
136+
"pod_network_range": "10.192.0.0/12",
137+
"service_network_range": "10.96.0.0/12",
138+
"pod_dns_domain": "cluster.local",
139+
"created_by_user_id": "/api/v1/user/5",
140+
"created_by_user_name": "admin",
141+
"created_time": 1588260014,
142+
"k8shosts_config": [
143+
{"node": "/api/v2/worker/k8shost/4", "role": "worker"},
144+
{"node": "/api/v2/worker/k8shost/5", "role": "master"},
145+
],
146+
"status": "ready",
147+
"status_message": "really ready",
148+
"api_endpoint_access": "api:1234",
149+
"dashboard_endpoint_access": "dashboard:1234",
150+
"admin_kube_config": "xyz==",
151+
"dashboard_token": "abc==",
152+
"persistent_storage": {"nimble_csi": False},
153+
},
154+
status_code=200,
155+
headers={},
156+
),
157+
)
158+
159+
BaseTestCase.registerHttpGetHandler(
160+
url="https://127.0.0.1:8080/api/v2/k8smanifest",
161+
response=MockResponse(
162+
json_data={
163+
"_version": "1.0",
164+
"supported_versions": [
165+
"1.14.10",
166+
"1.15.7",
167+
"1.16.4",
168+
"1.17.0",
169+
"1.17.1",
170+
"1.18.0",
171+
],
172+
"version_info": {
173+
"1.14.10": {
174+
"_version": "1.0",
175+
"min_upgrade_version": "1.13.0",
176+
"relnote_url": (
177+
"https://v1-14.docs.kubernetes.io/docs/setup"
178+
"/release/notes/"
179+
),
180+
"hpecsi": "1.14",
181+
},
182+
"1.15.7": {
183+
"_version": "1.0",
184+
"min_upgrade_version": "1.14.0",
185+
"relnote_url": (
186+
"https://v1-15.docs.kubernetes.io/docs/setup"
187+
"/release/notes/"
188+
),
189+
"hpecsi": "1.15",
190+
},
191+
"1.16.4": {
192+
"_version": "1.0",
193+
"min_upgrade_version": "1.15.0",
194+
"relnote_url": (
195+
"https://v1-16.docs.kubernetes.io/docs/setup"
196+
"/release/notes/"
197+
),
198+
"hpecsi": "1.16",
199+
},
200+
"1.17.0": {
201+
"_version": "1.0",
202+
"min_upgrade_version": "1.16.0",
203+
"relnote_url": (
204+
"https://v1-17.docs.kubernetes.io/docs/setup"
205+
"/release/notes/"
206+
),
207+
"hpecsi": "1.17",
208+
},
209+
"1.17.1": {
210+
"_version": "1.0",
211+
"min_upgrade_version": "1.17.0",
212+
"relnote_url": (
213+
"https://v1-17.docs.kubernetes.io/docs/setup"
214+
"/release/notes/"
215+
),
216+
"hpecsi": "1.17",
217+
},
218+
"1.18.0": {
219+
"_version": "1.0",
220+
"min_upgrade_version": "1.17.0",
221+
"relnote_url": (
222+
"https://kubernetes.io/docs/setup"
223+
"/release/notes/"
224+
),
225+
"hpecsi": "1.18",
226+
},
227+
},
228+
},
229+
status_code=200,
230+
headers={},
231+
),
232+
)
233+
234+
BaseTestCase.registerHttpPostHandler(
235+
url="https://127.0.0.1:8080/api/v2/k8scluster",
236+
response=MockResponse(
237+
json_data={},
238+
status_code=200,
239+
headers={"Location": "/api/v2/k8scluster/99"},
240+
),
241+
)
242+
243+
BaseTestCase.registerHttpDeleteHandler(
244+
url="https://127.0.0.1:8080/api/v2/k8scluster/999",
245+
response=MockResponse(
246+
json_data={},
247+
status_code=404,
248+
raise_for_status_flag=True,
249+
headers={},
250+
),
251+
)
252+
253+
BaseTestCase.registerHttpDeleteHandler(
254+
url="https://127.0.0.1:8080/api/v2/k8scluster/123",
255+
response=MockResponse(
256+
json_data={
257+
"_links": {"self": {"href": "/api/v2/k8scluster/123"}},
258+
"label": {"name": "def", "description": "my cluster"},
259+
"k8s_version": "1.17.0",
260+
"pod_network_range": "10.192.0.0/12",
261+
"service_network_range": "10.96.0.0/12",
262+
"pod_dns_domain": "cluster.local",
263+
"created_by_user_id": "/api/v1/user/5",
264+
"created_by_user_name": "admin",
265+
"created_time": 1588260014,
266+
"k8shosts_config": [
267+
{"node": "/api/v2/worker/k8shost/4", "role": "worker"},
268+
{"node": "/api/v2/worker/k8shost/5", "role": "master"},
269+
],
270+
"status": "ready",
271+
"status_message": "really ready",
272+
"api_endpoint_access": "api:1234",
273+
"dashboard_endpoint_access": "dashboard:1234",
274+
"admin_kube_config": "xyz==",
275+
"dashboard_token": "abc==",
276+
"persistent_storage": {"nimble_csi": False},
277+
},
278+
status_code=200,
279+
headers={},
280+
),
281+
)

0 commit comments

Comments
 (0)