Skip to content

Commit c0fc47f

Browse files
committed
Relocate ServiceTypesQuery tests to tests/services/test_types
1 parent 4a88066 commit c0fc47f

3 files changed

Lines changed: 166 additions & 133 deletions

File tree

tests/services/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
2+
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine
3+
4+
This module provides a framework for the use of DNS Service Discovery
5+
using IP multicast.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20+
USA
21+
"""

tests/services/test_types.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
"""Unit tests for zeroconf._services.types."""
6+
7+
import os
8+
import unittest
9+
import socket
10+
11+
import zeroconf as r
12+
from zeroconf import Zeroconf, ServiceInfo, ZeroconfServiceTypes
13+
14+
from .. import _clear_cache, has_working_ipv6
15+
16+
17+
class ServiceTypesQuery(unittest.TestCase):
18+
def test_integration_with_listener(self):
19+
20+
type_ = "_test-srvc-type._tcp.local."
21+
name = "xxxyyy"
22+
registration_name = "%s.%s" % (name, type_)
23+
24+
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
25+
desc = {'path': '/~paulsm/'}
26+
info = ServiceInfo(
27+
type_,
28+
registration_name,
29+
80,
30+
0,
31+
0,
32+
desc,
33+
"ash-2.local.",
34+
addresses=[socket.inet_aton("10.0.1.2")],
35+
)
36+
zeroconf_registrar.register_service(info)
37+
38+
try:
39+
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
40+
assert type_ in service_types
41+
_clear_cache(zeroconf_registrar)
42+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
43+
assert type_ in service_types
44+
45+
finally:
46+
zeroconf_registrar.close()
47+
48+
@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
49+
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
50+
def test_integration_with_listener_v6_records(self):
51+
52+
type_ = "_test-srvc-type._tcp.local."
53+
name = "xxxyyy"
54+
registration_name = "%s.%s" % (name, type_)
55+
addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com
56+
57+
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
58+
desc = {'path': '/~paulsm/'}
59+
info = ServiceInfo(
60+
type_,
61+
registration_name,
62+
80,
63+
0,
64+
0,
65+
desc,
66+
"ash-2.local.",
67+
addresses=[socket.inet_pton(socket.AF_INET6, addr)],
68+
)
69+
zeroconf_registrar.register_service(info)
70+
71+
try:
72+
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
73+
assert type_ in service_types
74+
_clear_cache(zeroconf_registrar)
75+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
76+
assert type_ in service_types
77+
78+
finally:
79+
zeroconf_registrar.close()
80+
81+
@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
82+
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
83+
def test_integration_with_listener_ipv6(self):
84+
85+
type_ = "_test-srvc-type._tcp.local."
86+
name = "xxxyyy"
87+
registration_name = "%s.%s" % (name, type_)
88+
89+
zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
90+
desc = {'path': '/~paulsm/'}
91+
info = ServiceInfo(
92+
type_,
93+
registration_name,
94+
80,
95+
0,
96+
0,
97+
desc,
98+
"ash-2.local.",
99+
addresses=[socket.inet_aton("10.0.1.2")],
100+
)
101+
zeroconf_registrar.register_service(info)
102+
103+
try:
104+
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
105+
assert type_ in service_types
106+
_clear_cache(zeroconf_registrar)
107+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
108+
assert type_ in service_types
109+
110+
finally:
111+
zeroconf_registrar.close()
112+
113+
def test_integration_with_subtype_and_listener(self):
114+
subtype_ = "_subtype._sub"
115+
type_ = "_type._tcp.local."
116+
name = "xxxyyy"
117+
# Note: discovery returns only DNS-SD type not subtype
118+
discovery_type = "%s.%s" % (subtype_, type_)
119+
registration_name = "%s.%s" % (name, type_)
120+
121+
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
122+
desc = {'path': '/~paulsm/'}
123+
info = ServiceInfo(
124+
discovery_type,
125+
registration_name,
126+
80,
127+
0,
128+
0,
129+
desc,
130+
"ash-2.local.",
131+
addresses=[socket.inet_aton("10.0.1.2")],
132+
)
133+
zeroconf_registrar.register_service(info)
134+
135+
try:
136+
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
137+
assert discovery_type in service_types
138+
_clear_cache(zeroconf_registrar)
139+
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
140+
assert discovery_type in service_types
141+
142+
finally:
143+
zeroconf_registrar.close()

tests/test_init.py

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66

77
import errno
88
import logging
9-
import os
109
import socket
1110
import time
1211
import unittest
1312
import unittest.mock
14-
from threading import Event
1513
from typing import Dict, Optional # noqa # used in type hints
1614

1715
import pytest
1816

1917
import zeroconf as r
20-
from zeroconf import ServiceBrowser, ServiceInfo, Zeroconf, ZeroconfServiceTypes, const
18+
from zeroconf import ServiceBrowser, ServiceInfo, Zeroconf, const
2119

22-
from . import has_working_ipv6, _clear_cache, _inject_response
20+
from . import _clear_cache
2321

2422
log = logging.getLogger('zeroconf')
2523
original_logging_level = logging.NOTSET
@@ -459,135 +457,6 @@ def test_lookups(self):
459457
assert registry.get_types() == [type_]
460458

461459

462-
class ServiceTypesQuery(unittest.TestCase):
463-
def test_integration_with_listener(self):
464-
465-
type_ = "_test-srvc-type._tcp.local."
466-
name = "xxxyyy"
467-
registration_name = "%s.%s" % (name, type_)
468-
469-
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
470-
desc = {'path': '/~paulsm/'}
471-
info = ServiceInfo(
472-
type_,
473-
registration_name,
474-
80,
475-
0,
476-
0,
477-
desc,
478-
"ash-2.local.",
479-
addresses=[socket.inet_aton("10.0.1.2")],
480-
)
481-
zeroconf_registrar.register_service(info)
482-
483-
try:
484-
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
485-
assert type_ in service_types
486-
_clear_cache(zeroconf_registrar)
487-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
488-
assert type_ in service_types
489-
490-
finally:
491-
zeroconf_registrar.close()
492-
493-
@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
494-
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
495-
def test_integration_with_listener_v6_records(self):
496-
497-
type_ = "_test-srvc-type._tcp.local."
498-
name = "xxxyyy"
499-
registration_name = "%s.%s" % (name, type_)
500-
addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com
501-
502-
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
503-
desc = {'path': '/~paulsm/'}
504-
info = ServiceInfo(
505-
type_,
506-
registration_name,
507-
80,
508-
0,
509-
0,
510-
desc,
511-
"ash-2.local.",
512-
addresses=[socket.inet_pton(socket.AF_INET6, addr)],
513-
)
514-
zeroconf_registrar.register_service(info)
515-
516-
try:
517-
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
518-
assert type_ in service_types
519-
_clear_cache(zeroconf_registrar)
520-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
521-
assert type_ in service_types
522-
523-
finally:
524-
zeroconf_registrar.close()
525-
526-
@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
527-
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
528-
def test_integration_with_listener_ipv6(self):
529-
530-
type_ = "_test-srvc-type._tcp.local."
531-
name = "xxxyyy"
532-
registration_name = "%s.%s" % (name, type_)
533-
534-
zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
535-
desc = {'path': '/~paulsm/'}
536-
info = ServiceInfo(
537-
type_,
538-
registration_name,
539-
80,
540-
0,
541-
0,
542-
desc,
543-
"ash-2.local.",
544-
addresses=[socket.inet_aton("10.0.1.2")],
545-
)
546-
zeroconf_registrar.register_service(info)
547-
548-
try:
549-
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
550-
assert type_ in service_types
551-
_clear_cache(zeroconf_registrar)
552-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
553-
assert type_ in service_types
554-
555-
finally:
556-
zeroconf_registrar.close()
557-
558-
def test_integration_with_subtype_and_listener(self):
559-
subtype_ = "_subtype._sub"
560-
type_ = "_type._tcp.local."
561-
name = "xxxyyy"
562-
# Note: discovery returns only DNS-SD type not subtype
563-
discovery_type = "%s.%s" % (subtype_, type_)
564-
registration_name = "%s.%s" % (name, type_)
565-
566-
zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
567-
desc = {'path': '/~paulsm/'}
568-
info = ServiceInfo(
569-
discovery_type,
570-
registration_name,
571-
80,
572-
0,
573-
0,
574-
desc,
575-
"ash-2.local.",
576-
addresses=[socket.inet_aton("10.0.1.2")],
577-
)
578-
zeroconf_registrar.register_service(info)
579-
580-
try:
581-
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
582-
assert discovery_type in service_types
583-
_clear_cache(zeroconf_registrar)
584-
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
585-
assert discovery_type in service_types
586-
587-
finally:
588-
zeroconf_registrar.close()
589-
590-
591460
def test_ptr_optimization():
592461

593462
# instantiate a zeroconf instance

0 commit comments

Comments
 (0)