Codebase list facter / 94d0d13
Merge pull request #199 from hkenney/ticket/master/7484_domain_fact_should_handle_tld (#7484) Domain Fact should handle TLD Daniel Pittman 12 years ago
2 changed file(s) with 118 addition(s) and 87 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 if name = Facter::Util::Resolution.exec('hostname') \
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
29 # 'hostname -f' --hkenney May 9, 2012
30 hostname_command = 'hostname'
31 can_do_hostname_f = Regexp.union /Linux/i, /FreeBSD/i, /Darwin/i
32 hostname_command = 'hostname -f' if Facter.value(:kernel) =~ can_do_hostname_f
33
34
35 if name = Facter::Util::Resolution.exec(hostname_command) \
2636 and name =~ /.*?\.(.+$)/
2737
2838 $1
33
44 describe "Domain name facts" do
55
6 describe "on linux" do
7 before do
8 Facter.fact(:kernel).stubs(:value).returns("Linux")
9 FileTest.stubs(:exists?).with("/etc/resolv.conf").returns(true)
10 end
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|
1112
12 it "should use the hostname binary" do
13 Facter::Util::Resolution.expects(:exec).with("hostname").returns "test.example.com"
14 Facter.fact(:domain).value.should == "example.com"
15 end
13 describe "on #{key}" do
14 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
23 it "should use the hostname binary" do
24 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "test.example.com"
25 Facter.fact(:domain).value.should == "example.com"
26 end
27
28 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")
31 Facter.fact(:domain).value.should == "example.com"
32 end
33
34
35 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 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
59 describe "when using /etc/resolv.conf" do
60 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
67 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 Facter.fact(:domain).value.should == 'example.com'
75 end
1676
17 it "should fall back to the dnsdomainname binary" do
18 Facter::Util::Resolution.expects(:exec).with("hostname").returns("myhost")
19 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").returns("example.com")
20 Facter.fact(:domain).value.should == "example.com"
21 end
77 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)
83 Facter.fact(:domain).value.should == 'example.org'
84 end
85
86 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)
91 Facter.fact(:domain).value.should == 'example.org'
92 end
2293
23
24 it "should fall back to /etc/resolv.conf" do
25 Facter::Util::Resolution.expects(:exec).with("hostname").at_least_once.returns("myhost")
26 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").at_least_once.returns("")
27 File.expects(:open).with('/etc/resolv.conf').at_least_once
28 Facter.fact(:domain).value
29 end
30
31 it "should attempt to resolve facts in a specific order" do
32 seq = sequence('domain')
33 Facter::Util::Resolution.stubs(:exec).with("hostname").in_sequence(seq).at_least_once
34 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").in_sequence(seq).at_least_once
35 File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
36 Facter.fact(:domain).value
37 end
38
39 describe "when using /etc/resolv.conf" do
40 before do
41 Facter::Util::Resolution.stubs(:exec).with("hostname")
42 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname")
43 @mock_file = mock()
44 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
45 end
46
47 it "should use the domain field over the search field" do
48 lines = [
49 "nameserver 4.2.2.1",
50 "search example.org",
51 "domain example.com",
52 ]
53 @mock_file.expects(:each).multiple_yields(*lines)
54 Facter.fact(:domain).value.should == 'example.com'
55 end
56
57 it "should fall back to the search field" do
58 lines = [
59 "nameserver 4.2.2.1",
60 "search example.org",
61 ]
62 @mock_file.expects(:each).multiple_yields(*lines)
63 Facter.fact(:domain).value.should == 'example.org'
64 end
65
66 it "should use the first domain in the search field" do
67 lines = [
68 "search example.org example.net",
69 ]
70 @mock_file.expects(:each).multiple_yields(*lines)
71 Facter.fact(:domain).value.should == 'example.org'
72 end
73
74 # Test permutations of domain and search
75 [
76 ["domain domain", "domain"],
77 ["domain search", "search"],
78 ["search domain", "domain"],
79 ["search search", "search"],
80 ["search domain notdomain", "domain"],
81 [["#search notdomain","search search"], "search"],
82 [["# search notdomain","search search"], "search"],
83 [["#domain notdomain","domain domain"], "domain"],
84 [["# domain notdomain","domain domain"], "domain"],
85 ].each do |tuple|
86 field = tuple[0]
87 expect = tuple[1]
88 it "should return #{expect} from \"#{field}\"" do
89 lines = [
90 field
91 ].flatten
92 @mock_file.expects(:each).multiple_yields(*lines)
93 Facter.fact(:domain).value.should == expect
94 # Test permutations of domain and search
95 [
96 ["domain domain", "domain"],
97 ["domain search", "search"],
98 ["search domain", "domain"],
99 ["search search", "search"],
100 ["search domain notdomain", "domain"],
101 [["#search notdomain","search search"], "search"],
102 [["# search notdomain","search search"], "search"],
103 [["#domain notdomain","domain domain"], "domain"],
104 [["# domain notdomain","domain domain"], "domain"],
105 ].each do |tuple|
106 field = tuple[0]
107 expect = tuple[1]
108 it "should return #{expect} from \"#{field}\"" do
109 lines = [
110 field
111 ].flatten
112 @mock_file.expects(:each).multiple_yields(*lines)
113 Facter.fact(:domain).value.should == expect
114 end
94115 end
95116 end
96117 end
97 end
118 end
98119
99120 describe "on Windows" do
100121 before(:each) do