Skip to content

Commit abb7fcb

Browse files
committed
add cli configure tests
Signed-off-by: Chris Snow <[email protected]>
1 parent 9c06fc6 commit abb7fcb

File tree

2 files changed

+63
-29
lines changed

2 files changed

+63
-29
lines changed

bin/cli.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,20 @@
3131

3232
import fire
3333
import jmespath
34+
import six
3435
import yaml
3536

36-
from hpecp.logger import Logger
37-
38-
from hpecp.gateway import (
39-
Gateway,
40-
GatewayStatus,
41-
)
42-
from hpecp.k8s_cluster import (
43-
K8sClusterHostConfig,
44-
K8sClusterStatus,
45-
)
46-
from hpecp.user import User
47-
4837
from hpecp import (
4938
APIException,
5039
APIItemConflictException,
5140
ContainerPlatformClient,
5241
ContainerPlatformClientException,
5342
)
43+
from hpecp.gateway import Gateway, GatewayStatus
44+
from hpecp.k8s_cluster import K8sClusterHostConfig, K8sClusterStatus
5445
from hpecp.k8s_worker import WorkerK8sStatus
46+
from hpecp.logger import Logger
47+
from hpecp.user import User
5548

5649
if sys.version_info[0] >= 3:
5750
unicode = str
@@ -1117,23 +1110,20 @@ def configure_cli():
11171110
controller_username = config_reader.username
11181111
controller_password = config_reader.password
11191112

1120-
if sys.version_info[0] >= 3:
1121-
raw_input = input
1122-
11231113
sys.stdout.write("Controller API Host [{}]: ".format(controller_api_host))
1124-
tmp = raw_input()
1114+
tmp = six.moves.input()
11251115
if tmp != "":
11261116
controller_api_host = tmp
11271117

11281118
sys.stdout.write("Controller API Port [{}]: ".format(controller_api_port))
1129-
tmp = input()
1119+
tmp = six.moves.input()
11301120
if tmp != "":
11311121
controller_api_port = tmp
11321122

11331123
sys.stdout.write(
11341124
"Controller uses ssl (True|False) [{}]: ".format(controller_use_ssl)
11351125
)
1136-
tmp = input()
1126+
tmp = six.moves.input()
11371127
if tmp != "":
11381128
controller_use_ssl = tmp
11391129

@@ -1142,24 +1132,24 @@ def configure_cli():
11421132
controller_verify_ssl
11431133
)
11441134
)
1145-
tmp = input()
1135+
tmp = six.moves.input()
11461136
if tmp != "":
11471137
controller_verify_ssl = tmp
11481138

11491139
sys.stdout.write(
11501140
"Controller warn ssl (True|False) [{}]: ".format(controller_warn_ssl)
11511141
)
1152-
tmp = input()
1142+
tmp = six.moves.input()
11531143
if tmp != "":
11541144
controller_warn_ssl = tmp
11551145

11561146
sys.stdout.write("Controller Username [{}]: ".format(controller_username))
1157-
tmp = raw_input()
1147+
tmp = six.moves.input()
11581148
if tmp != "":
11591149
controller_username = tmp
11601150

11611151
sys.stdout.write("Controller Password [{}]: ".format(controller_password))
1162-
tmp = raw_input()
1152+
tmp = six.moves.input()
11631153
if tmp != "":
11641154
controller_password = tmp
11651155

tests/cli/cli_test.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import requests
2828
import six
29+
from mock import MagicMock, mock, mock_open, patch
2930

3031
if six.PY2:
3132
from io import BytesIO as StringIO # noqa: F811
@@ -63,13 +64,13 @@ class TestCLI(TestCase):
6364
def setUp(self):
6465
file_data = dedent(
6566
"""[default]
66-
api_host = 127.0.0.1
67-
api_port = 8080
68-
use_ssl = True
69-
verify_ssl = False
70-
warn_ssl = True
71-
username = admin
72-
password = admin123"""
67+
api_host = 127.0.0.1
68+
api_port = 8080
69+
use_ssl = True
70+
verify_ssl = False
71+
warn_ssl = True
72+
username = admin
73+
password = admin123"""
7374
)
7475

7576
self.tmpFile = tempfile.NamedTemporaryFile(delete=True)
@@ -103,6 +104,49 @@ def test_config_file_missing(self):
103104
"Could not find configuration file 'this_file_should_not_exist'\n",
104105
)
105106

107+
def test_configure_cli_with_existing_hpecp_conf(self):
108+
109+
mock_data = dedent(
110+
"""[default]
111+
api_host = 127.0.0.1
112+
api_port = 8080
113+
use_ssl = True
114+
verify_ssl = False
115+
warn_ssl = True
116+
username = admin
117+
password = admin123"""
118+
)
119+
120+
if six.PY2:
121+
builtins_name = "__builtin__.open"
122+
else:
123+
builtins_name = "builtins.open"
124+
125+
m = mock_open(read_data=mock_data)
126+
with patch(builtins_name, m) as mock_file:
127+
128+
# mock the input capture to simulate user input
129+
# TODO: we want to send different data for each parameter
130+
six.moves.input = lambda *args: ("1234")
131+
132+
hpecp = self.cli.CLI()
133+
hpecp.configure_cli()
134+
135+
handle = m()
136+
handle.write.assert_has_calls(
137+
[
138+
mock.call("[default]\n"),
139+
mock.call("api_host = 1234\n"),
140+
mock.call("api_port = 1234\n"),
141+
mock.call("use_ssl = 1234\n"),
142+
mock.call("verify_ssl = 1234\n"),
143+
mock.call("warn_ssl = 1234\n"),
144+
mock.call("username = 1234\n"),
145+
mock.call("password = 1234\n"),
146+
mock.call("\n"),
147+
]
148+
)
149+
106150
def test_autocomplete_bash(self):
107151

108152
try:

0 commit comments

Comments
 (0)