]>
Commit | Line | Data |
---|---|---|
1a152698 CH |
1 | #!/usr/bin/env python |
2 | # | |
3 | # Shell-script style. | |
4 | ||
541bb91b | 5 | from __future__ import print_function |
1a152698 | 6 | import os |
a1e51a8a | 7 | import requests |
02945d9a | 8 | import shutil |
1a152698 CH |
9 | import subprocess |
10 | import sys | |
11 | import tempfile | |
a1e51a8a | 12 | import time |
1a152698 | 13 | |
7a0ea291 | 14 | try: |
15 | raw_input | |
16 | except NameError: | |
17 | raw_input = input | |
18 | ||
1a152698 | 19 | SQLITE_DB = 'pdns.sqlite3' |
7992c698 CH |
20 | WEBPORT = 5556 |
21 | DNSPORT = 5300 | |
bbef8f04 | 22 | APIKEY = '1234567890abcdefghijklmnopq-key' |
7cbc5255 | 23 | PDNSUTIL_CMD = ["../pdns/pdnsutil", "--config-dir=."] |
1a152698 CH |
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"; }; | |
ccfabd0d | 29 | zone "powerdnssec.org" { type master; file "powerdnssec.org"; }; |
86b3e4e8 | 30 | zone "cryptokeys.org" { type master; file "cryptokeys.org"; }; |
ccfabd0d CH |
31 | """ |
32 | ||
33 | AUTH_CONF_TPL = """ | |
34 | # Generated by runtests.py | |
a21e8566 | 35 | launch=gsqlite3,bind |
ccfabd0d CH |
36 | gsqlite3-dnssec=on |
37 | gsqlite3-database="""+SQLITE_DB+""" | |
38 | module-dir=../regression-tests/modules | |
a21e8566 CH |
39 | bind-config=bindbackend.conf |
40 | """ | |
41 | ||
42 | BINDBACKEND_CONF_TPL = """ | |
43 | # Generated by runtests.py | |
1a152698 CH |
44 | """ |
45 | ||
41942bb3 CH |
46 | ACL_LIST_TPL = """ |
47 | # Generated by runtests.py | |
48 | # local host | |
49 | 127.0.0.1 | |
50 | ::1 | |
51 | """ | |
52 | ||
02945d9a CH |
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= | |
2c4e6880 | 63 | allow-from-file=acl.list |
d07bf7ff | 64 | api-config-dir=%(conf_dir)s |
02945d9a CH |
65 | include-dir=%(conf_dir)s |
66 | """ | |
67 | ||
c1374bdb | 68 | |
02945d9a CH |
69 | def ensure_empty_dir(name): |
70 | if os.path.exists(name): | |
71 | shutil.rmtree(name) | |
72 | os.mkdir(name) | |
73 | ||
74 | ||
7992c698 CH |
75 | def format_call_args(cmd): |
76 | return "$ '%s'" % ("' '".join(cmd)) | |
77 | ||
78 | ||
79 | def 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 |
84 | wait = ('--wait' in sys.argv) |
85 | if wait: | |
86 | sys.argv.remove('--wait') | |
87 | ||
6754ef71 CH |
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 | ||
7c876c30 CH |
94 | daemon = (len(sys.argv) == 2) and sys.argv[1] or None |
95 | if daemon not in ('authoritative', 'recursor'): | |
541bb91b | 96 | print("Usage: ./runtests (authoritative|recursor)") |
7c876c30 CH |
97 | sys.exit(2) |
98 | ||
99 | daemon = sys.argv[1] | |
100 | ||
7992c698 | 101 | pdns_server = os.environ.get("PDNSSERVER", "../pdns/pdns_server") |
bf6096f5 | 102 | pdns_recursor = os.environ.get("PDNSRECURSOR", "../pdns/recursordist/pdns_recursor") |
7992c698 CH |
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 | ] | |
bf6096f5 | 109 | |
0567c609 CH |
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 | ||
118d3b31 | 118 | |
7c876c30 CH |
119 | if 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 | |
147 | else: | |
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 |
161 | print("Launching server...") |
162 | print(format_call_args(servercmd)) | |
7992c698 | 163 | serverproc = subprocess.Popen(servercmd, close_fds=True) |
1a152698 | 164 | |
541bb91b | 165 | print("Waiting for webserver port to become available...") |
a1e51a8a CH |
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: | |
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 | 181 | print("Query for example.com/A to create statistic data...") |
0567c609 | 182 | run_check_call([sdig, "127.0.0.1", str(DNSPORT), "example.com", "A"]) |
118d3b31 | 183 | |
541bb91b | 184 | print("Running tests...") |
7992c698 | 185 | returncode = 0 |
1a152698 CH |
186 | test_env = {} |
187 | test_env.update(os.environ) | |
7cbc5255 | 188 | test_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 | |
198 | try: | |
541bb91b | 199 | print("") |
7992c698 | 200 | run_check_call(["nosetests", "--with-xunit", "-v"] + tests, env=test_env) |
1a152698 | 201 | except subprocess.CalledProcessError as ex: |
7992c698 | 202 | returncode = ex.returncode |
1a152698 | 203 | finally: |
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 | 210 | sys.exit(returncode) |