Skip to content

Commit e6e0a60

Browse files
authored
chore: add a zc fixture and service info helper to dry up the tests (#1870)
1 parent 4307523 commit e6e0a60

7 files changed

Lines changed: 247 additions & 500 deletions

File tree

‎tests/__init__.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import ifaddr
1818

19-
from zeroconf import DNSIncoming, DNSOutgoing, DNSQuestion, DNSRecord, Zeroconf, const
19+
from zeroconf import DNSIncoming, DNSOutgoing, DNSQuestion, DNSRecord, ServiceInfo, Zeroconf, const
2020
from zeroconf._history import QuestionHistory
2121

2222
_MONOTONIC_RESOLUTION = time.get_clock_info("monotonic").resolution
@@ -59,6 +59,28 @@ def suppresses(self, question: DNSQuestion, now: float, known_answers: set[DNSRe
5959
return False
6060

6161

62+
def make_service_info(
63+
type_: str,
64+
name: str,
65+
*,
66+
port: int = 80,
67+
properties: dict | bytes | None = None,
68+
server: str = "spare-rig.local.",
69+
addresses: list[bytes] | None = None,
70+
) -> ServiceInfo:
71+
"""Build a ServiceInfo with the suite's canonical fixture values."""
72+
return ServiceInfo(
73+
type_,
74+
name,
75+
port,
76+
0,
77+
0,
78+
{"path": "/healthz/"} if properties is None else properties,
79+
server,
80+
addresses=[socket.inet_aton("10.7.4.2")] if addresses is None else addresses,
81+
)
82+
83+
6284
def mock_incoming_msg(records: Iterable[DNSRecord]) -> DNSIncoming:
6385
"""Build a `DNSIncoming` response message from a list of `DNSRecord`s."""
6486
generated = DNSOutgoing(const._FLAGS_QR_RESPONSE)

‎tests/conftest.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ def blockbuster(
9595
yield bb
9696

9797

98+
@pytest.fixture
99+
def zc() -> Iterator[Zeroconf]:
100+
"""A running Zeroconf bound to loopback, closed after the test."""
101+
instance = Zeroconf(interfaces=["127.0.0.1"])
102+
yield instance
103+
instance.close()
104+
105+
98106
@pytest.fixture(autouse=True)
99107
def verify_threads_ended():
100108
"""Verify that the threads are not running after the test."""

‎tests/services/test_registry.py‎

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import zeroconf as r
99
from zeroconf import ServiceInfo
1010

11+
from .. import make_service_info
12+
1113

1214
class TestServiceRegistry(unittest.TestCase):
1315
def test_only_register_once(self):
@@ -16,16 +18,7 @@ def test_only_register_once(self):
1618
registration_name = f"{name}.{type_}"
1719

1820
desc = {"path": "/healthz/"}
19-
info = ServiceInfo(
20-
type_,
21-
registration_name,
22-
80,
23-
0,
24-
0,
25-
desc,
26-
"spare-rig.local.",
27-
addresses=[socket.inet_aton("10.7.4.2")],
28-
)
21+
info = make_service_info(type_, registration_name, properties=desc)
2922

3023
registry = r.ServiceRegistry()
3124
registry.async_add(info)
@@ -83,16 +76,7 @@ def test_unregister_multiple_times(self):
8376
registration_name = f"{name}.{type_}"
8477

8578
desc = {"path": "/healthz/"}
86-
info = ServiceInfo(
87-
type_,
88-
registration_name,
89-
80,
90-
0,
91-
0,
92-
desc,
93-
"spare-rig.local.",
94-
addresses=[socket.inet_aton("10.7.4.2")],
95-
)
79+
info = make_service_info(type_, registration_name, properties=desc)
9680

9781
registry = r.ServiceRegistry()
9882
registry.async_add(info)
@@ -106,16 +90,7 @@ def test_lookups(self):
10690
registration_name = f"{name}.{type_}"
10791

10892
desc = {"path": "/healthz/"}
109-
info = ServiceInfo(
110-
type_,
111-
registration_name,
112-
80,
113-
0,
114-
0,
115-
desc,
116-
"spare-rig.local.",
117-
addresses=[socket.inet_aton("10.7.4.2")],
118-
)
93+
info = make_service_info(type_, registration_name, properties=desc)
11994

12095
registry = r.ServiceRegistry()
12196
registry.async_add(info)
@@ -156,16 +131,7 @@ def test_empty_buckets_are_removed_when_last_entry_is_removed(self):
156131
type_ = "_test-srvc-type._tcp.local."
157132
registration_name = f"xxxyyy.{type_}"
158133
desc = {"path": "/healthz/"}
159-
info = ServiceInfo(
160-
type_,
161-
registration_name,
162-
80,
163-
0,
164-
0,
165-
desc,
166-
"spare-rig.local.",
167-
addresses=[socket.inet_aton("10.7.4.2")],
168-
)
134+
info = make_service_info(type_, registration_name, properties=desc)
169135

170136
registry = r.ServiceRegistry()
171137
registry.async_add(info)

‎tests/services/test_types.py‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
import zeroconf as r
1212
from zeroconf import ServiceInfo, Zeroconf, ZeroconfServiceTypes
1313

14-
from .. import IPV6_LOOPBACK_FIND_TIMEOUT, LOOPBACK_FIND_TIMEOUT, _clear_cache, has_working_ipv6
14+
from .. import (
15+
IPV6_LOOPBACK_FIND_TIMEOUT,
16+
LOOPBACK_FIND_TIMEOUT,
17+
_clear_cache,
18+
has_working_ipv6,
19+
make_service_info,
20+
)
1521

1622
log = logging.getLogger("zeroconf")
1723
original_logging_level = logging.NOTSET
@@ -35,16 +41,7 @@ def test_integration_with_listener(quick_timing, disable_duplicate_packet_suppre
3541

3642
zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"])
3743
desc = {"path": "/healthz/"}
38-
info = ServiceInfo(
39-
type_,
40-
registration_name,
41-
80,
42-
0,
43-
0,
44-
desc,
45-
"spare-rig.local.",
46-
addresses=[socket.inet_aton("10.7.4.2")],
47-
)
44+
info = make_service_info(type_, registration_name, properties=desc)
4845
zeroconf_registrar.registry.async_add(info)
4946
try:
5047
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)
@@ -137,16 +134,7 @@ def test_integration_with_subtype_and_listener(quick_timing, disable_duplicate_p
137134

138135
zeroconf_registrar = Zeroconf(interfaces=["127.0.0.1"])
139136
desc = {"path": "/healthz/"}
140-
info = ServiceInfo(
141-
discovery_type,
142-
registration_name,
143-
80,
144-
0,
145-
0,
146-
desc,
147-
"spare-rig.local.",
148-
addresses=[socket.inet_aton("10.7.4.2")],
149-
)
137+
info = make_service_info(discovery_type, registration_name, properties=desc)
150138
zeroconf_registrar.registry.async_add(info)
151139
try:
152140
service_types = ZeroconfServiceTypes.find(interfaces=["127.0.0.1"], timeout=LOOPBACK_FIND_TIMEOUT)

0 commit comments

Comments
 (0)