]>
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' | |
14 | WEBPORT = '5556' | |
bbef8f04 | 15 | APIKEY = '1234567890abcdefghijklmnopq-key' |
1a152698 CH |
16 | |
17 | NAMED_CONF_TPL = """ | |
18 | # Generated by runtests.py | |
19 | options { directory "../regression-tests/zones/"; }; | |
20 | zone "example.com" { type master; file "example.com"; }; | |
21 | """ | |
22 | ||
41942bb3 CH |
23 | ACL_LIST_TPL = """ |
24 | # Generated by runtests.py | |
25 | # local host | |
26 | 127.0.0.1 | |
27 | ::1 | |
28 | """ | |
29 | ||
02945d9a CH |
30 | REC_EXAMPLE_COM_CONF_TPL = """ |
31 | # Generated by runtests.py | |
32 | auth-zones+=example.com=../regression-tests/zones/example.com | |
33 | """ | |
34 | ||
35 | REC_CONF_TPL = """ | |
36 | # Generated by runtests.py | |
37 | auth-zones= | |
38 | forward-zones= | |
39 | forward-zones-recurse= | |
40 | experimental-api-config-dir=%(conf_dir)s | |
41 | include-dir=%(conf_dir)s | |
42 | """ | |
43 | ||
c1374bdb | 44 | |
02945d9a CH |
45 | def ensure_empty_dir(name): |
46 | if os.path.exists(name): | |
47 | shutil.rmtree(name) | |
48 | os.mkdir(name) | |
49 | ||
50 | ||
5f8fa6d4 CH |
51 | wait = ('--wait' in sys.argv) |
52 | if wait: | |
53 | sys.argv.remove('--wait') | |
54 | ||
7c876c30 CH |
55 | daemon = (len(sys.argv) == 2) and sys.argv[1] or None |
56 | if daemon not in ('authoritative', 'recursor'): | |
57 | print "Usage: ./runtests (authoritative|recursor)" | |
58 | sys.exit(2) | |
59 | ||
60 | daemon = sys.argv[1] | |
61 | ||
62 | if daemon == 'authoritative': | |
63 | ||
64 | # Prepare sqlite DB with a single zone. | |
65 | subprocess.check_call(["rm", "-f", SQLITE_DB]) | |
66 | subprocess.check_call(["make", "-C", "../pdns", "zone2sql"]) | |
67 | ||
bf8abc96 | 68 | with open('../modules/gsqlite3backend/schema.sqlite3.sql', 'r') as schema_file: |
7c876c30 CH |
69 | subprocess.check_call(["sqlite3", SQLITE_DB], stdin=schema_file) |
70 | ||
71 | with open('named.conf', 'w') as named_conf: | |
72 | named_conf.write(NAMED_CONF_TPL) | |
73 | with tempfile.TemporaryFile() as tf: | |
74 | p = subprocess.Popen(["../pdns/zone2sql", "--transactions", "--gsqlite", "--named-conf=named.conf"], stdout=tf) | |
75 | p.communicate() | |
76 | if p.returncode != 0: | |
77 | raise Exception("zone2sql failed") | |
78 | tf.seek(0, os.SEEK_SET) # rewind | |
79 | subprocess.check_call(["sqlite3", SQLITE_DB], stdin=tf) | |
80 | ||
bbef8f04 | 81 | pdnscmd = ("../pdns/pdns_server --daemon=no --local-port=5300 --socket-dir=./ --module-dir=../regression-tests/modules --no-shuffle --launch=gsqlite3 --gsqlite3-dnssec --send-root-referral --experimental-dnsupdate=yes --cache-ttl=0 --no-config --gsqlite3-dnssec=on --gsqlite3-database="+SQLITE_DB+" --experimental-json-interface=yes --webserver=yes --webserver-port="+WEBPORT+" --webserver-address=127.0.0.1 --webserver-password=something --experimental-api-key="+APIKEY).split() |
7c876c30 CH |
82 | |
83 | else: | |
02945d9a CH |
84 | conf_dir = 'rec-conf.d' |
85 | ensure_empty_dir(conf_dir) | |
41942bb3 CH |
86 | with open('acl.list', 'w') as acl_list: |
87 | acl_list.write(ACL_LIST_TPL) | |
02945d9a CH |
88 | with open('recursor.conf', 'w') as recursor_conf: |
89 | recursor_conf.write(REC_CONF_TPL % locals()) | |
90 | with open(conf_dir+'/example.com..conf', 'w') as conf_file: | |
91 | conf_file.write(REC_EXAMPLE_COM_CONF_TPL) | |
7c876c30 | 92 | |
bbef8f04 | 93 | pdnscmd = ("../pdns/pdns_recursor --daemon=no --socket-dir=. --config-dir=. --allow-from-file=acl.list --local-port=5555 --experimental-webserver=yes --experimental-webserver-port="+WEBPORT+" --experimental-webserver-address=127.0.0.1 --experimental-webserver-password=something --experimental-api-key="+APIKEY).split() |
1a152698 CH |
94 | |
95 | ||
96 | # Now run pdns and the tests. | |
7c876c30 CH |
97 | print "Launching pdns..." |
98 | print ' '.join(pdnscmd) | |
99 | pdns = subprocess.Popen(pdnscmd, close_fds=True) | |
1a152698 | 100 | |
a1e51a8a CH |
101 | print "Waiting for webserver port to become available..." |
102 | available = False | |
103 | for try_number in range(0, 10): | |
104 | try: | |
105 | res = requests.get('http://127.0.0.1:%s/' % WEBPORT) | |
106 | available = True | |
107 | break | |
108 | except: | |
109 | time.sleep(0.5) | |
110 | ||
111 | if not available: | |
112 | print "Webserver port not reachable after 10 tries, giving up." | |
113 | pdns.terminate() | |
114 | pdns.wait() | |
115 | sys.exit(2) | |
116 | ||
7c876c30 | 117 | print "Running tests..." |
1a152698 CH |
118 | rc = 0 |
119 | test_env = {} | |
120 | test_env.update(os.environ) | |
bbef8f04 | 121 | test_env.update({'WEBPORT': WEBPORT, 'APIKEY': APIKEY, 'DAEMON': daemon}) |
1a152698 CH |
122 | |
123 | try: | |
124 | print "" | |
125 | p = subprocess.check_call(["nosetests", "--with-xunit"], env=test_env) | |
126 | except subprocess.CalledProcessError as ex: | |
127 | rc = ex.returncode | |
128 | finally: | |
5f8fa6d4 CH |
129 | if wait: |
130 | print "Waiting as requested, press ENTER to stop." | |
131 | raw_input() | |
1a152698 CH |
132 | pdns.terminate() |
133 | pdns.wait() | |
134 | ||
135 | sys.exit(rc) |