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