commit 21e07e92ca2d0341a09de48fadfa95625f6d9f55
Author: Trey Dockendorf <treydock@gmail.com>
Date:   Wed Apr 23 09:24:41 2014 -0500

    fixes #4599 - DNS and DHCP records for BMC and managed interfaces not using FQDN

diff --git a/app/models/concerns/orchestration/dhcp.rb b/app/models/concerns/orchestration/dhcp.rb
index b34128848..947a53da7 100644
--- a/app/models/concerns/orchestration/dhcp.rb
+++ b/app/models/concerns/orchestration/dhcp.rb
@@ -8,7 +8,7 @@ module Orchestration::DHCP
   end
 
   def dhcp?
-    name.present? && ip_available? && mac_available? && !subnet.nil? && subnet.dhcp? && managed?
+    hostname.present? && ip_available? && mac_available? && !subnet.nil? && subnet.dhcp? && managed?
   end
 
   def dhcp_record
@@ -58,7 +58,7 @@ module Orchestration::DHCP
   def dhcp_attrs
     return unless dhcp?
     dhcp_attr = { :name => name, :filename => operatingsystem.boot_filename(self),
-                  :ip => ip, :mac => mac, :hostname => name, :proxy => subnet.dhcp_proxy,
+                  :ip => ip, :mac => mac, :hostname => hostname, :proxy => subnet.dhcp_proxy,
                   :network => subnet.network, :nextServer => boot_server }
 
     if jumpstart?
@@ -94,7 +94,7 @@ module Orchestration::DHCP
   # do we need to update our dhcp reservations
   def dhcp_update_required?
     # IP Address / name changed
-    return true if ((old.ip != ip) or (old.name != name) or (old.mac != mac) or (old.subnet != subnet))
+    return true if ((old.ip != ip) or (old.hostname != hostname) or (old.mac != mac) or (old.subnet != subnet))
     # Handle jumpstart
     #TODO, abstract this way once interfaces are fully used
     if self.kind_of?(Host::Base) and jumpstart?
@@ -134,7 +134,7 @@ module Orchestration::DHCP
 
   def dhcp_conflict_detected?
     # we can't do any dhcp based validations when our MAC address is defined afterwards (e.g. in vm creation)
-    return false if mac.blank? or name.blank?
+    return false if mac.blank? or hostname.blank?
     return false unless dhcp?
 
     if dhcp_record and dhcp_record.conflicting? and (not overwrite?)
diff --git a/app/models/concerns/orchestration/dns.rb b/app/models/concerns/orchestration/dns.rb
index dc7364b6d..62dc47ae2 100644
--- a/app/models/concerns/orchestration/dns.rb
+++ b/app/models/concerns/orchestration/dns.rb
@@ -7,11 +7,11 @@ module Orchestration::DNS
   end
 
   def dns?
-    name.present? and ip_available? and !domain.nil? and !domain.proxy.nil? and managed?
+    hostname.present? and ip_available? and !domain.nil? and !domain.proxy.nil? and managed?
   end
 
   def reverse_dns?
-    name.present? and ip_available? and !subnet.nil? and subnet.dns? and managed?
+    hostname.present? and ip_available? and !subnet.nil? and subnet.dns? and managed?
   end
 
   def dns_a_record
@@ -61,11 +61,11 @@ module Orchestration::DNS
   private
 
   def dns_record_attrs
-    { :hostname => name, :ip => ip, :resolver => domain.resolver, :proxy => domain.proxy }
+    { :hostname => hostname, :ip => ip, :resolver => domain.resolver, :proxy => domain.proxy }
   end
 
   def reverse_dns_record_attrs
-    { :hostname => name, :ip => ip, :proxy => subnet.dns_proxy }
+    { :hostname => hostname, :ip => ip, :proxy => subnet.dns_proxy }
   end
 
   def queue_dns
@@ -83,7 +83,7 @@ module Orchestration::DNS
   end
 
   def queue_dns_update
