From 7261730dffce4bd7cac4f3f0bfe1f97a43599e0b Mon Sep 17 00:00:00 2001 From: Nguyen Ngoc Hieu Date: Fri, 6 Oct 2023 01:52:49 +0700 Subject: [PATCH] Fix health monitor information retrieval in API response Closes bug: #2038367 Behavior: In the response body of the LB API when creating a new load balancer, the information about the health monitor is always null, even though it has been configured. Reproduce: Using the Octavia API to create a new LB with all components. You cannot see any information about the health monitor that will be returned. Proposed Fix: Modify the assignment to use `data_model.health_monitor` instead of `pool.healthmonitor`. Change-Id: Ia914ad89b6fdf3606c3d4bff0a4c425348c15e0c --- octavia/api/v2/types/pool.py | 2 +- octavia/tests/unit/api/v2/types/test_pool.py | 71 +++++++++++++++++++ ...eval-in-api-response-d3b2e02a3a966f60.yaml | 7 ++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/fix-health-monitor-info-retrieval-in-api-response-d3b2e02a3a966f60.yaml diff --git a/octavia/api/v2/types/pool.py b/octavia/api/v2/types/pool.py index f3abead7f4..cd1148daf7 100644 --- a/octavia/api/v2/types/pool.py +++ b/octavia/api/v2/types/pool.py @@ -106,7 +106,7 @@ class PoolResponse(BasePoolType): if cls._full_response(): del pool.loadbalancers member_model = member.MemberFullResponse - if pool.healthmonitor: + if data_model.health_monitor: pool.healthmonitor = ( health_monitor.HealthMonitorFullResponse .from_data_model(data_model.health_monitor)) diff --git a/octavia/tests/unit/api/v2/types/test_pool.py b/octavia/tests/unit/api/v2/types/test_pool.py index 0007ae7a86..c7013ff9bc 100644 --- a/octavia/tests/unit/api/v2/types/test_pool.py +++ b/octavia/tests/unit/api/v2/types/test_pool.py @@ -17,8 +17,12 @@ from wsme import exc from wsme.rest import json as wsme_json from wsme import types as wsme_types +from octavia.api.common import types +from octavia.api.v2.types import health_monitor as health_monitor_type +from octavia.api.v2.types import member as member_type from octavia.api.v2.types import pool as pool_type from octavia.common import constants +from octavia.common import data_models from octavia.tests.unit.api.common import base @@ -224,3 +228,70 @@ class TestSessionPersistencePUT(base.BaseTypesTest, TestSessionPersistence): body = {"cookie_name": "cookie\nmonster"} self.assertRaises(exc.InvalidInput, wsme_json.fromjson, self._type, body) + + +class TestPoolResponse(base.BaseTypesTest): + + _type = pool_type.PoolResponse + + def test_pool_response_with_health_monitor(self): + health_monitor_id = uuidutils.generate_uuid() + health_monitor_model = data_models.HealthMonitor(id=health_monitor_id) + pool_model = data_models.Pool(health_monitor=health_monitor_model) + pool = self._type.from_data_model(data_model=pool_model) + self.assertEqual(pool.healthmonitor_id, health_monitor_id) + + def test_pool_response_with_members(self): + member_id = uuidutils.generate_uuid() + members = [data_models.Member(id=member_id)] + pool_model = data_models.Pool(members=members) + pool = self._type.from_data_model(data_model=pool_model) + self.assertIsInstance(pool.members[0], types.IdOnlyType) + self.assertEqual(pool.members[0].id, member_id) + + def test_pool_response_with_load_balancer(self): + load_balancer_id = uuidutils.generate_uuid() + load_balancer = data_models.LoadBalancer(id=load_balancer_id) + pool_model = data_models.Pool(load_balancer=load_balancer) + pool = self._type.from_data_model(data_model=pool_model) + self.assertIsInstance(pool.loadbalancers[0], types.IdOnlyType) + self.assertEqual(pool.loadbalancers[0].id, load_balancer_id) + + def test_pool_response_with_session_persistence(self): + session_persistence = data_models.SessionPersistence( + cookie_name="test" + ) + pool_model = data_models.Pool(session_persistence=session_persistence) + pool = self._type.from_data_model(data_model=pool_model) + self.assertEqual(pool.session_persistence.cookie_name, "test") + + def test_pool_response_without_children(self): + pool = self._type.from_data_model(data_model=data_models.Pool()) + self.assertEqual(len(pool.loadbalancers), 0) + self.assertIsNone(pool.session_persistence) + self.assertEqual(len(pool.members), 0) + self.assertEqual(len(pool.listeners), 0) + self.assertEqual(pool.healthmonitor_id, wsme_types.Unset) + + +class TestPoolFullResponse(base.BaseTypesTest): + + _type = pool_type.PoolFullResponse + + def test_pool_full_response_with_health_monitor(self): + health_monitor_model = data_models.HealthMonitor() + pool_model = data_models.Pool(health_monitor=health_monitor_model) + pool = self._type.from_data_model(data_model=pool_model) + self.assertIsInstance( + pool.healthmonitor, health_monitor_type.HealthMonitorFullResponse + ) + + def test_pool_full_response_with_members(self): + members = [data_models.Member()] + pool_model = data_models.Pool(members=members) + pool = self._type.from_data_model(data_model=pool_model) + self.assertIsInstance(pool.members[0], member_type.MemberFullResponse) + + def test_pool_full_response_without_children(self): + pool = self._type.from_data_model(data_model=data_models.Pool()) + self.assertIsNone(pool.healthmonitor) diff --git a/releasenotes/notes/fix-health-monitor-info-retrieval-in-api-response-d3b2e02a3a966f60.yaml b/releasenotes/notes/fix-health-monitor-info-retrieval-in-api-response-d3b2e02a3a966f60.yaml new file mode 100644 index 0000000000..91a6732349 --- /dev/null +++ b/releasenotes/notes/fix-health-monitor-info-retrieval-in-api-response-d3b2e02a3a966f60.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Bug fix: The response body of the LB API, when creating a new load + balancer, now correctly includes information about the health monitor. + Previously, this information was consistently null, despite configuring + a health monitor.