]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Reuse common port-related test fixtures
authorMichał Kępień <michal@isc.org>
Mon, 14 Mar 2022 07:59:32 +0000 (08:59 +0100)
committerMichał Kępień <michal@isc.org>
Mon, 14 Mar 2022 07:59:32 +0000 (08:59 +0100)
Most Python-based system tests need to know which ports were assigned to
a given test by bin/tests/system/get_ports.sh.  This is currently
handled by inspecting the values of various environment variables (set
by bin/tests/system/run.sh) and passing the port numbers to Python
scripts via pytest fixtures.  However, this glue code has so far been
copy-pasted into each system test using it, rather than reused.

Since pytest also looks for conftest.py files in parent directories,
move commonly used fixtures to bin/tests/system/conftest.py.  Set the
scope of all the moved fixtures to "session" as their return values are
only based on environment variables, so there is no point in recreating
them for every test requesting them.  Adjust test code accordingly.

15 files changed:
bin/tests/system/checkds/conftest.py
bin/tests/system/conftest.py [moved from bin/tests/system/wildcard/conftest.py with 57% similarity]
bin/tests/system/dispatch/conftest.py [deleted file]
bin/tests/system/dispatch/tests-connreset.py
bin/tests/system/doth/conftest.py
bin/tests/system/rpzextra/conftest.py
bin/tests/system/run.sh.in
bin/tests/system/shutdown/conftest.py
bin/tests/system/statschannel/conftest.py
bin/tests/system/statschannel/tests-json.py
bin/tests/system/statschannel/tests-xml.py
bin/tests/system/tcp/conftest.py
bin/tests/system/tcp/tests-tcp.py
bin/tests/system/timeouts/conftest.py
bin/tests/system/timeouts/tests-tcp.py

index bfe2ff40c6b30543022fd2acd5e85b74857ee760..5328ae0177ade68390b0b8104cd81855b67b9880 100644 (file)
@@ -9,7 +9,6 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-import os
 import pytest
 
 
@@ -45,27 +44,3 @@ def pytest_collection_modifyitems(config, items):
         for item in items:
             if "dnspython2" in item.keywords:
                 item.add_marker(skip_dnspython2)
-
-
-@pytest.fixture
-def named_port(request):
-    # pylint: disable=unused-argument
-    port = os.getenv("PORT")
-    if port is None:
-        port = 5301
-    else:
-        port = int(port)
-
-    return port
-
-
-@pytest.fixture
-def control_port(request):
-    # pylint: disable=unused-argument
-    port = os.getenv("CONTROLPORT")
-    if port is None:
-        port = 5301
-    else:
-        port = int(port)
-
-    return port
similarity index 57%
rename from bin/tests/system/wildcard/conftest.py
rename to bin/tests/system/conftest.py
index 057c3785dfe693195f26ac7cf133172b6e0f4f6f..2fd05add7d1a9aa5e6e43e8c30e9ff441462acbf 100644 (file)
@@ -1,3 +1,5 @@
+#!/usr/bin/python3
+
 # Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 #
 # SPDX-License-Identifier: MPL-2.0
 # information regarding copyright ownership.
 
 import os
+
 import pytest
 
 
-@pytest.fixture(scope='module')
+@pytest.fixture(scope='session')
 def named_port():
-    return int(os.environ.get("PORT", default=5300))
+    return int(os.environ.get('PORT', default=5300))
+
+
+@pytest.fixture(scope='session')
+def named_tlsport():
+    return int(os.environ.get('TLSPORT', default=8853))
+
+
+@pytest.fixture(scope='session')
+def control_port():
+    return int(os.environ.get('CONTROLPORT', default=9953))
diff --git a/bin/tests/system/dispatch/conftest.py b/bin/tests/system/dispatch/conftest.py
deleted file mode 100644 (file)
index c1c19fd..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
-#
-# SPDX-License-Identifier: MPL-2.0
-#
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0.  If a copy of the MPL was not distributed with this
-# file, you can obtain one at https://mozilla.org/MPL/2.0/.
-#
-# See the COPYRIGHT file distributed with this work for additional
-# information regarding copyright ownership.
-
-import os
-import pytest
-
-
-@pytest.fixture
-def port(request):
-    # pylint: disable=unused-argument
-    env_port = os.getenv("PORT")
-    if env_port is None:
-        env_port = 5300
-    else:
-        env_port = int(env_port)
-
-    return env_port
index 64c61a95d558d28fad68b7d6f5435becee7f3439..61758ffe6dd9cd62ffabd3c0aa7dc741dbc24358 100644 (file)
@@ -19,8 +19,8 @@ import dns.query
 import dns.rcode
 
 
-def test_connreset(port):
+def test_connreset(named_port):
     msg = dns.message.make_query("sub.example.", "A", want_dnssec=True,
                                  use_edns=0, payload=1232)
-    ans = dns.query.udp(msg, "10.53.0.2", timeout=10, port=port)
+    ans = dns.query.udp(msg, "10.53.0.2", timeout=10, port=named_port)
     assert ans.rcode() == dns.rcode.SERVFAIL
index 8978b652aabecf0635145a524f0fe864ddc68052..ed46680c710a5d5212f41e1a3af517e3c1f8deee 100644 (file)
@@ -11,7 +11,6 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-import os
 import shutil
 import subprocess
 
@@ -36,8 +35,3 @@ def gnutls_cli_executable():
         pytest.skip('gnutls-cli does not support the --logfile option')
 
     return executable
-
-
-@pytest.fixture
-def named_tlsport():
-    return int(os.environ.get('TLSPORT', '853'))
index 05bf1441905a0514ed75ae6401cedc79c52de990..8bbcf25ad7846f64387e9bed90b928ed427b9eb7 100644 (file)
@@ -40,15 +40,3 @@ def pytest_collection_modifyitems(config, items):
         for item in items:
             if "json" in item.keywords:
                 item.add_marker(no_jsonstats)
-
-
-@pytest.fixture
-def named_port(request):
-    # pylint: disable=unused-argument
-    port = os.getenv("PORT")
-    if port is None:
-        port = 5301
-    else:
-        port = int(port)
-
-    return port
index c8467e6b9473c4bf99606ab2b1d6046afad10e39..532134cee410a02a688b9fd5d4b9db11257ddc92 100644 (file)
@@ -111,7 +111,7 @@ if [ "${srcdir}" != "${builddir}" ]; then
         cp -a "${srcdir}/common" "${builddir}"
     fi
     # Some tests require additional files to work for out-of-tree test runs.
-    for file in ckdnsrps.sh digcomp.pl ditch.pl fromhex.pl kasp.sh packet.pl start.pl stop.pl testcrypto.sh; do
+    for file in ckdnsrps.sh conftest.py digcomp.pl ditch.pl fromhex.pl kasp.sh packet.pl start.pl stop.pl testcrypto.sh; do
         if [ ! -r "${file}" ]; then
             cp -a "${srcdir}/${file}" "${builddir}"
         fi
index 3c3f9d1165de2887533f890ea8b27c33eb621fcd..04741da104e71979915f50134b71b125ae91a7a7 100644 (file)
@@ -9,7 +9,6 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-import os
 import pytest
 
 
@@ -32,27 +31,3 @@ def pytest_collection_modifyitems(config, items):
         for item in items:
             if "dnspython" in item.keywords:
                 item.add_marker(skip_dnspython)
