| 1 |
"""Tests for ``GoveeController.start(require_all=...)`` partial-bind support. |
| 2 |
|
| 3 |
Bind failures are simulated by patching ``_create_listening_socket`` — the |
| 4 |
single funnel for socket creation and ``bind()`` — to raise ``OSError`` for |
| 5 |
chosen addresses. ``create_datagram_endpoint`` is stubbed so no real socket |
| 6 |
or event loop is needed. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import asyncio |
| 12 |
import errno |
| 13 |
import ipaddress |
| 14 |
import unittest |
| 15 |
from unittest.mock import Mock |
| 16 |
|
| 17 |
from govee_local_api.controller import GoveeController |
| 18 |
|
| 19 |
ADDRESSES = ["192.168.1.100/24", "10.0.0.100/8", "172.16.1.100/16"] |
| 20 |
|
| 21 |
|
| 22 |
def _make_controller(addresses=ADDRESSES) -> tuple[GoveeController, Mock]: |
| 23 |
"""Controller with a Mock loop, periodic tasks disabled and a Mock logger.""" |
| 24 |
logger = Mock() |
| 25 |
controller = GoveeController( |
| 26 |
loop=Mock(), |
| 27 |
listening_addresses=addresses, |
| 28 |
discovery_enabled=False, |
| 29 |
update_enabled=False, |
| 30 |
evict_enabled=False, |
| 31 |
logger=logger, |
| 32 |
) |
| 33 |
return controller, logger |
| 34 |
|
| 35 |
|
| 36 |
def _install_endpoints( |
| 37 |
controller: GoveeController, failures: dict[str, OSError] | None = None |
| 38 |
) -> dict[str, Mock]: |
| 39 |
"""Stub socket creation + endpoint creation. |
| 40 |
|
| 41 |
Addresses in ``failures`` raise the given OSError from |
| 42 |
``_create_listening_socket``; every other address yields a Mock transport |
| 43 |
(recorded in the returned dict by address) and a Mock protocol. |
| 44 |
""" |
| 45 |
failures = failures or {} |
| 46 |
transports: dict[str, Mock] = {} |
| 47 |
|
| 48 |
def create_socket(address): |
| 49 |
if address in failures: |
| 50 |
raise failures[address] |
| 51 |
sock = Mock(name=f"sock-{address}") |
| 52 |
sock.address = address |
| 53 |
return sock |
| 54 |
|
| 55 |
async def create_endpoint(protocol_factory, sock=None): |
| 56 |
transport = Mock(name=f"transport-{sock.address}") |
| 57 |
transport.is_closing.return_value = False |
| 58 |
transports[sock.address] = transport |
| 59 |
return transport, protocol_factory() |
| 60 |
|
| 61 |
controller._create_listening_socket = create_socket # type: ignore[method-assign] |
| 62 |
controller._loop.create_datagram_endpoint = create_endpoint # type: ignore[method-assign] |
| 63 |
return transports |
| 64 |
|
| 65 |
|
| 66 |
def _run(coro): |
| 67 |
return asyncio.run(coro) |
| 68 |
|
| 69 |
|
| 70 |
class TestAllBind(unittest.TestCase): |
| 71 |
def test_all_bind_reports_no_failures(self): |
| 72 |
controller, logger = _make_controller() |
| 73 |
transports = _install_endpoints(controller) |
| 74 |
|
| 75 |
_run(controller.start(require_all=False)) |
| 76 |
|
| 77 |
self.assertEqual(controller.bind_failures, []) |
| 78 |
self.assertEqual( |
| 79 |
controller.listening_addresses, |
| 80 |
["192.168.1.100", "10.0.0.100", "172.16.1.100"], |
| 81 |
) |
| 82 |
self.assertEqual(len(controller.networks), 3) |
| 83 |
self.assertEqual(len(controller._transports), 3) |
| 84 |
self.assertEqual(len(controller._protocols), 3) |
| 85 |
self.assertEqual( |
| 86 |
controller._transports, |
| 87 |
[transports[a] for a in controller.listening_addresses], |
| 88 |
) |
| 89 |
logger.warning.assert_not_called() |
| 90 |
|
| 91 |
def test_default_is_require_all(self): |
| 92 |
controller, _ = _make_controller() |
| 93 |
_install_endpoints(controller) |
| 94 |
_run(controller.start()) |
| 95 |
self.assertEqual(len(controller._transports), 3) |
| 96 |
self.assertEqual(controller.bind_failures, []) |
| 97 |
|
| 98 |
|
| 99 |
class TestPartialBind(unittest.TestCase): |
| 100 |
def test_one_failure_keeps_the_rest(self): |
| 101 |
controller, logger = _make_controller() |
| 102 |
error = OSError(errno.EADDRNOTAVAIL, "Cannot assign requested address") |
| 103 |
_install_endpoints(controller, {"10.0.0.100": error}) |
| 104 |
|
| 105 |
_run(controller.start(require_all=False)) |
| 106 |
|
| 107 |
self.assertEqual( |
| 108 |
controller.listening_addresses, ["192.168.1.100", "172.16.1.100"] |
| 109 |
) |
| 110 |
self.assertEqual( |
| 111 |
controller.networks, |
| 112 |
[ |
| 113 |
ipaddress.ip_network("192.168.1.0/24"), |
| 114 |
ipaddress.ip_network("172.16.0.0/16"), |
| 115 |
], |
| 116 |
) |
| 117 |
self.assertEqual(len(controller._transports), 2) |
| 118 |
self.assertEqual(len(controller._protocols), 2) |
| 119 |
self.assertEqual(controller.bind_failures, [("10.0.0.100", error)]) |
| 120 |
self.assertIs(controller.bind_failures[0][1], error) |
| 121 |
# One warning per failed address plus one summary line. |
| 122 |
self.assertEqual(logger.warning.call_count, 2) |
| 123 |
logger.error.assert_not_called() |
| 124 |
|
| 125 |
def test_index_alignment_after_partial_bind(self): |
| 126 |
"""Dropping the failed address from only one of the parallel lists |
| 127 |
would silently route commands out of the wrong interface.""" |
| 128 |
controller, _ = _make_controller() |
| 129 |
_install_endpoints( |
| 130 |
controller, |
| 131 |
{"192.168.1.100": OSError(errno.EADDRNOTAVAIL, "stale")}, |
| 132 |
) |
| 133 |
|
| 134 |
_run(controller.start(require_all=False)) |
| 135 |
|
| 136 |
transports = controller._transports |
| 137 |
self.assertIs( |
| 138 |
controller._get_best_transport_for_ip("10.20.30.40"), transports[0] |
| 139 |
) |
| 140 |
self.assertIs( |
| 141 |
controller._get_best_transport_for_ip("172.16.200.1"), transports[1] |
| 142 |
) |
| 143 |
# And the mapping is address → the transport actually bound there. |
| 144 |
by_address = dict(zip(controller.listening_addresses, transports)) |
| 145 |
self.assertEqual(by_address["10.0.0.100"]._mock_name, "transport-10.0.0.100") |
| 146 |
self.assertEqual( |
| 147 |
by_address["172.16.1.100"]._mock_name, "transport-172.16.1.100" |
| 148 |
) |
| 149 |
|
| 150 |
def test_protocols_are_aligned_with_transports(self): |
| 151 |
controller, _ = _make_controller() |
| 152 |
_install_endpoints( |
| 153 |
controller, {"10.0.0.100": OSError(errno.EADDRNOTAVAIL, "stale")} |
| 154 |
) |
| 155 |
_run(controller.start(require_all=False)) |
| 156 |
for address, protocol in zip( |
| 157 |
controller.listening_addresses, controller._protocols |
| 158 |
): |
| 159 |
self.assertEqual(protocol.listening_address, address) |
| 160 |
|
| 161 |
def test_bind_failures_returns_copy(self): |
| 162 |
controller, _ = _make_controller() |
| 163 |
_install_endpoints( |
| 164 |
controller, {"10.0.0.100": OSError(errno.EADDRNOTAVAIL, "stale")} |
| 165 |
) |
| 166 |
_run(controller.start(require_all=False)) |
| 167 |
controller.bind_failures.clear() |
| 168 |
self.assertEqual(len(controller.bind_failures), 1) |
| 169 |
|
| 170 |
|
| 171 |
class TestTotalFailure(unittest.TestCase): |
| 172 |
def test_all_fail_raises_oserror_with_errno(self): |
| 173 |
controller, _ = _make_controller() |
| 174 |
_install_endpoints( |
| 175 |
controller, |
| 176 |
{a.split("/")[0]: OSError(errno.EADDRNOTAVAIL, "stale") for a in ADDRESSES}, |
| 177 |
) |
| 178 |
|
| 179 |
with self.assertRaises(OSError) as cm: |
| 180 |
_run(controller.start(require_all=False)) |
| 181 |
|
| 182 |
self.assertIs(type(cm.exception), OSError) |
| 183 |
self.assertEqual(cm.exception.errno, errno.EADDRNOTAVAIL) |
| 184 |
self.assertEqual(controller._transports, []) |
| 185 |
self.assertEqual(controller._protocols, []) |
| 186 |
self.assertEqual(len(controller.bind_failures), 3) |
| 187 |
|
| 188 |
def test_mixed_errnos_prefer_eaddrinuse(self): |
| 189 |
controller, _ = _make_controller() |
| 190 |
inuse = OSError(errno.EADDRINUSE, "Address already in use") |
| 191 |
_install_endpoints( |
| 192 |
controller, |
| 193 |
{ |
| 194 |
"192.168.1.100": OSError(errno.EADDRNOTAVAIL, "stale"), |
| 195 |
"10.0.0.100": inuse, |
| 196 |
"172.16.1.100": OSError(errno.EACCES, "denied"), |
| 197 |
}, |
| 198 |
) |
| 199 |
|
| 200 |
with self.assertRaises(OSError) as cm: |
| 201 |
_run(controller.start(require_all=False)) |
| 202 |
|
| 203 |
self.assertIs(cm.exception, inuse) |
| 204 |
self.assertEqual(cm.exception.errno, errno.EADDRINUSE) |
| 205 |
|
| 206 |
def test_no_eaddrinuse_raises_first_collected(self): |
| 207 |
controller, _ = _make_controller() |
| 208 |
first = OSError(errno.EADDRNOTAVAIL, "stale") |
| 209 |
_install_endpoints( |
| 210 |
controller, |
| 211 |
{ |
| 212 |
"192.168.1.100": first, |
| 213 |
"10.0.0.100": OSError(errno.EACCES, "denied"), |
| 214 |
"172.16.1.100": OSError(errno.EPERM, "denied"), |
| 215 |
}, |
| 216 |
) |
| 217 |
with self.assertRaises(OSError) as cm: |
| 218 |
_run(controller.start(require_all=False)) |
| 219 |
self.assertIs(cm.exception, first) |
| 220 |
|
| 221 |
|
| 222 |
class TestRequireAll(unittest.TestCase): |
| 223 |
def test_one_failure_raises_and_closes_everything(self): |
| 224 |
"""Regression guard for the socket-leak protection: with |
| 225 |
require_all=True a single failure must leave no transport open.""" |
| 226 |
controller, _ = _make_controller() |
| 227 |
error = OSError(errno.EADDRINUSE, "Address already in use") |
| 228 |
transports = _install_endpoints(controller, {"10.0.0.100": error}) |
| 229 |
|
| 230 |
with self.assertRaises(OSError) as cm: |
| 231 |
_run(controller.start()) |
| 232 |
|
| 233 |
self.assertIs(cm.exception, error) |
| 234 |
self.assertEqual(controller._transports, []) |
| 235 |
self.assertEqual(controller._protocols, []) |
| 236 |
# The endpoint bound before the failure was closed; the one after |
| 237 |
# the failure was never attempted. |
| 238 |
self.assertEqual(set(transports), {"192.168.1.100"}) |
| 239 |
transports["192.168.1.100"].close.assert_called_once() |
| 240 |
# Nothing was tolerated, so nothing is reported as tolerated either. |
| 241 |
self.assertEqual(controller.bind_failures, []) |
| 242 |
|
| 243 |
def test_endpoint_creation_failure_closes_socket_and_raises(self): |
| 244 |
controller, _ = _make_controller() |
| 245 |
socks: list[Mock] = [] |
| 246 |
|
| 247 |
def create_socket(address): |
| 248 |
sock = Mock(name=f"sock-{address}") |
| 249 |
socks.append(sock) |
| 250 |
return sock |
| 251 |
|
| 252 |
async def create_endpoint(protocol_factory, sock=None): |
| 253 |
raise OSError(errno.EBADF, "bad fd") |
| 254 |
|
| 255 |
controller._create_listening_socket = create_socket # type: ignore[method-assign] |
| 256 |
controller._loop.create_datagram_endpoint = create_endpoint # type: ignore[method-assign] |
| 257 |
|
| 258 |
with self.assertRaises(OSError): |
| 259 |
_run(controller.start()) |
| 260 |
self.assertEqual(len(socks), 1) |
| 261 |
socks[0].close.assert_called_once() |
| 262 |
|
| 263 |
|
| 264 |
class TestRestart(unittest.TestCase): |
| 265 |
def test_second_start_retries_full_configured_set(self): |
| 266 |
controller, _ = _make_controller() |
| 267 |
_install_endpoints( |
| 268 |
controller, {"10.0.0.100": OSError(errno.EADDRNOTAVAIL, "stale")} |
| 269 |
) |
| 270 |
_run(controller.start(require_all=False)) |
| 271 |
self.assertEqual(len(controller.listening_addresses), 2) |
| 272 |
self.assertEqual(len(controller.bind_failures), 1) |
| 273 |
|
| 274 |
# The adapter came back: every configured address binds this time. |
| 275 |
attempted: list[str] = [] |
| 276 |
real_create = _install_endpoints(controller) |
| 277 |
original = controller._create_listening_socket |
| 278 |
|
| 279 |
def recording(address): |
| 280 |
attempted.append(address) |
| 281 |
return original(address) |
| 282 |
|
| 283 |
controller._create_listening_socket = recording # type: ignore[method-assign] |
| 284 |
_run(controller.start(require_all=False)) |
| 285 |
|
| 286 |
self.assertEqual(attempted, ["192.168.1.100", "10.0.0.100", "172.16.1.100"]) |
| 287 |
self.assertEqual( |
| 288 |
controller.listening_addresses, |
| 289 |
["192.168.1.100", "10.0.0.100", "172.16.1.100"], |
| 290 |
) |
| 291 |
self.assertEqual(len(controller.networks), 3) |
| 292 |
self.assertEqual(len(controller._transports), 3) |
| 293 |
self.assertEqual(len(real_create), 3) |
| 294 |
# bind_failures reflects only the most recent start(). |
| 295 |
self.assertEqual(controller.bind_failures, []) |
| 296 |
|
| 297 |
def test_bind_failures_reset_even_when_start_raises(self): |
| 298 |
controller, _ = _make_controller() |
| 299 |
_install_endpoints( |
| 300 |
controller, |
| 301 |
{a.split("/")[0]: OSError(errno.EADDRNOTAVAIL, "stale") for a in ADDRESSES}, |
| 302 |
) |
| 303 |
with self.assertRaises(OSError): |
| 304 |
_run(controller.start(require_all=False)) |
| 305 |
self.assertEqual(len(controller.bind_failures), 3) |
| 306 |
|
| 307 |
_install_endpoints(controller) |
| 308 |
_run(controller.start(require_all=False)) |
| 309 |
self.assertEqual(controller.bind_failures, []) |
| 310 |
self.assertEqual(len(controller._transports), 3) |
| 311 |
|
| 312 |
|
| 313 |
if __name__ == "__main__": |
| 314 |
unittest.main() |