Codebase list facter / 9c0cc1e
(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. Andrew Parker 10 years ago
1 changed file(s) with 115 addition(s) and 80 deletion(s). Raw diff Collapse all Expand all
00 #! /usr/bin/env ruby
11
22 require 'spec_helper'
3 require 'stringio'
34
45 describe "Domain name facts" do
56
6 { :linux => {:kernel => "Linux", :hostname_command => "hostname -f 2> /dev/null"},
7 :solaris => {:kernel => "SunOS", :hostname_command => "hostname 2> /dev/null"},
8 :darwin => {:kernel => "Darwin", :hostname_command => "hostname -f 2> /dev/null"},
9 :freebsd => {:kernel => "FreeBSD", :hostname_command => "hostname -f 2> /dev/null"},
10 :hpux => {:kernel => "HP-UX", :hostname_command => "hostname 2> /dev/null"},
11 }.each do |key, nested_hash|
12
13 describe "on #{key}" do
7 def resolv_conf_contains(*lines)
8 file_handle = StringIO.new(lines.join("\n"))
9 FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
10 File.stubs(:open).with("/etc/resolv.conf").yields(file_handle)
11 end
12
13 [
14 { :kernel => "Linux", :hostname_command => "hostname -f 2> /dev/null" },
15 { :kernel => "SunOS", :hostname_command => "hostname 2> /dev/null" },
16 { :kernel => "Darwin", :hostname_command => "hostname -f 2> /dev/null" },
17 { :kernel => "FreeBSD", :hostname_command => "hostname -f 2> /dev/null" },
18 { :kernel => "HP-UX", :hostname_command => "hostname 2> /dev/null" },
19 ].each do |scenario|
20
21 describe "on #{scenario[:kernel]}" do
22 let(:hostname_command) { scenario[:hostname_command] }
23 let(:dnsdomain_command) { "dnsdomainname 2> /dev/null" }
24
25 def the_hostname_is(value)
26 Facter::Util::Resolution.stubs(:exec).with(hostname_command).returns(value)
27 end
28
29 def the_dnsdomainname_is(value)
30 Facter::Util::Resolution.stubs(:exec).with(dnsdomain_command).returns(value)
31 end
32
1433 before do
15 Facter.fact(:kernel).stubs(:value).returns(nested_hash[:kernel])
16 FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
17 end
18
19 let :hostname_command do
20 nested_hash[:hostname_command]
34 Facter.fact(:kernel).stubs(:value).returns(scenario[:kernel])
2135 end
2236
2337 it "should use the hostname binary" do
24 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "test.example.com"
38 the_hostname_is("test.example.com")
39
2540 Facter.fact(:domain).value.should == "example.com"
2641 end
2742
2843 it "should fall back to the dnsdomainname binary" do
29 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns("myhost")
30 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").returns("example.com")
44 the_hostname_is("myhost")
45 the_dnsdomainname_is("example.com")
46
3147 Facter.fact(:domain).value.should == "example.com"
3248 end
3349
34
3550 it "should fall back to /etc/resolv.conf" do
36 Facter::Util::Resolution.expects(:exec).with(hostname_command).at_least_once.returns("myhost")
37 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").at_least_once.returns("")
38 File.expects(:open).with('/etc/resolv.conf').at_least_once
39 Facter.fact(:domain).value
40 end
41
42 it "should attempt to resolve facts in a specific order" do
43 seq = sequence('domain')
44 Facter::Util::Resolution.stubs(:exec).with(hostname_command).in_sequence(seq).at_least_once
45 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").in_sequence(seq).at_least_once
46 File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
47 Facter.fact(:domain).value
51 the_hostname_is("myhost")
52 the_dnsdomainname_is("")
53
54 resolv_conf_contains("domain testing.com")
55
56 Facter.fact(:domain).value.should == "testing.com"
4857 end
4958
5059 describe "Top level domain" do
5160 it "should find the domain name" do
52 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "ns01.tld"
53 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").never
54 File.expects(:exists?).with('/etc/resolv.conf').never
61 the_hostname_is("ns01.tld")
62
5563 Facter.fact(:domain).value.should == "tld"
5664 end
5765 end
5866
5967 describe "when using /etc/resolv.conf" do
6068 before do
61 Facter::Util::Resolution.stubs(:exec).with(hostname_command)
62 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null")
63 @mock_file = mock()
64 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
69 the_hostname_is("")
70 the_dnsdomainname_is("")
6571 end
6672
6773 it "should use the domain field over the search field" do
68 lines = [
69 "nameserver 4.2.2.1",
70 "search example.org",
71 "domain example.com",
72 ]
73 @mock_file.expects(:each).multiple_yields(*lines)
74 resolv_conf_contains(
75 "nameserver 4.2.2.1",
76 "search example.org",
77 "domain example.com"
78 )
79
7480 Facter.fact(:domain).value.should == 'example.com'
7581 end
7682
7783 it "should fall back to the search field" do
78 lines = [
79 "nameserver 4.2.2.1",
80 "search example.org",
81 ]
82 @mock_file.expects(:each).multiple_yields(*lines)
84 resolv_conf_contains(
85 "nameserver 4.2.2.1",
86 "search example.org"
87 )
88
8389 Facter.fact(:domain).value.should == 'example.org'
8490 end
8591
8692 it "should use the first domain in the search field" do
87 lines = [
88 "search example.org example.net",
89 ]
90 @mock_file.expects(:each).multiple_yields(*lines)
93 resolv_conf_contains("search example.org example.net")
94
9195 Facter.fact(:domain).value.should == 'example.org'
9296 end
9397
106110 # Why someone would have their machines named 'www.domain' or 'www.search', I
107111 # don't know, but we'll at least handle it properly
108112 [
109 ["domain domain", "domain"],
110 ["domain search", "search"],
111 ["search domain", "domain"],
112 ["search search", "search"],
113 ["search domain notdomain", "domain"],
114 [["#search notdomain","search search"], "search"],
115 [["# search notdomain","search search"], "search"],
116 [["#domain notdomain","domain domain"], "domain"],
117 [["# domain notdomain","domain domain"], "domain"],
113 [["domain domain"], "domain"],
114 [["domain search"], "search"],
115 [["search domain"], "domain"],
116 [["search search"], "search"],
117 [["search domain notdomain"], "domain"],
118 [["#search notdomain", "search search"], "search"],
119 [["# search notdomain", "search search"], "search"],
120 [["#domain notdomain", "domain domain"], "domain"],
121 [["# domain notdomain", "domain domain"], "domain"],
118122 ].each do |tuple|
119 field = tuple[0]
123 conf = tuple[0]
120124 expect = tuple[1]
121 it "should return #{expect} from \"#{field}\"" do
122 lines = [
123 field
124 ].flatten
125 @mock_file.expects(:each).multiple_yields(*lines)
125 it "should return #{expect} from \"#{conf}\"" do
126 resolv_conf_contains(*conf)
127
126128 Facter.fact(:domain).value.should == expect
127129 end
128130 end
209211 FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
210212 end
211213
212 [{:hostname => 'host.testdomain.', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
213 {:hostname => '', :dnsdomainname => 'testdomain.', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
214 {:hostname => '', :dnsdomainname => '', :resolve_domain => 'testdomain.', :resolve_search => '', :expect => 'testdomain'},
215 {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => 'testdomain.', :expect => 'testdomain'},
216 {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => nil}
214 [
215 {
216 :scenario => 'when there is only a hostname',
217 :hostname => 'host.testdomain.',
218 :dnsdomainname => '',
219 :resolve_domain => '',
220 :resolve_search => '',
221 :expect => 'testdomain'
222 },
223 {
224 :scenario => 'when there is only a domain name',
225 :hostname => '',
226 :dnsdomainname => 'testdomain.',
227 :resolve_domain => '',
228 :resolve_search => '',
229 :expect => 'testdomain'
230 },
231 {
232 :scenario => 'when there is only a resolve domain',
233 :hostname => '',
234 :dnsdomainname => '',
235 :resolve_domain => 'testdomain.',
236 :resolve_search => '',
237 :expect => 'testdomain'
238 },
239 {
240 :scenario => 'when there is only a resolve search',
241 :hostname => '',
242 :dnsdomainname => '',
243 :resolve_domain => '',
244 :resolve_search => 'testdomain.',
245 :expect => 'testdomain'
246 },
247 {
248 :scenario => 'when there is no information available',
249 :hostname => '',
250 :dnsdomainname => '',
251 :resolve_domain => '',
252 :resolve_search => '',
253 :expect => nil
254 }
217255 ].each do |scenario|
218256
219 describe "scenarios" do
257 describe scenario[:scenario] do
220258 before(:each) do
221259 Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname])
222260 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").returns(scenario[:dnsdomainname])
223 @mock_file = mock()
224 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
225 lines = [
226 "search #{scenario[:resolve_search]}",
227 "domain #{scenario[:resolve_domain]}",
228 ]
229 @mock_file.stubs(:each).multiple_yields(*lines)
261 resolv_conf_contains(
262 "search #{scenario[:resolve_search]}",
263 "domain #{scenario[:resolve_domain]}"
264 )
230265 end
231266
232267 it "should remove trailing dots" do