From c38ba0de1d72df79476254d0ae7b91db98d490fd Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 16 Apr 2020 15:48:38 -0700 Subject: Merge ohai resource / provider into a single resource Simplify this resource and squash some files Signed-off-by: Tim Smith --- lib/chef/provider/ohai.rb | 45 ---------------------- lib/chef/providers.rb | 1 - lib/chef/resource/ohai.rb | 20 +++++++++- spec/unit/provider/ohai_spec.rb | 84 ----------------------------------------- spec/unit/resource/ohai_spec.rb | 61 +++++++++++++++++++++++++++++- 5 files changed, 77 insertions(+), 134 deletions(-) delete mode 100644 lib/chef/provider/ohai.rb delete mode 100644 spec/unit/provider/ohai_spec.rb diff --git a/lib/chef/provider/ohai.rb b/lib/chef/provider/ohai.rb deleted file mode 100644 index 9cd67b764c..0000000000 --- a/lib/chef/provider/ohai.rb +++ /dev/null @@ -1,45 +0,0 @@ -# -# Author:: Michael Leianrtas () -# Copyright:: Copyright 2010-2016, Michael Leinartas -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "ohai" unless defined?(Ohai::System) - -class Chef - class Provider - class Ohai < Chef::Provider - provides :ohai - - def load_current_resource - true - end - - action :reload do - converge_by("re-run ohai and merge results into node attributes") do - ohai = ::Ohai::System.new - - # If new_resource.plugin is nil, ohai will reload all the plugins - # Otherwise it will only reload the specified plugin - # Note that any changes to plugins, or new plugins placed on - # the path are picked up by ohai. - ohai.all_plugins new_resource.plugin - node.automatic_attrs.merge! ohai.data - logger.info("#{new_resource} reloaded") - end - end - end - end -end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 104e200f55..b085160c0e 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -32,7 +32,6 @@ require_relative "provider/http_request" require_relative "provider/ifconfig" require_relative "provider/launchd" require_relative "provider/link" -require_relative "provider/ohai" require_relative "provider/mount" require_relative "provider/noop" require_relative "provider/package" diff --git a/lib/chef/resource/ohai.rb b/lib/chef/resource/ohai.rb index d48743b9ac..df106406ce 100644 --- a/lib/chef/resource/ohai.rb +++ b/lib/chef/resource/ohai.rb @@ -2,6 +2,7 @@ # Author:: Michael Leinartas () # Author:: Tyler Cloke () # Copyright:: Copyright 2010-2016, Michael Leinartas +# Copyright:: Copyright (c) Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,8 +33,23 @@ class Chef property :plugin, String, description: "The name of an Ohai plugin to be reloaded. If this property is not specified, #{Chef::Dist::PRODUCT} will reload all plugins." - default_action :reload - allowed_actions :reload + def load_current_resource + true + end + + action :reload do + converge_by("re-run ohai and merge results into node attributes") do + ohai = ::Ohai::System.new + + # If new_resource.plugin is nil, ohai will reload all the plugins + # Otherwise it will only reload the specified plugin + # Note that any changes to plugins, or new plugins placed on + # the path are picked up by ohai. + ohai.all_plugins new_resource.plugin + node.automatic_attrs.merge! ohai.data + logger.info("#{new_resource} reloaded") + end + end end end end diff --git a/spec/unit/provider/ohai_spec.rb b/spec/unit/provider/ohai_spec.rb deleted file mode 100644 index 11f11e3373..0000000000 --- a/spec/unit/provider/ohai_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -# -# Author:: Michael Leinartas () -# Copyright:: Copyright 2010-2016, Michael Leinartas -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require "spec_helper" - -require "chef/run_context" - -describe Chef::Provider::Ohai do - before(:each) do - # Copied from client_spec - @fqdn = "hostname.domainname" - @hostname = "hostname" - @platform = "example-platform" - @platform_version = "example-platform" - Chef::Config[:node_name] = @fqdn - mock_ohai = { - fqdn: @fqdn, - hostname: @hostname, - platform: @platform, - platform_version: @platform_version, - data: { - origdata: "somevalue", - }, - data2: { - origdata: "somevalue", - newdata: "somevalue", - }, - } - allow(mock_ohai).to receive(:all_plugins).and_return(true) - allow(mock_ohai).to receive(:data).and_return(mock_ohai[:data], - mock_ohai[:data2]) - allow(Ohai::System).to receive(:new).and_return(mock_ohai) - allow(Chef::Platform).to receive(:find_platform_and_version).and_return({ "platform" => @platform, - "platform_version" => @platform_version }) - # Fake node with a dummy save - @node = Chef::Node.new - @node.name(@fqdn) - allow(@node).to receive(:save).and_return(@node) - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, {}, @events) - @new_resource = Chef::Resource::Ohai.new("ohai_reload") - ohai = Ohai::System.new - ohai.all_plugins - @node.consume_external_attrs(ohai.data, {}) - - @provider = Chef::Provider::Ohai.new(@new_resource, @run_context) - end - - describe "when reloading ohai" do - before do - @node.automatic_attrs[:origdata] = "somevalue" - end - - it "applies updated ohai data to the node" do - expect(@node[:origdata]).to eq("somevalue") - expect(@node[:newdata]).to be_nil - @provider.run_action(:reload) - expect(@node[:origdata]).to eq("somevalue") - expect(@node[:newdata]).to eq("somevalue") - end - - it "should reload a specific plugin and cause node to pick up new values" do - @new_resource.plugin "someplugin" - @provider.run_action(:reload) - expect(@node[:origdata]).to eq("somevalue") - expect(@node[:newdata]).to eq("somevalue") - end - end -end diff --git a/spec/unit/resource/ohai_spec.rb b/spec/unit/resource/ohai_spec.rb index d0a8f7a77e..c293e854d3 100644 --- a/spec/unit/resource/ohai_spec.rb +++ b/spec/unit/resource/ohai_spec.rb @@ -19,8 +19,11 @@ require "spec_helper" describe Chef::Resource::Ohai do - - let(:resource) { Chef::Resource::Ohai.new("fakey_fakerton") } + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:resource) { Chef::Resource::Ohai.new("fakey_fakerton", run_context) } + let(:provider) { resource.provider_for_action(:reload) } it "has a resource name of :ohai" do expect(resource.resource_name).to eql(:ohai) @@ -55,4 +58,58 @@ describe Chef::Resource::Ohai do end end + describe "reload action" do + before(:each) do + # Copied from client_spec + @fqdn = "hostname.domainname" + @hostname = "hostname" + @platform = "example-platform" + @platform_version = "example-platform" + Chef::Config[:node_name] = @fqdn + mock_ohai = { + fqdn: @fqdn, + hostname: @hostname, + platform: @platform, + platform_version: @platform_version, + data: { + origdata: "somevalue", + }, + data2: { + origdata: "somevalue", + newdata: "somevalue", + }, + } + allow(mock_ohai).to receive(:all_plugins).and_return(true) + allow(mock_ohai).to receive(:data).and_return(mock_ohai[:data], + mock_ohai[:data2]) + allow(Ohai::System).to receive(:new).and_return(mock_ohai) + allow(Chef::Platform).to receive(:find_platform_and_version).and_return({ "platform" => @platform, + "platform_version" => @platform_version }) + # Fake node with a dummy save + node.name(@fqdn) + allow(node).to receive(:save).and_return(node) + @events = Chef::EventDispatch::Dispatcher.new + @run_context = Chef::RunContext.new(node, {}, @events) + @new_resource = Chef::Resource::Ohai.new("ohai_reload") + ohai = Ohai::System.new + ohai.all_plugins + node.consume_external_attrs(ohai.data, {}) + node.automatic_attrs[:origdata] = "somevalue" + end + + it "applies updated ohai data to the node" do + expect(node[:origdata]).to eq("somevalue") + expect(node[:newdata]).to be_nil + provider.run_action(:reload) + expect(node[:origdata]).to eq("somevalue") + expect(node[:newdata]).to eq("somevalue") + end + + it "supports reloading a specific plugin and causes node to pick up new values" do + resource.plugin "someplugin" + provider.run_action(:reload) + expect(node[:origdata]).to eq("somevalue") + expect(node[:newdata]).to eq("somevalue") + end + end end -- cgit v1.2.1