2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-29 13:38:26 +00:00

Allow to use an arbitrary numeric identifier for NamedInstance

In some cases, the numeric identifier doesn't correspond to the
directory name (i.e. `resolver` server in `shutdown` test, which is
supposed to be 10.53.0.3). These are typically servers that shouldn't be
auto-started by the runner, thus avoiding the typical `*ns<X>` name.

Support these server by allowing a fallback initialization with custom
numeric identifier in case it can't be parsed from the directory name.
This commit is contained in:
Nicki Křížek 2025-01-24 10:35:06 +01:00 committed by Michal Nowak
parent 37699ad84b
commit a24f71bae4
3 changed files with 20 additions and 19 deletions

View File

@ -50,13 +50,17 @@ class NamedInstance:
def __init__( def __init__(
self, self,
identifier: str, identifier: str,
num: Optional[int] = None,
ports: Optional[NamedPorts] = None, ports: Optional[NamedPorts] = None,
rndc_logger: Optional[logging.Logger] = None, rndc_logger: Optional[logging.Logger] = None,
rndc_executor: Optional[RNDCExecutor] = None, rndc_executor: Optional[RNDCExecutor] = None,
) -> None: ) -> None:
""" """
`identifier` must be an `ns<X>` string, where `<X>` is an integer `identifier` is the name of the instance's directory
identifier of the `named` instance this object should represent.
`num` is optional if the identifier is in a form of `ns<X>`, in which
case `<X>` is assumed to be numeric identifier; otherwise it must be
provided to assign a numeric identification to the server
`ports` is the `NamedPorts` instance listing the UDP/TCP ports on which `ports` is the `NamedPorts` instance listing the UDP/TCP ports on which
this `named` instance is listening for various types of traffic (both this `named` instance is listening for various types of traffic (both
@ -75,7 +79,7 @@ class NamedInstance:
self.system_test_name = self.directory.parent.name self.system_test_name = self.directory.parent.name
self.identifier = identifier self.identifier = identifier
self.ip = self._identifier_to_ip(identifier) self.num = self._identifier_to_num(identifier, num)
if ports is None: if ports is None:
ports = NamedPorts.from_env() ports = NamedPorts.from_env()
self.ports = ports self.ports = ports
@ -83,12 +87,21 @@ class NamedInstance:
self._rndc_executor = rndc_executor or RNDCBinaryExecutor() self._rndc_executor = rndc_executor or RNDCBinaryExecutor()
self._rndc_logger = rndc_logger self._rndc_logger = rndc_logger
@property
def ip(self) -> str:
"""IPv4 address of the instance."""
return f"10.53.0.{self.num}"
@staticmethod @staticmethod
def _identifier_to_ip(identifier: str) -> str: def _identifier_to_num(identifier: str, num: Optional[int] = None) -> int:
regex_match = re.match(r"^ns(?P<index>[0-9]{1,2})$", identifier) regex_match = re.match(r"^ns(?P<index>[0-9]{1,2})$", identifier)
if not regex_match: if not regex_match:
raise ValueError("Invalid named instance identifier" + identifier) if num is None:
return "10.53.0." + regex_match.group("index") raise ValueError(f'Can\'t parse numeric identifier from "{identifier}"')
return num
parsed_num = int(regex_match.group("index"))
assert num is None or num == parsed_num, "mismatched num and identifier"
return parsed_num
def rndc(self, command: str, ignore_errors: bool = False, log: bool = True) -> str: def rndc(self, command: str, ignore_errors: bool = False, log: bool = True) -> str:
""" """

View File

@ -137,18 +137,6 @@ def get_named_cmdline(cfg_dir, cfg_file="named.conf"):
return named_cmdline return named_cmdline
def get_custom_named_instance(assumed_ns):
# This test launches and monitors a named instance itself rather than using
# bin/tests/system/start.pl, so manually defining a NamedInstance here is
# necessary for sending RNDC commands to that instance. If this "custom"
# instance listens on 10.53.0.3, use "ns3" as the identifier passed to
# the NamedInstance constructor.
named_ports = isctest.instance.NamedPorts.from_env()
instance = isctest.instance.NamedInstance(assumed_ns, named_ports)
return instance
def assert_custom_named_is_alive(named_proc, resolver_ip): def assert_custom_named_is_alive(named_proc, resolver_ip):
assert named_proc.poll() is None, "named isn't running" assert named_proc.poll() is None, "named isn't running"
msg = dns.message.make_query("version.bind", "TXT", "CH") msg = dns.message.make_query("version.bind", "TXT", "CH")

View File

@ -170,7 +170,7 @@ def test_named_shutdown(kill_method):
cfg_dir = "resolver" cfg_dir = "resolver"
named_cmdline = isctest.run.get_named_cmdline(cfg_dir) named_cmdline = isctest.run.get_named_cmdline(cfg_dir)
instance = isctest.run.get_custom_named_instance("ns3") instance = isctest.instance.NamedInstance("resolver", num=3)
with open(os.path.join(cfg_dir, "named.run"), "ab") as named_log: with open(os.path.join(cfg_dir, "named.run"), "ab") as named_log:
with subprocess.Popen( with subprocess.Popen(