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"},
|
7 | |
:solaris => {:kernel => "SunOS", :hostname_command => "hostname"},
|
8 | |
:darwin => {:kernel => "Darwin", :hostname_command => "hostname -f"},
|
9 | |
:freebsd => {:kernel => "FreeBSD", :hostname_command => "hostname -f"},
|
10 | |
:hpux => {:kernel => "HP-UX", :hostname_command => "hostname"},
|
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]
|
21 | |
end
|
22 | |
|
|
34 |
Facter.fact(:kernel).stubs(:value).returns(scenario[:kernel])
|
|
35 |
end
|
|
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").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 | |
|
34 | |
|
|
49 |
|
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").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").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
|
48 | |
end
|
49 | |
|
50 | |
describe "Top level domain" do
|
|
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"
|
|
57 |
end
|
|
58 |
|
|
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").never
|
54 | |
File.expects(:exists?).with('/etc/resolv.conf').never
|
55 | |
Facter.fact(:domain).value.should == "tld"
|
56 | |
end
|
57 | |
end
|
58 | |
|
|
61 |
the_hostname_is("ns01.tld")
|
|
62 |
|
|
63 |
Facter.fact(:domain).value.should == "tld"
|
|
64 |
end
|
|
65 |
end
|
|
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")
|
63 | |
@mock_file = mock()
|
64 | |
File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
|
65 | |
end
|
66 | |
|
|
69 |
the_hostname_is("")
|
|
70 |
the_dnsdomainname_is("")
|
|
71 |
end
|
|
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 |
|
94 | 98 |
# Test permutations of domain and search, where 'domain' can be a value of
|
95 | 99 |
# the search keyword and the domain keyword
|
96 | 100 |
# and also where 'search' can be a value of the search keyword and the
|
|
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
|
129 | 131 |
end
|
130 | 132 |
end
|
131 | |
end
|
|
133 |
end
|
132 | 134 |
|
133 | 135 |
describe "on Windows" do
|
134 | 136 |
before(:each) do
|
|
172 | 174 |
end
|
173 | 175 |
end
|
174 | 176 |
|
175 | |
describe "with trailing dots" do
|
176 | |
describe "on Windows" do
|
|
177 |
describe "with trailing dots" do
|
|
178 |
describe "on Windows" do
|
177 | 179 |
before do
|
178 | 180 |
Facter.fact(:kernel).stubs(:value).returns("windows")
|
179 | 181 |
require 'facter/util/registry'
|
180 | 182 |
require 'facter/util/wmi'
|
181 | 183 |
end
|
182 | |
|
|
184 |
|
183 | 185 |
[{:registry => 'testdomain.', :wmi => '', :expect => 'testdomain'},
|
184 | 186 |
{:registry => '', :wmi => 'testdomain.', :expect => 'testdomain'},
|
185 | 187 |
].each do |scenario|
|
186 | 188 |
|
187 | |
describe "scenarios" do
|
|
189 |
describe "scenarios" do
|
188 | 190 |
before(:each) do
|
189 | 191 |
Facter::Util::Registry.stubs(:hklm_read).returns(scenario[:registry])
|
190 | 192 |
nic = stubs 'nic'
|
191 | 193 |
nic.stubs(:DNSDomain).returns(scenario[:wmi])
|
192 | 194 |
Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic])
|
193 | 195 |
end
|
194 | |
|
|
196 |
|
195 | 197 |
it "should return #{scenario[:expect]}" do
|
196 | 198 |
Facter.fact(:domain).value.should == scenario[:expect]
|
197 | 199 |
end
|
198 | |
|
|
200 |
|
199 | 201 |
it "should remove trailing dots" do
|
200 | 202 |
Facter.fact(:domain).value.should_not =~ /\.$/
|
201 | 203 |
end
|
202 | |
end
|
203 | |
end
|
|
204 |
end
|
|
205 |
end
|
204 | 206 |
end
|
205 | 207 |
|
206 | 208 |
describe "on everything else" do
|
|
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
|
220 | |
before(:each) do
|
221 | |
Facter::Util::Resolution.stubs(:exec).with("hostname -f").returns(scenario[:hostname])
|
222 | |
Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").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)
|
|
257 |
describe scenario[:scenario] do
|
|
258 |
before(:each) do
|
|
259 |
Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname])
|
|
260 |
Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").returns(scenario[:dnsdomainname])
|
|
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
|
|
237 | 272 |
Facter.fact(:domain).value.should == scenario[:expect]
|
238 | 273 |
end
|
239 | 274 |
end
|
240 | |
end
|
|
275 |
end
|
241 | 276 |
end
|
242 | 277 |
end
|
243 | 278 |
end
|