-    if old.ip != ip or old.name != name
+    if old.ip != ip or old.hostname != hostname
       queue.create(:name   => _("Remove DNS record for %s") % old, :priority => 9,
                    :action => [old, :del_dns_a_record]) if old.dns?
       queue.create(:name   => _("Remove Reverse DNS record for %s") % old, :priority => 9,
@@ -111,7 +111,7 @@ module Orchestration::DNS
   end
 
   def dns_conflict_detected?
-    return false if ip.blank? or name.blank?
+    return false if ip.blank? or hostname.blank?
     # can't validate anything if dont have an ip-address yet
     return false unless require_ip_validation?
     # we should only alert on conflicts if overwrite mode is off
diff --git a/app/models/nic/managed.rb b/app/models/nic/managed.rb
index b8921fc64..f61d6bfdd 100644
--- a/app/models/nic/managed.rb
+++ b/app/models/nic/managed.rb
@@ -43,6 +43,14 @@ module Nic
       @dhcp_record ||= Net::DHCP::Record.new(dhcp_attrs)
     end
 
+    def hostname
+      unless domain.nil?
+        "#{name}.#{domain.name}"
+      else
+        name
+      end
+    end
+
     protected
 
     def uniq_fields_with_hosts
@@ -53,7 +61,7 @@ module Nic
     def dhcp_attrs
       raise ::Foreman::Exception.new(N_("DHCP not supported for this NIC")) unless dhcp?
       {
-        :hostname => name,
+        :hostname => hostname,
         :ip       => ip,
         :mac      => mac,
         :proxy    => subnet.dhcp_proxy,
diff --git a/test/fixtures/nics.yml b/test/fixtures/nics.yml
index d80905bb7..4df6fc535 100644
--- a/test/fixtures/nics.yml
+++ b/test/fixtures/nics.yml
@@ -2,7 +2,7 @@ bmc:
   ip: 10.0.0.1
   mac: AA:AA:AA:AA:AA:AA
   type: Nic::BMC
-  name: host-bmc.domain.com
+  name: host-bmc
   host: one
   attrs:
     :username: foo
diff --git a/test/fixtures/subnets.yml b/test/fixtures/subnets.yml
index 19f7111ec..28d081faa 100644
--- a/test/fixtures/subnets.yml
+++ b/test/fixtures/subnets.yml
@@ -31,3 +31,11 @@ four:
    mask: 255.255.255.0
    tftp: two
    vlanid: 44
+
+five:
+  name: five
+  network: 10.0.0.0
+  mask: 255.255.255.0
+  dhcp: one
+  tftp: two
+  dns: three
diff --git a/test/unit/nic_test.rb b/test/unit/nic_test.rb
index 4086d6610..9d14dc0fb 100644
--- a/test/unit/nic_test.rb
+++ b/test/unit/nic_test.rb
@@ -52,4 +52,21 @@ class NicTest < ActiveSupport::TestCase
     assert_equal subnet.network, interface.network
     assert_equal subnet.vlanid, interface.vlanid
   end
+
+  test "Nic::BMC should have hostname containing name and domain name" do
+    subnet = subnets(:five)
+    domain = domains(:mydomain)
+    interface = nics(:bmc)
+    interface.subnet = subnet
+    interface.domain = domain
+    assert_equal "#{interface.name}.#{interface.domain.name}", interface.hostname
+  end
+
+  test "Nic::BMC should have hostname containing name when domain nil" do
+    subnet = subnets(:five)
+    interface = nics(:bmc)
+    interface.subnet = subnet
+    interface.domain = nil
+    assert_equal interface.name, interface.hostname
+  end
 end
diff --git a/test/unit/orchestration/dhcp_test.rb b/test/unit/orchestration/dhcp_test.rb
index 0b389ea60..c9ab8971e 100644
--- a/test/unit/orchestration/dhcp_test.rb
+++ b/test/unit/orchestration/dhcp_test.rb
@@ -22,6 +22,16 @@ class DhcpOrchestrationTest < ActiveSupport::TestCase
     end
   end
 
+  def test_bmc_should_have_valid_dhcp_record
+    if unattended?
+      b = nics(:bmc)
+      b.domain = domains(:mydomain)
+      b.subnet = subnets(:five)
+      assert b.dhcp?
+      assert_equal "#{b.name}.#{b.domain.name}-#{b.mac}/#{b.ip}", b.dhcp_record.to_s
+    end
+  end
+
   test "jumpstart parameter generation" do
     h = hosts(:sol10host)
     Resolv::DNS.any_instance.stubs(:getaddress).with("brsla01").returns("2.3.4.5").once
diff --git a/test/unit/orchestration/dns_test.rb b/test/unit/orchestration/dns_test.rb
index e1b1e23a1..dfda78145 100644
--- a/test/unit/orchestration/dns_test.rb
+++ b/test/unit/orchestration/dns_test.rb
@@ -51,4 +51,16 @@ class DnsOrchestrationTest < ActiveSupport::TestCase
       assert_not_nil h.dns_ptr_record
     end
   end
+
+  def test_bmc_should_have_valid_dns_records
+    if unattended?
+      b = nics(:bmc)
+      b.domain = domains(:mydomain)
+      b.subnet = subnets(:five)
+      assert b.dns?
+      assert b.reverse_dns?
+      assert_equal "#{b.name}.#{b.domain.name}/#{b.ip}", b.dns_a_record.to_s
+      assert_equal "#{b.ip}/#{b.name}.#{b.domain.name}", b.dns_ptr_record.to_s
+    end
+  end
 end
