Codebase list facter / f2a1923
(Maint) Remove whitespace errors Andrew Parker 10 years ago
2 changed file(s) with 33 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
2222 # Get the domain from various sources; the order of these
2323 # steps is important
2424
25 # In some OS 'hostname -f' will change the hostname to '-f'
26 # We know that Solaris and HP-UX exhibit this behavior
27 # On good OS, 'hostname -f' will return the FQDN which is preferable
28 # Due to dangerous behavior of 'hostname -f' on old OS, we will explicitly opt-in
25 # In some OS 'hostname -f' will change the hostname to '-f'
26 # We know that Solaris and HP-UX exhibit this behavior
27 # On good OS, 'hostname -f' will return the FQDN which is preferable
28 # Due to dangerous behavior of 'hostname -f' on old OS, we will explicitly opt-in
2929 # 'hostname -f' --hkenney May 9, 2012
3030 hostname_command = 'hostname'
3131 can_do_hostname_f = Regexp.union /Linux/i, /FreeBSD/i, /Darwin/i
3232 hostname_command = 'hostname -f' if Facter.value(:kernel) =~ can_do_hostname_f
3333
34
3534 if name = Facter::Util::Resolution.exec(hostname_command) \
3635 and name =~ /.*?\.(.+$)/
3736
1515 Facter.fact(:kernel).stubs(:value).returns(nested_hash[:kernel])
1616 FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
1717 end
18
19 let :hostname_command do
18
19 let :hostname_command do
2020 nested_hash[:hostname_command]
21 end
22
21 end
22
2323 it "should use the hostname binary" do
2424 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "test.example.com"
2525 Facter.fact(:domain).value.should == "example.com"
2626 end
27
27
2828 it "should fall back to the dnsdomainname binary" do
2929 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns("myhost")
3030 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").returns("example.com")
3131 Facter.fact(:domain).value.should == "example.com"
3232 end
33
34
33
34
3535 it "should fall back to /etc/resolv.conf" do
3636 Facter::Util::Resolution.expects(:exec).with(hostname_command).at_least_once.returns("myhost")
3737 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").at_least_once.returns("")
3838 File.expects(:open).with('/etc/resolv.conf').at_least_once
3939 Facter.fact(:domain).value
4040 end
41
41
4242 it "should attempt to resolve facts in a specific order" do
4343 seq = sequence('domain')
4444 Facter::Util::Resolution.stubs(:exec).with(hostname_command).in_sequence(seq).at_least_once
4646 File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
4747 Facter.fact(:domain).value
4848 end
49
50 describe "Top level domain" do
49
50 describe "Top level domain" do
5151 it "should find the domain name" do
5252 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "ns01.tld"
5353 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").never
5454 File.expects(:exists?).with('/etc/resolv.conf').never
55 Facter.fact(:domain).value.should == "tld"
56 end
57 end
58
55 Facter.fact(:domain).value.should == "tld"
56 end
57 end
58
5959 describe "when using /etc/resolv.conf" do
6060 before do
6161 Facter::Util::Resolution.stubs(:exec).with(hostname_command)
6363 @mock_file = mock()
6464 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
6565 end
66
66
6767 it "should use the domain field over the search field" do
6868 lines = [
6969 "nameserver 4.2.2.1",
8282 @mock_file.expects(:each).multiple_yields(*lines)
8383 Facter.fact(:domain).value.should == 'example.org'
8484 end
85
85
8686 it "should use the first domain in the search field" do
8787 lines = [
8888 "search example.org example.net",
9090 @mock_file.expects(:each).multiple_yields(*lines)
9191 Facter.fact(:domain).value.should == 'example.org'
9292 end
93
93
9494 # Test permutations of domain and search, where 'domain' can be a value of
9595 # the search keyword and the domain keyword
9696 # and also where 'search' can be a value of the search keyword and the
128128 end
129129 end
130130 end
131 end
131 end
132132
133133 describe "on Windows" do
134134 before(:each) do
172172 end
173173 end
174174
175 describe "with trailing dots" do
176 describe "on Windows" do
175 describe "with trailing dots" do
176 describe "on Windows" do
177177 before do
178178 Facter.fact(:kernel).stubs(:value).returns("windows")
179179 require 'facter/util/registry'
180180 require 'facter/util/wmi'
181181 end
182
182
183183 [{:registry => 'testdomain.', :wmi => '', :expect => 'testdomain'},
184184 {:registry => '', :wmi => 'testdomain.', :expect => 'testdomain'},
185185 ].each do |scenario|
186186
187 describe "scenarios" do
187 describe "scenarios" do
188188 before(:each) do
189189 Facter::Util::Registry.stubs(:hklm_read).returns(scenario[:registry])
190190 nic = stubs 'nic'
191191 nic.stubs(:DNSDomain).returns(scenario[:wmi])
192192 Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic])
193193 end
194
194
195195 it "should return #{scenario[:expect]}" do
196196 Facter.fact(:domain).value.should == scenario[:expect]
197197 end
198
198
199199 it "should remove trailing dots" do
200200 Facter.fact(:domain).value.should_not =~ /\.$/
201201 end
202 end
203 end
202 end
203 end
204204 end
205205
206206 describe "on everything else" do
212212 [{:hostname => 'host.testdomain.', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
213213 {:hostname => '', :dnsdomainname => 'testdomain.', :resolve_domain => '', :resolve_search => '', :expect => 'testdomain'},
214214 {:hostname => '', :dnsdomainname => '', :resolve_domain => 'testdomain.', :resolve_search => '', :expect => 'testdomain'},
215 {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => 'testdomain.', :expect => 'testdomain'},
215 {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => 'testdomain.', :expect => 'testdomain'},
216216 {:hostname => '', :dnsdomainname => '', :resolve_domain => '', :resolve_search => '', :expect => nil}
217217 ].each do |scenario|
218218
219219 describe "scenarios" do
220 before(:each) do
220 before(:each) do
221221 Facter::Util::Resolution.stubs(:exec).with("hostname -f").returns(scenario[:hostname])
222222 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").returns(scenario[:dnsdomainname])
223223 @mock_file = mock()
237237 Facter.fact(:domain).value.should == scenario[:expect]
238238 end
239239 end
240 end
240 end
241241 end
242242 end
243243 end