]>
git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.ixfrdist/ixfrdisttests.py
15 class IXFRDistTest(unittest
.TestCase
):
17 _ixfrDistStartupDelay
= 2.0
20 _config_template
= """
28 work-dir: 'ixfrdist.dir'
31 _config_domains
= None
32 _config_params
= ['_ixfrDistPort']
35 def startIXFRDist(cls
):
36 print("Launching ixfrdist..")
37 conffile
= 'ixfrdist.yml'
38 params
= tuple([getattr(cls
, param
) for param
in cls
._config
_params
])
40 with
open(conffile
, 'w') as conf
:
41 conf
.write("# Autogenerated by ixfrdisttests.py\n")
42 conf
.write(cls
._config
_template
% params
)
44 if cls
._config
_domains
is not None:
45 conf
.write("domains:\n")
47 for domain
, master
in cls
._config
_domains
.items():
48 conf
.write(" - domain: %s\n" % (domain
))
49 conf
.write(" master: %s\n" % (master
))
51 ixfrdistcmd
= [os
.environ
['IXFRDISTBIN'], '--config', conffile
, '--debug']
53 logFile
= 'ixfrdist.log'
54 with
open(logFile
, 'w') as fdLog
:
55 cls
._ixfrdist
= subprocess
.Popen(ixfrdistcmd
, close_fds
=True,
56 stdout
=fdLog
, stderr
=fdLog
)
58 if 'IXFRDIST_FAST_TESTS' in os
.environ
:
61 delay
= cls
._ixfrDistStartupDelay
65 if cls
._ixfrdist
.poll() is not None:
67 sys
.exit(cls
._ixfrdist
.returncode
)
70 def setUpSockets(cls
):
71 print("Setting up UDP socket..")
72 cls
._sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
73 cls
._sock
.settimeout(2.0)
74 cls
._sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
81 print("Launching tests..")
84 def tearDownClass(cls
):
85 cls
.tearDownIXFRDist()
88 def tearDownIXFRDist(cls
):
89 if 'IXFRDIST_FAST_TESTS' in os
.environ
:
96 cls
._ixfrdist
.terminate()
97 if cls
._ixfrdist
.poll() is None:
99 if cls
._ixfrdist
.poll() is None:
103 # There is a race-condition with the poll() and
104 # kill() statements, when the process is dead on the
105 # kill(), this is fine
106 if e
.errno
!= errno
.ESRCH
:
110 def sendUDPQuery(cls
, query
, timeout
=2.0, decode
=True, fwparams
=dict()):
112 cls
._sock
.settimeout(timeout
)
115 cls
._sock
.send(query
.to_wire())
116 data
= cls
._sock
.recv(4096)
117 except socket
.timeout
:
121 cls
._sock
.settimeout(None)
127 message
= dns
.message
.from_wire(data
, **fwparams
)
130 # FIXME: sendTCPQuery and sendTCPQueryMultiResponse, when they are done reading
131 # should wait for a short while on the socket to see if more data is coming
132 # and error if it does!
134 def sendTCPQuery(cls
, query
, timeout
=2.0):
135 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
137 sock
.settimeout(timeout
)
139 sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
142 wire
= query
.to_wire()
143 sock
.send(struct
.pack("!H", len(wire
)))
147 (datalen
,) = struct
.unpack("!H", data
)
148 data
= sock
.recv(datalen
)
149 except socket
.timeout
as e
:
150 print("Timeout: %s" % (str(e
)))
152 except socket
.error
as e
:
153 print("Network error: %s" % (str(e
)))
160 message
= dns
.message
.from_wire(data
)
164 def sendTCPQueryMultiResponse(cls
, query
, timeout
=2.0, count
=1):
165 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
167 sock
.settimeout(timeout
)
169 sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
172 wire
= query
.to_wire()
173 sock
.send(struct
.pack("!H", len(wire
)))
175 except socket
.timeout
as e
:
176 raise Exception("Timeout: %s" % (str(e
)))
177 except socket
.error
as e
:
178 raise Exception("Network error: %s" % (str(e
)))
181 for i
in range(count
):
185 (datalen
,) = struct
.unpack("!H", data
)
186 data
= sock
.recv(datalen
)
187 messages
.append(dns
.message
.from_wire(data
))
190 except socket
.timeout
as e
:
191 raise Exception("Timeout: %s" % (str(e
)))
192 except socket
.error
as e
:
193 raise Exception("Network error: %s" % (str(e
)))
198 # This function is called before every tests