]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.api/runtests.py
Merge pull request #7787 from rgacogne/auth-api-replace-ent
[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'
c6c3c165 23PDNSUTIL_CMD = [os.environ.get("PDNSUTIL", "../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 119if daemon == 'authoritative':
c6c3c165 120 zone2sql = os.environ.get("ZONE2SQL", "../pdns/zone2sql")
7c876c30 121
ccfabd0d 122 # Prepare sqlite DB with some zones.
7992c698 123 run_check_call(["rm", "-f", SQLITE_DB])
c6c3c165
PL
124 if zone2sql == "../pdns/zone2sql":
125 run_check_call(["make", "-C", "../pdns", "zone2sql"])
7c876c30 126
bf8abc96 127 with open('../modules/gsqlite3backend/schema.sqlite3.sql', 'r') as schema_file:
7992c698 128 run_check_call(["sqlite3", SQLITE_DB], stdin=schema_file)
7c876c30
CH
129
130 with open('named.conf', 'w') as named_conf:
131 named_conf.write(NAMED_CONF_TPL)
132 with tempfile.TemporaryFile() as tf:
c6c3c165 133 p = subprocess.Popen([zone2sql, "--transactions", "--gsqlite", "--named-conf=named.conf"], stdout=tf)
7c876c30
CH
134 p.communicate()
135 if p.returncode != 0:
136 raise Exception("zone2sql failed")
137 tf.seek(0, os.SEEK_SET) # rewind
7992c698 138 run_check_call(["sqlite3", SQLITE_DB], stdin=tf)
7c876c30 139
a21e8566
CH
140 with open('bindbackend.conf', 'w') as bindbackend_conf:
141 bindbackend_conf.write(BINDBACKEND_CONF_TPL)
142
143 with open('pdns.conf', 'w') as pdns_conf:
144 pdns_conf.write(AUTH_CONF_TPL)
ccfabd0d 145
7992c698
CH
146 run_check_call(PDNSUTIL_CMD + ["secure-zone", "powerdnssec.org"])
147 servercmd = [pdns_server] + common_args + ["--local-ipv6=", "--no-shuffle", "--dnsupdate=yes", "--cache-ttl=0", "--api=yes"]
7c876c30
CH
148
149else:
02945d9a
CH
150 conf_dir = 'rec-conf.d'
151 ensure_empty_dir(conf_dir)
41942bb3
CH
152 with open('acl.list', 'w') as acl_list:
153 acl_list.write(ACL_LIST_TPL)
02945d9a
CH
154 with open('recursor.conf', 'w') as recursor_conf:
155 recursor_conf.write(REC_CONF_TPL % locals())
156 with open(conf_dir+'/example.com..conf', 'w') as conf_file:
157 conf_file.write(REC_EXAMPLE_COM_CONF_TPL)
7c876c30 158
2c4e6880 159 servercmd = [pdns_recursor] + common_args
1a152698
CH
160
161
162# Now run pdns and the tests.
541bb91b
CH
163print("Launching server...")
164print(format_call_args(servercmd))
7992c698 165serverproc = subprocess.Popen(servercmd, close_fds=True)
1a152698 166
541bb91b 167print("Waiting for webserver port to become available...")
a1e51a8a
CH
168available = False
169for try_number in range(0, 10):
170 try:
171 res = requests.get('http://127.0.0.1:%s/' % WEBPORT)
172 available = True
173 break
174 except:
175 time.sleep(0.5)
176
177if not available:
541bb91b 178 print("Webserver port not reachable after 10 tries, giving up.")
7992c698
CH
179 serverproc.terminate()
180 serverproc.wait()
a1e51a8a
CH
181 sys.exit(2)
182
541bb91b 183print("Query for example.com/A to create statistic data...")
0567c609 184run_check_call([sdig, "127.0.0.1", str(DNSPORT), "example.com", "A"])
118d3b31 185
541bb91b 186print("Running tests...")
7992c698 187returncode = 0
1a152698
CH
188test_env = {}
189test_env.update(os.environ)
7cbc5255 190test_env.update({
7992c698 191 'WEBPORT': str(WEBPORT),
7cbc5255
CH
192 'APIKEY': APIKEY,
193 'DAEMON': daemon,
194 'SQLITE_DB': SQLITE_DB,
195 'PDNSUTIL_CMD': ' '.join(PDNSUTIL_CMD),
d19c22a1
CHB
196 'SDIG': sdig,
197 'DNSPORT': str(DNSPORT)
7cbc5255 198})
1a152698
CH
199
200try:
541bb91b 201 print("")
7992c698 202 run_check_call(["nosetests", "--with-xunit", "-v"] + tests, env=test_env)
1a152698 203except subprocess.CalledProcessError as ex:
7992c698 204 returncode = ex.returncode
1a152698 205finally:
5f8fa6d4 206 if wait:
541bb91b 207 print("Waiting as requested, press ENTER to stop.")
5f8fa6d4 208 raw_input()
7992c698
CH
209 serverproc.terminate()
210 serverproc.wait()
1a152698 211
7992c698 212sys.exit(returncode)