querying.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # Copyright (C) 2011 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. # This script provides querying functionality
  16. # The most important step is
  17. #
  18. # query for <name> [type X] [class X] [to <addr>[:port]] should have rcode <rc>
  19. #
  20. # By default, it will send queries to 127.0.0.1:47806 unless specified
  21. # otherwise. The rcode is always checked. If the result is not NO_ANSWER,
  22. # the result will be stored in last_query_result, which can then be inspected
  23. # more closely, for instance with the step
  24. #
  25. # "the last query response should have <property> <value>"
  26. #
  27. # Also see example.feature for some examples
  28. from lettuce import *
  29. import subprocess
  30. import re
  31. #
  32. # define a class to easily access different parts
  33. # We may consider using our full library for this, but for now
  34. # simply store several parts of the response as text values in
  35. # this structure.
  36. # (this actually has the advantage of not relying on our own libraries
  37. # to test our own, well, libraries)
  38. #
  39. # The following attributes are 'parsed' from the response, all as strings,
  40. # and end up as direct attributes of the QueryResult object:
  41. # opcode, rcode, id, flags, qdcount, ancount, nscount, adcount
  42. # (flags is one string with all flags, in the order they appear in the
  43. # response packet.)
  44. #
  45. # this will set 'rcode' as the result code, we 'define' one additional
  46. # rcode, "NO_ANSWER", if the dig process returned an error code itself
  47. # In this case none of the other attributes will be set.
  48. #
  49. # The different sections will be lists of strings, one for each RR in the
  50. # section. The question section will start with ';', as per dig output
  51. #
  52. # See server_from_sqlite3.feature for various examples to perform queries
  53. class QueryResult(object):
  54. status_re = re.compile("opcode: ([A-Z])+, status: ([A-Z]+), id: ([0-9]+)")
  55. flags_re = re.compile("flags: ([a-z ]+); QUERY: ([0-9]+), ANSWER: " +
  56. "([0-9]+), AUTHORITY: ([0-9]+), ADDITIONAL: ([0-9]+)")
  57. def __init__(self, name, qtype, qclass, address, port):
  58. """
  59. Constructor. This fires of a query using dig.
  60. Parameters:
  61. name: The domain name to query
  62. qtype: The RR type to query. Defaults to A if it is None.
  63. qclass: The RR class to query. Defaults to IN if it is None.
  64. address: The IP adress to send the query to.
  65. port: The port number to send the query to.
  66. All parameters must be either strings or have the correct string
  67. representation.
  68. Only one query attempt will be made.
  69. """
  70. args = [ 'dig', '+tries=1', '@' + str(address), '-p', str(port) ]
  71. if qtype is not None:
  72. args.append('-t')
  73. args.append(str(qtype))
  74. if qclass is not None:
  75. args.append('-c')
  76. args.append(str(qclass))
  77. args.append(name)
  78. dig_process = subprocess.Popen(args, 1, None, None, subprocess.PIPE,
  79. None)
  80. result = dig_process.wait()
  81. if result != 0:
  82. self.rcode = "NO_ANSWER"
  83. else:
  84. self.rcode = None
  85. parsing = "HEADER"
  86. self.question_section = []
  87. self.answer_section = []
  88. self.authority_section = []
  89. self.additional_section = []
  90. self.line_handler = self.parse_header
  91. for out in dig_process.stdout:
  92. self.line_handler(out)
  93. def _check_next_header(self, line):
  94. """
  95. Returns true if we found a next header, and sets the internal
  96. line handler to the appropriate value.
  97. """
  98. if line == ";; ANSWER SECTION:\n":
  99. self.line_handler = self.parse_answer
  100. elif line == ";; AUTHORITY SECTION:\n":
  101. self.line_handler = self.parse_authority
  102. elif line == ";; ADDITIONAL SECTION:\n":
  103. self.line_handler = self.parse_additional
  104. elif line.startswith(";; Query time"):
  105. self.line_handler = self.parse_footer
  106. else:
  107. return False
  108. return True
  109. def parse_header(self, line):
  110. """
  111. Parse the header lines of the query response.
  112. Parameters:
  113. line: The current line of the response.
  114. """
  115. if not self._check_next_header(line):
  116. status_match = self.status_re.search(line)
  117. flags_match = self.flags_re.search(line)
  118. if status_match is not None:
  119. self.opcode = status_match.group(1)
  120. self.rcode = status_match.group(2)
  121. elif flags_match is not None:
  122. self.flags = flags_match.group(1)
  123. self.qdcount = flags_match.group(2)
  124. self.ancount = flags_match.group(3)
  125. self.nscount = flags_match.group(4)
  126. self.adcount = flags_match.group(5)
  127. def parse_question(self, line):
  128. """
  129. Parse the question section lines of the query response.
  130. Parameters:
  131. line: The current line of the response.
  132. """
  133. if not self._check_next_header(line):
  134. if line != "\n":
  135. self.question_section.append(line.strip())
  136. def parse_answer(self, line):
  137. """
  138. Parse the answer section lines of the query response.
  139. Parameters:
  140. line: The current line of the response.
  141. """
  142. if not self._check_next_header(line):
  143. if line != "\n":
  144. self.answer_section.append(line.strip())
  145. def parse_authority(self, line):
  146. """
  147. Parse the authority section lines of the query response.
  148. Parameters:
  149. line: The current line of the response.
  150. """
  151. if not self._check_next_header(line):
  152. if line != "\n":
  153. self.authority_section.append(line.strip())
  154. def parse_additional(self, line):
  155. """
  156. Parse the additional section lines of the query response.
  157. Parameters:
  158. line: The current line of the response.
  159. """
  160. if not self._check_next_header(line):
  161. if line != "\n":
  162. self.additional_section.append(line.strip())
  163. def parse_footer(self, line):
  164. """
  165. Parse the footer lines of the query response.
  166. Parameters:
  167. line: The current line of the response.
  168. """
  169. pass
  170. @step('A query for ([\w.]+) (?:type ([A-Z0-9]+) )?(?:class ([A-Z]+) )?' +
  171. '(?:to ([^:]+)(?::([0-9]+))? )?should have rcode ([\w.]+)')
  172. def query(step, query_name, qtype, qclass, addr, port, rcode):
  173. """
  174. Run a query, check the rcode of the response, and store the query
  175. result in world.last_query_result.
  176. Parameters:
  177. query_name ('query for <name>'): The domain name to query.
  178. qtype ('type <type>', optional): The RR type to query. Defaults to A.
  179. qclass ('class <class>', optional): The RR class to query. Defaults to IN.
  180. addr ('to <address>', optional): The IP address of the nameserver to query.
  181. Defaults to 127.0.0.1.
  182. port (':<port>', optional): The port number of the nameserver to query.
  183. Defaults to 47806.
  184. rcode ('should have rcode <rcode>'): The expected rcode of the answer.
  185. """
  186. if qtype is None:
  187. qtype = "A"
  188. if qclass is None:
  189. qclass = "IN"
  190. if addr is None:
  191. addr = "127.0.0.1"
  192. if port is None:
  193. port = 47806
  194. query_result = QueryResult(query_name, qtype, qclass, addr, port)
  195. assert query_result.rcode == rcode,\
  196. "Expected: " + rcode + ", got " + query_result.rcode
  197. world.last_query_result = query_result
  198. @step('The SOA serial for ([\w.]+) should be ([0-9]+)')
  199. def query_soa(step, query_name, serial):
  200. """
  201. Convenience function to check the SOA SERIAL value of the given zone at
  202. the nameserver at the default address (127.0.0.1:47806).
  203. Parameters:
  204. query_name ('for <name>'): The zone to find the SOA record for.
  205. serial ('should be <number>'): The expected value of the SOA SERIAL.
  206. If the rcode is not NOERROR, or the answer section does not contain the
  207. SOA record, this step fails.
  208. """
  209. query_result = QueryResult(query_name, "SOA", "IN", "127.0.0.1", "47806")
  210. assert "NOERROR" == query_result.rcode,\
  211. "Got " + query_result.rcode + ", expected NOERROR"
  212. assert len(query_result.answer_section) == 1,\
  213. "Too few or too many answers in SOA response"
  214. soa_parts = query_result.answer_section[0].split()
  215. assert serial == soa_parts[6],\
  216. "Got SOA serial " + soa_parts[6] + ", expected " + serial
  217. @step('last query response should have (\S+) (.+)')
  218. def check_last_query(step, item, value):
  219. """
  220. Check a specific value in the reponse from the last successful query sent.
  221. Parameters:
  222. item: The item to check the value of
  223. value: The expected value.
  224. This performs a very simple direct string comparison of the QueryResult
  225. member with the given item name and the given value.
  226. Fails if the item is unknown, or if its value does not match the expected
  227. value.
  228. """
  229. assert world.last_query_result is not None
  230. assert item in world.last_query_result.__dict__
  231. lq_val = world.last_query_result.__dict__[item]
  232. assert str(value) == str(lq_val),\
  233. "Got: " + str(lq_val) + ", expected: " + str(value)
  234. @step('([a-zA-Z]+) section of the last query response should be')
  235. def check_last_query_section(step, section):
  236. """
  237. Check the entire contents of the given section of the response of the last
  238. query.
  239. Parameters:
  240. section ('<section> section'): The name of the section (QUESTION, ANSWER,
  241. AUTHORITY or ADDITIONAL).
  242. The expected response is taken from the multiline part of the step in the
  243. scenario. Differing whitespace is ignored, but currently the order is
  244. significant.
  245. Fails if they do not match.
  246. """
  247. response_string = None
  248. if section.lower() == 'question':
  249. response_string = "\n".join(world.last_query_result.question_section)
  250. elif section.lower() == 'answer':
  251. response_string = "\n".join(world.last_query_result.answer_section)
  252. elif section.lower() == 'authority':
  253. response_string = "\n".join(world.last_query_result.answer_section)
  254. elif section.lower() == 'additional':
  255. response_string = "\n".join(world.last_query_result.answer_section)
  256. else:
  257. assert False, "Unknown section " + section
  258. # replace whitespace of any length by one space
  259. response_string = re.sub("[ \t]+", " ", response_string)
  260. expect = re.sub("[ \t]+", " ", step.multiline)
  261. assert response_string.strip() == expect.strip(),\
  262. "Got:\n'" + response_string + "'\nExpected:\n'" + step.multiline +"'"