From 18a0e7c6299c14ff9114fc59a867190421407139 Mon Sep 17 00:00:00 2001 From: Christian Hofstaedtler Date: Sat, 2 Jan 2016 23:29:23 +0100 Subject: [PATCH] Deduplicate dnsdist test code --- regression-tests.dnsdist/dnsdist.conf | 15 -- regression-tests.dnsdist/dnsdisttests.py | 41 +++- regression-tests.dnsdist/test_Advanced.py | 210 +----------------- .../test_EdnsClientSubnet.py | 46 ---- 4 files changed, 40 insertions(+), 272 deletions(-) delete mode 100644 regression-tests.dnsdist/dnsdist.conf diff --git a/regression-tests.dnsdist/dnsdist.conf b/regression-tests.dnsdist/dnsdist.conf deleted file mode 100644 index 13f311214..000000000 --- a/regression-tests.dnsdist/dnsdist.conf +++ /dev/null @@ -1,15 +0,0 @@ - truncateTC(true) - addAnyTCRule() - addAction(RegexRule("evil[0-9]{4,}\\.regex\\.tests\\.powerdns\\.com$"), RCodeAction(5)) - mySMN = newSuffixMatchNode() - mySMN:add(newDNSName("nameAndQtype.tests.powerdns.com.")) - addAction(AndRule{SuffixMatchNodeRule(mySMN), QTypeRule("TXT")}, RCodeAction(4)) - block=newDNSName("powerdns.org.") - function blockFilter(remote, qname, qtype, dh) - if(qname:isPartOf(block)) - then - print("Blocking *.powerdns.org") - return true - end - return false - end diff --git a/regression-tests.dnsdist/dnsdisttests.py b/regression-tests.dnsdist/dnsdisttests.py index b4a289948..f66f8c8a0 100644 --- a/regression-tests.dnsdist/dnsdisttests.py +++ b/regression-tests.dnsdist/dnsdisttests.py @@ -11,6 +11,8 @@ import sys import threading import time import unittest +import random + class DNSDistTest(unittest.TestCase): """ @@ -23,11 +25,30 @@ class DNSDistTest(unittest.TestCase): """ _dnsDistPort = 5340 _testServerPort = 5350 - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort) + " 127.0.0.1:" + str(_testServerPort)).split() _toResponderQueue = Queue.Queue() _fromResponderQueue = Queue.Queue() _dnsdist = None _responsesCounter = {} + _config_template = """ + newServer{address="127.0.0.1:%s"} + truncateTC(true) + addAnyTCRule() + addAction(RegexRule("evil[0-9]{4,}\\\\.regex\\\\.tests\\\\.powerdns\\\\.com$"), RCodeAction(5)) + mySMN = newSuffixMatchNode() + mySMN:add(newDNSName("nameAndQtype.tests.powerdns.com.")) + addAction(AndRule{SuffixMatchNodeRule(mySMN), QTypeRule("TXT")}, RCodeAction(4)) + block=newDNSName("powerdns.org.") + function blockFilter(remote, qname, qtype, dh) + if(qname:isPartOf(block)) + then + print("Blocking *.powerdns.org") + return true + end + return false + end + """ + _config_params = ['_testServerPort'] + _acl = ['127.0.0.1/32'] @classmethod def startResponders(cls): @@ -46,12 +67,24 @@ class DNSDistTest(unittest.TestCase): @classmethod def startDNSDist(cls, shutUp=True): print("Launching dnsdist..") - print(' '.join(cls._dnsdistcmd)) + conffile = 'dnsdist_test.conf' + params = tuple([getattr(cls, param) for param in cls._config_params]) + print(params) + 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, + '-l', '127.0.0.1:%d' % cls._dnsDistPort] + for acl in cls._acl: + dnsdistcmd.extend(['--acl', acl]) + print(' '.join(dnsdistcmd)) + if shutUp: with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) + cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) + cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True) time.sleep(1) diff --git a/regression-tests.dnsdist/test_Advanced.py b/regression-tests.dnsdist/test_Advanced.py index b60fc7538..662999b18 100644 --- a/regression-tests.dnsdist/test_Advanced.py +++ b/regression-tests.dnsdist/test_Advanced.py @@ -10,35 +10,12 @@ from dnsdisttests import DNSDistTest class TestAdvancedFixupCase(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ truncateTC(true) fixupCase(true) newServer{address="127.0.0.1:%s"} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_fixupcase.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_fixupcase.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testAdvancedFixupCase(self): """ Send a query with lower and upper chars, @@ -77,34 +54,11 @@ class TestAdvancedFixupCase(DNSDistTest): class TestAdvancedRemoveRD(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ addNoRecurseRule("norecurse.advanced.tests.powerdns.com.") newServer{address="127.0.0.1:%s"} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_noRD.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_noRD.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testAdvancedNoRD(self): """ Send a query with RD, @@ -174,34 +128,11 @@ class TestAdvancedRemoveRD(DNSDistTest): class TestAdvancedAddCD(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ addDisableValidationRule("setcd.advanced.tests.powerdns.com.") newServer{address="127.0.0.1:%s"} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_setCD.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_setCD.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testAdvancedSetCD(self): """ Send a query with CD cleared, @@ -270,34 +201,11 @@ class TestAdvancedAddCD(DNSDistTest): class TestAdvancedSpoof(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ addDomainSpoof("spoof.tests.powerdns.com.", "192.0.2.1", "2001:DB8::1") newServer{address="127.0.0.1:%s"} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_spoof.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_spoof.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testSpoofA(self): """ Send an A query to "spoof.tests.powerdns.com.", @@ -355,34 +263,11 @@ class TestAdvancedSpoof(DNSDistTest): class TestAdvancedPoolRouting(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ newServer{address="127.0.0.1:%s", pool="real"} addPoolRule("pool.tests.powerdns.com", "real") """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_pool.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_pool.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testPolicyPool(self): """ Send an A query to "pool.tests.powerdns.com.", @@ -427,8 +312,8 @@ class TestAdvancedPoolRouting(DNSDistTest): class TestAdvancedRoundRobinLB(DNSDistTest): - _dnsDistPort = 5340 _testServer2Port = 5351 + _config_params = ['_testServerPort', '_testServer2Port'] _config_template = """ setServerPolicy(roundrobin) s1 = newServer{address="127.0.0.1:%s"} @@ -437,28 +322,6 @@ class TestAdvancedRoundRobinLB(DNSDistTest): s2:setUp() """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_rr_lb.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_rr_lb.conf', 'w') as conf: - conf.write(cls._config_template % (str(cls._testServerPort), str(cls._testServer2Port))) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - @classmethod def startResponders(cls): print("Launching responders..") @@ -519,8 +382,8 @@ class TestAdvancedRoundRobinLB(DNSDistTest): class TestAdvancedRoundRobinLBOneDown(DNSDistTest): - _dnsDistPort = 5340 _testServer2Port = 5351 + _config_params = ['_testServerPort', '_testServer2Port'] _config_template = """ setServerPolicy(roundrobin) s1 = newServer{address="127.0.0.1:%s"} @@ -529,28 +392,6 @@ class TestAdvancedRoundRobinLBOneDown(DNSDistTest): s2:setDown() """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_rr_lb_down.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_rr_lb_down.conf', 'w') as conf: - conf.write(cls._config_template % (str(cls._testServerPort), str(cls._testServer2Port))) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testRRWithOneDown(self): """ Send 100 A queries to "rr.tests.powerdns.com.", @@ -594,32 +435,10 @@ class TestAdvancedRoundRobinLBOneDown(DNSDistTest): class TestAdvancedACL(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ newServer{address="127.0.0.1:%s"} """ - - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_acl.conf --acl 192.0.2.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_acl.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) + _acl = ['192.0.2.1/32'] def testACLBlocked(self): """ @@ -638,34 +457,11 @@ class TestAdvancedACL(DNSDistTest): class TestAdvancedDelay(DNSDistTest): - _dnsDistPort = 5340 _config_template = """ addAction(AllRule(), DelayAction(1000)) newServer{address="127.0.0.1:%s"} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_delay.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_delay.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testDelayed(self): """ Send an A query to "tests.powerdns.com.", diff --git a/regression-tests.dnsdist/test_EdnsClientSubnet.py b/regression-tests.dnsdist/test_EdnsClientSubnet.py index aa8ca34c5..7147bb995 100644 --- a/regression-tests.dnsdist/test_EdnsClientSubnet.py +++ b/regression-tests.dnsdist/test_EdnsClientSubnet.py @@ -14,7 +14,6 @@ class TestEdnsClientSubnetNoOverride(DNSDistTest): original query. """ - _dnsDistPort = 5340 _config_template = """ truncateTC(true) block=newDNSName("powerdns.org.") @@ -29,28 +28,6 @@ class TestEdnsClientSubnetNoOverride(DNSDistTest): newServer{address="127.0.0.1:%s", useClientSubnet=true} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_ecs_no_override.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_ecs_no_override.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testWithoutEDNS(self): """ Send a query without EDNS, check that the query @@ -166,7 +143,6 @@ class TestEdnsClientSubnetOverride(DNSDistTest): option, overwriting any existing value. """ - _dnsDistPort = 5340 _config_template = """ truncateTC(true) block=newDNSName("powerdns.org.") @@ -184,28 +160,6 @@ class TestEdnsClientSubnetOverride(DNSDistTest): newServer{address="127.0.0.1:%s", useClientSubnet=true} """ - _dnsdistcmd = (os.environ['DNSDISTBIN'] + " -C dnsdist_ecs_override.conf --acl 127.0.0.1/32 -l 127.0.0.1:" + str(_dnsDistPort)).split() - - @classmethod - def startDNSDist(cls, shutUp=True): - print("Launching dnsdist..") - with open('dnsdist_ecs_override.conf', 'w') as conf: - conf.write(cls._config_template % str(cls._testServerPort)) - - print(' '.join(cls._dnsdistcmd)) - if shutUp: - with open(os.devnull, 'w') as fdDevNull: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True, stdout=fdDevNull, stderr=fdDevNull) - else: - cls._dnsdist = subprocess.Popen(cls._dnsdistcmd, close_fds=True) - - time.sleep(1) - - if cls._dnsdist.poll() is not None: - cls._dnsdist.terminate() - cls._dnsdist.wait() - sys.exit(cls._dnsdist.returncode) - def testWithoutEDNS(self): """ Send a query without EDNS, check that the query -- 2.40.0