From 20db122d19b1736a010fd0a60ea6cece77cacf13 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Fri, 1 Jul 2016 19:20:39 +0200 Subject: [PATCH] Issue #176 Making solution for skipping of reverse DNS entry creation for CNAMES more readable. Adding tests. --- features/dns_resolution.feature | 9 +++++++++ landrush.gemspec | 1 - lib/landrush/action/setup.rb | 24 +++++++++++++----------- test/landrush/action/setup_test.rb | 18 +++++++++--------- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/features/dns_resolution.feature b/features/dns_resolution.feature index ea48fa5..0cd2b85 100644 --- a/features/dns_resolution.feature +++ b/features/dns_resolution.feature @@ -20,6 +20,15 @@ Feature: dns_resolution And the hostname "my-host.landrush-acceptance-test" should resolve to "10.10.10.123" on the host And the hostname "my-host.landrush-acceptance-test" should resolve to "10.10.10.123" on the guest + When I successfully run `bundle exec vagrant landrush set my-static-host.landrush-acceptance-test 42.42.42.42` + Then the hostname "my-static-host.landrush-acceptance-test" should resolve to "42.42.42.42" on the internal DNS server + And the hostname "my-static-host.landrush-acceptance-test" should resolve to "42.42.42.42" on the host + And the hostname "my-static-host.landrush-acceptance-test" should resolve to "42.42.42.42" on the guest + + When I successfully run `bundle exec vagrant landrush set my-static-cname-host.landrush-acceptance-test my-static-host.landrush-acceptance-test` + Then the hostname "my-static-cname-host.landrush-acceptance-test" should resolve to "42.42.42.42" on the internal DNS server + And the hostname "my-static-cname-host.landrush-acceptance-test" should resolve to "42.42.42.42" on the host + Examples: | box | provider | | debian/jessie64 | virtualbox | diff --git a/landrush.gemspec b/landrush.gemspec index a240503..94a0544 100644 --- a/landrush.gemspec +++ b/landrush.gemspec @@ -30,6 +30,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'rubydns', '0.8.5' spec.add_dependency 'win32-process' - spec.add_dependency 'json' spec.add_dependency 'landrush-ip', '~> 0.2.3' end diff --git a/lib/landrush/action/setup.rb b/lib/landrush/action/setup.rb index 85f0c28..5732bf3 100644 --- a/lib/landrush/action/setup.rb +++ b/lib/landrush/action/setup.rb @@ -18,6 +18,12 @@ def call(env) post_boot_setup if enabled? end + def host_ip_address + static_private_network_ip || machine.guest.capability(:read_host_visible_ip_address) + end + + private + def pre_boot_setup record_dependent_vm add_prerequisite_network_interface @@ -67,19 +73,19 @@ def setup_static_dns config.hosts.each do |hostname, dns_value| dns_value ||= host_ip_address unless Store.hosts.has?(hostname, dns_value) - info "adding static entry: #{hostname} => #{dns_value}" + info "adding static DNS entry: #{hostname} => #{dns_value}" Store.hosts.set hostname, dns_value - unless static_dns_ip_address(dns_value).nil? - Store.hosts.set(IPAddr.new(dns_value).reverse, hostname) + if ip_address?(dns_value) + reverse_dns = IPAddr.new(dns_value).reverse + info "adding static reverse DNS entry: #{reverse_dns} => #{dns_value}" + Store.hosts.set(reverse_dns, hostname) end end end end - def static_dns_ip_address(dns_value) - return IPAddr.new(dns_value) - rescue StandardError - return nil + def ip_address?(value) + !(value =~ Resolv::IPv4::Regex).nil? end def record_machine_dns_entry @@ -97,10 +103,6 @@ def record_machine_dns_entry end end - def host_ip_address - static_private_network_ip || machine.guest.capability(:read_host_visible_ip_address) - end - def private_network_exists? machine.config.vm.networks.any? { |type, _| type == :private_network } end diff --git a/test/landrush/action/setup_test.rb b/test/landrush/action/setup_test.rb index d4502b2..6680765 100644 --- a/test/landrush/action/setup_test.rb +++ b/test/landrush/action/setup_test.rb @@ -81,32 +81,32 @@ module Action env[:machine].config.vm.network :private_network, ip: '42.42.42.42' setup.call(env) - setup.static_private_network_ip.must_equal '42.42.42.42' + Store.hosts.get('somehost.vagrant.test').must_equal '42.42.42.42' end - it "for multiple private network IPs host visible IP cannot be retrieved w/o starting the VM" do + it "for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set" do skip('Not working on Windows, since it will also do the network config') if Vagrant::Util::Platform.windows? app = proc {} setup = Setup.new(app, nil) env = fake_environment + env[:machine].config.vm.network :private_network, ip: '42.42.42.41' env[:machine].config.vm.network :private_network, ip: '42.42.42.42' - + env[:machine].config.landrush.host_ip_address = '42.42.42.42' setup.call(env) - setup.static_private_network_ip.must_be_nil + Store.hosts.get('somehost.vagrant.test').must_equal '42.42.42.42' end - it "for multiple private network IPs host visible IP cant be retrieved if host_ip_address is set" do + it "is possible to add cnames via the config.landrush.host configuration option" do skip('Not working on Windows, since it will also do the network config') if Vagrant::Util::Platform.windows? app = proc {} setup = Setup.new(app, nil) env = fake_environment - env[:machine].config.vm.network :private_network, ip: '42.42.42.41' - env[:machine].config.vm.network :private_network, ip: '42.42.42.42' - env[:machine].config.landrush.host_ip_address = '42.42.42.42' + env[:machine].config.landrush.host 'foo', 'bar' setup.call(env) - setup.static_private_network_ip.must_equal '42.42.42.42' + + Store.hosts.get('foo').must_equal 'bar' end describe 'after boot' do