loadzone.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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; can be None to make an
  25. empty zone.
  26. db_file (str): SQLite3 DB file to load the zone into
  27. """
  28. sqlite_datasrc_cfg = '{"database_file": "' + db_file + '"}'
  29. if zone_file is not None:
  30. args = ['b10-loadzone', '-c', sqlite_datasrc_cfg, zone, zone_file]
  31. else:
  32. args = ['b10-loadzone', '-c', sqlite_datasrc_cfg, '-e', zone]
  33. loadzone = subprocess.Popen(args, 1, None, None,
  34. subprocess.PIPE, subprocess.PIPE)
  35. (stdout, stderr) = loadzone.communicate()
  36. result = loadzone.returncode
  37. world.last_loadzone_stdout = stdout
  38. world.last_loadzone_stderr = stderr
  39. assert result == 0, "loadzone exit code: " + str(result) +\
  40. "\nstdout:\n" + str(stdout) +\
  41. "stderr:\n" + str(stderr)
  42. @step('load zone (\S+) to DB file (\S+) from (\S+)')
  43. def load_zone_to_dbfile(step, zone, db_file, zone_file):
  44. """Load a zone into a data source from a zone file.
  45. It currently only works for an SQLite3-based data source. Its
  46. DB file name should be specified.
  47. Step definition:
  48. load zone <zone_name> to DB file <db_file> from <zone_file>
  49. """
  50. run_loadzone(zone, zone_file, db_file)
  51. @step('make empty zone (\S+) in DB file (\S+)')
  52. def make_empty_zone_to_dbfile(step, zone, db_file):
  53. """Make an empty zone into a data source.
  54. If a non-empty zone already exists in the data source, it will be emptied;
  55. otherwise, a new empty zone will be created.
  56. It currently only works for an SQLite3-based data source. Its
  57. DB file name should be specified.
  58. Step definition:
  59. make empty zone <zone_name> to DB file <db_file>
  60. """
  61. run_loadzone(zone, None, db_file)
  62. @step('load (\d+) records for zone (\S+) to DB file (\S+)')
  63. def load_zone_rr_to_dbfile(step, num_records, zone, db_file):
  64. """Load a zone with a specified number of RRs into a data source.
  65. It currently only works for an SQLite3-based data source. Its
  66. DB file name should be specified.
  67. It creates the content of the zone dynamically so the total number of
  68. RRs of the zone is the specified number, including mandatory SOA and NS.
  69. Step definition:
  70. load zone <zone_name> to DB file <db_file> from <zone_file>
  71. """
  72. num_records = int(num_records)
  73. assert num_records >= 2, 'zone must have at least 2 RRs: SOA and NS'
  74. num_records -= 2
  75. with tempfile.NamedTemporaryFile(mode='w', prefix='zone-file',
  76. dir='data/', delete=True) as f:
  77. filename = f.name
  78. f.write('$TTL 3600\n')
  79. f.write('$ORIGIN .\n') # so it'll work whether or not zone ends with .
  80. f.write(zone + ' IN SOA . . 0 0 0 0 0\n')
  81. f.write(zone + ' IN NS 0.' + zone + '\n')
  82. count = 0
  83. while count < num_records:
  84. f.write(str(count) + '.' + zone + ' IN A 192.0.2.1\n')
  85. count += 1
  86. f.flush()
  87. run_loadzone(zone, f.name, db_file)