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