(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
0 | 0 | #! /usr/bin/env ruby |
1 | 1 | |
2 | 2 | require 'spec_helper' |
3 | require 'stringio' | |
3 | 4 | |
4 | 5 | describe "Domain name facts" do |
5 | 6 | |
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 | ||
14 | 33 | 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]) | |
21 | 35 | end |
22 | 36 | |
23 | 37 | 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 | ||
25 | 40 | Facter.fact(:domain).value.should == "example.com" |
26 | 41 | end |
27 | 42 | |
28 | 43 | 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 | ||
31 | 47 | Facter.fact(:domain).value.should == "example.com" |
32 | 48 | end |
33 | 49 | |
34 | ||
35 | 50 | 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" | |
48 | 57 | end |
49 | 58 | |
50 | 59 | describe "Top level domain" do |
51 | 60 | 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 | ||
55 | 63 | Facter.fact(:domain).value.should == "tld" |
56 | 64 | end |
57 | 65 | end |
58 | 66 | |
59 | 67 | describe "when using /etc/resolv.conf" do |
60 | 68 | 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("") | |
65 | 71 | end |
66 | 72 | |
67 | 73 | 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 | ||
74 | 80 | Facter.fact(:domain).value.should == 'example.com' |
75 | 81 | end |
76 | 82 | |
77 | 83 | 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 | ||
83 | 89 | Facter.fact(:domain).value.should == 'example.org' |
84 | 90 | end |
85 | 91 | |
86 | 92 | 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 | ||
91 | 95 | Facter.fact(:domain).value.should == 'example.org' |
92 | 96 | end |
93 | 97 | |
106 | 110 | # Why someone would have their machines named 'www.domain' or 'www.search', I |
107 | 111 | # don't know, but we'll at least handle it properly |
108 | 112 | [ |
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"], | |
118 | 122 | ].each do |tuple| |
119 | field = tuple[0] | |
123 | conf = tuple[0] | |
120 | 124 | 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 | ||
126 | 128 | Facter.fact(:domain).value.should == expect |
127 | 129 | end |
128 | 130 | end |
209 | 211 | FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true) |
210 | 212 | end |
211 | 213 | |
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 | } | |
217 | 255 | ].each do |scenario| |
218 | 256 | |
219 | describe "scenarios" do | |
257 | describe scenario[:scenario] do | |
220 | 258 | before(:each) do |
221 | 259 | Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname]) |
222 | 260 | 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 | ) | |
230 | 265 | end |
231 | 266 | |
232 | 267 | it "should remove trailing dots" do |