[Pkg-puppet-devel] [facter] 34/46: (Maint) Remove duplication in domain tests

Stig Sandbeck Mathisen ssm at debian.org
Sun Sep 1 10:47:32 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 9c0cc1e7a02efcc0322f72e5418bde57993dcdf6
Author: Andrew Parker <andy at puppetlabs.com>
Date:   Tue May 28 16:22:29 2013 -0700

    (Maint) Remove duplication in domain tests
    
    The tests were full of duplication around what commands would be
    executed and how. This works toward removing the duplication and even
    combines two tests together to provide a less implementation specific
    assertion about the precedence of the resolv.conf data.
---
 spec/unit/domain_spec.rb |  189 +++++++++++++++++++++++++++-------------------
 1 file changed, 112 insertions(+), 77 deletions(-)

diff --git a/spec/unit/domain_spec.rb b/spec/unit/domain_spec.rb
index 14016d8..96176f9 100755
--- a/spec/unit/domain_spec.rb
+++ b/spec/unit/domain_spec.rb
@@ -1,94 +1,98 @@
 #! /usr/bin/env ruby
 
 require 'spec_helper'
+require 'stringio'
 
 describe "Domain name facts" do
 
-  { :linux => {:kernel => "Linux", :hostname_command => "hostname -f 2> /dev/null"},
-    :solaris => {:kernel => "SunOS", :hostname_command => "hostname 2> /dev/null"},
-    :darwin => {:kernel => "Darwin", :hostname_command => "hostname -f 2> /dev/null"},
-    :freebsd => {:kernel => "FreeBSD", :hostname_command => "hostname -f 2> /dev/null"},
-    :hpux => {:kernel => "HP-UX", :hostname_command => "hostname 2> /dev/null"},
-  }.each do |key, nested_hash|
+  def resolv_conf_contains(*lines)
+    file_handle = StringIO.new(lines.join("\n"))
+    FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
+    File.stubs(:open).with("/etc/resolv.conf").yields(file_handle)
+  end
 
-    describe "on #{key}" do
-      before do
-        Facter.fact(:kernel).stubs(:value).returns(nested_hash[:kernel])
-        FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
+  [
+    { :kernel => "Linux", :hostname_command => "hostname -f 2> /dev/null" },
+    { :kernel => "SunOS", :hostname_command => "hostname 2> /dev/null" },
+    { :kernel => "Darwin", :hostname_command => "hostname -f 2> /dev/null" },
+    { :kernel => "FreeBSD", :hostname_command => "hostname -f 2> /dev/null" },
+    { :kernel => "HP-UX", :hostname_command => "hostname 2> /dev/null" },
+  ].each do |scenario|
+
+    describe "on #{scenario[:kernel]}" do
+      let(:hostname_command) { scenario[:hostname_command] }
+      let(:dnsdomain_command) { "dnsdomainname 2> /dev/null" }
+
+      def the_hostname_is(value)
+        Facter::Util::Resolution.stubs(:exec).with(hostname_command).returns(value)
       end
 
-      let :hostname_command do
-        nested_hash[:hostname_command]
+      def the_dnsdomainname_is(value)
+        Facter::Util::Resolution.stubs(:exec).with(dnsdomain_command).returns(value)
+      end
+
+      before do
+        Facter.fact(:kernel).stubs(:value).returns(scenario[:kernel])
       end
 
       it "should use the hostname binary" do
-        Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "test.example.com"
+        the_hostname_is("test.example.com")
+
         Facter.fact(:domain).value.should == "example.com"
       end
 
       it "should fall back to the dnsdomainname binary" do
-        Facter::Util::Resolution.expects(:exec).with(hostname_command).returns("myhost")
-        Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").returns("example.com")
+        the_hostname_is("myhost")
+        the_dnsdomainname_is("example.com")
+
         Facter.fact(:domain).value.should == "example.com"
       end
 
-
       it "should fall back to /etc/resolv.conf" do
-        Facter::Util::Resolution.expects(:exec).with(hostname_command).at_least_once.returns("myhost")
-        Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").at_least_once.returns("")
-        File.expects(:open).with('/etc/resolv.conf').at_least_once
-        Facter.fact(:domain).value
-      end
+        the_hostname_is("myhost")
+        the_dnsdomainname_is("")
 
-      it "should attempt to resolve facts in a specific order" do
-        seq = sequence('domain')
-        Facter::Util::Resolution.stubs(:exec).with(hostname_command).in_sequence(seq).at_least_once
-        Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").in_sequence(seq).at_least_once
-        File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
-        Facter.fact(:domain).value
+        resolv_conf_contains("domain testing.com")
+
+        Facter.fact(:domain).value.should == "testing.com"
       end
 
       describe "Top level domain" do
         it "should find the domain name" do
