Skip to content

Commit 992b7cd

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

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

bin/cli.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,10 @@ def get(self, id, output="yaml", params=None):
188188
elif output == "json-pp":
189189
print(json.dumps(json_data, indent=4, sort_keys=True,))
190190
else:
191-
191+
192192
print(
193193
yaml.dump(
194-
yaml.load(
195-
json.dumps(json_data), Loader=yaml.FullLoader,
196-
)
194+
yaml.load(json.dumps(json_data), Loader=yaml.FullLoader,)
197195
)
198196
)
199197

tests/library/k8s_worker_test.py

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -398,27 +398,57 @@ def test_with_only_ssh_key_content_provided(
398398
is passed to the library method 'create_with_ssh_key()'.
399399
"""
400400

401+
def mock_k8s_worker_get(*args, **kwargs):
402+
worker = WorkerK8s(
403+
{
404+
"status": "bundle",
405+
"approved_worker_pubkey": [],
406+
"tags": [],
407+
"hostname": "",
408+
"ipaddr": "10.1.0.186",
409+
"setup_log": (
410+
"/var/log/bluedata/install/"
411+
"k8shost_setup_10.1.0.186-"
412+
"2020-4-26-18-49-10"
413+
),
414+
"_links": {"self": {"href": "/api/v2/worker/k8shost/5"}},
415+
}
416+
)
417+
return worker
418+
419+
def mock_create_with_ssh_key(*args, **kwargs):
420+
return "/api/v2/worker/k8shost/5"
421+
422+
def mock_get_client():
423+
client = ContainerPlatformClient(
424+
username="",
425+
password="",
426+
api_host="",
427+
api_port=9090,
428+
use_ssl=True,
429+
verify_ssl=True,
430+
warn_ssl=True,
431+
)
432+
client.session_id = "ABC"
433+
client.k8s_worker.get = mock_k8s_worker_get
434+
client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key
435+
return client
436+
437+
# support debugging if this test fails
401438
with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}):
402-
with patch.object(
403-
K8sWorkerController,
404-
"create_with_ssh_key",
405-
return_value="/api/v2/worker/k8shost/1",
406-
) as mock_create_with_ssh_key:
407-
try:
408-
hpecp_cli = self.cli.CLI()
409-
hpecp_cli.k8sworker.create_with_ssh_key(
410-
ip="127.0.0.1", ssh_key="test_ssh_key",
411-
)
412-
except Exception:
413-
self.fail("Unexpected exception.")
439+
hpecp_cli = self.cli.CLI()
414440

415-
mock_create_with_ssh_key.assert_called_once_with(
416-
ip="127.0.0.1", ssh_key_data="test_ssh_key", tags=[],
417-
)
441+
# manually patch methods due to json serialization error
442+
# when using Mock or MagicMock
443+
self.cli.get_client = mock_get_client
444+
445+
hpecp_cli.k8sworker.create_with_ssh_key(
446+
ip="127.0.0.1", ssh_key="test_ssh_key",
447+
)
418448

419449
stdout = self.out.getvalue().strip()
420450

421-
self.assertEqual(stdout, "/api/v2/worker/k8shost/1")
451+
self.assertEqual(stdout, "/api/v2/worker/k8shost/5")
422452

423453
@patch("requests.post", side_effect=mocked_requests_post)
424454
@patch("hpecp.k8s_worker")
@@ -525,40 +555,40 @@ def test_with_only_ssh_key_file_provided(self, mock_login_response):
525555

526556
def mock_k8s_worker_get(*args, **kwargs):
527557
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-
)
558+
{
559+
"status": "bundle",
560+
"approved_worker_pubkey": [],
561+
"tags": [],
562+
"hostname": "",
563+
"ipaddr": "10.1.0.186",
564+
"setup_log": (
565+
"/var/log/bluedata/install/"
566+
"k8shost_setup_10.1.0.186-"
567+
"2020-4-26-18-49-10"
568+
),
569+
"_links": {"self": {"href": "/api/v2/worker/k8shost/5"}},
570+
}
571+
)
542572
return worker
543573

544574
def mock_create_with_ssh_key(*args, **kwargs):
545575
return "/api/v2/worker/k8shost/5"
546576

547577
def mock_get_client():
548578
client = ContainerPlatformClient(
549-
username = "",
550-
password= "",
551-
api_host= "",
579+
username="",
580+
password="",
581+
api_host="",
552582
api_port=9090,
553583
use_ssl=True,
554584
verify_ssl=True,
555585
warn_ssl=True,
556-
)
586+
)
557587
client.session_id = "ABC"
558588
client.k8s_worker.get = mock_k8s_worker_get
559589
client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key
560590
return client
561-
591+
562592
# support debugging if this test fails
563593
with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}):
564594
hpecp_cli = self.cli.CLI()
@@ -568,7 +598,8 @@ def mock_get_client():
568598
self.cli.get_client = mock_get_client
569599

570600
hpecp_cli.k8sworker.create_with_ssh_key(
571-
ip="127.0.0.1", ssh_key_file=ssh_key_file.name,
601+
ip="127.0.0.1", ssh_key_file=ssh_key_file.name
602+
)
572603

573604
stdout = self.out.getvalue().strip()
574605

0 commit comments

Comments
 (0)