Skip to content

Commit 2a78856

Browse files
committed
add test for missing config file
Signed-off-by: Chris Snow <[email protected]>
1 parent b23ed38 commit 2a78856

File tree

1 file changed

+24
-72
lines changed

1 file changed

+24
-72
lines changed

tests/cli/cli_test.py

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@
2525
from unittest import TestCase
2626

2727
import requests
28+
import six
2829
from mock import patch
2930

31+
if six.PY2:
32+
from io import BytesIO as StringIO # noqa: F811
33+
else:
34+
from io import StringIO
35+
3036

3137
class MockResponse:
3238
def __init__(
@@ -55,70 +61,6 @@ def json(self):
5561

5662

5763
class TestCLI(TestCase):
58-
59-
# pylint: disable=no-method-argument
60-
def mocked_requests_post(*args, **kwargs):
61-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
62-
return MockResponse(
63-
json_data={},
64-
status_code=200,
65-
headers={
66-
"location": (
67-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
68-
)
69-
},
70-
)
71-
raise RuntimeError("Unhandle POST request: " + args[0])
72-
73-
# pylint: disable=no-method-argument
74-
def mocked_requests_get(*args, **kwargs):
75-
if args[0] == "https://127.0.0.1:8080/api/v2/k8scluster":
76-
return MockResponse(
77-
json_data={
78-
"_links": {"self": {"href": "/api/v2/k8scluster"}},
79-
"_embedded": {
80-
"k8sclusters": [
81-
{
82-
"_links": {
83-
"self": {"href": "/api/v2/k8scluster/20"}
84-
},
85-
"label": {
86-
"name": "def",
87-
"description": "my cluster",
88-
},
89-
"k8s_version": "1.17.0",
90-
"pod_network_range": "10.192.0.0/12",
91-
"service_network_range": "10.96.0.0/12",
92-
"pod_dns_domain": "cluster.local",
93-
"created_by_user_id": "/api/v1/user/5",
94-
"created_by_user_name": "admin",
95-
"created_time": 1588260014,
96-
"k8shosts_config": [
97-
{
98-
"node": "/api/v2/worker/k8shost/4",
99-
"role": "worker",
100-
},
101-
{
102-
"node": "/api/v2/worker/k8shost/5",
103-
"role": "master",
104-
},
105-
],
106-
"status": "ready",
107-
"status_message": "really ready",
108-
"api_endpoint_access": "api:1234",
109-
"dashboard_endpoint_access": "dashboard:1234",
110-
"admin_kube_config": "xyz==",
111-
"dashboard_token": "abc==",
112-
"persistent_storage": {"nimble_csi": False},
113-
}
114-
]
115-
},
116-
},
117-
status_code=200,
118-
headers={},
119-
)
120-
raise RuntimeError("Unhandle GET request: " + args[0])
121-
12264
def setUp(self):
12365
file_data = dedent(
12466
"""[default]
@@ -141,18 +83,28 @@ def setUp(self):
14183
self.cli = cli
14284
self.cli.HPECP_CONFIG_FILE = self.tmpFile.name
14385

86+
self.saved_stdout = sys.stdout
87+
self.out = StringIO()
88+
sys.stdout = self.out
89+
14490
def tearDown(self):
14591
self.tmpFile.close()
92+
sys.stdout = self.saved_stdout
14693

147-
def test_autocomplete_bash(self):
94+
def test_config_file_missing(self):
14895

149-
hpecp = self.cli.CLI()
150-
hpecp.autocomplete.bash()
96+
with self.assertRaises(SystemExit) as cm:
97+
self.cli.HPECP_CONFIG_FILE = "this_file_should_not_exist"
98+
hpecp = self.cli.get_client()
99+
100+
self.assertEqual(cm.exception.code, 1)
101+
102+
self.assertEqual(
103+
self.out.getvalue(),
104+
"Could not find configuration file 'this_file_should_not_exist'\n",
105+
)
151106

152-
# TODO move this to tests/library/k8s_cluster_test.py
153-
@patch("requests.post", side_effect=mocked_requests_post)
154-
@patch("requests.get", side_effect=mocked_requests_get)
155-
def test_k8scluster_list(self, mock_post, mock_get):
107+
def test_autocomplete_bash(self):
156108

157109
hpecp = self.cli.CLI()
158-
hpecp.k8scluster.list()
110+
hpecp.autocomplete.bash()

0 commit comments

Comments
 (0)