Skip to content

Commit 6b20cae

Browse files
committed
fix(test): test_with_only_ssh_key_file_provided
Signed-off-by: Chris Snow <[email protected]>
1 parent 15296da commit 6b20cae

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

bin/cli.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
def intercept_exception(wrapped, instance, args, kwargs):
102102
"""Handle Exceptions.""" # noqa: D202
103103

104-
def _handle_unknown_exception(ex):
104+
def _unknown_exception_handler(ex):
105105
"""Handle unknown exceptions."""
106106
if _log.level == 10: # "DEBUG"
107107
print(
@@ -125,7 +125,7 @@ def _handle_unknown_exception(ex):
125125
print(ae, file=sys.stderr)
126126
sys.exit(1)
127127
except APIUnknownException as ue:
128-
_handle_unknown_exception(ue)
128+
_unknown_exception_handler(ue)
129129
except (
130130
APIException,
131131
APIItemNotFoundException,
@@ -136,7 +136,7 @@ def _handle_unknown_exception(ex):
136136
print(e.message, file=sys.stderr)
137137
sys.exit(1)
138138
except Exception as ex:
139-
_handle_unknown_exception(ex)
139+
_unknown_exception_handler(ex)
140140

141141

142142
@intercept_exception
@@ -181,16 +181,18 @@ def get(self, id, output="yaml", params=None):
181181
self.client, self.client_module_name
182182
)
183183
response = self.client_module_property.get(id=id, params=params)
184+
json_data = response.json
184185

185186
if output == "json":
186-
print(json.dumps(response.json))
187+
print(json.dumps(json_data))
187188
elif output == "json-pp":
188-
print(json.dumps(response.json, indent=4, sort_keys=True,))
189+
print(json.dumps(json_data, indent=4, sort_keys=True,))
189190
else:
191+
190192
print(
191193
yaml.dump(
192194
yaml.load(
193-
json.dumps(response.json), Loader=yaml.FullLoader,
195+
json.dumps(json_data), Loader=yaml.FullLoader,
194196
)
195197
)
196198
)
@@ -697,11 +699,13 @@ def create_with_ssh_key(
697699
timeout_secs=wait_for_operation_secs,
698700
)
699701

700-
if self.get(id=id).status == "error":
702+
if get_client().k8s_worker.get(id=worker_id).status == "error":
701703
print(
702704
(
703705
"Create request has errored. "
704-
"Check status message with `hpecp k8sworker get {}".format(id)
706+
"Check status message with `hpecp k8sworker get {}".format(
707+
id
708+
)
705709
),
706710
file=sys.stderr,
707711
)

tests/library/k8s_worker_test.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from unittest import TestCase
2+
from unittest.mock import Mock
23
import requests
3-
from mock import patch
4+
from mock import patch, MagicMock
5+
import sys
6+
import os
7+
import json
48

59
from hpecp import ContainerPlatformClient
6-
from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus
10+
from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus, WorkerK8s
711
from hpecp.exceptions import APIItemConflictException, APIItemNotFoundException
812
from .base_test import session_mock_response, BaseTestCase
913
import tempfile
1014

15+
try:
16+
from imp import reload
17+
except Exception:
18+
from importlib import reload
19+
1120

1221
class MockResponse:
1322
def __init__(
@@ -507,35 +516,63 @@ def test_with_only_ssh_key_content_provided_raises_general_exception(
507516
"Expected: `{}`, Actual: `{}`".format(expected_err, stderr),
508517
)
509518

510-
@patch("requests.post", side_effect=mocked_requests_post)
511-
@patch("hpecp.k8s_worker")
512-
def test_with_only_ssh_key_file_provided(self, mock_post, mock_k8sworker):
519+
@patch("requests.post", side_effect=mocked_requests_post) # Login response
520+
def test_with_only_ssh_key_file_provided(self, mock_login_response):
513521

514522
ssh_key_file = tempfile.NamedTemporaryFile(delete=True, mode="w")
515523
ssh_key_file.write("test_ssh_key_file_data")
516524
ssh_key_file.flush()
517525

526+
def mock_k8s_worker_get(*args, **kwargs):
527+
worker = WorkerK8s(
528+
{
529+
"status": "bundle",
530+
"approved_worker_pubkey": [],
531+
"tags": [],
532+
"hostname": "",
533+
"ipaddr": "10.1.0.186",
534+
"setup_log": (
535+
"/var/log/bluedata/install/"
536+
"k8shost_setup_10.1.0.186-"
537+
"2020-4-26-18-49-10"
538+
),
539+
"_links": {"self": {"href": "/api/v2/worker/k8shost/5"}},
540+
}
541+
)
542+
return worker
543+
544+
def mock_create_with_ssh_key(*args, **kwargs):
545+
return "/api/v2/worker/k8shost/5"
546+
547+
def mock_get_client():
548+
client = ContainerPlatformClient(
549+
username = "",
550+
password= "",
551+
api_host= "",
552+
api_port=9090,
553+
use_ssl=True,
554+
verify_ssl=True,
555+
warn_ssl=True,
556+
)
557+
client.session_id = "ABC"
558+
client.k8s_worker.get = mock_k8s_worker_get
559+
client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key
560+
return client
561+
562+
# support debugging if this test fails
518563
with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}):
519-
with patch.object(
520-
K8sWorkerController,
521-
"create_with_ssh_key",
522-
return_value="/api/v2/worker/k8shost/1",
523-
) as mock_create_with_ssh_key:
524-
try:
525-
hpecp_cli = self.cli.CLI()
526-
hpecp_cli.k8sworker.create_with_ssh_key(
527-
ip="127.0.0.1", ssh_key_file=ssh_key_file.name,
528-
)
529-
except Exception as e:
530-
self.fail("Unexpected exception. {}".format(e))
564+
hpecp_cli = self.cli.CLI()
531565

532-
mock_create_with_ssh_key.assert_called_once_with(
533-
ip="127.0.0.1", ssh_key_data="test_ssh_key_file_data", tags=[],
534-
)
566+
# manually patch methods due to json serialization error
567+
# when using Mock or MagicMock
568+
self.cli.get_client = mock_get_client
569+
570+
hpecp_cli.k8sworker.create_with_ssh_key(
571+
ip="127.0.0.1", ssh_key_file=ssh_key_file.name,
535572

536573
stdout = self.out.getvalue().strip()
537574

538-
self.assertEqual(stdout, "/api/v2/worker/k8shost/1")
575+
self.assertEqual(stdout, "/api/v2/worker/k8shost/5")
539576

540577
ssh_key_file.close()
541578

0 commit comments

Comments
 (0)