summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-11-28 10:17:17 -0800
committerGitHub <noreply@github.com>2018-11-28 10:17:17 -0800
commit6cd31b3d820293878f4e6e6cb47d4d3b4bb65d94 (patch)
treeba52d7f30c7d248d15896b96f9cb05133f7a2eea
parentaf633604e28442884b131dd51a09738251d5f2b6 (diff)
parent836a50ab355c3fc329d52fb129cb86b0fd36b612 (diff)
downloadohai-6cd31b3d820293878f4e6e6cb47d4d3b4bb65d94.tar.gz
Merge pull request #1310 from MsysTechnologiesllc/vijay/MSYS-933_Adds_specs_for_fix_FQDN_is_being_set_as_the_machine_name_instead_of_FQDN
Windows: Fix for fqdn is being set as the machine name instead of fqdn
-rw-r--r--lib/ohai/plugins/hostname.rb9
-rw-r--r--spec/unit/plugins/hostname_spec.rb67
2 files changed, 75 insertions, 1 deletions
diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb
index 0f103c5d..348158ed 100644
--- a/lib/ohai/plugins/hostname.rb
+++ b/lib/ohai/plugins/hostname.rb
@@ -175,7 +175,14 @@ Ohai.plugin(:Hostname) do
else
# host is not in dns. optionally use:
# C:\WINDOWS\system32\drivers\etc\hosts
- fqdn Socket.gethostbyaddr(info.last).first
+ info[3..info.length].reverse.each do |addr|
+ hostent = Socket.gethostbyaddr(addr)
+ if hostent.first =~ /.+?\.(.*)/
+ fqdn hostent.first
+ break
+ end
+ end
+ fqdn info.first unless fqdn
end
domain collect_domain
end
diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb
index 87719129..c4523434 100644
--- a/spec/unit/plugins/hostname_spec.rb
+++ b/spec/unit/plugins/hostname_spec.rb
@@ -17,6 +17,8 @@
#
require_relative "../../spec_helper.rb"
+require "wmi-lite/wmi"
+require "socket"
describe Ohai::System, "hostname plugin" do
before(:each) do
@@ -88,3 +90,68 @@ describe Ohai::System, "hostname plugin" do
end
end
end
+
+describe Ohai::System, "hostname plugin for windows", :windows_only do
+ let(:success) { true }
+
+ let(:host) do
+ {
+ "name" => "local",
+ "dnshostname" => "local",
+ }
+ end
+
+ let(:info) do
+ [
+ "local",
+ [],
+ 23,
+ "address1",
+ "address2",
+ "address3",
+ "address4"
+ ]
+ end
+
+ let(:local_hostent) do
+ [
+ "local",
+ [],
+ 23,
+ "address"
+ ]
+ end
+
+ let(:fqdn_hostent) do
+ [
+ "local.dx.internal.cloudapp.net",
+ [],
+ 23,
+ "address"
+ ]
+ end
+
+ before(:each) do
+ @plugin = get_plugin("hostname")
+ allow(WmiLite::Wmi).to receive(:new).and_return(success)
+ allow(success).to receive(:first_of).with("Win32_ComputerSystem").and_return(host)
+ allow(Socket).to receive(:gethostname).and_return("local")
+ allow(Socket).to receive(:gethostbyname).with(anything()).and_return(info)
+ end
+
+ context "when hostname is not set for the machine" do
+ it "should return short machine name" do
+ allow(Socket).to receive(:gethostbyaddr).with(anything()).and_return(local_hostent)
+ @plugin.run
+ expect(@plugin[:fqdn]).to eq("local")
+ end
+ end
+
+ context "when hostname is set for the machine" do
+ it "should return the fqdn of the machine" do
+ allow(Socket).to receive(:gethostbyaddr).with(anything()).and_return(fqdn_hostent)
+ @plugin.run
+ expect(@plugin[:fqdn]).to eq("local.dx.internal.cloudapp.net")
+ end
+ end
+end