]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - regression-tests.dnsdist/dnsdisttests.py
Merge pull request #7496 from rgacogne/auth-catch-invalid-slave-soa
[thirdparty/pdns.git] / regression-tests.dnsdist / dnsdisttests.py
index aa79d941e5d9b8084d6eb7b08c07c0be9ae9efa1..e1df6cfa7df44a5631334af0776505a88560a587 100644 (file)
@@ -17,12 +17,15 @@ import libnacl
 import libnacl.utils
 
 # Python2/3 compatibility hacks
-if sys.version_info[0] == 2:
+try:
+  from queue import Queue
+except ImportError:
   from Queue import Queue
+
+try:
   range = xrange
-else:
-  from queue import Queue
-  range = range  # allow re-export of the builtin name
+except NameError:
+  pass
 
 
 class DNSDistTest(unittest.TestCase):
@@ -43,7 +46,6 @@ class DNSDistTest(unittest.TestCase):
     _dnsdistStartupDelay = 2.0
     _dnsdist = None
     _responsesCounter = {}
-    _shutUp = True
     _config_template = """
     """
     _config_params = ['_testServerPort']
@@ -66,16 +68,16 @@ class DNSDistTest(unittest.TestCase):
         cls._TCPResponder.start()
 
     @classmethod
-    def startDNSDist(cls, shutUp=True):
+    def startDNSDist(cls):
         print("Launching dnsdist..")
-        conffile = 'dnsdist_test.conf'
+        confFile = os.path.join('configs', 'dnsdist_%s.conf' % (cls.__name__))
         params = tuple([getattr(cls, param) for param in cls._config_params])
         print(params)
-        with open(conffile, 'w') as conf:
+        with open(confFile, 'w') as conf:
             conf.write("-- Autogenerated by dnsdisttests.py\n")
             conf.write(cls._config_template % params)
 
-        dnsdistcmd = [os.environ['DNSDISTBIN'], '-C', conffile,
+        dnsdistcmd = [os.environ['DNSDISTBIN'], '-C', confFile,
                       '-l', '%s:%d' % (cls._dnsDistListeningAddr, cls._dnsDistPort) ]
         for acl in cls._acl:
             dnsdistcmd.extend(['--acl', acl])
@@ -83,15 +85,17 @@ class DNSDistTest(unittest.TestCase):
 
         # validate config with --check-config, which sets client=true, possibly exposing bugs.
         testcmd = dnsdistcmd + ['--check-config']
-        output = subprocess.check_output(testcmd, close_fds=True)
-        if output != b'Configuration \'dnsdist_test.conf\' OK!\n':
+        try:
+            output = subprocess.check_output(testcmd, stderr=subprocess.STDOUT, close_fds=True)
+        except subprocess.CalledProcessError as exc:
+            raise AssertionError('dnsdist --check-config failed (%d): %s' % (exc.returncode, exc.output))
+        expectedOutput = ('Configuration \'%s\' OK!\n' % (confFile)).encode()
+        if output != expectedOutput:
             raise AssertionError('dnsdist --check-config failed: %s' % output)
 
-        if shutUp:
-            with open(os.devnull, 'w') as fdDevNull:
-                cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdDevNull)
-        else:
-            cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True)
+        logFile = os.path.join('configs', 'dnsdist_%s.log' % (cls.__name__))
+        with open(logFile, 'w') as fdLog:
+          cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdLog, stderr=fdLog)
 
         if 'DNSDIST_FAST_TESTS' in os.environ:
             delay = 0.5
@@ -115,7 +119,7 @@ class DNSDistTest(unittest.TestCase):
     def setUpClass(cls):
 
         cls.startResponders()
-        cls.startDNSDist(cls._shutUp)
+        cls.startDNSDist()
         cls.setUpSockets()
 
         print("Launching tests..")
@@ -173,7 +177,11 @@ class DNSDistTest(unittest.TestCase):
 
     @classmethod
     def UDPResponder(cls, port, fromQueue, toQueue, trailingDataResponse=False):
+        # trailingDataResponse=True means "ignore trailing data".
+        # Other values are either False (meaning "raise an exception")
+        # or are interpreted as a response RCODE for queries with trailing data.
         ignoreTrailing = trailingDataResponse is True
+
         sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
         sock.bind(("127.0.0.1", port))
@@ -183,7 +191,7 @@ class DNSDistTest(unittest.TestCase):
             try:
                 request = dns.message.from_wire(data, ignore_trailing=ignoreTrailing)
             except dns.message.TrailingJunk as e:
-                if trailingDataResponse is False:
+                if trailingDataResponse is False or forceRcode is True:
                     raise
                 print("UDP query with trailing data, synthesizing response")
                 request = dns.message.from_wire(data, ignore_trailing=True)
@@ -200,7 +208,11 @@ class DNSDistTest(unittest.TestCase):
 
     @classmethod
     def TCPResponder(cls, port, fromQueue, toQueue, trailingDataResponse=False, multipleResponses=False):
+        # trailingDataResponse=True means "ignore trailing data".
+        # Other values are either False (meaning "raise an exception")
+        # or are interpreted as a response RCODE for queries with trailing data.
         ignoreTrailing = trailingDataResponse is True
+
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
         try:
@@ -224,7 +236,7 @@ class DNSDistTest(unittest.TestCase):
             try:
                 request = dns.message.from_wire(data, ignore_trailing=ignoreTrailing)
             except dns.message.TrailingJunk as e:
-                if trailingDataResponse is False:
+                if trailingDataResponse is False or forceRcode is True:
                     raise
                 print("TCP query with trailing data, synthesizing response")
                 request = dns.message.from_wire(data, ignore_trailing=True)