Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion munet/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .cleanup import cleanup_previous
from .cleanup import is_running_in_rundir
from .compat import PytestConfig
from .exceptions import MunetConfigError

logger = None

Expand Down Expand Up @@ -207,8 +208,10 @@ def main(*args):
status = asyncio.get_event_loop().run_until_complete(async_main(args, config))
except KeyboardInterrupt:
logger.info("Exiting, received KeyboardInterrupt in main")
except MunetConfigError as error:
logger.critical("Exiting, topology config error: %s", error)
except Exception as error:
logger.info("Exiting, unexpected exception %s", error, exc_info=True)
logger.info("Exiting, unexpected exception: %s", error, exc_info=True)

return status

Expand Down
37 changes: 22 additions & 15 deletions munet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from . import config as munet_config
from . import linux
from .exceptions import MunetConfigError

try:
import pexpect
Expand All @@ -50,10 +51,6 @@
detailed_cmd_logging = False


class MunetError(Exception):
"""A generic munet error."""


class CalledProcessError(subprocess.CalledProcessError):
"""Improved logging subclass of subprocess.CalledProcessError."""

Expand Down Expand Up @@ -2740,13 +2737,14 @@ def __init__(self, name=None, mtu=None, unet=None, **kwargs):

super().__init__(name, pid=unet_pid, nsflags=unet.nsflags, unet=unet, **kwargs)

self.set_intf_basename(self.name + "-e")
self.set_intf_basename("br{}".format(self.id) + "-e")

self.mtu = mtu

self.logger.debug("Bridge: Creating")

# assert len(self.name) <= 16 # Make sure fits in IFNAMSIZE
if len(name) >= 16: # Make sure fits in IFNAMSIZE
raise MunetConfigError(f"Bridge name exceeds 15 chars: {name!r}")
self.cmd_raises(f"ip link delete {name} || true")
self.cmd_raises(f"ip link add {name} type bridge")
if self.mtu:
Expand Down Expand Up @@ -2956,18 +2954,22 @@ def add_link(self, node1, node2, if1, if2, mtu=None, **intf_constraints):
node2 = self.hosts[node2]
name2 = node2.name

hostnames = ()
if name1 in self.switches:
assert name2 in self.hosts
hostnames = (name2,)
elif name2 in self.switches:
assert name1 in self.hosts
hostnames = (name1,)
name1, name2 = name2, name1
if1, if2 = if2, if1
else:
# p2p link
assert name1 in self.hosts
assert name2 in self.hosts
hostnames = (name1, name2)
isp2p = True

for name in hostnames:
if name not in self.hosts:
raise MunetConfigError(f"Unknown host: {name!r}")

lname = "{}:{}-{}:{}".format(name1, if1, name2, if2)
self.logger.debug("%s: add_link %s%s", self, lname, " p2p" if isp2p else "")
self.links[lname] = (name1, if1, name2, if2)
Expand Down Expand Up @@ -3008,11 +3010,16 @@ def add_link(self, node1, node2, if1, if2, mtu=None, **intf_constraints):
if mtu is None:
mtu = switch.mtu

if len(nsif1) > 16:
self.logger.error('"%s" len %s > 16', nsif1, len(nsif1))
elif len(nsif2) > 16:
self.logger.error('"%s" len %s > 16', nsif2, len(nsif2))
assert len(nsif1) < 16 and len(nsif2) < 16 # Make sure fits in IFNAMSIZE
if len(nsif1) >= 16:
self.logger.error('"%s" len %s >= 16', nsif1, len(nsif1))
elif len(nsif2) >= 16:
self.logger.error('"%s" len %s >= 16', nsif2, len(nsif2))

# Make sure fits in IFNAMSIZE
if len(nsif1) >= 16:
raise MunetConfigError(f"Interface name exceeds 15 chars: {nsif1!r}")
if len(nsif2) >= 16:
raise MunetConfigError(f"Interface name exceeds 15 chars: {nsif2!r}")

self.logger.debug("%s: Creating veth pair for link %s", self, lname)

Expand Down
22 changes: 19 additions & 3 deletions munet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from copy import deepcopy
from typing import overload

from .exceptions import MunetConfigError

def find_with_kv(lst, k, v):
if lst:
Expand Down Expand Up @@ -58,7 +59,7 @@ def merge_using_key(a, b, k):


def list_to_dict_with_key(lst, k):
"""Convert a YANG styl list of objects to dict of objects.
"""Convert a YANG style list of objects to dict of objects.

This function converts a YANG style list of objects (dictionaries) to a plain python
dictionary of objects (dictionaries). The value for the supplied key for each
Expand All @@ -70,10 +71,19 @@ def list_to_dict_with_key(lst, k):
lst: a *list* of python dictionary objects.
k: the key value contained in each dictionary object in the list.

Raises:
MunetConfigError if duplicate keys are present

Returns:
A dictionary of objects (dictionaries).
"""
return {x[k]: x for x in (lst if lst else [])}
result = {}
for x in lst or []:
key = x[k]
if key in result:
raise MunetConfigError(f"Duplicate '{k}' value: {key!r}")
result[key] = x
return result


def config_to_dict_with_key(c, ck, k):
Expand All @@ -87,10 +97,16 @@ def config_to_dict_with_key(c, ck, k):
ck: The key identifying the list of objects from ``c``.
k: The key to pass to :py:func:`list_to_dict_with_key`.

Raises:
MunetConfigError if duplicate keys are present

Returns:
A dictionary of objects (dictionaries).
"""
c[ck] = list_to_dict_with_key(c.get(ck, []), k)
try:
c[ck] = list_to_dict_with_key(c.get(ck, []), k)
except MunetConfigError as error:
raise MunetConfigError(f"Invalid '{ck}' configuration: {error}") from error
return c[ck]


Expand Down
16 changes: 16 additions & 0 deletions munet/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 eval: (blacken-mode 1) -*-
# SPDX-License-Identifier: GPL-2.0-or-later
#
# November 18 2026, Liam Brady <lbrady@labn.net>
#
# Copyright 2026, LabN Consulting, L.L.C.
#
"""A module that defines munet exceptions."""


class MunetError(Exception):
"""A generic munet error."""


class MunetConfigError(MunetError):
"""Invalid munet topology or configuration."""
7 changes: 7 additions & 0 deletions munet/mutest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from munet.base import Bridge
from munet.cli import async_cli
from munet.compat import PytestConfig
from munet.exceptions import MunetConfigError
from munet.mutest import userapi as uapi
from munet.native import L3NodeMixin
from munet.native import Munet
Expand Down Expand Up @@ -69,6 +70,9 @@ async def get_unet(
pytestconfig=PytestConfig(args),
unshare_inline=unshare,
)
except MunetConfigError as error:
# Config errors are expected user errors; propagate without a traceback.
raise
except Exception as error:
logging.debug("unet build failed: %s", error, exc_info=True)
raise
Expand Down Expand Up @@ -327,6 +331,9 @@ async def run_tests(args):
errlog.warning("KeyboardInterrupt while running test %s", test_name)
passed, failed, e = 0, 0, error
raise
except MunetConfigError as error:
errlog.error("Topology error in test %s: %s", test, error)
passed, failed, e = 0, 0, error
except Exception as error:
logging.error(
"Error executing test %s: %s", test, error, exc_info=True
Expand Down
Loading
Loading