summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Dibowitz <phil@ipom.com>2014-03-14 13:12:54 -0700
committerPhil Dibowitz <phil@ipom.com>2014-03-14 13:12:54 -0700
commit5b8f3754bc4ee6a863abe1a79d7f4326c11afedd (patch)
treebd4a952ed7f079988d726318763c69d3556f5cfc
parent37854159e2cf80dc7b04c4b499e42ced7313fce9 (diff)
parentbe519c2052cf5cf3c635aaf4e0e8a324da07db58 (diff)
downloadohai-5b8f3754bc4ee6a863abe1a79d7f4326c11afedd.tar.gz
Merge pull request #296 from jaymzh/hostnamefix_6
Fix `hostname --fqdn` stupidity on linux -- ohai6
-rw-r--r--CHANGELOG.md2
-rw-r--r--CONTRIBUTIONS.md1
-rw-r--r--lib/ohai/plugins/linux/hostname.rb18
-rw-r--r--spec/unit/plugins/linux/hostname_spec.rb13
4 files changed, 29 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6b411c09..6cda51ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,7 @@
## Unreleased
-*
+* Work around libc bug in `hostname --fqdn`
## Last Release: 7.0.0.rc.0 (01/20/2014)
diff --git a/CONTRIBUTIONS.md b/CONTRIBUTIONS.md
index 59e801c0..18bb9079 100644
--- a/CONTRIBUTIONS.md
+++ b/CONTRIBUTIONS.md
@@ -5,3 +5,4 @@ Example Contribution:
* **kalistec**: Improved file resource greatly.
-->
# OHAI Contributions:
+* **jaymzh**: Work around libc bug in `hostname --fqdn`
diff --git a/lib/ohai/plugins/linux/hostname.rb b/lib/ohai/plugins/linux/hostname.rb
index 896ad821..2565a18c 100644
--- a/lib/ohai/plugins/linux/hostname.rb
+++ b/lib/ohai/plugins/linux/hostname.rb
@@ -20,7 +20,21 @@ provides "hostname", "fqdn"
hostname from("hostname -s")
begin
- fqdn from("hostname --fqdn")
+ ourfqdn = from("hostname --fqdn")
+ # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely
+ # returns a blank string. WTF.
+ if ourfqdn.nil? || ourfqdn.empty?
+ Ohai::Log.debug("hostname --fqdn returned an empty string, retrying " +
+ "once.")
+ ourfqdn = from("hostname --fqdn")
+ end
+
+ if ourfqdn.nil? || ourfqdn.empty?
+ Ohai::Log.debug("hostname --fqdn returned an empty string twice and " +
+ "will not be set.")
+ else
+ fqdn ourfqdn
+ end
rescue
- Ohai::Log.debug("hostname -f returned an error, probably no domain is set")
+ Ohai::Log.debug("hostname --fqdn returned an error, probably no domain set")
end
diff --git a/spec/unit/plugins/linux/hostname_spec.rb b/spec/unit/plugins/linux/hostname_spec.rb
index 33ab866b..7a7ab83c 100644
--- a/spec/unit/plugins/linux/hostname_spec.rb
+++ b/spec/unit/plugins/linux/hostname_spec.rb
@@ -45,8 +45,17 @@ describe Ohai::System, "Linux hostname plugin" do
@ohai._require_plugin("linux::hostname")
@ohai.fqdn.should == nil
end
-
end
-
+
+ describe "when hostname --fqdn is emtpy" do
+ before(:each) do
+ @ohai.stub!(:from).with("hostname --fqdn").
+ and_return("", "katie.bethell")
+ end
+ it "should call it twice" do
+ @ohai._require_plugin("linux::hostname")
+ @ohai.fqdn.should == "katie.bethell"
+ end
+ end
end