diff options
author | Tim Smith <tsmith84@gmail.com> | 2021-04-09 10:18:43 -0700 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2021-04-09 10:31:17 -0700 |
commit | 7ed583a40d4a6646f9f9c55f164060ad766ab5af (patch) | |
tree | ba610bbfeb363219469f3463929d8d11f8c0cbf1 | |
parent | 1dfbd15afb830f8aa8959ea9e79ed2648d3c2997 (diff) | |
download | ohai-macos_hostname.tar.gz |
Use scutil to get hostname and computernamemacos_hostname
Fetch the hostname and computername in a more macOS way.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/hostname.rb | 6 | ||||
-rw-r--r-- | spec/unit/plugins/darwin/hostname_spec.rb | 9 | ||||
-rw-r--r-- | spec/unit/plugins/freebsd/hostname_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/plugins/linux/hostname_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/plugins/netbsd/hostname_spec.rb | 5 | ||||
-rw-r--r-- | spec/unit/plugins/openbsd/hostname_spec.rb | 5 |
6 files changed, 11 insertions, 18 deletions
diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb index 7940ada2..87a463ef 100644 --- a/lib/ohai/plugins/hostname.rb +++ b/lib/ohai/plugins/hostname.rb @@ -42,7 +42,7 @@ Ohai.plugin(:Hostname) do end # forward and reverse lookup to canonicalize FQDN (hostname -f equivalent) - # this is ipv6-safe, works on ruby 1.8.7+ + # this is ipv6-safe def resolve_fqdn require "socket" unless defined?(Socket) require "ipaddr" unless defined?(IPAddr) @@ -96,8 +96,8 @@ Ohai.plugin(:Hostname) do end collect_data(:darwin) do - hostname from_cmd("hostname -s") - machinename from_cmd("hostname") + hostname from_cmd("scutil --get HostName") + machinename from_cmd("scutil --get ComputerName") begin our_fqdn = resolve_fqdn # Sometimes... very rarely, but sometimes, 'hostname --fqdn' falsely diff --git a/spec/unit/plugins/darwin/hostname_spec.rb b/spec/unit/plugins/darwin/hostname_spec.rb index 831781a3..e16c758a 100644 --- a/spec/unit/plugins/darwin/hostname_spec.rb +++ b/spec/unit/plugins/darwin/hostname_spec.rb @@ -22,14 +22,13 @@ describe Ohai::System, "Darwin hostname plugin" do before do @plugin = get_plugin("hostname") allow(@plugin).to receive(:collect_os).and_return(:darwin) - allow(@plugin).to receive(:shell_out).with("hostname -s").and_return(mock_shell_out(0, "katie", "")) - allow(@plugin).to receive(:shell_out).with("hostname").and_return(mock_shell_out(0, "katie.local", "")) + allow(@plugin).to receive(:shell_out).with("scutil --get HostName").and_return(mock_shell_out(0, "katie", "")) + allow(@plugin).to receive(:shell_out).with("scutil --get ComputerName").and_return(mock_shell_out(0, "katie.local", "")) allow(@plugin).to receive(:resolve_fqdn).and_return("katie.bethell") end - it_should_check_from("darwin::hostname", "hostname", "hostname -s", "katie") - - it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") + it_should_check_from("darwin::hostname", "hostname", "scutil --get HostName", "katie") + it_should_check_from("darwin::hostname", "machinename", "scutil --get ComputerName", "katie.local") it "uses #resolve_fqdn to find the fqdn" do @plugin.run diff --git a/spec/unit/plugins/freebsd/hostname_spec.rb b/spec/unit/plugins/freebsd/hostname_spec.rb index 025ccbf9..9211c3c8 100644 --- a/spec/unit/plugins/freebsd/hostname_spec.rb +++ b/spec/unit/plugins/freebsd/hostname_spec.rb @@ -28,8 +28,6 @@ describe Ohai::System, "FreeBSD hostname plugin" do end it_should_check_from("freebsd::hostname", "hostname", "hostname -s", "katie") - it_should_check_from("freebsd::hostname", "fqdn", "hostname -f", "katie.bethell") - it_should_check_from("freebsd::hostname", "machinename", "hostname", "katie.local") end diff --git a/spec/unit/plugins/linux/hostname_spec.rb b/spec/unit/plugins/linux/hostname_spec.rb index 9bc6e86d..1700e289 100644 --- a/spec/unit/plugins/linux/hostname_spec.rb +++ b/spec/unit/plugins/linux/hostname_spec.rb @@ -28,9 +28,7 @@ describe Ohai::System, "Linux hostname plugin" do end it_should_check_from("linux::hostname", "hostname", "hostname -s", "katie") - it_should_check_from("linux::hostname", "fqdn", "hostname --fqdn", "katie.bethell") - it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") describe "when domain name is unset" do diff --git a/spec/unit/plugins/netbsd/hostname_spec.rb b/spec/unit/plugins/netbsd/hostname_spec.rb index 0a960d1a..6f09f433 100644 --- a/spec/unit/plugins/netbsd/hostname_spec.rb +++ b/spec/unit/plugins/netbsd/hostname_spec.rb @@ -27,9 +27,8 @@ describe Ohai::System, "NetBSD hostname plugin" do allow(@plugin).to receive(:resolve_fqdn).and_return("katie.bethell") end - it_should_check_from("linux::hostname", "hostname", "hostname -s", "katie") - - it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") + it_should_check_from("netbsd::hostname", "hostname", "hostname -s", "katie") + it_should_check_from("netbsd::hostname", "machinename", "hostname", "katie.local") it "uses #resolve_fqdn to find the fqdn" do @plugin.run diff --git a/spec/unit/plugins/openbsd/hostname_spec.rb b/spec/unit/plugins/openbsd/hostname_spec.rb index 0ed89787..18767b7a 100644 --- a/spec/unit/plugins/openbsd/hostname_spec.rb +++ b/spec/unit/plugins/openbsd/hostname_spec.rb @@ -27,9 +27,8 @@ describe Ohai::System, "OpenBSD hostname plugin" do allow(@plugin).to receive(:resolve_fqdn).and_return("katie.bethell") end - it_should_check_from("linux::hostname", "hostname", "hostname -s", "katie") - - it_should_check_from("linux::hostname", "machinename", "hostname", "katie.local") + it_should_check_from("openbsd::hostname", "hostname", "hostname -s", "katie") + it_should_check_from("openbsd::hostname", "machinename", "hostname", "katie.local") it "uses #resolve_fqdn to find the fqdn" do @plugin.run |