-
-
-@pytest.fixture
-def named_port(request):
-    # pylint: disable=unused-argument
-    port = os.getenv("PORT")
-    if port is None:
-        port = 5301
-    else:
-        port = int(port)
-
-    return port
-
-
-@pytest.fixture
-def control_port(request):
-    # pylint: disable=unused-argument
-    port = os.getenv("CONTROLPORT")
-    if port is None:
-        port = 5301
-    else:
-        port = int(port)
-
-    return port
index 2bc5f116eb06428051f7d533d72a0259247ce6a2..59a903ca2b898053ea06f44fcac6524aee71c83d 100644 (file)
@@ -87,21 +87,9 @@ def pytest_collection_modifyitems(config, items):
 def statsport(request):
     # pylint: disable=unused-argument
     env_port = os.getenv("EXTRAPORT1")
-    if port is None:
+    if env_port is None:
         env_port = 5301
     else:
         env_port = int(env_port)
 
     return env_port
-
-
-@pytest.fixture
-def port(request):
-    # pylint: disable=unused-argument
-    env_port = os.getenv("PORT")
-    if port is None:
-        env_port = 5300
-    else:
-        env_port = int(env_port)
-
-    return env_port
index a368260b2830ef6fbd4f6bc754f4c424a06fe349..6af335e1d2c7eff1b07882ef0a32100abdaea471 100755 (executable)
@@ -107,7 +107,7 @@ def test_zone_with_many_keys_json(statsport):
 @pytest.mark.dnspython
 @pytest.mark.skipif(os.getenv("HAVEJSONSTATS", "unset") != "1",
                     reason="JSON not configured")
-def test_traffic_json(port, statsport):
+def test_traffic_json(named_port, statsport):
     generic.test_traffic(fetch_traffic_json,
                          statsip="10.53.0.2", statsport=statsport,
-                         port=port)
+                         port=named_port)
index 912716bfd8a9bd049fc824c2603fef8d974b69f1..0dd3b6b075788083d2db7d149638380a013229f6 100755 (executable)
@@ -137,7 +137,7 @@ def test_zone_with_many_keys_xml(statsport):
 @pytest.mark.dnspython
 @pytest.mark.skipif(os.getenv("HAVEXMLSTATS", "unset") != "1",
                     reason="XML not configured")
-def test_traffic_xml(port, statsport):
+def test_traffic_xml(named_port, statsport):
     generic.test_traffic(fetch_traffic_xml,
                          statsip="10.53.0.2", statsport=statsport,
-                         port=port)
+                         port=named_port)
index 0ce749b3fda87eb4734de6e8304cd216806a3d05..4789c92d90b8df4e2bf1da1e55e93651a17591de 100644 (file)
@@ -9,7 +9,6 @@
 # See the COPYRIGHT file distributed with this work for additional
 # information regarding copyright ownership.
 
-import os
 import pytest
 
 
@@ -45,15 +44,3 @@ def pytest_collection_modifyitems(config, items):
         for item in items:
             if "dnspython2" in item.keywords:
                 item.add_marker(skip_dnspython2)
