summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvijaymmali1990 <vijay.mali@msystechnologies.com>2018-11-28 16:52:17 +0530
committervijaymmali1990 <vijay.mali@msystechnologies.com>2018-11-28 17:21:47 +0530
commit836a50ab355c3fc329d52fb129cb86b0fd36b612 (patch)
treeba52d7f30c7d248d15896b96f9cb05133f7a2eea
parent8ef11a7b494b16446f324af9903ca7c729db03f2 (diff)
downloadohai-836a50ab355c3fc329d52fb129cb86b0fd36b612.tar.gz
MSYS-933 Fixes FQDN is being set as machine name instead of FQDN
Signed-off-by: vijaymmali1990 <vijay.mali@msystechnologies.com>
-rw-r--r--lib/ohai/plugins/hostname.rb10
-rw-r--r--spec/unit/plugins/hostname_spec.rb67
2 files changed, 70 insertions, 7 deletions
diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb
index 15d19091..348158ed 100644
--- a/lib/ohai/plugins/hostname.rb
+++ b/lib/ohai/plugins/hostname.rb
@@ -173,20 +173,16 @@ Ohai.plugin(:Hostname) do
if info.first =~ /.+?\.(.*)/
fqdn info.first
else
- #host is not in dns. optionally use:
- #C:\WINDOWS\system32\drivers\etc\hosts
- found_fqdn = false
+ # host is not in dns. optionally use:
+ # C:\WINDOWS\system32\drivers\etc\hosts
info[3..info.length].reverse.each do |addr|
hostent = Socket.gethostbyaddr(addr)
if hostent.first =~ /.+?\.(.*)/
fqdn hostent.first
- found_fqdn = true
break
end
end
- if !found_fqdn
- fqdn info.first
- 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