Support for verifying all the A records from a zone file

This commit is contained in:
R. Tyler Croy 2017-01-17 20:55:29 -08:00
parent a00f259630
commit 57bbb4f721
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
3 changed files with 33 additions and 2 deletions

View File

@ -20,9 +20,15 @@ module Zonespec
self.described_class.hostname)
end
def verify_all_a_records!
load_file.a.each do |record|
next if record[:name] == '@'
it { should serve_a_record(record[:name], record[:host]) }
end
end
def verify_all_cnames!
zf = Zonefile.from_file(@@zone_file)
zf.cname.each do |cname|
load_file.cname.each do |cname|
host = cname[:host]
if host == '@'
host = "#{@@zone_name}."
@ -30,6 +36,11 @@ module Zonespec
it { should serve_cname(cname[:name], host) }
end
end
private
def load_file
Zonefile.from_file(@@zone_file)
end
end
end
end
@ -50,3 +61,14 @@ RSpec::Matchers.define :serve_cname do |name, host|
"serve the CNAME `#{name}` for `#{host}`"
end
end
RSpec::Matchers.define :serve_a_record do |name, host|
match do |zone|
raise "Must use within zone()" unless zone.kind_of? Zonespec::ZoneHandler
zone.serves_a_record?(name, host)
end
description do |zone|
"serve the A record `#{name}` => `#{host}`"
end
end

View File

@ -18,6 +18,14 @@ module Zonespec
return "Zone for #{@zone_name}"
end
def serves_a_record?(name, host)
type = 'A'
fqdn = "#{name}.#{@zone_name}"
response = resolver.query(fqdn, type, 'IN')
answers = response.answer.select { |a| a.type == type }
answers.first.address.to_s == host
end
def serves_cname?(name, host)
type = 'CNAME'
# if the CNAME ends with a dot, then it's already a FQDN

View File

@ -4,6 +4,7 @@ require 'zonespec/rspec'
describe dns_server('8.8.8.8') do
describe zone('jenkins.io', 'spec/acceptance/jenkins.io.zone') do
verify_all_cnames!
verify_all_a_records!
end
describe zone('jenkins.io') do