]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Coalesce system test variables
authorTom Krizek <tkrizek@isc.org>
Thu, 4 Jan 2024 13:48:04 +0000 (14:48 +0100)
committerNicki Křížek <nicki@isc.org>
Thu, 9 May 2024 15:08:09 +0000 (17:08 +0200)
Provide a single point of access to all the variables used by tests. Use
a custom dict-like structure to access the underlying data without
making a copy. This allows the individual modules to update the contents
at runtime, which is used for some variables.

bin/tests/system/conftest.py
bin/tests/system/isctest/vars/all.py

index 109618e1aa76c86fae7d8ffaea2071e0cd82bf78..75d665d9fb6aac499f31fd62a66700a19a1bb7de 100644 (file)
@@ -276,11 +276,12 @@ def control_port(ports):
 @pytest.fixture(scope="module")
 def env(ports):
     """Dictionary containing environment variables for the test."""
-    env = os.environ.copy()
+    env = dict(isctest.vars.ALL)
     for portname, portnum in ports.items():
         env[portname] = str(portnum)
     env["builddir"] = f"{env['TOP_BUILDDIR']}/{SYSTEM_TEST_DIR_GIT_PATH}"
     env["srcdir"] = f"{env['TOP_SRCDIR']}/{SYSTEM_TEST_DIR_GIT_PATH}"
+    os.environ.update(env)
     return env
 
 
index 7eaa48f3d42450c341faf7069f548880445e6630..58e1689af2b99440e44423ea70fbd7b84637ac74 100644 (file)
@@ -9,10 +9,43 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
+from collections import ChainMap
+
 # pylint: disable=import-error
 from .autoconf import AC_VARS  # type: ignore
 
 # pylint: enable=import-error
 from .basic import BASIC_VARS
 
-ALL = {**AC_VARS, **BASIC_VARS}
+
+class VarLookup(ChainMap):
+    """A dictionary-like structure to coalesce the variables from different
+    modules without making a copy (which would prevent updating these values
+    from inside the modules)."""
+
+    def __init__(self, *maps):
+        keys = set()
+        for m in maps:
+            overlap = keys.intersection(m.keys())
+            if overlap:
+                raise RuntimeError(f"key(s) are defined multiple times: {overlap}")
+            keys = keys.union(m.keys())
+        super().__init__(*maps)
+
+    def __setitem__(self, *args, **kwargs):
+        raise RuntimeError("read-only structure")
+
+    def keys(self):
+        result = set()
+        for m in self.maps:
+            for key, val in m.items():
+                if val is None:  # treat None as unset
+                    continue
+                result.add(key)
+        return list(result)
+
+    def __iter__(self):
+        return iter(self.keys())
+
+
+ALL = VarLookup(AC_VARS, BASIC_VARS)