Skip to content

Commit

Permalink
Add language_alias parameter to RetrieveExasolNodeIPAddressUDFDeployer
Browse files Browse the repository at this point in the history
  • Loading branch information
tkilias committed Feb 17, 2024
1 parent bb5093d commit e00bff3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

class RetrieveExasolNodeIPAddressUDFDeployer:

def render_udf_create_statement(self, udf_name: UDFName):
def render_udf_create_statement(self, udf_name: UDFName, language_alias: str):
udf_create_statement = textwrap.dedent(f"""
CREATE OR REPLACE PYTHON3_AAF SET SCRIPT {udf_name.fully_qualified}(ignored INTEGER)
CREATE OR REPLACE {language_alias} SET SCRIPT {udf_name.fully_qualified}(ignored INTEGER)
EMITS (ip_address VARCHAR(10), network_prefix INTEGER) AS
from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf import \
RetrieveExasolNodeIPAddressUDF
from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf import RetrieveExasolNodeIPAddressUDF
def run(ctx):
RetrieveExasolNodeIPAddressUDF().run(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def test(setup_database, pyexasol_connection, upload_language_container):
udf_name = UDFNameBuilder().create(name="RETRIEVE_EXASOL_NODE_IP_ADDRESS",
schema=SchemaName(schema_name))
udf_create_statement = \
RetrieveExasolNodeIPAddressUDFDeployer().render_udf_create_statement(udf_name)
RetrieveExasolNodeIPAddressUDFDeployer().render_udf_create_statement(udf_name=udf_name,
language_alias="PYTHON3_AAF")
pyexasol_connection.execute(udf_create_statement)
rs = pyexasol_connection.execute(
f"""
Expand Down
9 changes: 7 additions & 2 deletions tests/integration_tests/with_db/test_udf_communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import dill
import pytest
from exasol_data_science_utils_python.schema.schema_name import SchemaName
from exasol_data_science_utils_python.schema.udf_name import UDFName
from exasol_data_science_utils_python.schema.udf_name_builder import UDFNameBuilder

from exasol_advanced_analytics_framework.udf_communication.ip_address import Port, IPAddress
from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf_deployer import \
Expand All @@ -16,8 +19,10 @@ def deploy_retrieve_exasol_node_ip_address_udf(setup_database,
bucketfs_connection_name, schema_name = setup_database
udf_name = f"RETRIEVE_EXASOL_NODE_IP_ADDRESS"
udf_create_statement = \
RetrieveExasolNodeIPAddressUDFDeployer.render_udf_create_statement(
schema_name=schema_name, udf_name=udf_name)
RetrieveExasolNodeIPAddressUDFDeployer().render_udf_create_statement(
udf_name=UDFNameBuilder().create(udf_name, SchemaName(schema_name)),
language_alias="PYTHON3_AAF"
)
pyexasol_connection.execute(udf_create_statement)
return schema_name, udf_name

Expand Down
Original file line number Diff line number Diff line change
@@ -1,112 +1,23 @@
import re
import textwrap
from typing import Union
from unittest.mock import create_autospec, MagicMock, Mock, call

import pytest
from python_hosts import Hosts, HostsEntry

from exasol_advanced_analytics_framework.udf_communication.host_ip_addresses import HostIPAddresses
from exasol_advanced_analytics_framework.udf_communication.ip_address import IPAddress
from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf import \
RetrieveExasolNodeIPAddressUDF
from tests.mock_cast import mock_cast


def test_ip_address_found():
host_ip_addresses_mock: Union[HostIPAddresses, MagicMock] = create_autospec(HostIPAddresses)
mock_cast(host_ip_addresses_mock.get_all_ip_addresses).return_value = [
IPAddress(ip_address="192.168.0.1", network_prefix=16),
IPAddress(ip_address="193.169.0.1", network_prefix=16)
]
hosts_mock: Union[Hosts, MagicMock] = create_autospec(Hosts)
hosts_mock.entries = [
HostsEntry(entry_type="ipv4", address="192.168.0.1", names=["n11"]),
HostsEntry(entry_type="ipv4", address="192.168.0.2", names=["n12"])
]
udf = RetrieveExasolNodeIPAddressUDF(
host_ip_addresses=host_ip_addresses_mock,
hosts=hosts_mock
)
ctx = Mock()
udf.run(ctx)
assert ctx.mock_calls == [call.emit('192.168.0.1', 16)]


def test_ip_address_not_found():
host_ip_addresses_mock: Union[HostIPAddresses, MagicMock] = create_autospec(HostIPAddresses)
mock_cast(host_ip_addresses_mock.get_all_ip_addresses).return_value = [
IPAddress(ip_address="193.169.0.1", network_prefix=16)
]
hosts_mock: Union[Hosts, MagicMock] = create_autospec(Hosts)
hosts_mock.entries = [
HostsEntry(entry_type="ipv4", address="192.168.0.1", names=["n11"]),
HostsEntry(entry_type="ipv4", address="192.168.0.2", names=["n12"])
]
udf = RetrieveExasolNodeIPAddressUDF(
host_ip_addresses=host_ip_addresses_mock,
hosts=hosts_mock
)
ctx = Mock()
with pytest.raises(RuntimeError, match=re.escape(
textwrap.dedent(
"""
No or multiple possible IP addresses for current node found: []
Hosts entries: [HostsEntry(entry_type='ipv4', address='192.168.0.1', names=['n11'], comment=None), HostsEntry(entry_type='ipv4', address='192.168.0.2', names=['n12'], comment=None)]
IP addresses: [IPAddress(ip_address='193.169.0.1', network_prefix=16)]
"""
).strip()
)):
udf.run(ctx)


def test_no_exasol_node_in_hosts():
host_ip_addresses_mock: Union[HostIPAddresses, MagicMock] = create_autospec(HostIPAddresses)
mock_cast(host_ip_addresses_mock.get_all_ip_addresses).return_value = [
IPAddress(ip_address="192.168.0.1", network_prefix=16)
]
hosts_mock: Union[Hosts, MagicMock] = create_autospec(Hosts)
hosts_mock.entries = [
HostsEntry(entry_type="ipv4", address="192.168.0.1", names=["test"]),
]
udf = RetrieveExasolNodeIPAddressUDF(
host_ip_addresses=host_ip_addresses_mock,
hosts=hosts_mock
)
ctx = Mock()
with pytest.raises(RuntimeError, match=re.escape(
textwrap.dedent(
"""
No or multiple possible IP addresses for current node found: []
Hosts entries: [HostsEntry(entry_type='ipv4', address='192.168.0.1', names=['test'], comment=None)]
IP addresses: [IPAddress(ip_address='192.168.0.1', network_prefix=16)]
"""
).strip()
)):
udf.run(ctx)


def test_udf_host_file(tmp_path):
host_ip_addresses_mock: Union[HostIPAddresses, MagicMock] = create_autospec(HostIPAddresses)
mock_cast(host_ip_addresses_mock.get_all_ip_addresses).return_value = [
IPAddress(ip_address="192.168.0.1", network_prefix=16),
IPAddress(ip_address="193.169.0.1", network_prefix=16),
]
host_file_str = textwrap.dedent(
"""
# hosts is generated by exaconf
127.0.0.1 localhost localhost.localdomain
192.168.0.1 n11
192.168.0.2 n12
"""
)
tmp_host_file = tmp_path / "hosts"
tmp_host_file.write_text(host_file_str)
hosts = Hosts(path=tmp_host_file)
udf = RetrieveExasolNodeIPAddressUDF(
host_ip_addresses=host_ip_addresses_mock,
hosts=hosts
)
ctx = Mock()
udf.run(ctx)
assert ctx.mock_calls == [call.emit('192.168.0.1', 16)]
from exasol_data_science_utils_python.schema.schema_name import SchemaName
from exasol_data_science_utils_python.schema.udf_name import UDFName
from exasol_data_science_utils_python.schema.udf_name_builder import UDFNameBuilder

from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf_deployer import \
RetrieveExasolNodeIPAddressUDFDeployer


def test():
udf_name = UDFNameBuilder().create(name="RETRIEVE_EXASOL_NODE_IP_ADDRESS",
schema=SchemaName("MY_SCHEMA"))
udf_create_statement = \
RetrieveExasolNodeIPAddressUDFDeployer().render_udf_create_statement(udf_name=udf_name,
language_alias="PYTHON3_AAF")
assert udf_create_statement == """
CREATE OR REPLACE PYTHON3_AAF SET SCRIPT "MY_SCHEMA"."RETRIEVE_EXASOL_NODE_IP_ADDRESS"(ignored INTEGER)
EMITS (ip_address VARCHAR(10), network_prefix INTEGER) AS
from exasol_advanced_analytics_framework.udf_communication.retrieve_exasol_node_ip_address_udf import RetrieveExasolNodeIPAddressUDF
def run(ctx):
RetrieveExasolNodeIPAddressUDF().run(ctx)
/
"""

0 comments on commit e00bff3

Please sign in to comment.