|
1 | 1 | from unittest import TestCase |
| 2 | +from unittest.mock import Mock |
2 | 3 | import requests |
3 | | -from mock import patch |
| 4 | +from mock import patch, MagicMock |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import json |
4 | 8 |
|
5 | 9 | from hpecp import ContainerPlatformClient |
6 | | -from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus |
| 10 | +from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus, WorkerK8s |
7 | 11 | from hpecp.exceptions import APIItemConflictException, APIItemNotFoundException |
8 | 12 | from .base_test import session_mock_response, BaseTestCase |
9 | 13 | import tempfile |
10 | 14 |
|
| 15 | +try: |
| 16 | + from imp import reload |
| 17 | +except Exception: |
| 18 | + from importlib import reload |
| 19 | + |
11 | 20 |
|
12 | 21 | class MockResponse: |
13 | 22 | def __init__( |
@@ -507,35 +516,63 @@ def test_with_only_ssh_key_content_provided_raises_general_exception( |
507 | 516 | "Expected: `{}`, Actual: `{}`".format(expected_err, stderr), |
508 | 517 | ) |
509 | 518 |
|
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): |
513 | 521 |
|
514 | 522 | ssh_key_file = tempfile.NamedTemporaryFile(delete=True, mode="w") |
515 | 523 | ssh_key_file.write("test_ssh_key_file_data") |
516 | 524 | ssh_key_file.flush() |
517 | 525 |
|
| 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 |
518 | 563 | 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() |
531 | 565 |
|
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, |
535 | 572 |
|
536 | 573 | stdout = self.out.getvalue().strip() |
537 | 574 |
|
538 | | - self.assertEqual(stdout, "/api/v2/worker/k8shost/1") |
| 575 | + self.assertEqual(stdout, "/api/v2/worker/k8shost/5") |
539 | 576 |
|
540 | 577 | ssh_key_file.close() |
541 | 578 |
|
|
0 commit comments