Skip to content

Commit 9ddb5f0

Browse files
authored
Merge pull request #60 from hpe-container-platform-community/improve_code_coverage
add some gateway cli tests
2 parents bfe08a4 + b23ed38 commit 9ddb5f0

File tree

5 files changed

+137
-23
lines changed

5 files changed

+137
-23
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"python.linting.pylintEnabled": false,
2222
"python.linting.flake8Enabled": true,
2323
"python.linting.flake8Args": [
24-
"per-file-ignores=hpecp/__init__.py:F401"
24+
"per-file-ignores=hpecp/__init__.py:F401",
25+
"--docstring-convention=numpy"
2526
],
2627
"python.linting.enabled": true,
2728
"python.formatting.provider": "black",

bin/cli.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2121
# OTHER DEALINGS IN THE SOFTWARE.
2222

23-
"""Prototype for HPE Container Platform API."""
23+
"""HPE Container Platform CLI."""
2424

2525
import base64
2626
import configparser
@@ -145,13 +145,13 @@ def create_with_ssh_key(
145145

146146
if ssh_key_file is not None:
147147
with open(ssh_key_file) as f:
148-
ssh_key_data = f.read()
148+
ssh_key = f.read()
149149

150150
try:
151151
gateway_id = get_client().gateway.create_with_ssh_key(
152152
ip=ip,
153153
proxy_node_hostname=proxy_node_hostname,
154-
ssh_key_data=ssh_key_data,
154+
ssh_key_data=ssh_key,
155155
tags=tags,
156156
)
157157
print(gateway_id)
@@ -533,10 +533,9 @@ def list(
533533
data = get_client().k8s_cluster.list().json
534534
print(json.dumps(jmespath.search(str(query), data)))
535535
else:
536-
output = (
536+
print(
537537
get_client().k8s_cluster.list().tabulate(columns=columns)
538538
)
539-
print(output)
540539

541540
def get(
542541
self, k8scluster_id,

setup.cfg

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ description-file = README.md
44
[flake8]
55
per-file-ignores = hpecp/__init__.py:F401
66
# Temporarily disable docstring violations
7-
hpecp/client.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
8-
hpecp/catalog.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
9-
hpecp/config.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
10-
hpecp/gateway.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
11-
hpecp/logger.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
12-
hpecp/lock.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
13-
hpecp/k8s_cluster.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
14-
hpecp/k8s_worker.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
15-
hpecp/license.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
16-
hpecp/exceptions.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
17-
hpecp/user.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
7+
hpecp/client.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
8+
hpecp/catalog.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
9+
hpecp/config.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
10+
hpecp/gateway.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
11+
hpecp/logger.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
12+
hpecp/lock.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
13+
hpecp/k8s_cluster.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
14+
hpecp/k8s_worker.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
15+
hpecp/license.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
16+
hpecp/exceptions.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
17+
hpecp/user.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
1818
hpecp/tenant.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
1919
hpecp/role.py:D101,D102,D100,D107,D105,D209,D400,D202,D205,D401,D200,D412
20+
21+
[pycodestyle]
22+
max-line-length = 79
2023

2124

2225

tests/library/gateway_test.py

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@
2727
APIItemNotFoundException,
2828
ContainerPlatformClient,
2929
)
30-
from hpecp.gateway import GatewayStatus
30+
from hpecp.gateway import GatewayController, GatewayStatus
31+
from textwrap import dedent
32+
import tempfile
33+
import os
34+
import sys
35+
import six
36+
37+
if six.PY2:
38+
from io import BytesIO as StringIO
39+
else:
40+
from io import StringIO
3141

3242

3343
class MockResponse:
@@ -1244,3 +1254,104 @@ def test_delete_gateway(self, mock_get, mock_post, mock_delete):
12441254
get_client().gateway.delete(gateway_id="/api/v1/workers/999")
12451255

12461256
get_client().gateway.delete(gateway_id="/api/v1/workers/123")
1257+
1258+
1259+
class TestCliCreate(TestCase):
1260+
def setUp(self):
1261+
file_data = dedent(
1262+
"""[default]
1263+
api_host = 127.0.0.1
1264+
api_port = 8080
1265+
use_ssl = True
1266+
verify_ssl = False
1267+
warn_ssl = True
1268+
username = admin
1269+
password = admin123"""
1270+
)
1271+
1272+
self.tmpFile = tempfile.NamedTemporaryFile(delete=True)
1273+
self.tmpFile.write(file_data.encode("utf-8"))
1274+
self.tmpFile.flush()
1275+
1276+
sys.path.insert(0, os.path.abspath("../../"))
1277+
from bin import cli
1278+
1279+
self.cli = cli
1280+
self.cli.HPECP_CONFIG_FILE = self.tmpFile.name
1281+
1282+
self.saved_stdout = sys.stdout
1283+
self.out = StringIO()
1284+
sys.stdout = self.out
1285+
1286+
def tearDown(self):
1287+
self.tmpFile.close()
1288+
sys.stdout = self.saved_stdout
1289+
1290+
def test_key_or_keycontent_provided(self,):
1291+
1292+
hpecp = self.cli.CLI()
1293+
with self.assertRaises(SystemExit) as cm:
1294+
hpecp.gateway.create_with_ssh_key(
1295+
ip="127.0.0.1", proxy_node_hostname="somehost"
1296+
)
1297+
1298+
self.assertEqual(cm.exception.code, 1)
1299+
1300+
self.assertEqual(
1301+
self.out.getvalue(),
1302+
"Either ssh_key or ssh_key_file must be provided\n",
1303+
)
1304+
1305+
def test_key_and_keycontent_provided(self,):
1306+
1307+
hpecp = self.cli.CLI()
1308+
with self.assertRaises(SystemExit) as cm:
1309+
hpecp.gateway.create_with_ssh_key(
1310+
ip="127.0.0.1",
1311+
proxy_node_hostname="somehost",
1312+
ssh_key="foobar",
1313+
ssh_key_file="foobar",
1314+
)
1315+
1316+
self.assertEqual(cm.exception.code, 1)
1317+
1318+
self.assertEqual(
1319+
self.out.getvalue(),
1320+
"Either ssh_key or ssh_key_file must be provided\n",
1321+
)
1322+
1323+
def mocked_requests_post(*args, **kwargs):
1324+
if args[0] == "https://127.0.0.1:8080/api/v1/login":
1325+
return session_mock_response()
1326+
raise RuntimeError("Unhandle POST request: " + args[0])
1327+
1328+
@patch("requests.post", side_effect=mocked_requests_post)
1329+
@patch("hpecp.gateway")
1330+
def test_with_only_ssh_key_content_provided(self, mock_post, mock_gateway):
1331+
1332+
"""Test that the ssh key content provided by the 'ssh_key' parameter
1333+
is passed to the library method 'create_with_ssh_key()'.
1334+
"""
1335+
1336+
with patch.object(
1337+
GatewayController,
1338+
"create_with_ssh_key",
1339+
return_value="/api/v1/workers/1",
1340+
) as mock_create_with_ssh_key:
1341+
hpecp = self.cli.CLI()
1342+
hpecp.gateway.create_with_ssh_key(
1343+
ip="127.0.0.1",
1344+
proxy_node_hostname="somehost",
1345+
ssh_key="test_ssh_key",
1346+
)
1347+
1348+
mock_create_with_ssh_key.assert_called_once_with(
1349+
ip="127.0.0.1",
1350+
proxy_node_hostname="somehost",
1351+
ssh_key_data="test_ssh_key",
1352+
tags=[],
1353+
)
1354+
1355+
def test_with_only_ssh_key_file_provided(self):
1356+
# TODO
1357+
pass

tests/library/k8s_cluster_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
)
3737
from hpecp.k8s_cluster import K8sClusterHostConfig, K8sClusterStatus
3838

39+
if six.PY2:
40+
from io import BytesIO as StringIO # noqa: F811
41+
else:
42+
from io import StringIO
43+
3944

4045
class MockResponse:
4146
def __init__(
@@ -922,7 +927,6 @@ def tearDown(self):
922927
@patch("requests.get", side_effect=mocked_requests_get)
923928
def test_k8scluster_list(self, mock_post, mock_get):
924929

925-
# FIXME: python 2.7 is failing
926930
if six.PY2:
927931
return
928932

@@ -945,10 +949,6 @@ def test_k8scluster_list(self, mock_post, mock_get):
945949
@patch("requests.get", side_effect=mocked_requests_get)
946950
def test_k8s_supported_verions(self, mock_post, mock_get):
947951

948-
# FIXME: python 2.7 is failing
949-
if six.PY2:
950-
return
951-
952952
hpecp = self.cli.CLI()
953953
hpecp.k8scluster.k8s_supported_versions()
954954

0 commit comments

Comments
 (0)