-          Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "ns01.tld"
-          Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").never
-          File.expects(:exists?).with('/etc/resolv.conf').never
+          the_hostname_is("ns01.tld")
+
           Facter.fact(:domain).value.should == "tld"
         end
       end
 
       describe "when using /etc/resolv.conf" do
         before do
-          Facter::Util::Resolution.stubs(:exec).with(hostname_command)
-          Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null")
-          @mock_file = mock()
-          File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
+          the_hostname_is("")
+          the_dnsdomainname_is("")
         end
 
         it "should use the domain field over the search field" do
-          lines = [
-                   "nameserver 4.2.2.1",
-                   "search example.org",
-                   "domain example.com",
-                  ]
-          @mock_file.expects(:each).multiple_yields(*lines)
+          resolv_conf_contains(
+            "nameserver 4.2.2.1",
+            "search example.org",
+            "domain example.com"
+          )
+
           Facter.fact(:domain).value.should == 'example.com'
         end
 
         it "should fall back to the search field" do
-          lines = [
-                   "nameserver 4.2.2.1",
-                   "search example.org",
-                  ]
-          @mock_file.expects(:each).multiple_yields(*lines)
+          resolv_conf_contains(
+            "nameserver 4.2.2.1",
+            "search example.org"
+          )
+
           Facter.fact(:domain).value.should == 'example.org'
         end
 
         it "should use the first domain in the search field" do
-          lines = [
-                   "search example.org example.net",
-                  ]
-          @mock_file.expects(:each).multiple_yields(*lines)
+          resolv_conf_contains("search example.org example.net")
+
           Facter.fact(:domain).value.should == 'example.org'
         end
 
@@ -107,23 +111,21 @@ describe "Domain name facts" do
         # 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"],
-         ["search search", "search"],
-         ["search domain notdomain", "domain"],
-         [["#search notdomain","search search"], "search"],
-         [["# search notdomain","search search"], "search"],
-         [["#domain notdomain","domain domain"], "domain"],
-         [["# domain notdomain","domain domain"], "domain"],
+         [["domain domain"], "domain"],
+         [["domain search"], "search"],
+         [["search domain"], "domain"],
+         [["search search"], "search"],
+         [["search domain notdomain"], "domain"],
+         [["#search notdomain", "search search"], "search"],
+         [["# search notdomain", "search search"], "search"],
+         [["#domain notdomain", "domain domain"], "domain"],
+         [["# domain notdomain", "domain domain"], "domain"],
         ].each do |tuple|
-          field  = tuple[0]
+          conf  = tuple[0]
           expect = tuple[1]
-          it "should return #{expect} from \"#{field}\"" do
-            lines = [
-                     field
-                    ].flatten
-            @mock_file.expects(:each).multiple_yields(*lines)
+          it "should return #{expect} from \"#{conf}\"" do
+            resolv_conf_contains(*conf)
+
             Facter.fact(:domain).value.should == expect
           end
         end
@@ -210,24 +212,57 @@ describe "Domain name facts" do
         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}
+      [
+        {
+          :scenario => 'when there is only a hostname',
+          :hostname => 'host.testdomain.',
+          :dnsdomainname => '',
+          :resolve_domain => '',
+          :resolve_search => '',
+          :expect => 'testdomain'
+        },
+        {
+          :scenario => 'when there is only a domain name',
+          :hostname => '',
+          :dnsdomainname => 'testdomain.',
+          :resolve_domain => '',
+          :resolve_search => '',
+          :expect => 'testdomain'
+        },
+        {
+          :scenario => 'when there is only a resolve domain',
+          :hostname => '',
+          :dnsdomainname => '',
+          :resolve_domain => 'testdomain.',
+          :resolve_search => '',
+          :expect => 'testdomain'
+        },
+        {
+          :scenario => 'when there is only a resolve search',
+          :hostname => '',
+          :dnsdomainname => '',
+          :resolve_domain => '',
+          :resolve_search => 'testdomain.',
+          :expect => 'testdomain'
+        },
+        {
+          :scenario => 'when there is no information available',
+          :hostname => '',
+          :dnsdomainname => '',
+          :resolve_domain => '',
+          :resolve_search => '',
+          :expect => nil
+        }
       ].each do |scenario|
 
-        describe "scenarios" do
+        describe scenario[:scenario] do
           before(:each) do
             Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname])
             Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").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)
+            resolv_conf_contains(
+              "search #{scenario[:resolve_search]}",
+              "domain #{scenario[:resolve_domain]}"
+            )
           end
 
           it "should remove trailing dots" do

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list