loadzone.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2013 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. from lettuce import *
  16. import subprocess
  17. import tempfile
  18. def run_loadzone(zone, zone_file, db_file):
  19. """Run b10-loadzone.
  20. It currently only works for an SQLite3-based data source, and takes
  21. its DB file as a parameter.
  22. Parameters:
  23. zone (str): the zone name
  24. zone_file (str): master zone file for the zone
  25. db_file (str): SQLite3 DB file to load the zone into
  26. """
  27. sqlite_datasrc_cfg = '{"database_file": "' + db_file + '"}'
  28. args = ['b10-loadzone', '-c', sqlite_datasrc_cfg, zone, zone_file]
  29. loadzone = subprocess.Popen(args, 1, None, None,
  30. subprocess.PIPE, subprocess.PIPE)
  31. (stdout, stderr) = loadzone.communicate()
  32. result = loadzone.returncode
  33. world.last_loadzone_stdout = stdout
  34. world.last_loadzone_stderr = stderr
  35. assert result == 0, "loadzone exit code: " + str(result) +\
  36. "\nstdout:\n" + str(stdout) +\
  37. "stderr:\n" + str(stderr)
  38. @step('load zone (\S+) to DB file (\S+) from (\S+)')
  39. def load_zone_to_dbfile(step, zone, db_file, zone_file):
  40. """Load a zone into a data source from a zone file.
  41. It currently only works for an SQLite3-based data source. Its
  42. DB file name should be specified.
  43. Step definition:
  44. load zone <zone_name> to DB file <db_file> from <zone_file>
  45. """
  46. run_loadzone(zone, zone_file, db_file)
  47. @step('load (\d+) records for zone (\S+) to DB file (\S+)')
  48. def load_zone_rr_to_dbfile(step, num_records, zone, db_file):
  49. """Load a zone with a specified number of RRs into a data source.
  50. It currently only works for an SQLite3-based data source. Its
  51. DB file name should be specified.
  52. It creates the content of the zone dynamically so the total number of
  53. RRs of the zone is the specified number, including mandatory SOA and NS.
  54. Step definition:
  55. load zone <zone_name> to DB file <db_file> from <zone_file>
  56. """
  57. num_records = int(num_records)
  58. assert num_records >= 2, 'zone must have at least 2 RRs: SOA and NS'
  59. num_records -= 2
  60. with tempfile.NamedTemporaryFile(mode='w', prefix='zone-file',
  61. dir='data/', delete=True) as f:
  62. filename = f.name
  63. f.write('$TTL 3600\n')
  64. f.write('$ORIGIN .\n') # so it'll work whether or not zone ends with .
  65. f.write(zone + ' IN SOA . . 0 0 0 0 0\n')
  66. f.write(zone + ' IN NS 0.' + zone + '\n')
  67. count = 0
  68. while count < num_records:
  69. f.write(str(count) + '.' + zone + ' IN A 192.0.2.1\n')
  70. count += 1
  71. f.flush()
  72. run_loadzone(zone, f.name, db_file)