Commit c1396f2db8 for frr
commit c1396f2db8c2ac1b62c9a2785892422222d24086
Author: Jafar Al-Gharaibeh <jafar@atcorp.com>
Date: Tue Aug 25 12:34:12 2026 -0500
tests: skip RPKI topotests when FRR is built without RPKI
bgp_rpki_topo1 and bgp_aspa_topo1 start bgpd with -M bgpd_rpki. On a
build without --enable-rpki every test then fails with "bgpd failed to
connect to zebra", which hides the real reason.
Add topotest.require_frr_module(), called from setup_module(). It
reads the configure arguments from "bgpd -v" and skips the module's
tests when the feature was not enabled. When it was enabled but the
module is not in the module directory, the tests error at setup with
the path that was checked, so a missing module is obvious. The module
directory comes from --with-moduledir, or else ${libdir}/frr/modules,
with ${prefix}, ${exec_prefix} and ${libdir} expanded as configure
does. Packaged layouts that keep modules outside the daemon directory
and source builds that pass --with-moduledir=${prefix}/... both work.
If the configure arguments are not available, a missing module skips.
topogen.get_frrdir() returns the daemon directory from pytest.ini, so
the check probes the same daemons the tests run.
Signed-off-by: Jafar Al-Gharaibeh <jafar@atcorp.com>
diff --git a/tests/topotests/bgp_aspa_topo1/test_bgp_aspa_topo1.py b/tests/topotests/bgp_aspa_topo1/test_bgp_aspa_topo1.py
index bbe178c846..3b36dffeb7 100644
--- a/tests/topotests/bgp_aspa_topo1/test_bgp_aspa_topo1.py
+++ b/tests/topotests/bgp_aspa_topo1/test_bgp_aspa_topo1.py
@@ -34,7 +34,7 @@ sys.path.append(os.path.join(CWD, "../"))
# pylint: disable=C0413
from lib import topotest
-from lib.topogen import Topogen, get_topogen
+from lib.topogen import Topogen, get_frrdir, get_topogen
from lib.common_config import step
from lib.topolog import logger
@@ -59,6 +59,9 @@ def build_topo(tgen):
def setup_module(mod):
+ # r2 runs bgpd with -M bgpd_rpki.
+ topotest.require_frr_module("bgpd_rpki", "rpki", get_frrdir())
+
tgen = Topogen(build_topo, mod.__name__)
tgen.start_topology()
diff --git a/tests/topotests/bgp_rpki_topo1/test_bgp_rpki_topo1.py b/tests/topotests/bgp_rpki_topo1/test_bgp_rpki_topo1.py
index 50e0ea7447..769658bf1c 100644
--- a/tests/topotests/bgp_rpki_topo1/test_bgp_rpki_topo1.py
+++ b/tests/topotests/bgp_rpki_topo1/test_bgp_rpki_topo1.py
@@ -15,7 +15,7 @@ sys.path.append(os.path.join(CWD, "../"))
# pylint: disable=C0413
from lib import topotest
-from lib.topogen import Topogen, TopoRouter, get_topogen
+from lib.topogen import Topogen, TopoRouter, get_frrdir, get_topogen
from lib.common_config import step, shutdown_bringup_interface_in_kernel
from lib.topolog import logger
@@ -43,6 +43,9 @@ def setup_module(mod):
if not topotest.platform_has_vrf():
pytest.skip("platform does not support VRF")
+ # r2 runs bgpd with -M bgpd_rpki.
+ topotest.require_frr_module("bgpd_rpki", "rpki", get_frrdir())
+
tgen = Topogen(build_topo, mod.__name__)
tgen.start_topology()
diff --git a/tests/topotests/lib/topogen.py b/tests/topotests/lib/topogen.py
index a74c4a0ecb..73363c556e 100644
--- a/tests/topotests/lib/topogen.py
+++ b/tests/topotests/lib/topogen.py
@@ -131,6 +131,21 @@ tgen_defaults = {
}
+def _read_config():
+ "Read `pytest.ini` at the root dir of topotests over the defaults."
+ config = configparser.ConfigParser(tgen_defaults)
+ config.read(os.path.join(CWD, "../pytest.ini"))
+ return config
+
+
+def get_frrdir():
+ """
+ Return the FRR daemon directory that Topogen starts daemons from, as set
+ in `pytest.ini`. Usable before a Topogen exists.
+ """
+ return _read_config().get(Topogen.CONFIG_SECTION, "frrdir")
+
+
class Topogen(object):
"A topology test builder helper."
@@ -290,9 +305,7 @@ class Topogen(object):
Loads the configuration file `pytest.ini` located at the root dir of
topotests.
"""
- self.config = configparser.ConfigParser(tgen_defaults)
- pytestini_path = os.path.join(CWD, "../pytest.ini")
- self.config.read(pytestini_path)
+ self.config = _read_config()
def add_router(self, name=None, cls=None, **params):
"""
diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py
index 630cb2a6ee..c777cdfa08 100644
--- a/tests/topotests/lib/topotest.py
+++ b/tests/topotests/lib/topotest.py
@@ -908,6 +908,179 @@ def module_present(module, load=True):
return module_present_freebsd(module, load)
+def _frr_bindirs(frrdir=None):
+ """Candidate FRR daemon directories: `frrdir` alone when given."""
+ if frrdir is not None:
+ return [frrdir]
+ return ["/usr/lib/frr", "/usr/lib64/frr"]
+
+
+@functools.lru_cache(maxsize=None)
+def frr_configure_args(frrdir=None):
+ """
+ Return the configure arguments of the installed daemons, as a tuple,
+ from ``bgpd -v`` or ``zebra -v``. Empty if they cannot be determined,
+ for example with ``--disable-version-build-config``. The probe runs
+ once per directory.
+ """
+ for d in _frr_bindirs(frrdir):
+ for daemon in ("bgpd", "zebra"):
+ path = os.path.join(d, daemon)
+ if not os.path.isfile(path):
+ continue
+ # The exit status is ignored: the version text is what matters.
+ try:
+ out = subprocess.run(
+ [path, "-v"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL,
+ universal_newlines=True,
+ check=False,
+ ).stdout
+ except OSError:
+ continue
+ # Example: configured with:\n\t'--prefix=/usr' '--enable-rpki' ...
+ m = re.search(r"configured with:\s*\n\s*(.*)", out)
+ if m:
+ args = re.findall(r"'([^']*)'", m.group(1))
+ if args:
+ return tuple(args)
+ return ()
+
+
+def _configure_value(args, option, separate=True):
+ """
+ Return the last value given for `option` in `args`, or None. With
+ `separate`, ``option value`` is accepted as well as ``option=value``.
+ """
+ value = None
+ it = iter(args)
+ for arg in it:
+ if arg.startswith(option + "="):
+ value = arg.split("=", 1)[1]
+ elif separate and arg == option:
+ value = next(it, None)
+ return value
+
+
+def _expand_configure_vars(value, variables):
+ """
+ Expand ``${name}`` and ``$name`` references in a configure directory
+ value, as configure does for the installation directory variables.
+ Return None if a reference is left that cannot be expanded.
+ """
+ for _ in range(len(variables) + 1):
+ if "$" not in value:
+ return os.path.normpath(value)
+ for name, sub in variables.items():
+ value = value.replace("${%s}" % name, sub)
+ value = re.sub(r"\$%s\b" % name, lambda _m, s=sub: s, value)
+ return None if "$" in value else os.path.normpath(value)
+
+
+def frr_module_dir(frrdir=None):
+ """
+ Return the directory the installed daemons load dlopen modules from:
+ ``--with-moduledir`` if given, else ``${libdir}/frr/modules``, with
+ ``${prefix}``, ``${exec_prefix}`` and ``${libdir}`` expanded as
+ configure does. None if the configure arguments are unknown or the
+ directory refers to anything else.
+ """
+ args = frr_configure_args(frrdir)
+ if not args:
+ return None
+ variables = {
+ "prefix": _configure_value(args, "--prefix") or "/usr/local",
+ "exec_prefix": _configure_value(args, "--exec-prefix") or "${prefix}",
+ "libdir": _configure_value(args, "--libdir") or "${exec_prefix}/lib",
+ }
+ moduledir = _configure_value(args, "--with-moduledir", separate=False)
+ if moduledir is None:
+ moduledir = "${libdir}/frr/modules"
+ return _expand_configure_vars(moduledir, variables)
+
+
+def frr_module_available(name, frrdir=None):
+ """
+ Return True if an FRR dlopen module is installed.
+
+ `name` is the module basename as passed to `-M` (e.g. ``bgpd_rpki``).
+ It is looked for in ``frr_module_dir()``, or in ``{frrdir}/modules`` if
+ that is unknown.
+ """
+ moduledir = frr_module_dir(frrdir)
+ if moduledir is not None:
+ dirs = [moduledir]
+ else:
+ dirs = [os.path.join(d, "modules") for d in _frr_bindirs(frrdir)]
+ for d in dirs:
+ if os.path.isfile(os.path.join(d, "{}.so".format(name))):
+ return True
+ return False
+
+
+def frr_configured_with(feature, frrdir=None):
+ """
+ Return whether FRR was configured with ``--enable-<feature>``.
+
+ Arguments are matched exactly, and the last one for the feature wins,
+ as with autoconf: only ``--enable-<feature>`` or
+ ``--enable-<feature>=yes`` enables it.
+
+ Returns:
+ True - configure args are known and enable `feature`
+ False - configure args are known and do not enable `feature`
+ None - configure args unavailable. Callers must not treat this as
+ "feature disabled"; fall back to another signal such as
+ ``frr_module_available()``.
+ """
+ args = frr_configure_args(frrdir)
+ if not args:
+ return None
+
+ value = "no"
+ for arg in args:
+ if arg == "--enable-" + feature:
+ value = "yes"
+ elif arg.startswith("--enable-{}=".format(feature)):
+ value = arg.split("=", 1)[1]
+ elif arg == "--disable-" + feature:
+ value = "no"
+ return value == "yes"
+
+
+def require_frr_module(module, feature, frrdir=None):
+ """
+ Skip or error the calling module when a dlopen module it loads is missing.
+
+ Call from ``setup_module()``. Without ``--enable-<feature>`` the tests
+ are skipped. With it, a missing ``<module>.so`` errors the tests at
+ setup, so a missing module is obvious instead of every test failing with
+ "bgpd failed to connect to zebra". On a packaged install this includes
+ the package that carries the module not being installed. When the
+ configure arguments or the module directory are unknown, a missing
+ module skips.
+ """
+ import pytest # pylint: disable=import-outside-toplevel
+
+ enabled = frr_configured_with(feature, frrdir)
+ if enabled is False:
+ pytest.skip("FRR built without --enable-{}".format(feature))
+ if frr_module_available(module, frrdir):
+ return
+ moduledir = frr_module_dir(frrdir)
+ if enabled is True and moduledir is not None:
+ pytest.fail(
+ "FRR was built with --enable-{} but {} is not installed".format(
+ feature, os.path.join(moduledir, "{}.so".format(module))
+ )
+ )
+ pytest.skip(
+ "FRR module {}.so not found (cannot confirm --enable-{} or the "
+ "module directory)".format(module, feature)
+ )
+
+
def version_cmp(v1, v2):
"""
Compare two version strings and returns: