Exemple #1
0
def test_discoverable_all_services_ready_set_services_ready(dns_sd):
    discoverable = dns_sd.ServiceContainer()
    service1 = dns_sd.Service(type_='foo')
    discoverable.add_service(service1)
    service2 = dns_sd.Service(type_='bar')
    discoverable.add_service(service2)

    service1.ready = True
    service2.ready = True

    assert discoverable.services_ready is True
Exemple #2
0
def test_discoverable_adding_service_works(dns_sd):
    discoverable = dns_sd.ServiceContainer()
    service = dns_sd.Service(type_='foo')

    discoverable.add_service(service)

    assert service in discoverable.services
Exemple #3
0
def test_stopping_service_discovery_resets_all_services(dns_sd, sd, zeroconf):
    service1 = dns_sd.Service(type_='halrcomp')
    sd.register(service1)
    service2 = dns_sd.Service(type_='halrcmd')
    sd.register(service2)
    sd.browser = object()  # dummy
    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'Foo on Bar 127.0.0.1._machinekit._tcp.local.',
    )

    sd.stop()

    assert service1.ready is False
    assert service2.ready is False
Exemple #4
0
def test_unregistering_service_directly_works(dns_sd, sd):
    service = dns_sd.Service()
    sd.register(service)

    sd.unregister(service)

    assert service not in sd.services
def test_service_info_updates_all_values_of_service(dns_sd):
    service = dns_sd.Service(type_='halrcomp')
    service_info = ServiceInfoFactory().create(
        name='Foo on Bar',
        uuid=b'987654321',
        version=b'5',
        host='10.0.0.10',
        protocol='tcp',
        port=12456,
        server='sandybox.local',
    )
    service.add_service_info(service_info)

    service_info = ServiceInfoFactory().create(
        name='Foo on Bar',
        uuid=b'nBzl8w',
        version=b'10',
        host='10.0.0.10',
        protocol='udp',
        port=12456,
        server='forest.local',
    )
    service.update_service_info(service_info)

    assert service.uri == 'udp://10.0.0.10:12456'
    assert service.name == service_info.name
    assert service.uuid == 'nBzl8w'
    assert service.version == 10
    assert service.host_name == 'forest.local'
    assert service.host_address == '10.0.0.10'
Exemple #6
0
def test_registering_services_from_service_container_works(dns_sd, sd):
    service = dns_sd.Service()
    discoverable = dns_sd.ServiceContainer()
    discoverable.services.append(service)

    sd.register(discoverable)

    assert service in sd.services
Exemple #7
0
def test_service_info_with_incomplete_values_is_ignored_by_service(dns_sd):
    service = dns_sd.Service(type_='launcher')
    service_info = ServiceInfoFactory().create(properties={})

    service.add_service_info(service_info)

    assert service.uri == ''
    assert service.uuid == ''
    assert service.version == b''
Exemple #8
0
def test_clearing_service_infos_resets_values_of_service(dns_sd):
    service = dns_sd.Service(type_='foobar')
    service.add_service_info(ServiceInfoFactory().create())
    service.add_service_info(ServiceInfoFactory().create())

    service.clear_service_infos()

    assert service.ready is False
    assert service.uri == ''
Exemple #9
0
def test_service_discovered_updates_registered_services(dns_sd, sd, zeroconf):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)

    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'Foo on Bar 127.0.0.1._machinekit._tcp.local.',
    )

    assert service.ready is True
Exemple #10
0
def test_service_discovered_without_service_info_does_not_update_registered_services(
        dns_sd, sd, zeroconf_without_service_info):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)

    sd.add_service(
        zeroconf_without_service_info,
        '_machinekit._tcp.local.',
        'Foo on Bar 127.0.0.1._machinekit._tcp.local.',
    )

    assert service.ready is False
Exemple #11
0
def test_service_discovery_filters_in_discovered_service_with_correct_uuid(
        dns_sd, sd, zeroconf):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)
    sd.filter = dns_sd.ServiceDiscoveryFilter(
        txt_records={b'uuid': b'12345678'})

    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'SuperPrint 192.168.7.2._machinekit._tcp.local.',
    )
    assert service.ready is True
Exemple #12
0
def test_service_info_returs_raw_uri_if_hostname_is_not_matched(dns_sd):
    service = dns_sd.Service(type_='halrcomp')
    service_info = ServiceInfoFactory().create(
        host='thinkpad.local',
        protocol='tcp',
        port=12456,
        server='sandybox.local',
        address='10.0.0.10',
    )

    service.add_service_info(service_info)

    assert service.uri == 'tcp://thinkpad.local:12456'
Exemple #13
0
def test_setting_ready_property_of_service_triggers_callback(dns_sd):
    cb_called = [False]

    def cb(_):
        cb_called[0] = True

    service = dns_sd.Service(type_='halrcomp')
    service.on_ready_changed.append(cb)
    service_info = ServiceInfoFactory().create()

    service.add_service_info(service_info)

    assert cb_called[0] is True
Exemple #14
0
def test_removing_service_info_resets_all_relevant_values_of_service(dns_sd):
    service = dns_sd.Service(type_='blahus')
    service_info = ServiceInfoFactory().create()
    service.add_service_info(service_info)

    service.remove_service_info(service_info.name)

    assert service.uri == ''
    assert service.name == ''
    assert service.uuid == ''
    assert service.version == 0
    assert service.host_name == ''
    assert service.host_address == ''
Exemple #15
0
def test_service_discovery_filters_out_discovered_service_with_wrong_uuid(
        dns_sd, sd, zeroconf):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)
    sd.filter = dns_sd.ServiceDiscoveryFilter(
        txt_records={b'uuid': b'87654321'})

    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'Machinekit on MyBox 12.0.0.1._machinekit._tcp.local.',
    )

    assert service.ready is False
Exemple #16
0
def test_unregistering_when_running_throws_error(dns_sd, sd):
    service = dns_sd.Service()

    def dummy():
        pass

    sd._start_discovery = dummy
    sd.start()

    try:
        sd.unregister(service)
    except RuntimeError:
        assert True

    assert service not in sd.services
Exemple #17
0
def test_service_disappeared_updates_registered_services(dns_sd, sd, zeroconf):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)

    zeroconf.get_service_info.return_value = ServiceInfoFactory.create(
        name='Foo on Bar', host='127.0.0.1')
    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'Foo on Bar 127.0.0.1._machinekit._tcp.local.',
    )
    zeroconf.get_service_info.return_value = None
    sd.remove_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'Foo on Bar 127.0.0.1._machinekit._tcp.local.',
    )

    assert service.ready is False
Exemple #18
0
def test_service_discovery_filters_in_disappeared_service_with_correct_uuid(
        dns_sd, sd, zeroconf):
    service = dns_sd.Service(type_='halrcomp')
    sd.register(service)
    sd.filter = dns_sd.ServiceDiscoveryFilter(
        txt_records={b'uuid': b'12345678'})

    zeroconf.get_service_info.return_value = ServiceInfoFactory.create(
        name='SuperPrint', host='192.168.7.2')
    sd.add_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'SuperPrint 192.168.7.2._machinekit._tcp.local.',
    )
    sd.remove_service(
        zeroconf,
        '_machinekit._tcp.local.',
        'SuperPrint 192.168.7.2._machinekit._tcp.local.',
    )
    assert service.ready is False