Compare commits

...

1 Commits

Author SHA1 Message Date
Štěpán Balážik
7bc5ab40c6 Unify the way ports are stored in Python system tests
`ports` fixture now returns NamedPorts instance directly.
We still need to map them to legacy enviroment variables for the system
tests written in shell, so there is NamedPorts.ATTRIBUTE_TO_ENV_VAR for
that.
2024-03-07 08:49:17 +01:00
3 changed files with 68 additions and 43 deletions

View File

@@ -257,51 +257,37 @@ def base_port(request, module_base_ports):
@pytest.fixture(scope="module")
def ports(base_port):
def ports(base_port: int) -> isctest.instance.NamedPorts:
"""Dictionary containing port names and their assigned values."""
return {
"PORT": base_port,
"TLSPORT": base_port + 1,
"HTTPPORT": base_port + 2,
"HTTPSPORT": base_port + 3,
"EXTRAPORT1": base_port + 4,
"EXTRAPORT2": base_port + 5,
"EXTRAPORT3": base_port + 6,
"EXTRAPORT4": base_port + 7,
"EXTRAPORT5": base_port + 8,
"EXTRAPORT6": base_port + 9,
"EXTRAPORT7": base_port + 10,
"EXTRAPORT8": base_port + 11,
"CONTROLPORT": base_port + 12,
}
return isctest.instance.NamedPorts(base_port)
@pytest.fixture(scope="module")
def named_port(ports):
return ports["PORT"]
def named_port(ports: isctest.instance.NamedPorts):
return ports.dns
@pytest.fixture(scope="module")
def named_tlsport(ports):
return ports["TLSPORT"]
def named_tlsport(ports: isctest.instance.NamedPorts):
return ports.tls
@pytest.fixture(scope="module")
def named_httpsport(ports):
return ports["HTTPSPORT"]
def named_httpsport(ports: isctest.instance.NamedPorts):
return ports.https
@pytest.fixture(scope="module")
def control_port(ports):
return ports["CONTROLPORT"]
def control_port(ports: isctest.instance.NamedPorts):
return ports.control
@pytest.fixture(scope="module")
def env(ports):
def env(ports: isctest.instance.NamedPorts):
"""Dictionary containing environment variables for the test."""
env = os.environ.copy()
for portname, portnum in ports.items():
env[portname] = str(portnum)
for attribute_name, environment_variable in ports.ATTRIBUTE_TO_ENV_VAR.items():
env[environment_variable] = str(getattr(ports, attribute_name))
env["builddir"] = f"{env['TOP_BUILDDIR']}/{SYSTEM_TEST_DIR_GIT_PATH}"
env["srcdir"] = f"{env['TOP_SRCDIR']}/{SYSTEM_TEST_DIR_GIT_PATH}"
return env
@@ -625,17 +611,13 @@ def system_test( # pylint: disable=too-many-arguments,too-many-statements
@pytest.fixture
def servers(ports, system_test_dir):
def servers(ports: isctest.instance.NamedPorts, system_test_dir):
instances = {}
for entry in system_test_dir.rglob("*"):
if entry.is_dir():
try:
dir_name = entry.name
# LATER: Make ports fixture return NamedPorts directly
named_ports = isctest.instance.NamedPorts(
dns=int(ports["PORT"]), rndc=int(ports["CONTROLPORT"])
)
instance = isctest.instance.NamedInstance(dir_name, named_ports)
instance = isctest.instance.NamedInstance(dir_name, ports)
instances[dir_name] = instance
except ValueError:
continue

View File

@@ -11,7 +11,7 @@
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
from typing import NamedTuple, Optional
from typing import Optional
import logging
import os
@@ -21,9 +21,55 @@ from .rndc import RNDCBinaryExecutor, RNDCException, RNDCExecutor
from .log import info, LogFile, WatchLogFromStart, WatchLogFromHere
class NamedPorts(NamedTuple):
dns: int = 53
rndc: int = 953
# pylint: disable=too-many-instance-attributes
class NamedPorts:
dns = 53
tls = 853
http = 80
https = 443
extra1 = 1337
extra2 = extra1 + 1
extra3 = extra1 + 2
extra4 = extra1 + 3
extra5 = extra1 + 4
extra6 = extra1 + 5
extra7 = extra1 + 6
extra8 = extra1 + 7
control = 953
ATTRIBUTE_TO_ENV_VAR = {
"dns": "PORT",
"tls": "TLSPORT",
"http": "HTTPPORT",
"https": "HTTPSPORT",
"extra1": "EXTRAPORT1",
"extra2": "EXTRAPORT2",
"extra3": "EXTRAPORT3",
"extra4": "EXTRAPORT4",
"extra5": "EXTRAPORT5",
"extra6": "EXTRAPORT6",
"extra7": "EXTRAPORT7",
"extra8": "EXTRAPORT8",
"control": "CONTROLPORT",
}
def __init__(self, base_port: Optional[int] = None) -> None:
if base_port is None:
# Defaults from above will be used
return
self.dns = base_port
self.tls = base_port + 1
self.http = base_port + 2
self.https = base_port + 3
self.extra1 = base_port + 4
self.extra2 = base_port + 5
self.extra3 = base_port + 6
self.extra4 = base_port + 7
self.extra5 = base_port + 8
self.extra6 = base_port + 9
self.extra7 = base_port + 10
self.extra8 = base_port + 11
self.control = base_port + 12
class NamedInstance:
@@ -115,7 +161,7 @@ class NamedInstance:
>>> response = ns4.rndc("stop", ignore_errors=True, log=False)
"""
try:
response = self._rndc_executor.call(self.ip, self.ports.rndc, command)
response = self._rndc_executor.call(self.ip, self.ports.control, command)
if log:
self._rndc_log(command, response)
except RNDCException as exc:

View File

@@ -186,15 +186,12 @@ def test_named_shutdown(ports, kill_method):
# necessary for sending RNDC commands to that instance. This "custom"
# instance listens on 10.53.0.3, so use "ns3" as the identifier passed to
# the NamedInstance constructor.
named_ports = isctest.instance.NamedPorts(
dns=ports["PORT"], rndc=ports["CONTROLPORT"]
)
instance = isctest.instance.NamedInstance("ns3", named_ports)
instance = isctest.instance.NamedInstance("ns3", ports)
# We create a resolver instance that will be used to send queries.
resolver = dns.resolver.Resolver()
resolver.nameservers = ["10.53.0.3"]
resolver.port = named_ports.dns
resolver.port = ports.dns
named_cmdline = [named, "-c", cfg_file, "-d", "99", "-g"]
with open(os.path.join(cfg_dir, "named.run"), "ab") as named_log: