|
1 | 1 | from unittest import TestCase |
2 | 2 | import requests |
3 | | -from mock import patch |
| 3 | +from mock import patch, MagicMock |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import json |
4 | 7 |
|
5 | 8 | from hpecp import ContainerPlatformClient |
6 | | -from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus |
| 9 | +from hpecp.k8s_worker import K8sWorkerController, WorkerK8sStatus, WorkerK8s |
7 | 10 | from hpecp.exceptions import APIItemConflictException, APIItemNotFoundException |
8 | 11 | from .base_test import session_mock_response, BaseTestCase |
9 | 12 | import tempfile |
10 | 13 |
|
| 14 | +try: |
| 15 | + from imp import reload |
| 16 | +except Exception: |
| 17 | + from importlib import reload |
| 18 | + |
11 | 19 |
|
12 | 20 | class MockResponse: |
13 | 21 | def __init__( |
@@ -389,26 +397,57 @@ def test_with_only_ssh_key_content_provided( |
389 | 397 | is passed to the library method 'create_with_ssh_key()'. |
390 | 398 | """ |
391 | 399 |
|
392 | | - with patch.object( |
393 | | - K8sWorkerController, |
394 | | - "create_with_ssh_key", |
395 | | - return_value="/api/v2/worker/k8shost/1", |
396 | | - ) as mock_create_with_ssh_key: |
397 | | - try: |
398 | | - hpecp_cli = self.cli.CLI() |
399 | | - hpecp_cli.k8sworker.create_with_ssh_key( |
400 | | - ip="127.0.0.1", ssh_key="test_ssh_key", |
401 | | - ) |
402 | | - except Exception: |
403 | | - self.fail("Unexpected exception.") |
| 400 | + def mock_k8s_worker_get(*args, **kwargs): |
| 401 | + worker = WorkerK8s( |
| 402 | + { |
| 403 | + "status": "bundle", |
| 404 | + "approved_worker_pubkey": [], |
| 405 | + "tags": [], |
| 406 | + "hostname": "", |
| 407 | + "ipaddr": "10.1.0.186", |
| 408 | + "setup_log": ( |
| 409 | + "/var/log/bluedata/install/" |
| 410 | + "k8shost_setup_10.1.0.186-" |
| 411 | + "2020-4-26-18-49-10" |
| 412 | + ), |
| 413 | + "_links": {"self": {"href": "/api/v2/worker/k8shost/5"}}, |
| 414 | + } |
| 415 | + ) |
| 416 | + return worker |
| 417 | + |
| 418 | + def mock_create_with_ssh_key(*args, **kwargs): |
| 419 | + return "/api/v2/worker/k8shost/5" |
| 420 | + |
| 421 | + def mock_get_client(): |
| 422 | + client = ContainerPlatformClient( |
| 423 | + username="", |
| 424 | + password="", |
| 425 | + api_host="", |
| 426 | + api_port=9090, |
| 427 | + use_ssl=True, |
| 428 | + verify_ssl=True, |
| 429 | + warn_ssl=True, |
| 430 | + ) |
| 431 | + client.session_id = "ABC" |
| 432 | + client.k8s_worker.get = mock_k8s_worker_get |
| 433 | + client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key |
| 434 | + return client |
404 | 435 |
|
405 | | - mock_create_with_ssh_key.assert_called_once_with( |
406 | | - ip="127.0.0.1", ssh_key_data="test_ssh_key", tags=[], |
407 | | - ) |
| 436 | + # support debugging if this test fails |
| 437 | + with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}): |
| 438 | + hpecp_cli = self.cli.CLI() |
| 439 | + |
| 440 | + # manually patch methods due to json serialization error |
| 441 | + # when using Mock or MagicMock |
| 442 | + self.cli.get_client = mock_get_client |
| 443 | + |
| 444 | + hpecp_cli.k8sworker.create_with_ssh_key( |
| 445 | + ip="127.0.0.1", ssh_key="test_ssh_key", |
| 446 | + ) |
408 | 447 |
|
409 | 448 | stdout = self.out.getvalue().strip() |
410 | 449 |
|
411 | | - self.assertEqual(stdout, "/api/v2/worker/k8shost/1") |
| 450 | + self.assertEqual(stdout, "/api/v2/worker/k8shost/5") |
412 | 451 |
|
413 | 452 | @patch("requests.post", side_effect=mocked_requests_post) |
414 | 453 | @patch("hpecp.k8s_worker") |
@@ -506,34 +545,64 @@ def test_with_only_ssh_key_content_provided_raises_general_exception( |
506 | 545 | "Expected: `{}`, Actual: `{}`".format(expected_err, stderr), |
507 | 546 | ) |
508 | 547 |
|
509 | | - @patch("requests.post", side_effect=mocked_requests_post) |
510 | | - @patch("hpecp.k8s_worker") |
511 | | - def test_with_only_ssh_key_file_provided(self, mock_post, mock_k8sworker): |
| 548 | + @patch("requests.post", side_effect=mocked_requests_post) # Login response |
| 549 | + def test_with_only_ssh_key_file_provided(self, mock_login_response): |
512 | 550 |
|
513 | 551 | ssh_key_file = tempfile.NamedTemporaryFile(delete=True, mode="w") |
514 | 552 | ssh_key_file.write("test_ssh_key_file_data") |
515 | 553 | ssh_key_file.flush() |
516 | 554 |
|
517 | | - with patch.object( |
518 | | - K8sWorkerController, |
519 | | - "create_with_ssh_key", |
520 | | - return_value="/api/v2/worker/k8shost/1", |
521 | | - ) as mock_create_with_ssh_key: |
522 | | - try: |
523 | | - hpecp_cli = self.cli.CLI() |
524 | | - hpecp_cli.k8sworker.create_with_ssh_key( |
525 | | - ip="127.0.0.1", ssh_key_file=ssh_key_file.name, |
526 | | - ) |
527 | | - except Exception as e: |
528 | | - self.fail("Unexpected exception. {}".format(e)) |
| 555 | + def mock_k8s_worker_get(*args, **kwargs): |
| 556 | + worker = WorkerK8s( |
| 557 | + { |
| 558 | + "status": "bundle", |
| 559 | + "approved_worker_pubkey": [], |
| 560 | + "tags": [], |
| 561 | + "hostname": "", |
| 562 | + "ipaddr": "10.1.0.186", |
| 563 | + "setup_log": ( |
| 564 | + "/var/log/bluedata/install/" |
| 565 | + "k8shost_setup_10.1.0.186-" |
| 566 | + "2020-4-26-18-49-10" |
| 567 | + ), |
| 568 | + "_links": {"self": {"href": "/api/v2/worker/k8shost/5"}}, |
| 569 | + } |
| 570 | + ) |
| 571 | + return worker |
| 572 | + |
| 573 | + def mock_create_with_ssh_key(*args, **kwargs): |
| 574 | + return "/api/v2/worker/k8shost/5" |
| 575 | + |
| 576 | + def mock_get_client(): |
| 577 | + client = ContainerPlatformClient( |
| 578 | + username="", |
| 579 | + password="", |
| 580 | + api_host="", |
| 581 | + api_port=9090, |
| 582 | + use_ssl=True, |
| 583 | + verify_ssl=True, |
| 584 | + warn_ssl=True, |
| 585 | + ) |
| 586 | + client.session_id = "ABC" |
| 587 | + client.k8s_worker.get = mock_k8s_worker_get |
| 588 | + client.k8s_worker.create_with_ssh_key = mock_create_with_ssh_key |
| 589 | + return client |
529 | 590 |
|
530 | | - mock_create_with_ssh_key.assert_called_once_with( |
531 | | - ip="127.0.0.1", ssh_key_data="test_ssh_key_file_data", tags=[], |
532 | | - ) |
| 591 | + # support debugging if this test fails |
| 592 | + with patch.dict("os.environ", {"LOG_LEVEL": "DEBUG"}): |
| 593 | + hpecp_cli = self.cli.CLI() |
| 594 | + |
| 595 | + # manually patch methods due to json serialization error |
| 596 | + # when using Mock or MagicMock |
| 597 | + self.cli.get_client = mock_get_client |
| 598 | + |
| 599 | + hpecp_cli.k8sworker.create_with_ssh_key( |
| 600 | + ip="127.0.0.1", ssh_key_file=ssh_key_file.name |
| 601 | + ) |
533 | 602 |
|
534 | 603 | stdout = self.out.getvalue().strip() |
535 | 604 |
|
536 | | - self.assertEqual(stdout, "/api/v2/worker/k8shost/1") |
| 605 | + self.assertEqual(stdout, "/api/v2/worker/k8shost/5") |
537 | 606 |
|
538 | 607 | ssh_key_file.close() |
539 | 608 |
|
|
0 commit comments