From aeb87ba2795ace7dcf0e15a3f691f7783c1dbb0c Mon Sep 17 00:00:00 2001 From: jneo8 Date: Fri, 10 Jan 2025 16:23:32 +0800 Subject: [PATCH] fix: Read region configuration to create watcher client - fix/Read watcher configuration from clusterdb to build watcher client - doc/Update guests_on_hypervisor func docstring - doc/Add comment for TIMEOUT and TIMEOUT_INTERVAL variables --- sunbeam-python/sunbeam/core/openstack_api.py | 2 +- sunbeam-python/sunbeam/core/watcher.py | 17 ++++++++---- .../tests/unit/sunbeam/core/test_watcher.py | 27 ++++++++++++++----- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/sunbeam-python/sunbeam/core/openstack_api.py b/sunbeam-python/sunbeam/core/openstack_api.py index c19a5ce9..a4ccb251 100644 --- a/sunbeam-python/sunbeam/core/openstack_api.py +++ b/sunbeam-python/sunbeam/core/openstack_api.py @@ -48,7 +48,7 @@ def guests_on_hypervisor( """Return a list of guests that run on the given hypervisor. :param hypervisor_name: Name of hypervisor - :param jhelper: Juju helpers for retrieving admin credentials + :param conn: Admin connection :raises: openstack.exceptions.SDKException """ return list( diff --git a/sunbeam-python/sunbeam/core/watcher.py b/sunbeam-python/sunbeam/core/watcher.py index 9acf4002..06c31539 100644 --- a/sunbeam-python/sunbeam/core/watcher.py +++ b/sunbeam-python/sunbeam/core/watcher.py @@ -22,13 +22,17 @@ from watcherclient.common.apiclient.exceptions import NotFound from watcherclient.v1 import client as watcher_client -from sunbeam.core.common import SunbeamException +from sunbeam.core.common import SunbeamException, read_config +from sunbeam.core.deployment import Deployment from sunbeam.core.juju import JujuHelper from sunbeam.core.openstack_api import get_admin_connection +from sunbeam.steps.openstack import REGION_CONFIG_KEY LOG = logging.getLogger(__name__) +# Timeout while waiting for the watcher resource to reach the target state. TIMEOUT = 60 * 3 +# Sleep interval between querying watcher resources. SLEEP_INTERVAL = 5 ENABLE_MAINTENANCE_AUDIT_TEMPLATE_NAME = "Sunbeam Cluster Maintaining Template" ENABLE_MAINTENANCE_STRATEGY_NAME = "host_maintenance" @@ -39,12 +43,15 @@ WORKLOAD_BALANCING_AUDIT_TEMPLATE_NAME = "Sunbeam Cluster Workload Balancing Template" -def get_watcher_client(jhelper: JujuHelper) -> watcher_client.Client: - conn = get_admin_connection(jhelper=jhelper) +def get_watcher_client(deployment: Deployment) -> watcher_client.Client: + region = read_config(deployment.get_client(), REGION_CONFIG_KEY)["region"] + conn = get_admin_connection( + jhelper=JujuHelper(deployment.get_connected_controller()) + ) + watcher_endpoint = conn.session.get_endpoint( service_type="infra-optim", - # TODO: get region - region_name="RegionOne", + region_name=region, ) return watcher_client.Client(session=conn.session, endpoint=watcher_endpoint) diff --git a/sunbeam-python/tests/unit/sunbeam/core/test_watcher.py b/sunbeam-python/tests/unit/sunbeam/core/test_watcher.py index d2996aa3..1c4e2f65 100644 --- a/sunbeam-python/tests/unit/sunbeam/core/test_watcher.py +++ b/sunbeam-python/tests/unit/sunbeam/core/test_watcher.py @@ -18,23 +18,38 @@ import sunbeam.core.watcher as watcher_helper from sunbeam.core.common import SunbeamException -from sunbeam.core.juju import JujuHelper +from sunbeam.core.deployment import Deployment +@patch("sunbeam.core.watcher.read_config") +@patch("sunbeam.core.watcher.JujuHelper") @patch("sunbeam.core.watcher.get_admin_connection") @patch("sunbeam.core.watcher.watcher_client.Client") -def test_get_watcher_client(mock_watcher_client, mock_get_admin_connection): +def test_get_watcher_client( + mock_watcher_client, + mock_get_admin_connection, + mock_jhelper, + mock_read_config, +): mock_conn = Mock() mock_conn.session.get_endpoint.return_value = "fake_endpoint" + mock_read_config.return_value = {"region": "fake_region"} mock_get_admin_connection.return_value = mock_conn - mock_jhelper = Mock(spec=JujuHelper) + mock_deployment = Mock(spec=Deployment) - client = watcher_helper.get_watcher_client(mock_jhelper) + client = watcher_helper.get_watcher_client(mock_deployment) + + mock_read_config.assert_called_once_with( + mock_deployment.get_client.return_value, "Region" + ) + mock_jhelper.assert_called_once_with( + mock_deployment.get_connected_controller.return_value + ) + mock_get_admin_connection.assert_called_once_with(jhelper=mock_jhelper.return_value) - mock_get_admin_connection.assert_called_once_with(jhelper=mock_jhelper) mock_conn.session.get_endpoint.assert_called_once_with( service_type="infra-optim", - region_name="RegionOne", + region_name="fake_region", ) mock_watcher_client.assert_called_once_with( session=mock_conn.session,