diff options
author | Phil Dibowitz <phild@fb.com> | 2014-03-10 14:45:09 -0700 |
---|---|---|
committer | Phil Dibowitz <phild@fb.com> | 2014-03-13 14:24:09 -0700 |
commit | be519c2052cf5cf3c635aaf4e0e8a324da07db58 (patch) | |
tree | bd4a952ed7f079988d726318763c69d3556f5cfc | |
parent | 37854159e2cf80dc7b04c4b499e42ced7313fce9 (diff) | |
download | ohai-be519c2052cf5cf3c635aaf4e0e8a324da07db58.tar.gz |
Fix `hostname --fqdn` stupidity on linux
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | CONTRIBUTIONS.md | 1 | ||||
-rw-r--r-- | lib/ohai/plugins/linux/hostname.rb | 18 | ||||
-rw-r--r-- | spec/unit/plugins/linux/hostname_spec.rb | 13 |
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 |