Please review pull request #200: (#3909) Strip trailing dots from domain fact opened by (hkenney)

Description:

Prior to this commit, the domain could end in '.' or could not.
Now, normalizing to no trailing '.' in domain fact. This also
works properly for top level domains.

  • Opened: Wed May 09 23:34:53 UTC 2012
  • Based on: puppetlabs:master (1bd98fd94b5e577216b80789ee9f933dd5cc74f8)
  • Requested merge: hkenney:ticket/master/3909_strip_trailing_dots_from_domain_fact (fd657641189985768ed970bc3782807ad27d70df)

Diff follows:

diff --git a/lib/facter/domain.rb b/lib/facter/domain.rb
index 2ae909a..f0a7c85 100644
--- a/lib/facter/domain.rb
+++ b/lib/facter/domain.rb
@@ -26,11 +26,11 @@
     if name = Facter::Util::Resolution.exec('hostname') \
       and name =~ /.*?\.(.+$)/
 
-      $1
+      return_value = $1
     elsif domain = Facter::Util::Resolution.exec('dnsdomainname') \
-      and domain =~ /.+\..+/
+      and domain =~ /.+/
 
-      domain
+      return_value = domain
     elsif FileTest.exists?("/etc/resolv.conf")
       domain = nil
       search = nil
@@ -43,9 +43,11 @@
           end
         }
       }
-      next domain if domain
-      next search if search
+      return_value ||= domain
+      return_value ||= search
     end
+    return_value = '' if return_value.nil?
+    return_value.gsub(/\.$/, '')
   end
 end
 
@@ -63,6 +65,6 @@
         break
       }
     end
-    domain
+    domain.gsub(/\.$/, '')
   end
 end
diff --git a/spec/unit/domain_spec.rb b/spec/unit/domain_spec.rb
index 07195ca..c776b48 100755
--- a/spec/unit/domain_spec.rb
+++ b/spec/unit/domain_spec.rb
@@ -72,8 +72,21 @@
         Facter.fact(:domain).value.should == 'example.org'
       end
 
-      # Test permutations of domain and search
-      [
+      # Test permutations of domain and search, where 'domain' can be a value of
+      # the search keyword and the domain keyword
+      # and also where 'search' can be a value of the search keyword and the
+      # domain keyword
+      # For example, /etc/resolv.conf may look like:
+      #     domain domain
+      # or
+      #     search search
+      # or
+      #     domain search
+      #
+      #
+      # Why someone would have their machines named 'www.domain' or 'www.search', I
+      # don't know, but we'll at least handle it properly
+       [
         ["domain domain", "domain"],
         ["domain search", "search"],
         ["search domain", "domain"],
@@ -133,4 +146,66 @@
       end
     end
   end
+
+  describe "with trailing dots" do 
+    describe "on Windows" do 
+      before do
+        Facter.fact(:kernel).stubs(:value).returns("windows")
+        require 'facter/util/registry'
+        require 'facter/util/wmi'
+      end 
+      [{:registry => 'testdomain.', :wmi => '', :expect => 'testdomain'},
+       {:registry => '', :wmi => 'testdomain.', :expect => 'testdomain'},
+      ].each do |scenario|
+        describe "scenarios" do 
+          before(:each) do
+            Facter::Util::Registry.stubs(:hklm_read).returns(scenario[:registry])
+            nic = stubs 'nic'
+            nic.stubs(:DNSDomain).returns(scenario[:wmi])
+            Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic])
+          end 
+          it "should return #{scenario[:expect]}" do
+            Facter.fact(:domain).value.should == scenario[:expect]
+          end 
+          it "should remove trailing dots"  do
+            Facter.fact(:domain).value.should_not =~ /\.$/
+          end
+        end 
+      end 
+    end
+    describe "on everything else" do
+      before do
+        Facter.fact(:kernel).stubs(:value).returns("Linux")
+        FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
+      end
+      [{:hostname => 'host.testdomain.', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
+       {:hostname => '', :dnsdomainname => 'testdomain.', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
+       {:hostname => '', :dnsdomainname => '', :resolve_domain => 'testdomain.', :resolve_search => '', :expect => 'testdomain'},
+       {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => 'testdomain.', :expect => 'testdomain'}, 
+       {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => nil}
+      ].each do |scenario|
+        describe "scenarios" do
+          before(:each) do 
+            Facter::Util::Resolution.stubs(:exec).with("hostname").returns(scenario[:hostname])
+            Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").returns(scenario[:dnsdomainname])
+            @mock_file = mock()
+            File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
+            lines = [
+                     "search #{scenario[:resolve_search]}",
+                     "domain #{scenario[:resolve_domain]}",
+                    ]
+            @mock_file.stubs(:each).multiple_yields(*lines)
+          end
+          it "should remove trailing dots" do
+            Facter.fact(:domain).value.should_not =~ /\.$/
+          end
+          it "should return #{scenario[:expect]}" do
+            Facter.fact(:domain).value.should == scenario[:expect]
+          end
+        end
+      end
+      
+    end
+     
+  end
 end

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to