15 from eqdnsmessage
import AssertEqualDNSMessageMixin
17 class IXFRDistTest(AssertEqualDNSMessageMixin
, unittest
.TestCase
):
19 _ixfrDistStartupDelay
= 2.0
22 _config_template
= """
30 work-dir: 'ixfrdist.dir'
33 _config_domains
= None
34 _config_params
= ['_ixfrDistPort']
37 def startIXFRDist(cls
):
38 print("Launching ixfrdist..")
39 conffile
= 'ixfrdist.yml'
40 params
= tuple([getattr(cls
, param
) for param
in cls
._config
_params
])
42 with
open(conffile
, 'w') as conf
:
43 conf
.write("# Autogenerated by ixfrdisttests.py\n")
44 conf
.write(cls
._config
_template
% params
)
46 if cls
._config
_domains
is not None:
47 conf
.write("domains:\n")
49 for item
in cls
._config
_domains
:
50 conf
.write(" - domain: %s\n" % (item
['domain']))
51 conf
.write(" master: %s\n" % (item
['master']))
52 if ('notify' in item
) :
53 conf
.write(" notify: %s\n" % (item
['notify']))
55 ixfrdistcmd
= [os
.environ
['IXFRDISTBIN'], '--config', conffile
, '--debug']
57 logFile
= 'ixfrdist.log'
58 with
open(logFile
, 'w') as fdLog
:
59 cls
._ixfrdist
= subprocess
.Popen(ixfrdistcmd
, close_fds
=True,
60 stdout
=fdLog
, stderr
=fdLog
)
62 if 'IXFRDIST_FAST_TESTS' in os
.environ
:
65 delay
= cls
._ixfrDistStartupDelay
69 if cls
._ixfrdist
.poll() is not None:
71 sys
.exit(cls
._ixfrdist
.returncode
)
74 def setUpSockets(cls
):
75 print("Setting up UDP socket..")
76 cls
._sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
77 cls
._sock
.settimeout(2.0)
78 cls
._sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
85 print("Launching tests..")
88 def tearDownClass(cls
):
89 cls
.tearDownIXFRDist()
92 def tearDownIXFRDist(cls
):
93 if 'IXFRDIST_FAST_TESTS' in os
.environ
:
100 cls
._ixfrdist
.terminate()
101 if cls
._ixfrdist
.poll() is None:
103 if cls
._ixfrdist
.poll() is None:
107 # There is a race-condition with the poll() and
108 # kill() statements, when the process is dead on the
109 # kill(), this is fine
110 if e
.errno
!= errno
.ESRCH
:
114 def sendUDPQuery(cls
, query
, timeout
=2.0, decode
=True, fwparams
=dict()):
116 cls
._sock
.settimeout(timeout
)
119 cls
._sock
.send(query
.to_wire())
120 data
= cls
._sock
.recv(4096)
121 except socket
.timeout
:
125 cls
._sock
.settimeout(None)
131 message
= dns
.message
.from_wire(data
, **fwparams
)
134 # FIXME: sendTCPQuery and sendTCPQueryMultiResponse, when they are done reading
135 # should wait for a short while on the socket to see if more data is coming
136 # and error if it does!
138 def sendTCPQuery(cls
, query
, timeout
=2.0):
139 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
141 sock
.settimeout(timeout
)
143 sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
146 wire
= query
.to_wire()
147 sock
.send(struct
.pack("!H", len(wire
)))
151 (datalen
,) = struct
.unpack("!H", data
)
152 data
= sock
.recv(datalen
)
153 except socket
.timeout
as e
:
154 print("Timeout: %s" % (str(e
)))
156 except socket
.error
as e
:
157 print("Network error: %s" % (str(e
)))
164 message
= dns
.message
.from_wire(data
)
168 def sendTCPQueryMultiResponse(cls
, query
, timeout
=2.0, count
=1):
169 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
171 sock
.settimeout(timeout
)
173 sock
.connect(("127.0.0.1", cls
._ixfrDistPort
))
176 wire
= query
.to_wire()
177 sock
.send(struct
.pack("!H", len(wire
)))
179 except socket
.timeout
as e
:
180 raise Exception("Timeout: %s" % (str(e
)))
181 except socket
.error
as e
:
182 raise Exception("Network error: %s" % (str(e
)))
185 for i
in range(count
):
189 (datalen
,) = struct
.unpack("!H", data
)
190 data
= sock
.recv(datalen
)
191 messages
.append(dns
.message
.from_wire(data
))
194 except socket
.timeout
as e
:
195 raise Exception("Timeout: %s" % (str(e
)))
196 except socket
.error
as e
:
197 raise Exception("Network error: %s" % (str(e
)))
202 # This function is called before every tests
203 super(IXFRDistTest
, self
).setUp()