def setUp(self):
     self.private_address = Address("127.0.0.1", 5701)
     self.public_address = Address("192.168.0.1", 5701)
     self.lookup[self.private_address] = self.public_address
     self.lookup[Address("127.0.0.2", 5701)] = Address("192.168.0.2", 5701)
     self.cloud_discovery = HazelcastCloudDiscovery("", "", 0)
     self.cloud_discovery.discover_nodes = lambda: self.lookup
     self.translator = HazelcastCloudAddressTranslator("", "", 0)
     self.translator.cloud_discovery = self.cloud_discovery
Example #2
0
    def _create_address_translator(self):
        network_config = self.config.network_config
        cloud_config = network_config.cloud_config
        cloud_discovery_token = self.properties.get(
            self.properties.HAZELCAST_CLOUD_DISCOVERY_TOKEN)

        address_list_provided = len(network_config.addresses) != 0
        if cloud_discovery_token != "" and cloud_config.enabled:
            raise HazelcastIllegalStateError(
                "Ambiguous Hazelcast.cloud configuration. "
                "Both property based and client configuration based settings are provided "
                "for Hazelcast cloud discovery together. Use only one.")

        hazelcast_cloud_enabled = cloud_discovery_token != "" or cloud_config.enabled
        self._is_discovery_configuration_consistent(address_list_provided,
                                                    hazelcast_cloud_enabled)

        if hazelcast_cloud_enabled:
            if cloud_config.enabled:
                discovery_token = cloud_config.discovery_token
            else:
                discovery_token = cloud_discovery_token
            host, url = HazelcastCloudDiscovery.get_host_and_url(
                self.config.get_properties(), discovery_token)
            return HazelcastCloudAddressTranslator(
                host, url, self._get_connection_timeout(), self._logger_extras)

        return DefaultAddressTranslator()
class HazelcastCloudTranslatorTest(TestCase):
    lookup = dict()
    private_address = None
    public_address = None
    logger = None
    translator = None

    def setUp(self):
        self.private_address = Address("127.0.0.1", 5701)
        self.public_address = Address("192.168.0.1", 5701)
        self.lookup[self.private_address] = self.public_address
        self.lookup[Address("127.0.0.2", 5701)] = Address("192.168.0.2", 5701)
        self.cloud_discovery = HazelcastCloudDiscovery("", "", 0)
        self.cloud_discovery.discover_nodes = lambda: self.lookup
        self.translator = HazelcastCloudAddressTranslator("", "", 0)
        self.translator.cloud_discovery = self.cloud_discovery

    def test_translate_when_address_is_none(self):
        actual = self.translator.translate(None)

        self.assertIsNone(actual)

    def test_translate(self):
        actual = self.translator.translate(self.private_address)

        self.assertEqual(self.public_address.host, actual.host)
        self.assertEqual(self.private_address.port, actual.port)

    def test_refresh_and_translate(self):
        self.translator.refresh()
        actual = self.translator.translate(self.private_address)

        self.assertEqual(self.public_address.host, actual.host)
        self.assertEqual(self.private_address.port, actual.port)

    def test_translate_when_not_found(self):
        not_available_address = Address("127.0.0.3", 5701)
        actual = self.translator.translate(not_available_address)

        self.assertIsNone(actual)

    def test_refresh_with_exception(self):
        cloud_discovery = HazelcastCloudDiscovery("", "", 0)
        cloud_discovery.discover_nodes = self.mock_discover_nodes_with_exception
        translator = HazelcastCloudAddressTranslator("", "", 0)
        translator.cloud_discovery = cloud_discovery
        translator.refresh()

    def mock_discover_nodes_with_exception(self):
        raise Exception("Expected exception")
 def test_refresh_with_exception(self):
     cloud_discovery = HazelcastCloudDiscovery("", "", 0)
     cloud_discovery.discover_nodes = self.mock_discover_nodes_with_exception
     translator = HazelcastCloudAddressTranslator("", "", 0)
     translator.cloud_discovery = cloud_discovery
     translator.refresh()