diff options
author | lamont-granquist <lamont@scriptkiddie.org> | 2014-03-05 15:57:06 -0800 |
---|---|---|
committer | lamont-granquist <lamont@scriptkiddie.org> | 2014-03-05 15:57:06 -0800 |
commit | 202facd4da034d34da3a34c2d33e45b6681016cb (patch) | |
tree | 0e7b5f611fc819329c3fb040a24320d4fec153bf | |
parent | ad6abcc84a0bd9bd47b636f7004588510615c188 (diff) | |
parent | 7bd5a1248df8245695da5eda7522977f57646d18 (diff) | |
download | chef-202facd4da034d34da3a34c2d33e45b6681016cb.tar.gz |
Merge pull request #1216 from opscode/lcg/add-machinename
add ohai[:machinename]
-rw-r--r-- | lib/chef/client.rb | 7 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 8 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 37 |
3 files changed, 46 insertions, 6 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb index aa03f043c2..ac814333b4 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -293,13 +293,10 @@ class Chef end def node_name - name = Chef::Config[:node_name] || ohai[:fqdn] || ohai[:hostname] + name = Chef::Config[:node_name] || ohai[:fqdn] || ohai[:machinename] || ohai[:hostname] Chef::Config[:node_name] = name - unless name - msg = "Unable to determine node name: configure node_name or configure the system's hostname and fqdn" - raise Chef::Exceptions::CannotDetermineNodeName, msg - end + raise Chef::Exceptions::CannotDetermineNodeName unless name # node names > 90 bytes only work with authentication protocol >= 1.1 # see discussion in config.rb. diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 3877e3d17a..f976263188 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -50,7 +50,13 @@ class Chef class Override < RuntimeError; end class UnsupportedAction < RuntimeError; end class MissingLibrary < RuntimeError; end - class CannotDetermineNodeName < RuntimeError; end + + class CannotDetermineNodeName < RuntimeError + def initialize + super "Unable to determine node name: configure node_name or configure the system's hostname and fqdn" + end + end + class User < RuntimeError; end class Group < RuntimeError; end class Link < RuntimeError; end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index e4143d7653..9688cce2f4 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -27,11 +27,13 @@ require 'rbconfig' describe Chef::Client do let(:hostname) { "hostname" } + let(:machinename) { "machinename.example.org" } let(:fqdn) { "hostname.example.org" } let(:ohai_data) do { :fqdn => fqdn, :hostname => hostname, + :machinename => machinename, :platform => 'example-platform', :platform_version => 'example-platform-1.0', :data => {} @@ -560,5 +562,40 @@ describe Chef::Client do end end + describe "setting node name" do + context "when machinename, hostname and fqdn are all set" do + it "favors the fqdn" do + expect(client.node_name).to eql(fqdn) + end + end + + context "when fqdn is missing" do + # ohai 7 should always have machinename == return of hostname + let(:fqdn) { nil } + it "favors the machinename" do + expect(client.node_name).to eql(machinename) + end + end + + context "when fqdn and machinename are missing" do + # ohai 6 will not have machinename, return the short hostname + let(:fqdn) { nil } + let(:machinename) { nil } + it "falls back to hostname" do + expect(client.node_name).to eql(hostname) + end + end + + context "when they're all missing" do + let(:machinename) { nil } + let(:hostname) { nil } + let(:fqdn) { nil } + + it "throws an exception" do + expect { client.node_name }.to raise_error(Chef::Exceptions::CannotDetermineNodeName) + end + end + + end end |