transfer.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 transfer (ixfr/axfr) test functionality
  16. # It provides steps to perform the client side of a transfer,
  17. # and inspect the results.
  18. #
  19. # Like querying.py, it uses dig to do the transfers, and
  20. # places its output in a result structure
  21. #
  22. # This is done in a different file with different steps than
  23. # querying, because the format of dig's output is
  24. # very different than that of normal queries
  25. from lettuce import *
  26. import subprocess
  27. import re
  28. class TransferResult(object):
  29. """This object stores transfer results, which is essentially simply
  30. a list of RR strings. These are stored, as read from dig's output,
  31. in the list 'records'. So for an IXFR transfer it contains
  32. the exact result as returned by the server.
  33. If this list is empty, the transfer failed for some reason (dig
  34. does not really show error results well, unfortunately).
  35. We may add some smarter inspection functionality to this class
  36. later.
  37. """
  38. def __init__(self, args):
  39. """Perform the transfer by calling dig, and store the results.
  40. args is the array of arguments to pass to Popen(), this
  41. is passed as is since for IXFR and AXFR there can be very
  42. different options"""
  43. self.records = []
  44. # Technically, using a pipe here can fail; since we don't expect
  45. # large output right now, this works, but should we get a test
  46. # where we do have a lot of output, this could block, and we will
  47. # need to read the output in a different way.
  48. dig_process = subprocess.Popen(args, 1, None, None, subprocess.PIPE,
  49. None)
  50. result = dig_process.wait()
  51. assert result == 0
  52. for l in dig_process.stdout:
  53. line = l.strip()
  54. if len(line) > 0 and line[0] != ';':
  55. self.records.append(line)
  56. @step('An AXFR transfer of ([\w.]+)(?: from ([^:]+|\[[0-9a-fA-F:]+\])(?::([0-9]+))?)?')
  57. def perform_axfr(step, zone_name, address, port):
  58. """
  59. Perform an AXFR transfer, and store the result as an instance of
  60. TransferResult in world.transfer_result.
  61. Step definition:
  62. An AXFR transfer of <zone_name> [from <address>:<port>]
  63. Address defaults to ::1
  64. Port defaults to 47806
  65. """
  66. if address is None:
  67. address = "::1"
  68. # convert [IPv6_addr] to IPv6_addr:
  69. address = re.sub(r"\[(.+)\]", r"\1", address)
  70. if port is None:
  71. port = 47806
  72. args = [ 'dig', 'AXFR', '@' + str(address), '-p', str(port), zone_name ]
  73. world.transfer_result = TransferResult(args)
  74. @step('An IXFR transfer of ([\w.]+) (\d+)(?: from ([^:]+)(?::([0-9]+))?)?(?: over (tcp|udp))?')
  75. def perform_ixfr(step, zone_name, serial, address, port, protocol):
  76. """
  77. Perform an IXFR transfer, and store the result as an instance of
  78. TransferResult in world.transfer_result.
  79. Step definition:
  80. An IXFR transfer of <zone_name> <serial> [from <address>:port] [over <tcp|udp>]
  81. Address defaults to 127.0.0.1
  82. Port defaults to 47806
  83. If either tcp or udp is specified, only this protocol will be used.
  84. """
  85. if address is None:
  86. address = "127.0.0.1"
  87. if port is None:
  88. port = 47806
  89. args = [ 'dig', 'IXFR=' + str(serial), '@' + str(address), '-p', str(port), zone_name ]
  90. if protocol is not None:
  91. assert protocol == 'tcp' or protocol == 'udp', "Unknown protocol: " + protocol
  92. if protocol == 'tcp':
  93. args.append('+tcp')
  94. elif protocol == 'udp':
  95. args.append('+notcp')
  96. world.transfer_result = TransferResult(args)
  97. @step('transfer result should have (\d+) rrs?')
  98. def check_transfer_result_count(step, number_of_rrs):
  99. """
  100. Check the number of rrs in the transfer result object created by
  101. the AXFR transfer or IXFR transfer step.
  102. Step definition:
  103. transfer result should have <number> rr[s]
  104. Fails if the number of RRs is not equal to number
  105. """
  106. assert int(number_of_rrs) == len(world.transfer_result.records),\
  107. "Got " + str(len(world.transfer_result.records)) +\
  108. " records, expected " + str(number_of_rrs)
  109. @step('full result of the last transfer should be')
  110. def check_full_transfer_result(step):
  111. """
  112. Check the complete output from the last transfer call.
  113. Step definition:
  114. full result of the last transfer should be <multiline value>
  115. Whitespace is normalized in both the multiline value and the
  116. output, but the order of the output is not.
  117. Fails if there is any difference between the two. Prints
  118. full output and expected value upon failure.
  119. """
  120. records_string = "\n".join(world.transfer_result.records)
  121. records_string = re.sub("[ \t]+", " ", records_string)
  122. expect = re.sub("[ \t]+", " ", step.multiline)
  123. assert records_string.strip() == expect.strip(),\
  124. "Got:\n'" + records_string + "'\nExpected:\n'" + expect + "'"