From ac229a2fd50d30d0ef5f56858706275efece0e96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Mon, 14 Mar 2022 08:59:32 +0100 Subject: [PATCH] Rework imports in dnspython-based system tests Ensure all "import dns.*" statements are always placed after pytest.importorskip('dns') calls, in order to allow the latter to fulfill their purpose. Explicitly import all dnspython modules used by each dnspython-based test to avoid relying on nested imports. Replace function-scoped imports with global imports to reduce code duplication. (cherry picked from commit 49312d6bb2e4d4c7fe513e6832f0521394bb37f6) --- bin/tests/system/checkds/tests-checkds.py | 9 ++++++- bin/tests/system/doth/tests_gnutls.py | 2 ++ bin/tests/system/shutdown/tests-shutdown.py | 3 ++- bin/tests/system/tcp/tests-tcp.py | 8 ++---- bin/tests/system/timeouts/tests-tcp.py | 29 +++++---------------- bin/tests/system/wildcard/tests-wildcard.py | 2 ++ 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/bin/tests/system/checkds/tests-checkds.py b/bin/tests/system/checkds/tests-checkds.py index 40611e8d05..68b197c73e 100755 --- a/bin/tests/system/checkds/tests-checkds.py +++ b/bin/tests/system/checkds/tests-checkds.py @@ -17,10 +17,17 @@ import subprocess import sys import time -import dns.resolver import pytest pytest.importorskip('dns', minversion='2.0.0') +import dns.exception +import dns.message +import dns.name +import dns.query +import dns.rcode +import dns.rdataclass +import dns.rdatatype +import dns.resolver def has_signed_apex_nsec(zone, response): diff --git a/bin/tests/system/doth/tests_gnutls.py b/bin/tests/system/doth/tests_gnutls.py index 0e7879e04b..ea86a961b2 100644 --- a/bin/tests/system/doth/tests_gnutls.py +++ b/bin/tests/system/doth/tests_gnutls.py @@ -21,6 +21,8 @@ import pytest pytest.importorskip('dns') import dns.exception import dns.message +import dns.name +import dns.rdataclass import dns.rdatatype diff --git a/bin/tests/system/shutdown/tests-shutdown.py b/bin/tests/system/shutdown/tests-shutdown.py index 9ed222bb1a..b8de379d66 100755 --- a/bin/tests/system/shutdown/tests-shutdown.py +++ b/bin/tests/system/shutdown/tests-shutdown.py @@ -19,10 +19,11 @@ import subprocess from string import ascii_lowercase as letters import time -import dns.resolver import pytest pytest.importorskip('dns') +import dns.exception +import dns.resolver def do_work(named_proc, resolver, rndc_cmd, kill_method, n_workers, n_queries): diff --git a/bin/tests/system/tcp/tests-tcp.py b/bin/tests/system/tcp/tests-tcp.py index 7a12dc2be2..da70b1ef38 100644 --- a/bin/tests/system/tcp/tests-tcp.py +++ b/bin/tests/system/tcp/tests-tcp.py @@ -20,13 +20,14 @@ import time import pytest pytest.importorskip('dns', minversion='2.0.0') +import dns.message +import dns.query TIMEOUT = 10 def create_msg(qname, qtype): - import dns.message msg = dns.message.make_query(qname, qtype, want_dnssec=True, use_edns=0, payload=4096) return msg @@ -43,8 +44,6 @@ def create_socket(host, port): def test_tcp_garbage(named_port): - import dns.query - with create_socket("10.53.0.7", named_port) as sock: msg = create_msg("a.example.", "A") @@ -68,9 +67,6 @@ def test_tcp_garbage(named_port): def test_tcp_garbage_response(named_port): - import dns.query - import dns.message - with create_socket("10.53.0.7", named_port) as sock: msg = create_msg("a.example.", "A") diff --git a/bin/tests/system/timeouts/tests-tcp.py b/bin/tests/system/timeouts/tests-tcp.py index a17380af58..e883dddda4 100644 --- a/bin/tests/system/timeouts/tests-tcp.py +++ b/bin/tests/system/timeouts/tests-tcp.py @@ -19,13 +19,18 @@ import time import pytest pytest.importorskip('dns', minversion='2.0.0') +import dns.edns +import dns.message +import dns.name +import dns.query +import dns.rdataclass +import dns.rdatatype TIMEOUT = 10 def create_msg(qname, qtype): - import dns.message msg = dns.message.make_query(qname, qtype, want_dnssec=True, use_edns=0, payload=4096) return msg @@ -39,8 +44,6 @@ 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", named_port)) @@ -60,8 +63,6 @@ def test_idle_timeout(named_port): # # The idle timeout is 5 seconds, so the third message should fail # - import dns.rcode - msg = create_msg("example.", "A") with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(("10.53.0.1", named_port)) @@ -90,8 +91,6 @@ def test_keepalive_timeout(named_port): # # Keepalive is 7 seconds, so the third message should succeed. # - import dns.rcode - msg = create_msg("example.", "A") kopt = dns.edns.GenericOption(11, b'\x00') msg.use_edns(edns=True, payload=4096, options=[kopt]) @@ -119,8 +118,6 @@ def test_pipelining_timeout(named_port): # # The pipelining should only timeout after the last message is received # - import dns.query - msg = create_msg("example.", "A") with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(("10.53.0.1", named_port)) @@ -156,10 +153,6 @@ def test_long_axfr(named_port): # The timers should not fire during AXFR, thus the connection should not # close abruptly # - 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", named_port)) @@ -186,8 +179,6 @@ def test_long_axfr(named_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", named_port)) @@ -215,10 +206,6 @@ def test_send_timeout(named_port): @pytest.mark.long 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", named_port)) @@ -250,10 +237,6 @@ def test_max_transfer_idle_out(named_port): @pytest.mark.long 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", named_port)) diff --git a/bin/tests/system/wildcard/tests-wildcard.py b/bin/tests/system/wildcard/tests-wildcard.py index 3c724b1335..f4134225e1 100755 --- a/bin/tests/system/wildcard/tests-wildcard.py +++ b/bin/tests/system/wildcard/tests-wildcard.py @@ -35,7 +35,9 @@ import dns.message import dns.name import dns.query import dns.rcode +import dns.rdataclass import dns.rdatatype +import dns.rrset pytest.importorskip("hypothesis") from hypothesis import given