Skip to content

Commit

Permalink
fix(MDL): Throw on faulty pipeline connection address
Browse files Browse the repository at this point in the history
See merge request Karabo/Framework!8138
  • Loading branch information
dgoeries committed Feb 22, 2024
2 parents 3243402 + afaf0e7 commit af47b04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
27 changes: 13 additions & 14 deletions src/pythonKarabo/karabo/middlelayer/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@
def get_hostname_from_interface(address_range):
"""returns the ip address of the local interfaces
in case of bad input, it will return the result of
`socket.gethostname()`
in case of bad input, it will throw
"""
try:
network = ip_network(address_range)
for interface in net_if_addrs().values():
for nic in interface:
if nic.family != socket.AF_INET:
continue
addr = ip_address(nic.address)
if addr in network:
return nic.address
except ValueError:
pass
return socket.gethostname()
# ip_network will throw if address cannot be found
network = ip_network(address_range)
for interface in net_if_addrs().values():
for nic in interface:
if nic.family != socket.AF_INET:
continue
addr = ip_address(nic.address)
if addr in network:
return nic.address
raise ValueError(
f"{address_range} does not appear to be an IPv4 or IPv6 network")


class CancelQueue(Queue):
Expand Down Expand Up @@ -1202,6 +1200,7 @@ def toDataAndAttrs(self, instance):


class OutputChannel(Node):

def __init__(self, cls=None, **kwargs):
if cls is None:
# Child check for output schema
Expand Down
26 changes: 23 additions & 3 deletions src/pythonKarabo/karabo/middlelayer/tests/remote_pipeline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

from karabo.middlelayer import (
AccessLevel, AccessMode, Assignment, Bool, Configurable, Device, Hash,
InputChannel, Int32, Node, OutputChannel, Overwrite, PipelineContext,
PipelineMetaData, Slot, State, Timestamp, UInt32, background, call,
getDevice, getSchema, isAlive, setWait, slot, waitUntil)
InputChannel, Int32, NetworkOutput, Node, OutputChannel, Overwrite,
PipelineContext, PipelineMetaData, Slot, State, Timestamp, UInt32,
background, call, getDevice, getSchema, isAlive, setWait, slot, waitUntil)
from karabo.middlelayer.testing import AsyncDeviceContext, run_test, sleepUntil

FIXED_TIMESTAMP = Timestamp("2009-04-20T10:32:22 UTC")
Expand Down Expand Up @@ -876,3 +876,23 @@ def new_connect_handler(channel):
await wait_for(waitUntil(lambda: input_proxy.received > 0),
timeout=timeout)
assert receiver.received > 0


@pytest.mark.timeout(30)
@run_test
async def test_output_fail_address(event_loop):
"""Test the output with a wrong address"""

class Output(NetworkOutput):
hostname = Overwrite(defaultValue="192.168.1/22")

class SimpleSender(Device):

output = Node(Output)

config = {"_deviceId_": "outputraisedevice"}

output_device = SimpleSender(config)
with pytest.raises(ValueError) as exc:
await output_device.startInstance()
assert "does not appear to be an IPv4 or IPv6 network" in str(exc)

0 comments on commit af47b04

Please sign in to comment.