]> git.ipfire.org Git - thirdparty/pdns.git/blame - regression-tests.api/runtests.py
Merge pull request #2964 from Habbie/checkzone-nodot
[thirdparty/pdns.git] / regression-tests.api / runtests.py
CommitLineData
1a152698
CH
1#!/usr/bin/env python
2#
3# Shell-script style.
4
5import os
a1e51a8a 6import requests
02945d9a 7import shutil
1a152698
CH
8import subprocess
9import sys
10import tempfile
a1e51a8a 11import time
1a152698
CH
12
13SQLITE_DB = 'pdns.sqlite3'
14WEBPORT = '5556'
bbef8f04 15APIKEY = '1234567890abcdefghijklmnopq-key'
1a152698
CH
16
17NAMED_CONF_TPL = """
18# Generated by runtests.py
19options { directory "../regression-tests/zones/"; };
20zone "example.com" { type master; file "example.com"; };
21"""
22
41942bb3
CH
23ACL_LIST_TPL = """
24# Generated by runtests.py
25# local host
26127.0.0.1
27::1
28"""
29
02945d9a
CH
30REC_EXAMPLE_COM_CONF_TPL = """
31# Generated by runtests.py
32auth-zones+=example.com=../regression-tests/zones/example.com
33"""
34
35REC_CONF_TPL = """
36# Generated by runtests.py
37auth-zones=
38forward-zones=
39forward-zones-recurse=
40experimental-api-config-dir=%(conf_dir)s
41include-dir=%(conf_dir)s
42"""
43
c1374bdb 44
02945d9a
CH
45def ensure_empty_dir(name):
46 if os.path.exists(name):
47 shutil.rmtree(name)
48 os.mkdir(name)
49
50
5f8fa6d4
CH
51wait = ('--wait' in sys.argv)
52if wait:
53 sys.argv.remove('--wait')
54
7c876c30
CH
55daemon = (len(sys.argv) == 2) and sys.argv[1] or None
56if daemon not in ('authoritative', 'recursor'):
57 print "Usage: ./runtests (authoritative|recursor)"
58 sys.exit(2)
59
60daemon = sys.argv[1]
61
62if 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
83else:
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
97print "Launching pdns..."
98print ' '.join(pdnscmd)
99pdns = subprocess.Popen(pdnscmd, close_fds=True)
1a152698 100
a1e51a8a
CH
101print "Waiting for webserver port to become available..."
102available = False
103for 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
111if 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 117print "Running tests..."
1a152698
CH
118rc = 0
119test_env = {}
120test_env.update(os.environ)
bbef8f04 121test_env.update({'WEBPORT': WEBPORT, 'APIKEY': APIKEY, 'DAEMON': daemon})
1a152698
CH
122
123try:
124 print ""
125 p = subprocess.check_call(["nosetests", "--with-xunit"], env=test_env)
126except subprocess.CalledProcessError as ex:
127 rc = ex.returncode
128finally:
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
135sys.exit(rc)