Commit 370e633e authored by Colin Shea's avatar Colin Shea

make ZoneGenerator more testable. and then test it

parent c69ec833
......@@ -8,6 +8,8 @@ require "zone"
require "zone_generator"
require 'zonefile'
generator = ZoneGenerator.new Dir.pwd
config = YAML.load_file(ENV['ZONE_CONFIG_FILE'] || "config.yaml")
generator = ZoneGenerator.new(config, Dir.pwd)
generator.purge!
generator.generate
generator.deploy
......@@ -2,8 +2,13 @@ require 'fileutils'
class ZoneGenerator
def initialize(basedir)
@config = YAML.load_file("config.yaml")
attr_accessor :config, :soa, :generated, :zones_dir, :template_dir
attr_reader :basedir
def initialize(config, basedir)
@config = config
@basedir = basedir
@soa = {
origin: "@",
ttl: "86400",
......@@ -15,9 +20,9 @@ class ZoneGenerator
minimumTTL: "11h"
}.merge(@config['soa'])
@zones_dir = File.expand_path(@config['ruby_zones'])
@template_dir = File.expand_path(@config['templates'])
@generated = File.expand_path(@config['output'])
@zones_dir = File.expand_path(@config['ruby_zones'], basedir)
@template_dir = File.expand_path(@config['templates'], basedir)
@generated = File.expand_path(@config['output'], basedir)
@tmp_named = "#{@generated}/named.conf"
@tmp_zones = "#{@generated}/zones"
......@@ -26,7 +31,10 @@ class ZoneGenerator
if (email = @soa[:email]).include?("@")
@soa[:email] = email.sub("@",".") << "."
end
end
# Delete the old generated directory, and rebuild it.
def purge!
FileUtils.rm_rf @generated
FileUtils.mkdir_p @generated
FileUtils.mkdir_p @tmp_zones
......
require 'zone_generator'
class GeneratorTest < Minitest::Test
def setup
@config = YAML.load_file(File.dirname(__FILE__) + '/../example/config.yaml')
@gen = ZoneGenerator.new(@config, File.expand_path('.', File.dirname(__FILE__) + '/../example/'))
end
def test_files_present
assert File.directory? @gen.basedir
assert File.directory? @gen.generated
assert File.directory? @gen.zones_dir
assert File.directory? @gen.template_dir
end
def test_purge
@gen.purge!
assert Dir.empty? File.expand_path(@config['output'] + '/zones', @gen.basedir)
end
def test_generate
@gen.purge!
capture_io do
@gen.generate_zone File.join(@gen.zones_dir, 'example.com.rb'), 'example.com'
end
assert File.exists? File.expand_path(@config['output'] + '/zones/example.com.zone', @gen.basedir)
end
def test_generate_all
@gen.purge!
capture_io do
@gen.generate
end
assert File.exists? File.expand_path(@config['output'] + '/zones/example.com.zone', @gen.basedir)
named_conf = File.expand_path(@config['output'] + '/named.conf', @gen.basedir)
assert File.exists? named_conf
assert File.foreach(named_conf).grep(/zone "example\.com"/)
end
end
......@@ -23,3 +23,4 @@ require 'zonefile'
require_relative 'zone_test'
require_relative 'zonefile_test'
require_relative 'generate_test'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment