diff --git a/tests/services/openstack/test_service_fixed_get_mac.py b/tests/services/openstack/test_service_fixed_get_mac.py index 9fc4bcb04..389fcfa81 100644 --- a/tests/services/openstack/test_service_fixed_get_mac.py +++ b/tests/services/openstack/test_service_fixed_get_mac.py @@ -43,8 +43,18 @@ from ...utils.test import UDSTransactionTestCase +from uds.services.OpenStack.openstack import client from uds.services.OpenStack.openstack import types as openstack_types +# Undecorated mac-resolution logic (the @cached wrapper would need a real cache backend). +# Running it against the mocked api keeps these tests exercising the real behaviour while +# the service simply delegates to api.get_server_mac(). +_real_get_server_mac = getattr(client.OpenStackClient.get_server_mac, '__wrapped__') + + +def _bind_real_get_server_mac(api: mock.MagicMock) -> None: + api.get_server_mac.side_effect = lambda vmid, **kw: _real_get_server_mac(api, vmid, **kw) + def _server_with_addresses( addresses: list[openstack_types.ServerInfo.AddresInfo], @@ -72,6 +82,7 @@ def _service_with_api(self) -> tuple[typing.Any, mock.MagicMock]: with fixtures.patched_provider() as prov: service = fixtures.create_fixed_service(prov) api = mock.MagicMock() + _bind_real_get_server_mac(api) service._api = api # the api property returns the cached client without hitting the provider return service, api @@ -98,14 +109,14 @@ def test_get_mac_falls_back_to_neutron_port(self) -> None: self.assertEqual(mac, 'FA:16:3E:AB:CD:EF') api.list_ports.assert_called_once_with(device_id='sid1') - def test_get_mac_empty_when_server_not_found(self) -> None: - # Machine missing: no IndexError, no leak -> '' so the caller can retry + def test_get_mac_raises_when_server_not_found(self) -> None: + # Server missing: get_server_mac does not swallow it; the caller handles NotFoundError. service, api = self._service_with_api() api.get_server_info.side_effect = exceptions.services.generics.NotFoundError('Not found') - mac = service.get_mac('sid1') + with self.assertRaises(exceptions.services.generics.NotFoundError): + service.get_mac('sid1') - self.assertEqual(mac, '') api.list_ports.assert_not_called() def test_get_mac_empty_when_no_addresses_and_no_ports(self) -> None: diff --git a/tests/services/openstack/test_service_get_mac.py b/tests/services/openstack/test_service_get_mac.py index 92bfaeba5..fda34b40c 100644 --- a/tests/services/openstack/test_service_get_mac.py +++ b/tests/services/openstack/test_service_get_mac.py @@ -46,8 +46,18 @@ from ...utils.test import UDSTransactionTestCase +from uds.services.OpenStack.openstack import client from uds.services.OpenStack.openstack import types as openstack_types +# Undecorated mac-resolution logic (the @cached wrapper would need a real cache backend). +# Running it against the mocked api keeps these tests exercising the real behaviour while +# the service simply delegates to api.get_server_mac(). +_real_get_server_mac = getattr(client.OpenStackClient.get_server_mac, '__wrapped__') + + +def _bind_real_get_server_mac(api: mock.MagicMock) -> None: + api.get_server_mac.side_effect = lambda vmid, **kw: _real_get_server_mac(api, vmid, **kw) + def _server_with_addresses( addresses: list[openstack_types.ServerInfo.AddresInfo], @@ -75,6 +85,7 @@ def _service_with_api(self) -> tuple[typing.Any, mock.MagicMock]: with fixtures.patched_provider() as prov: service = fixtures.create_live_service(prov) api = mock.MagicMock() + _bind_real_get_server_mac(api) service.cached_api = api # property returns this without hitting the provider return service, api @@ -105,14 +116,15 @@ def test_get_mac_falls_back_to_neutron_port(self) -> None: self.assertEqual(mac, 'FA:16:3E:AB:CD:EF') api.list_ports.assert_called_once_with(device_id='sid1') - def test_get_mac_empty_when_server_not_found(self) -> None: - # Machine still being prepared: server not queryable yet -> '' (retry, no ERROR) + def test_get_mac_raises_when_server_not_found(self) -> None: + # Server not queryable yet: get_server_mac does not swallow it; the caller/state + # checker handles the NotFoundError. service, api = self._service_with_api() api.get_server_info.side_effect = exceptions.services.generics.NotFoundError('Not found') - mac = service.get_mac(None, 'sid1', for_unique_id=True) + with self.assertRaises(exceptions.services.generics.NotFoundError): + service.get_mac(None, 'sid1', for_unique_id=True) - self.assertEqual(mac, '') api.list_ports.assert_not_called() def test_get_mac_empty_when_no_addresses_and_no_ports(self) -> None: