]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.api/runtests.py
Merge pull request #6823 from klaus3000/load-ourserial-on-NOTIFY
[thirdparty/pdns.git] / regression-tests.api / runtests.py
CommitLineData
1a152698
CH
1#!/usr/bin/env python
2#
3# Shell-script style.
4
541bb91b 5from __future__ import print_function
1a152698 6import os
a1e51a8a 7import requests
02945d9a 8import shutil
1a152698
CH
9import subprocess
10import sys
11import tempfile
a1e51a8a 12import time
1a152698 13
7a0ea291 14try:
15 raw_input
16except NameError:
17 raw_input = input
18
1a152698 19SQLITE_DB = 'pdns.sqlite3'
7992c698
CH
20WEBPORT = 5556
21DNSPORT = 5300
bbef8f04 22APIKEY = '1234567890abcdefghijklmnopq-key'
7cbc5255 23PDNSUTIL_CMD = ["../pdns/pdnsutil", "--config-dir=."]
1a152698
CH
24
25NAMED_CONF_TPL = """
26# Generated by runtests.py
27options { directory "../regression-tests/zones/"; };
28zone "example.com" { type master; file "example.com"; };
ccfabd0d 29zone "powerdnssec.org" { type master; file "powerdnssec.org"; };
86b3e4e8 30zone "cryptokeys.org" { type master; file "cryptokeys.org"; };
ccfabd0d
CH
31"""
32
33AUTH_CONF_TPL = """
34# Generated by runtests.py
a21e8566 35launch=gsqlite3,bind
ccfabd0d
CH
36gsqlite3-dnssec=on
37gsqlite3-database="""+SQLITE_DB+"""
38module-dir=../regression-tests/modules
a21e8566
CH
39bind-config=bindbackend.conf
40"""
41
42BINDBACKEND_CONF_TPL = """
43# Generated by runtests.py
1a152698
CH
44"""
45
41942bb3
CH
46ACL_LIST_TPL = """
47# Generated by runtests.py
48# local host
49127.0.0.1
50::1
51"""
52
02945d9a
CH
53REC_EXAMPLE_COM_CONF_TPL = """
54# Generated by runtests.py
55auth-zones+=example.com=../regression-tests/zones/example.com
56"""
57
58REC_CONF_TPL = """
59# Generated by runtests.py
60auth-zones=
61forward-zones=
62forward-zones-recurse=
2c4e6880 63allow-from-file=acl.list
d07bf7ff 64api-config-dir=%(conf_dir)s
02945d9a
CH
65include-dir=%(conf_dir)s
66"""
67
c1374bdb 68
02945d9a
CH
69def ensure_empty_dir(name):
70 if os.path.exists(name):
71 shutil.rmtree(name)
72 os.mkdir(name)
73
74
7992c698
CH
75def format_call_args(cmd):
76 return "$ '%s'" % ("' '".join(cmd))
77
78
79def run_check_call(cmd, *args, **kwargs):
541bb91b 80 print(format_call_args(cmd))
7992c698
CH
81 subprocess.check_call(cmd, *args, **kwargs)
82
83
5f8fa6d4
CH
84wait = ('--wait' in sys.argv)
85if wait:
86 sys.argv.remove('--wait')
87
6754ef71
CH
88tests = [opt for opt in sys.argv if opt.startswith('--tests=')]
89if tests:
90 for opt in tests:
91 sys.argv.remove(opt)
92tests = [opt.split('=', 1)[1] for opt in tests]
93
7c876c30
CH
94daemon = (len(sys.argv) == 2) and sys.argv[1] or None
95if daemon not in ('authoritative', 'recursor'):
541bb91b 96 print("Usage: ./runtests (authoritative|recursor)")
7c876c30
CH
97 sys.exit(2)
98
99daemon = sys.argv[1]
100
7992c698 101pdns_server = os.environ.get("PDNSSERVER", "../pdns/pdns_server")
bf6096f5 102pdns_recursor = os.environ.get("PDNSRECURSOR", "../pdns/recursordist/pdns_recursor")
7992c698
CH
103common_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]
bf6096f5 109
0567c609
CH
110# Take sdig if it exists (recursor in travis), otherwise build it from Authoritative source.
111sdig = os.environ.get("SDIG", "")
112if sdig:
113 sdig = os.path.abspath(sdig)
114if not sdig or not os.path.exists(sdig):
115 run_check_call(["make", "-C", "../pdns", "sdig"])
116 sdig = "../pdns/sdig"
117
118d3b31 118
7c876c30
CH
119if daemon == 'authoritative':
120
ccfabd0d 121 # Prepare sqlite DB with some zones.
7992c698
CH
122 run_check_call(["rm", "-f", SQLITE_DB])
123 run_check_call(["make", "-C", "../pdns", "zone2sql"])
7c876c30 124
bf8abc96 125 with open('../modules/gsqlite3backend/schema.sqlite3.sql', 'r') as schema_file:
7992c698 126 run_check_call(["sqlite3", SQLITE_DB], stdin=schema_file)
7c876c30
CH
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
7992c698 136 run_check_call(["sqlite3", SQLITE_DB], stdin=tf)
7c876c30 137
a21e8566
CH
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)
ccfabd0d 143
7992c698
CH
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"]
7c876c30
CH
146
147else:
02945d9a
CH
148 conf_dir = 'rec-conf.d'
149 ensure_empty_dir(conf_dir)
41942bb3
CH
150 with open('acl.list', 'w') as acl_list:
151 acl_list.write(ACL_LIST_TPL)
02945d9a
CH
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)
7c876c30 156
2c4e6880 157 servercmd = [pdns_recursor] + common_args
1a152698
CH
158
159
160# Now run pdns and the tests.
541bb91b
CH
161print("Launching server...")
162print(format_call_args(servercmd))
7992c698 163serverproc = subprocess.Popen(servercmd, close_fds=True)
1a152698 164
541bb91b 165print("Waiting for webserver port to become available...")
a1e51a8a
CH
166available = False
167for 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
175if not available:
541bb91b 176 print("Webserver port not reachable after 10 tries, giving up.")
7992c698
CH
177 serverproc.terminate()
178 serverproc.wait()
a1e51a8a
CH
179 sys.exit(2)
180
541bb91b 181print("Query for example.com/A to create statistic data...")
0567c609 182run_check_call([sdig, "127.0.0.1", str(DNSPORT), "example.com", "A"])
118d3b31 183
541bb91b 184print("Running tests...")
7992c698 185returncode = 0
1a152698
CH
186test_env = {}
187test_env.update(os.environ)
7cbc5255 188test_env.update({
7992c698 189 'WEBPORT': str(WEBPORT),
7cbc5255
CH
190 'APIKEY': APIKEY,
191 'DAEMON': daemon,
192 'SQLITE_DB': SQLITE_DB,
193 'PDNSUTIL_CMD': ' '.join(PDNSUTIL_CMD),
d19c22a1
CHB
194 'SDIG': sdig,
195 'DNSPORT': str(DNSPORT)
7cbc5255 196})
1a152698
CH
197
198try:
541bb91b 199 print("")
7992c698 200 run_check_call(["nosetests", "--with-xunit", "-v"] + tests, env=test_env)
1a152698 201except subprocess.CalledProcessError as ex:
7992c698 202 returncode = ex.returncode
1a152698 203finally:
5f8fa6d4 204 if wait:
541bb91b 205 print("Waiting as requested, press ENTER to stop.")
5f8fa6d4 206 raw_input()
7992c698
CH
207 serverproc.terminate()
208 serverproc.wait()
1a152698 209
7992c698 210sys.exit(returncode)