-
-
-@pytest.fixture
-def port(request):
-    # pylint: disable=unused-argument
-    env_port = os.getenv("PORT")
-    if port is None:
-        env_port = 5300
-    else:
-        env_port = int(env_port)
-
-    return env_port
index 7ff84f264b9ce05d4decd9f1fecac0f275a2ee4b..3e6fadf89028b6d048be8d2270eeb10046b53801 100644 (file)
@@ -41,10 +41,10 @@ def create_socket(host, port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_tcp_garbage(port):
+def test_tcp_garbage(named_port):
     import dns.query
 
-    with create_socket("10.53.0.7", port) as sock:
+    with create_socket("10.53.0.7", named_port) as sock:
 
         msg = create_msg("a.example.", "A")
         (sbytes, stime) = dns.query.send_tcp(sock, msg, timeout())
@@ -68,11 +68,11 @@ def test_tcp_garbage(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_tcp_garbage_response(port):
+def test_tcp_garbage_response(named_port):
     import dns.query
     import dns.message
 
-    with create_socket("10.53.0.7", port) as sock:
+    with create_socket("10.53.0.7", named_port) as sock:
 
         msg = create_msg("a.example.", "A")
         (sbytes, stime) = dns.query.send_tcp(sock, msg, timeout())
index d92d87f72c1d2e59fdbf759ced2b3b9c6de70eaf..2639774c1e0755064f493c328536ab39b30d8c6a 100644 (file)
@@ -55,15 +55,3 @@ def pytest_collection_modifyitems(config, items):
         for item in items:
             if "long" in item.keywords:
                 item.add_marker(skip_long_tests)
-
-
-@pytest.fixture
-def port(request):
-    # pylint: disable=unused-argument
-    env_port = os.getenv("PORT")
-    if port is None:
-        env_port = 5300
-    else:
-        env_port = int(env_port)
-
-    return env_port
index a1c94839ea55c5409941d1e57ecf49a4dbb4286f..8481a3e9b32478bf7ed603a0ca5139875ed356e5 100644 (file)
@@ -34,14 +34,14 @@ def timeout():
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_initial_timeout(port):
+def test_initial_timeout(named_port):
     #
     # The initial timeout is 2.5 seconds, so this should timeout
     #
     import dns.query
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         time.sleep(3)
 
@@ -57,7 +57,7 @@ def test_initial_timeout(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_idle_timeout(port):
+def test_idle_timeout(named_port):
     #
     # The idle timeout is 5 seconds, so the third message should fail
     #
@@ -65,7 +65,7 @@ def test_idle_timeout(port):
 
     msg = create_msg("example.", "A")
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         time.sleep(1)
 
@@ -89,7 +89,7 @@ def test_idle_timeout(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_keepalive_timeout(port):
+def test_keepalive_timeout(named_port):
     #
     # Keepalive is 7 seconds, so the third message should succeed.
     #
@@ -100,7 +100,7 @@ def test_keepalive_timeout(port):
     msg.use_edns(edns=True, payload=4096, options=[kopt])
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         time.sleep(1)
 
@@ -120,7 +120,7 @@ def test_keepalive_timeout(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_pipelining_timeout(port):
+def test_pipelining_timeout(named_port):
     #
     # The pipelining should only timeout after the last message is received
     #
@@ -128,7 +128,7 @@ def test_pipelining_timeout(port):
 
     msg = create_msg("example.", "A")
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         time.sleep(1)
 
@@ -158,7 +158,7 @@ def test_pipelining_timeout(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_long_axfr(port):
+def test_long_axfr(named_port):
     #
     # The timers should not fire during AXFR, thus the connection should not
     # close abruptly
@@ -168,7 +168,7 @@ def test_long_axfr(port):
     import dns.rdatatype
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         name = dns.name.from_text("example.")
         msg = create_msg("example.", "AXFR")
@@ -194,11 +194,11 @@ def test_long_axfr(port):
 
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
-def test_send_timeout(port):
+def test_send_timeout(named_port):
     import dns.query
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         # Send and receive single large RDATA over TCP
         msg = create_msg("large.example.", "TXT")
@@ -225,13 +225,13 @@ def test_send_timeout(port):
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
 @pytest.mark.long
-def test_max_transfer_idle_out(port):
+def test_max_transfer_idle_out(named_port):
     import dns.query
     import dns.rdataclass
     import dns.rdatatype
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         name = dns.name.from_text("example.")
         msg = create_msg("example.", "AXFR")
@@ -262,13 +262,13 @@ def test_max_transfer_idle_out(port):
 @pytest.mark.dnspython
 @pytest.mark.dnspython2
 @pytest.mark.long
-def test_max_transfer_time_out(port):
+def test_max_transfer_time_out(named_port):
     import dns.query
     import dns.rdataclass
     import dns.rdatatype
 
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
-        sock.connect(("10.53.0.1", port))
+        sock.connect(("10.53.0.1", named_port))
 
         name = dns.name.from_text("example.")
         msg = create_msg("example.", "AXFR")