[Pkg-puppet-devel] [facter] 33/46: (#20938) Ignore stderr from domain commands
Stig Sandbeck Mathisen
ssm at debian.org
Sun Sep 1 10:47:32 UTC 2013
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository facter.
commit 120403340aca38831bbdae4c64d6f8fc151fca52
Author: Andrew Parker <andy at puppetlabs.com>
Date: Tue May 28 15:52:14 2013 -0700
(#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).
---
lib/facter/domain.rb | 12 +++++++++---
spec/unit/domain_spec.rb | 24 ++++++++++++------------
2 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/lib/facter/domain.rb b/lib/facter/domain.rb
index 2bfe68b..5d176e6 100644
--- a/lib/facter/domain.rb
+++ b/lib/facter/domain.rb
@@ -28,15 +28,21 @@ Facter.add(:domain) do
# On good OS, 'hostname -f' will return the FQDN which is preferable
# Due to dangerous behavior of 'hostname -f' on old OS, we will explicitly opt-in
# 'hostname -f' --hkenney May 9, 2012
- hostname_command = 'hostname'
+ basic_hostname = 'hostname 2> /dev/null'
+ full_hostname = 'hostname -f 2> /dev/null'
can_do_hostname_f = Regexp.union /Linux/i, /FreeBSD/i, /Darwin/i
- hostname_command = 'hostname -f' if Facter.value(:kernel) =~ can_do_hostname_f
+
+ hostname_command = if Facter.value(:kernel) =~ can_do_hostname_f
+ full_hostname
+ else
+ basic_hostname
+ end
if name = Facter::Util::Resolution.exec(hostname_command) \
and name =~ /.*?\.(.+$)/
return_value = $1
- elsif domain = Facter::Util::Resolution.exec('dnsdomainname') \
+ elsif domain = Facter::Util::Resolution.exec('dnsdomainname 2> /dev/null') \
and domain =~ /.+/
return_value = domain
diff --git a/spec/unit/domain_spec.rb b/spec/unit/domain_spec.rb
index 27fb706..14016d8 100755
--- a/spec/unit/domain_spec.rb
+++ b/spec/unit/domain_spec.rb
@@ -4,11 +4,11 @@ require 'spec_helper'
describe "Domain name facts" do
- { :linux => {:kernel => "Linux", :hostname_command => "hostname -f"},
- :solaris => {:kernel => "SunOS", :hostname_command => "hostname"},
- :darwin => {:kernel => "Darwin", :hostname_command => "hostname -f"},
- :freebsd => {:kernel => "FreeBSD", :hostname_command => "hostname -f"},
- :hpux => {:kernel => "HP-UX", :hostname_command => "hostname"},
+ { :linux => {:kernel => "Linux", :hostname_command => "hostname -f 2> /dev/null"},
+ :solaris => {:kernel => "SunOS", :hostname_command => "hostname 2> /dev/null"},
+ :darwin => {:kernel => "Darwin", :hostname_command => "hostname -f 2> /dev/null"},
+ :freebsd => {:kernel => "FreeBSD", :hostname_command => "hostname -f 2> /dev/null"},
+ :hpux => {:kernel => "HP-UX", :hostname_command => "hostname 2> /dev/null"},
}.each do |key, nested_hash|
describe "on #{key}" do
@@ -28,14 +28,14 @@ describe "Domain name facts" do
it "should fall back to the dnsdomainname binary" do
Facter::Util::Resolution.expects(:exec).with(hostname_command).returns("myhost")
- Facter::Util::Resolution.expects(:exec).with("dnsdomainname").returns("example.com")
+ Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").returns("example.com")
Facter.fact(:domain).value.should == "example.com"
end
it "should fall back to /etc/resolv.conf" do
Facter::Util::Resolution.expects(:exec).with(hostname_command).at_least_once.returns("myhost")
- Facter::Util::Resolution.expects(:exec).with("dnsdomainname").at_least_once.returns("")
+ Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").at_least_once.returns("")
File.expects(:open).with('/etc/resolv.conf').at_least_once
Facter.fact(:domain).value
end
@@ -43,7 +43,7 @@ describe "Domain name facts" do
it "should attempt to resolve facts in a specific order" do
seq = sequence('domain')
Facter::Util::Resolution.stubs(:exec).with(hostname_command).in_sequence(seq).at_least_once
- Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").in_sequence(seq).at_least_once
+ Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").in_sequence(seq).at_least_once
File.expects(:open).with('/etc/resolv.conf').in_sequence(seq).at_least_once
Facter.fact(:domain).value
end
@@ -51,7 +51,7 @@ describe "Domain name facts" do
describe "Top level domain" do
it "should find the domain name" do
Facter::Util::Resolution.expects(:exec).with(hostname_command).returns "ns01.tld"
- Facter::Util::Resolution.expects(:exec).with("dnsdomainname").never
+ Facter::Util::Resolution.expects(:exec).with("dnsdomainname 2> /dev/null").never
File.expects(:exists?).with('/etc/resolv.conf').never
Facter.fact(:domain).value.should == "tld"
end
@@ -60,7 +60,7 @@ describe "Domain name facts" do
describe "when using /etc/resolv.conf" do
before do
Facter::Util::Resolution.stubs(:exec).with(hostname_command)
- Facter::Util::Resolution.stubs(:exec).with("dnsdomainname")
+ Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null")
@mock_file = mock()
File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
end
@@ -219,8 +219,8 @@ describe "Domain name facts" do
describe "scenarios" do
before(:each) do
- Facter::Util::Resolution.stubs(:exec).with("hostname -f").returns(scenario[:hostname])
- Facter::Util::Resolution.stubs(:exec).with("dnsdomainname").returns(scenario[:dnsdomainname])
+ Facter::Util::Resolution.stubs(:exec).with("hostname -f 2> /dev/null").returns(scenario[:hostname])
+ Facter::Util::Resolution.stubs(:exec).with("dnsdomainname 2> /dev/null").returns(scenario[:dnsdomainname])
@mock_file = mock()
File.stubs(:open).with("/etc/resolv.conf").yields(@mock_file)
lines = [
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git
More information about the Pkg-puppet-devel
mailing list