]> git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.api/runtests.py
Merge pull request #7537 from andreydomas/dnsnameset
[thirdparty/pdns.git] / regression-tests.api / runtests.py
1 #!/usr/bin/env python
2 #
3 # Shell-script style.
4
5 from __future__ import print_function
6 import os
7 import requests
8 import shutil
9 import subprocess
10 import sys
11 import tempfile
12 import time
13
14 try:
15 raw_input
16 except NameError:
17 raw_input = input
18
19 SQLITE_DB = 'pdns.sqlite3'
20 WEBPORT = 5556
21 DNSPORT = 5300
22 APIKEY = '1234567890abcdefghijklmnopq-key'
23 PDNSUTIL_CMD = ["../pdns/pdnsutil", "--config-dir=."]
24
25 NAMED_CONF_TPL = """
26 # Generated by runtests.py
27 options { directory "../regression-tests/zones/"; };
28 zone "example.com" { type master; file "example.com"; };
29 zone "powerdnssec.org" { type master; file "powerdnssec.org"; };
30 zone "cryptokeys.org" { type master; file "cryptokeys.org"; };
31 """
32
33 AUTH_CONF_TPL = """
34 # Generated by runtests.py
35 launch=gsqlite3,bind
36 gsqlite3-dnssec=on
37 gsqlite3-database="""+SQLITE_DB+"""
38 module-dir=../regression-tests/modules
39 bind-config=bindbackend.conf
40 """
41
42 BINDBACKEND_CONF_TPL = """
43 # Generated by runtests.py
44 """
45
46 ACL_LIST_TPL = """
47 # Generated by runtests.py
48 # local host
49 127.0.0.1
50 ::1
51 """
52
53 REC_EXAMPLE_COM_CONF_TPL = """
54 # Generated by runtests.py
55 auth-zones+=example.com=../regression-tests/zones/example.com
56 """
57
58 REC_CONF_TPL = """
59 # Generated by runtests.py
60 auth-zones=
61 forward-zones=
62 forward-zones-recurse=
63 allow-from-file=acl.list
64 api-config-dir=%(conf_dir)s
65 include-dir=%(conf_dir)s
66 """
67
68
69 def ensure_empty_dir(name):
70 if os.path.exists(name):
71 shutil.rmtree(name)
72 os.mkdir(name)
73
74
75 def format_call_args(cmd):
76 return "$ '%s'" % ("' '".join(cmd))
77
78
79 def run_check_call(cmd, *args, **kwargs):
80 print(format_call_args(cmd))
81 subprocess.check_call(cmd, *args, **kwargs)
82
83
84 wait = ('--wait' in sys.argv)
85 if wait:
86 sys.argv.remove('--wait')
87
88 tests = [opt for opt in sys.argv if opt.startswith('--tests=')]
89 if tests:
90 for opt in tests:
91 sys.argv.remove(opt)
92 tests = [opt.split('=', 1)[1] for opt in tests]
93
94 daemon = (len(sys.argv) == 2) and sys.argv[1] or None
95 if daemon not in ('authoritative', 'recursor'):
96 print("Usage: ./runtests (authoritative|recursor)")
97 sys.exit(2)
98
99 daemon = sys.argv[1]
100
101 pdns_server = os.environ.get("PDNSSERVER", "../pdns/pdns_server")
102 pdns_recursor = os.environ.get("PDNSRECURSOR", "../pdns/recursordist/pdns_recursor")
103 common_args = [
104 "--daemon=no", "--socket-dir=.", "--config-dir=.",
105 "--local-address=127.0.0.1", "--local-port="+str(DNSPORT),
106 "--webserver=yes", "--webserver-port="+str(WEBPORT), "--webserver-address=127.0.0.1", "--webserver-password=something",
107 "--api-key="+APIKEY
108 ]
109
110 # Take sdig if it exists (recursor in travis), otherwise build it from Authoritative source.
111 sdig = os.environ.get("SDIG", "")
112 if sdig:
113 sdig = os.path.abspath(sdig)
114 if not sdig or not os.path.exists(sdig):
115 run_check_call(["make", "-C", "../pdns", "sdig"])
116 sdig = "../pdns/sdig"
117
118
119 if daemon == 'authoritative':
120
121 # Prepare sqlite DB with some zones.
122 run_check_call(["rm", "-f", SQLITE_DB])
123 run_check_call(["make", "-C", "../pdns", "zone2sql"])
124
125 with open('../modules/gsqlite3backend/schema.sqlite3.sql', 'r') as schema_file:
126 run_check_call(["sqlite3", SQLITE_DB], stdin=schema_file)
127
128 with open('named.conf', 'w') as named_conf:
129 named_conf.write(NAMED_CONF_TPL)
130 with tempfile.TemporaryFile() as tf:
131 p = subprocess.Popen(["../pdns/zone2sql", "--transactions", "--gsqlite", "--named-conf=named.conf"], stdout=tf)
132 p.communicate()
133 if p.returncode != 0:
134 raise Exception("zone2sql failed")
135 tf.seek(0, os.SEEK_SET) # rewind
136 run_check_call(["sqlite3", SQLITE_DB], stdin=tf)
137
138 with open('bindbackend.conf', 'w') as bindbackend_conf:
139 bindbackend_conf.write(BINDBACKEND_CONF_TPL)
140
141 with open('pdns.conf', 'w') as pdns_conf:
142 pdns_conf.write(AUTH_CONF_TPL)
143
144 run_check_call(PDNSUTIL_CMD + ["secure-zone", "powerdnssec.org"])
145 servercmd = [pdns_server] + common_args + ["--local-ipv6=", "--no-shuffle", "--dnsupdate=yes", "--cache-ttl=0", "--api=yes"]
146
147 else:
148 conf_dir = 'rec-conf.d'
149 ensure_empty_dir(conf_dir)
150 with open('acl.list', 'w') as acl_list:
151 acl_list.write(ACL_LIST_TPL)
152 with open('recursor.conf', 'w') as recursor_conf:
153 recursor_conf.write(REC_CONF_TPL % locals())
154 with open(conf_dir+'/example.com..conf', 'w') as conf_file:
155 conf_file.write(REC_EXAMPLE_COM_CONF_TPL)
156
157 servercmd = [pdns_recursor] + common_args
158
159
160 # Now run pdns and the tests.
161 print("Launching server...")
162 print(format_call_args(servercmd))
163 serverproc = subprocess.Popen(servercmd, close_fds=True)
164
165 print("Waiting for webserver port to become available...")
166 available = False
167 for try_number in range(0, 10):
168 try:
169 res = requests.get('http://127.0.0.1:%s/' % WEBPORT)
170 available = True
171 break
172 except:
173 time.sleep(0.5)
174
175 if not available:
176 print("Webserver port not reachable after 10 tries, giving up.")
177 serverproc.terminate()
178 serverproc.wait()
179 sys.exit(2)
180
181 print("Query for example.com/A to create statistic data...")
182 run_check_call([sdig, "127.0.0.1", str(DNSPORT), "example.com", "A"])
183
184 print("Running tests...")
185 returncode = 0
186 test_env = {}
187 test_env.update(os.environ)
188 test_env.update({
189 'WEBPORT': str(WEBPORT),
190 'APIKEY': APIKEY,
191 'DAEMON': daemon,
192 'SQLITE_DB': SQLITE_DB,
193 'PDNSUTIL_CMD': ' '.join(PDNSUTIL_CMD),
194 'SDIG': sdig,
195 'DNSPORT': str(DNSPORT)
196 })
197
198 try:
199 print("")
200 run_check_call(["nosetests", "--with-xunit", "-v"] + tests, env=test_env)
201 except subprocess.CalledProcessError as ex:
202 returncode = ex.returncode
203 finally:
204 if wait:
205 print("Waiting as requested, press ENTER to stop.")
206 raw_input()
207 serverproc.terminate()
208 serverproc.wait()
209
210 sys.exit(returncode)