Codebase list facter / 1204033
(#20938) Ignore stderr from domain commands Previously, when the hostname of a system is in a messed up stated, the hostname and dnsdomainname commands would output warning messages to stderr. The facter command execution system does not capture this, and so the stderr output ends up making it all the way out to the caller and likely to the user, as is the case for puppet when using facter. This commit takes the simple approach of redirecting the error output to /dev/null, which should be sufficient for the platforms that use these commands (non-windows). Andrew Parker 10 years ago
2 changed file(s) with 21 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
2727 # On good OS, 'hostname -f' will return the FQDN which is preferable
2828 # Due to dangerous behavior of 'hostname -f' on old OS, we will explicitly opt-in
2929 # 'hostname -f' --hkenney May 9, 2012
30 hostname_command = 'hostname'
30 basic_hostname = 'hostname 2> /dev/null'
31 full_hostname = 'hostname -f 2> /dev/null'
3132 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 hostname_command = if Facter.value(:kernel) =~ can_do_hostname_f
35 full_hostname
36 else
37 basic_hostname
38 end
3339
3440 if name = Facter::Util::Resolution.exec(hostname_command) \
3541 and name =~ /.*?\.(.+$)/
3642
3743 return_value = $1
38 elsif domain = Facter::Util::Resolution.exec('dnsdomainname') \
44 elsif domain = Facter::Util::Resolution.exec('dnsdomainname 2> /dev/null') \
3945 and domain =~ /.+/
4046
4147 return_value = domain
33
44 describe "Domain name facts" do
55
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"},
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"},
1111 }.each do |key, nested_hash|
1212
1313 describe "on #{key}" do
2727
2828 it "should fall back to the dnsdomainname binary" do
2929 Facter::Util::Resolution.expects(:exec).with(hostname_command).returns("myhost")
30 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").returns("example.com")
30 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").returns("example.com")
3131 Facter.fact(:domain).value.should == "example.com"
3232 end
3333
3434
3535 it "should fall back to /etc/resolv.conf" do
3636 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("")
37 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").at_least_once.returns("")
3838 File.expects(:open).with('/etc/resolv.conf').at_least_once
3939 Facter.fact(:domain).value
4040 end
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
45 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").in_sequence(seq).at_least_once
45 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").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
5050 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"
53 Facter::Util::Resolution.expects(:exec).with("dnsdomainname").never
53 Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").never
5454 File.expects(:exists?).with('/etc/resolv.conf').never
5555 Facter.fact(:domain).value.should == "tld"
5656 end
5959 describe "when using /etc/resolv.conf" do
6060 before do
6161 Facter::Util::Resolution.stubs(:exec).with(hostname_command)
62 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname")
62 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null")
6363 @mock_file = mock()
6464 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
6565 end
218218
219219 describe "scenarios" do
220220 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])
221 Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname])
222 Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").returns(scenario[:dnsdomainname])
223223 @mock_file = mock()
224224 File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
225225 lines = [