Skip to content

Commit 90f9226

Browse files
authored
chore: consolidate the loopback fixtures and adopt them in test_asyncio (#1871)
1 parent e6e0a60 commit 90f9226

5 files changed

Lines changed: 226 additions & 241 deletions

File tree

‎tests/conftest.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ 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-
10698
@pytest.fixture(autouse=True)
10799
def verify_threads_ended():
108100
"""Verify that the threads are not running after the test."""
@@ -113,7 +105,7 @@ def verify_threads_ended():
113105

114106

115107
@pytest.fixture
116-
def zc_loopback() -> Generator[Zeroconf]:
108+
def zc() -> Generator[Zeroconf]:
117109
"""Yield a loopback `Zeroconf` and close it on teardown.
118110
119111
Replaces the inline `zc = Zeroconf(interfaces=["127.0.0.1"])` +
@@ -128,7 +120,7 @@ def zc_loopback() -> Generator[Zeroconf]:
128120

129121

130122
@pytest_asyncio.fixture
131-
async def aiozc_loopback() -> AsyncGenerator[AsyncZeroconf]:
123+
async def aiozc() -> AsyncGenerator[AsyncZeroconf]:
132124
"""Yield a loopback `AsyncZeroconf` and close it on teardown.
133125
134126
Replaces the inline `aiozc = AsyncZeroconf(interfaces=["127.0.0.1"])`

‎tests/services/test_info.py‎

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,14 +2378,11 @@ async def test_async_request_incomplete_without_txt_record(quick_request_timing)
23782378
([const._TYPE_AAAA], False),
23792379
],
23802380
)
2381-
def test_load_from_cache_with_nsec(
2382-
zc_loopback: r.Zeroconf, nsec_rdtypes: list[int], expected_complete: bool
2383-
) -> None:
2381+
def test_load_from_cache_with_nsec(zc: r.Zeroconf, nsec_rdtypes: list[int], expected_complete: bool) -> None:
23842382
"""An NSEC listing SRV but not TXT denies the TXT record and completes the service."""
23852383
type_ = "_http._tcp.local."
23862384
registration_name = f"nsecdenial.{type_}"
23872385
host = "nsecdenial.local."
2388-
zc = zc_loopback
23892386
zc.cache.async_add_records(
23902387
[
23912388
r.DNSService(
@@ -2424,13 +2421,13 @@ def test_load_from_cache_with_nsec(
24242421

24252422
@pytest.mark.asyncio
24262423
@pytest.mark.usefixtures("quick_request_timing")
2427-
async def test_async_request_completes_on_nsec_txt_denial(aiozc_loopback: AsyncZeroconf) -> None:
2424+
async def test_async_request_completes_on_nsec_txt_denial(aiozc: AsyncZeroconf) -> None:
24282425
"""async_request succeeds promptly when the responder denies the TXT record via NSEC."""
24292426
type_ = "_http._tcp.local."
24302427
registration_name = f"nsecrequest.{type_}"
24312428
host = "nsecrequest.local."
2432-
await aiozc_loopback.zeroconf.async_wait_for_start()
2433-
zc = aiozc_loopback.zeroconf
2429+
await aiozc.zeroconf.async_wait_for_start()
2430+
zc = aiozc.zeroconf
24342431

24352432
zc.record_manager.async_updates_from_response(
24362433
mock_incoming_msg(
@@ -2496,13 +2493,13 @@ async def test_async_request_completes_on_nsec_txt_denial(aiozc_loopback: AsyncZ
24962493

24972494

24982495
@pytest.mark.asyncio
2499-
async def test_nsec_after_txt_record_is_ignored(aiozc_loopback: AsyncZeroconf) -> None:
2496+
async def test_nsec_after_txt_record_is_ignored(aiozc: AsyncZeroconf) -> None:
25002497
"""An NSEC arriving after a real TXT record does not clear the properties."""
25012498
type_ = "_http._tcp.local."
25022499
registration_name = f"nsecaftertxt.{type_}"
25032500
host = "nsecaftertxt.local."
2504-
await aiozc_loopback.zeroconf.async_wait_for_start()
2505-
zc = aiozc_loopback.zeroconf
2501+
await aiozc.zeroconf.async_wait_for_start()
2502+
zc = aiozc.zeroconf
25062503

25072504
zc.record_manager.async_updates_from_response(
25082505
mock_incoming_msg(
@@ -2551,7 +2548,7 @@ async def test_nsec_after_txt_record_is_ignored(aiozc_loopback: AsyncZeroconf) -
25512548
assert info._is_complete
25522549

25532550

2554-
def test_unhandled_record_type_at_service_name_is_ignored(zc_loopback: r.Zeroconf) -> None:
2551+
def test_unhandled_record_type_at_service_name_is_ignored(zc: r.Zeroconf) -> None:
25552552
"""A record type with no handler at the service name does not change state."""
25562553
type_ = "_http._tcp.local."
25572554
registration_name = f"unhandledrec.{type_}"
@@ -2563,19 +2560,19 @@ def test_unhandled_record_type_at_service_name_is_ignored(zc_loopback: r.Zerocon
25632560
120,
25642561
f"other.{type_}",
25652562
)
2566-
info.async_update_records(zc_loopback, r.current_time_millis(), [RecordUpdate(ptr_record, None)])
2563+
info.async_update_records(zc, r.current_time_millis(), [RecordUpdate(ptr_record, None)])
25672564
assert info._txt_seen is False
25682565
assert info._is_complete is False
25692566

25702567

25712568
@pytest.mark.asyncio
2572-
async def test_own_nsec_response_does_not_complete_service(aiozc_loopback: AsyncZeroconf) -> None:
2569+
async def test_own_nsec_response_does_not_complete_service(aiozc: AsyncZeroconf) -> None:
25732570
"""The NSEC our own responder emits for a missing address type must not deny the TXT record."""
25742571
type_ = "_http._tcp.local."
25752572
registration_name = f"ownnsec.{type_}"
25762573
host = "ownnsec.local."
2577-
await aiozc_loopback.zeroconf.async_wait_for_start()
2578-
zc = aiozc_loopback.zeroconf
2574+
await aiozc.zeroconf.async_wait_for_start()
2575+
zc = aiozc.zeroconf
25792576

25802577
registered = ServiceInfo(
25812578
type_,
@@ -2604,14 +2601,14 @@ async def test_own_nsec_response_does_not_complete_service(aiozc_loopback: Async
26042601
],
26052602
)
26062603
async def test_address_resolver_bails_on_nsec_denial(
2607-
aiozc_loopback: AsyncZeroconf,
2604+
aiozc: AsyncZeroconf,
26082605
resolver_class: Callable[[str], ServiceInfo],
26092606
nsec_rdtypes: list[int],
26102607
) -> None:
26112608
"""A cached NSEC denying the wanted address type fails the request without waiting."""
26122609
host = "nsec-addr-denial.local."
2613-
await aiozc_loopback.zeroconf.async_wait_for_start()
2614-
zc = aiozc_loopback.zeroconf
2610+
await aiozc.zeroconf.async_wait_for_start()
2611+
zc = aiozc.zeroconf
26152612

26162613
zc.record_manager.async_updates_from_response(
26172614
mock_incoming_msg(
@@ -2638,11 +2635,11 @@ async def test_address_resolver_bails_on_nsec_denial(
26382635

26392636

26402637
@pytest.mark.asyncio
2641-
async def test_address_resolver_bails_on_live_nsec_denial(aiozc_loopback: AsyncZeroconf) -> None:
2638+
async def test_address_resolver_bails_on_live_nsec_denial(aiozc: AsyncZeroconf) -> None:
26422639
"""An NSEC denial arriving mid request wakes the waiter and fails it early."""
26432640
host = "nsec-live-denial.local."
2644-
await aiozc_loopback.zeroconf.async_wait_for_start()
2645-
zc = aiozc_loopback.zeroconf
2641+
await aiozc.zeroconf.async_wait_for_start()
2642+
zc = aiozc.zeroconf
26462643

26472644
resolver = r.AddressResolverIPv6(host)
26482645
start = time.monotonic()
@@ -2670,12 +2667,12 @@ async def test_address_resolver_bails_on_live_nsec_denial(aiozc_loopback: AsyncZ
26702667

26712668
@pytest.mark.asyncio
26722669
async def test_address_resolver_succeeds_despite_partial_denial(
2673-
aiozc_loopback: AsyncZeroconf,
2670+
aiozc: AsyncZeroconf,
26742671
) -> None:
26752672
"""Denial of one address family does not fail a resolver that accepts either."""
26762673
host = "nsec-partial-denial.local."
2677-
await aiozc_loopback.zeroconf.async_wait_for_start()
2678-
zc = aiozc_loopback.zeroconf
2674+
await aiozc.zeroconf.async_wait_for_start()
2675+
zc = aiozc.zeroconf
26792676

26802677
zc.record_manager.async_updates_from_response(
26812678
mock_incoming_msg(
@@ -2707,12 +2704,12 @@ async def test_address_resolver_succeeds_despite_partial_denial(
27072704

27082705
@pytest.mark.asyncio
27092706
async def test_address_resolver_bails_when_other_family_is_present(
2710-
aiozc_loopback: AsyncZeroconf,
2707+
aiozc: AsyncZeroconf,
27112708
) -> None:
27122709
"""A cached NSEC denial bails a single family resolver even when the other family resolved."""
27132710
host = "nsec-mixed-denial.local."
2714-
await aiozc_loopback.zeroconf.async_wait_for_start()
2715-
zc = aiozc_loopback.zeroconf
2711+
await aiozc.zeroconf.async_wait_for_start()
2712+
zc = aiozc.zeroconf
27162713

27172714
zc.record_manager.async_updates_from_response(
27182715
mock_incoming_msg(
@@ -2744,11 +2741,11 @@ async def test_address_resolver_bails_when_other_family_is_present(
27442741

27452742

27462743
@pytest.mark.asyncio
2747-
async def test_address_denial_cleared_on_new_request(aiozc_loopback: AsyncZeroconf) -> None:
2744+
async def test_address_denial_cleared_on_new_request(aiozc: AsyncZeroconf) -> None:
27482745
"""A denial only lasts one request; a host that gains the record resolves again."""
27492746
host = "nsec-denial-reset.local."
2750-
await aiozc_loopback.zeroconf.async_wait_for_start()
2751-
zc = aiozc_loopback.zeroconf
2747+
await aiozc.zeroconf.async_wait_for_start()
2748+
zc = aiozc.zeroconf
27522749

27532750
zc.record_manager.async_updates_from_response(
27542751
mock_incoming_msg(
@@ -2790,14 +2787,14 @@ async def test_address_denial_cleared_on_new_request(aiozc_loopback: AsyncZeroco
27902787

27912788
@pytest.mark.asyncio
27922789
async def test_service_info_bails_when_all_address_types_denied(
2793-
aiozc_loopback: AsyncZeroconf,
2790+
aiozc: AsyncZeroconf,
27942791
) -> None:
27952792
"""An NSEC denying both address families at the host fails the request early."""
27962793
type_ = "_http._tcp.local."
27972794
registration_name = f"alldenied.{type_}"
27982795
host = "alldenied-host.local."
2799-
await aiozc_loopback.zeroconf.async_wait_for_start()
2800-
zc = aiozc_loopback.zeroconf
2796+
await aiozc.zeroconf.async_wait_for_start()
2797+
zc = aiozc.zeroconf
28012798

28022799
zc.record_manager.async_updates_from_response(
28032800
mock_incoming_msg(
@@ -2838,7 +2835,7 @@ async def test_service_info_bails_when_all_address_types_denied(
28382835
assert time.monotonic() - start < 2
28392836

28402837

2841-
def test_nsec_for_unrelated_name_is_ignored(zc_loopback: r.Zeroconf) -> None:
2838+
def test_nsec_for_unrelated_name_is_ignored(zc: r.Zeroconf) -> None:
28422839
"""An NSEC for a name that is neither the service nor the server changes nothing."""
28432840
type_ = "_http._tcp.local."
28442841
registration_name = f"unrelatednsec.{type_}"
@@ -2851,7 +2848,7 @@ def test_nsec_for_unrelated_name_is_ignored(zc_loopback: r.Zeroconf) -> None:
28512848
"other-host.local.",
28522849
[const._TYPE_A],
28532850
)
2854-
info.async_update_records(zc_loopback, r.current_time_millis(), [RecordUpdate(foreign_nsec, None)])
2851+
info.async_update_records(zc, r.current_time_millis(), [RecordUpdate(foreign_nsec, None)])
28552852
assert info._ipv4_denied is False
28562853
assert info._ipv6_denied is False
28572854
assert info._txt_seen is False
@@ -2882,7 +2879,7 @@ def _srv(name: str, host: str) -> r.DNSService:
28822879

28832880

28842881
@pytest.mark.parametrize("nsec_first", [False, True])
2885-
def test_nsec_and_srv_in_same_batch_record_denial(zc_loopback: r.Zeroconf, nsec_first: bool) -> None:
2882+
def test_nsec_and_srv_in_same_batch_record_denial(zc: r.Zeroconf, nsec_first: bool) -> None:
28862883
"""A batch with an SRV and an NSEC for its target records the denial in either order."""
28872884
type_ = "_http._tcp.local."
28882885
registration_name = f"nsecfirst.{type_}"
@@ -2892,11 +2889,11 @@ def test_nsec_and_srv_in_same_batch_record_denial(zc_loopback: r.Zeroconf, nsec_
28922889
records = [_nsec(host, [const._TYPE_TXT]), _srv(registration_name, host)]
28932890
if not nsec_first:
28942891
records.reverse()
2895-
info.async_update_records(zc_loopback, now, [RecordUpdate(record, None) for record in records])
2892+
info.async_update_records(zc, now, [RecordUpdate(record, None) for record in records])
28962893
assert (info._ipv4_denied, info._ipv6_denied) == (True, True)
28972894

28982895

2899-
def test_denial_is_reset_when_srv_changes_server(zc_loopback: r.Zeroconf) -> None:
2896+
def test_denial_is_reset_when_srv_changes_server(zc: r.Zeroconf) -> None:
29002897
"""A denial learned for the old SRV target does not apply to a new target."""
29012898
type_ = "_http._tcp.local."
29022899
registration_name = f"srvmove.{type_}"
@@ -2907,7 +2904,7 @@ def test_denial_is_reset_when_srv_changes_server(zc_loopback: r.Zeroconf) -> Non
29072904
now = r.current_time_millis()
29082905

29092906
info.async_update_records(
2910-
zc_loopback,
2907+
zc,
29112908
now,
29122909
[
29132910
RecordUpdate(_srv(registration_name, old_host), None),
@@ -2917,34 +2914,32 @@ def test_denial_is_reset_when_srv_changes_server(zc_loopback: r.Zeroconf) -> Non
29172914
assert (info._ipv4_denied, info._ipv6_denied) == (True, True)
29182915

29192916
# moving to a host with no cached NSEC clears the denial
2920-
info.async_update_records(zc_loopback, now, [RecordUpdate(_srv(registration_name, new_host), None)])
2917+
info.async_update_records(zc, now, [RecordUpdate(_srv(registration_name, new_host), None)])
29212918
assert (info._ipv4_denied, info._ipv6_denied) == (False, False)
29222919

29232920
# moving to a host with a cached NSEC re-establishes it from the cache
2924-
zc_loopback.cache.async_add_records([_nsec(denied_host, [const._TYPE_TXT])])
2925-
info.async_update_records(zc_loopback, now, [RecordUpdate(_srv(registration_name, denied_host), None)])
2921+
zc.cache.async_add_records([_nsec(denied_host, [const._TYPE_TXT])])
2922+
info.async_update_records(zc, now, [RecordUpdate(_srv(registration_name, denied_host), None)])
29262923
assert (info._ipv4_denied, info._ipv6_denied) == (True, True)
29272924

29282925

2929-
def test_newer_nsec_clears_previous_denial(zc_loopback: r.Zeroconf) -> None:
2926+
def test_newer_nsec_clears_previous_denial(zc: r.Zeroconf) -> None:
29302927
"""A later NSEC whose bitmap includes a previously denied type clears that denial."""
29312928
type_ = "_http._tcp.local."
29322929
registration_name = f"nsecrefresh.{type_}"
29332930
host = "nsecrefresh-host.local."
29342931
info = ServiceInfo(type_, registration_name)
29352932
now = r.current_time_millis()
2936-
info.async_update_records(zc_loopback, now, [RecordUpdate(_srv(registration_name, host), None)])
2933+
info.async_update_records(zc, now, [RecordUpdate(_srv(registration_name, host), None)])
29372934

2938-
info.async_update_records(zc_loopback, now, [RecordUpdate(_nsec(host, [const._TYPE_A]), None)])
2935+
info.async_update_records(zc, now, [RecordUpdate(_nsec(host, [const._TYPE_A]), None)])
29392936
assert (info._ipv4_denied, info._ipv6_denied) == (False, True)
29402937

2941-
info.async_update_records(
2942-
zc_loopback, now, [RecordUpdate(_nsec(host, [const._TYPE_A, const._TYPE_AAAA]), None)]
2943-
)
2938+
info.async_update_records(zc, now, [RecordUpdate(_nsec(host, [const._TYPE_A, const._TYPE_AAAA]), None)])
29442939
assert (info._ipv4_denied, info._ipv6_denied) == (False, False)
29452940

29462941

2947-
def test_multiple_nsec_records_in_one_batch(zc_loopback: r.Zeroconf) -> None:
2942+
def test_multiple_nsec_records_in_one_batch(zc: r.Zeroconf) -> None:
29482943
"""A batch with more than one NSEC processes every denial it carries."""
29492944
type_ = "_http._tcp.local."
29502945
registration_name = f"multinsec.{type_}"
@@ -2956,19 +2951,19 @@ def test_multiple_nsec_records_in_one_batch(zc_loopback: r.Zeroconf) -> None:
29562951
_nsec(registration_name, [const._TYPE_SRV]),
29572952
_nsec(host, [const._TYPE_TXT]),
29582953
]
2959-
info.async_update_records(zc_loopback, now, [RecordUpdate(record, None) for record in records])
2954+
info.async_update_records(zc, now, [RecordUpdate(record, None) for record in records])
29602955
assert info._txt_seen is True
29612956
assert (info._ipv4_denied, info._ipv6_denied) == (True, True)
29622957

29632958

29642959
@pytest.mark.asyncio
2965-
async def test_v4_only_host_with_nsec_still_resolves(aiozc_loopback: AsyncZeroconf) -> None:
2960+
async def test_v4_only_host_with_nsec_still_resolves(aiozc: AsyncZeroconf) -> None:
29662961
"""The common case: a v4 only host denying AAAA via NSEC still resolves the service."""
29672962
type_ = "_http._tcp.local."
29682963
registration_name = f"v4onlynsec.{type_}"
29692964
host = "v4onlynsec-host.local."
2970-
await aiozc_loopback.zeroconf.async_wait_for_start()
2971-
zc = aiozc_loopback.zeroconf
2965+
await aiozc.zeroconf.async_wait_for_start()
2966+
zc = aiozc.zeroconf
29722967

29732968
zc.record_manager.async_updates_from_response(
29742969
mock_incoming_msg(
@@ -3000,7 +2995,7 @@ async def test_v4_only_host_with_nsec_still_resolves(aiozc_loopback: AsyncZeroco
30002995
assert info.addresses == [socket.inet_aton("127.0.0.1")]
30012996

30022997

3003-
def test_legacy_inverted_nsec_at_own_name_does_not_fail_request(zc_loopback: r.Zeroconf) -> None:
2998+
def test_legacy_inverted_nsec_at_own_name_does_not_fail_request(zc: r.Zeroconf) -> None:
30042999
"""A pre-0.150.4 inverted NSEC from a service with server == name never denies the request."""
30053000
type_ = "_http._tcp.local."
30063001
registration_name = f"legacyownname.{type_}"
@@ -3009,7 +3004,7 @@ def test_legacy_inverted_nsec_at_own_name_does_not_fail_request(zc_loopback: r.Z
30093004
# legacy v4 only responder registered without server=: NSEC at its own
30103005
# name with the inverted bitmap listing the missing AAAA
30113006
info.async_update_records(
3012-
zc_loopback,
3007+
zc,
30133008
now,
30143009
[
30153010
RecordUpdate(_srv(registration_name, registration_name), None),
@@ -3020,7 +3015,7 @@ def test_legacy_inverted_nsec_at_own_name_does_not_fail_request(zc_loopback: r.Z
30203015
assert info._is_denied is False
30213016

30223017
info.async_update_records(
3023-
zc_loopback,
3018+
zc,
30243019
now,
30253020
[
30263021
RecordUpdate(
@@ -3039,12 +3034,12 @@ def test_legacy_inverted_nsec_at_own_name_does_not_fail_request(zc_loopback: r.Z
30393034
assert info._is_denied is False
30403035

30413036

3042-
def test_denied_flag_is_ignored_when_address_is_held(zc_loopback: r.Zeroconf) -> None:
3037+
def test_denied_flag_is_ignored_when_address_is_held(zc: r.Zeroconf) -> None:
30433038
"""A denial never vetoes an address the client already holds."""
30443039
type_ = "_http._tcp.local."
30453040
registration_name = f"guardednsec.{type_}"
30463041
host = "guardednsec-host.local."
3047-
zc_loopback.cache.async_add_records(
3042+
zc.cache.async_add_records(
30483043
[
30493044
_srv(registration_name, host),
30503045
r.DNSAddress(
@@ -3060,7 +3055,7 @@ def test_denied_flag_is_ignored_when_address_is_held(zc_loopback: r.Zeroconf) ->
30603055

30613056
info = ServiceInfo(type_, registration_name)
30623057
# incomplete only because the TXT record is still missing
3063-
assert info.load_from_cache(zc_loopback) is False
3058+
assert info.load_from_cache(zc) is False
30643059
assert (info._ipv4_denied, info._ipv6_denied) == (True, True)
30653060
assert info.addresses == [socket.inet_aton("127.0.0.1")]
30663061
# the held A record keeps the request querying instead of fast failing

0 commit comments

Comments
 (0)