def testAdd(self): """ Tests the addition of an endpoint """ # Prepare an ImportEndpoint endpoint = beans.ImportEndpoint("service-uid", "some-framework", ["configA", "configB"], "name", "test.spec", {}) endpoint_same = beans.ImportEndpoint("service-uid", "some-other-framework", ["configA", "configB"], "other-name", "other.spec", {}) endpoint_local = beans.ImportEndpoint("other-service-uid", self.framework_uid, ["configA", "configB"], "name", "test.spec", {}) # Register the endpoint self.assertTrue(self.service.add(endpoint), "ImportEndpoint refused") # Refuse next registration (same bean) self.assertFalse(self.service.add(endpoint), "ImportEndpoint double-registration") # Refuse endpoints with the same UID self.assertFalse(self.service.add(endpoint_same), "ImportEndpoint double-UID") # Refuse endpoints the same framework UID self.assertFalse(self.service.add(endpoint_local), "ImportEndpoint local framework")
def testSynonyms(self): """ Tests synonyms property handling """ # Specifications spec_1 = 'sample.spec' spec_2 = 'sample.spec2' spec_3 = 'sample.spec3' python_specs = [ 'python:/{0}'.format(spec) for spec in (spec_2, spec_3) ] spec_java = 'org.pelix.sample.ISpec2' java_specs = ['java:/{0}'.format(spec_java)] # Prepare an ImportEndpoint endpoint = beans.ImportEndpoint( "service-uid", "some-framework", ["configA", "configB"], "name", # "Normal" specification spec_1, # Synonyms {pelix.remote.PROP_SYNONYMS: python_specs + java_specs}) # Register the endpoint self.service.add(endpoint) # Check its specifications: Python ones don't have a prefix self.assertIn(spec_1, endpoint.specifications) self.assertIn(spec_2, endpoint.specifications) self.assertIn(spec_3, endpoint.specifications) # Java one is kept as is self.assertIn(java_specs[0], endpoint.specifications)
def testLost(self): """ Tests the lost framework event """ endpoint = beans.ImportEndpoint("service-uid", "some-framework", ["configA", "configB"], "name", "test.spec", {}) # No error if the framework wasn't known self.assertIsNone(self.service.lost_framework("other-framework"), "Loss of an unknown framework has been accepted") # Register the endpoint self.assertTrue(self.service.add(endpoint), "ImportEndpoint refused") # Loss must succeed self.assertIsNone(self.service.lost_framework(endpoint.framework), "Error losing a framework") # The endpoint must have been removed self.assertFalse(self.service.remove(endpoint.uid), "Removal of a lost endpoint has been accepted") self.assertFalse(self.service.update(endpoint.uid, {}), "Update of a lost endpoint has been accepted") self.assertTrue(self.service.add(endpoint), "Addition refused")
def testListener(self): """ Tests the listener """ # Prepare endpoints context = self.framework.get_bundle_context() endpoint = beans.ImportEndpoint("service-uid", "some-framework", ["configA", "configB"], "name", "test.spec", {}) for raise_exception in (False, True): # Prepare a listener listener = ImportListener() listener.raise_exception = raise_exception # Register a endpoint self.assertTrue(self.service.add(endpoint), "ImportEndpoint refused") # Register the listener svc_reg = context.register_service( pelix.remote.SERVICE_IMPORT_ENDPOINT_LISTENER, listener, {pelix.remote.PROP_REMOTE_CONFIGS_SUPPORTED: "configA"}) # The listener must have been notified self.assertListEqual(listener.events, [ADDED], "Listener not notified of addition") listener.clear() # Update the endpoint self.service.update(endpoint.uid, {}) self.assertListEqual(listener.events, [UPDATED], "Listener not notified of update") listener.clear() # Remove it self.service.remove(endpoint.uid) self.assertListEqual(listener.events, [REMOVED], "Listener not notified of removal") listener.clear() # Re-register the endpoint and lose the framework self.assertTrue(self.service.add(endpoint), "ImportEndpoint refused") self.service.lost_framework(endpoint.framework) self.assertListEqual( listener.events, [ADDED, REMOVED], "Bad notification of the loss of a framework") # Unregister the service svc_reg.unregister()
def testConvertBeans(self): """ Tests the conversion of an ExportEndpoint to an EndpointDescription and of an EndpointDescription to an ImportEndpoint bean """ # Prepare ExportEndpoint & ImportEndpoint beans specifications = self.svc_ref.get_property(pelix.constants.OBJECTCLASS) export_bean = beans.ExportEndpoint("some.endpoint.uid", "some.framework.uid", ["configurationA"], "some.endpoint.name", self.svc_ref, self.service, {"extra.property": 42}) import_bean = beans.ImportEndpoint( export_bean.uid, export_bean.framework, export_bean.configurations, export_bean.name, export_bean.specifications, export_bean.make_import_properties()) # Convert it to an EndpointDescription bean description_bean = beans.EndpointDescription.from_export(export_bean) # ... ensure its content is valid self.assertEqual(description_bean.get_id(), export_bean.uid) self.assertEqual(description_bean.get_framework_uuid(), export_bean.framework) self.assertEqual(description_bean.get_configuration_types(), export_bean.configurations) # ExportEndpoint specifications are prefixed by "python:/" self.assertEqual(description_bean.get_interfaces(), specifications) self.assertDictContainsSubset(export_bean.make_import_properties(), description_bean.get_properties()) # Convert the result to an ImportEndpoint bean descripted_import = description_bean.to_import() # ... ensure its content is valid for field in ('uid', 'framework', 'configurations', 'specifications'): self.assertEqual(getattr(descripted_import, field), getattr(import_bean, field)) self.assertDictContainsSubset(import_bean.properties, descripted_import.properties)
def testRemove(self): """ Tests the removal of an endpoint """ # Prepare an ImportEndpoint endpoint = beans.ImportEndpoint("service-uid", "some-framework", ["configA", "configB"], "name", "test.spec", {}) # No error on unknown endpoint self.assertFalse(self.service.remove(endpoint.uid), "Removal of an unknown endpoint has been accepted") # Register the endpoint self.assertTrue(self.service.add(endpoint), "Addition refused") # Removal must succeed self.assertTrue(self.service.remove(endpoint.uid), "Error removing an endpoint")
def addService(self, zeroconf, svc_type, name): """ Called by Zeroconf when a record is updated :param zeroconf: The Zeroconf instance than notifies of the modification :param svc_type: Service type :param name: Service name """ # Get information about the service info = self._get_service_info(svc_type, name) if info is None: _logger.warning("Timeout reading service information: %s - %s", svc_type, name) return # Read properties properties = self._deserialize_properties(info.getProperties()) try: sender_uid = properties[pelix.remote.PROP_ENDPOINT_FRAMEWORK_UUID] if sender_uid == self._fw_uid: # We sent this message return except KeyError: # Not a Pelix message _logger.warning("Not a Pelix record: %s", properties) return if svc_type == ZeroconfDiscovery.DNS_DISPATCHER_TYPE: # Dispatcher servlet found, get source info address = to_str(socket.inet_ntoa(info.getAddress())) port = info.getPort() self._access.send_discovered(address, port, properties['pelix.access.path']) elif svc_type == self._rs_type: # Remote service # Get the first available configuration configuration = properties[pelix.remote.PROP_IMPORTED_CONFIGS] if not is_string(configuration): configuration = configuration[0] # Ensure we have a list of specifications specs = properties[pelix.constants.OBJECTCLASS] if is_string(specs): specs = [specs] try: # Make an import bean endpoint = beans.ImportEndpoint( properties[pelix.remote.PROP_ENDPOINT_ID], properties[pelix.remote.PROP_ENDPOINT_FRAMEWORK_UUID], [configuration], None, specs, properties) except KeyError as ex: # Log a warning on incomplete endpoints _logger.warning( "Incomplete endpoint description, " "missing %s: %s", ex, properties) return else: # Register the endpoint if self._registry.add(endpoint): # Associate the mDNS name to the endpoint on success self._imported_endpoints[name] = endpoint.uid