From aa2241ab17bc131c80f358f87006099fb108fb04 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Mon, 21 Mar 2016 11:54:04 -0700 Subject: allow use_inline_resources for core chef providers * removes the DSL from InlineResources class * ActionClass is now responsible for mixing the DSL into action classes * apt_update provider converted over to use new syntax - does not need to mixin DeclareResource DSL - declares use_inline_resources - uses declare_resource instead of build_resource - converts def action_stuff to action :stuff - uses an execute resource instead of shell_out! - does not need the converge_by to update the action --- lib/chef/provider.rb | 3 --- lib/chef/provider/apt_update.rb | 25 +++++++++++-------------- lib/chef/resource.rb | 6 +++++- lib/chef/resource/action_class.rb | 3 +++ spec/unit/lwrp_spec.rb | 2 +- spec/unit/provider/apt_update_spec.rb | 20 ++++++++++---------- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index cef95a4db8..ebabb7b9eb 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -390,9 +390,6 @@ class Chef end end end - - require "chef/dsl/recipe" - include Chef::DSL::Recipe end protected diff --git a/lib/chef/provider/apt_update.rb b/lib/chef/provider/apt_update.rb index 449b298804..542d273339 100644 --- a/lib/chef/provider/apt_update.rb +++ b/lib/chef/provider/apt_update.rb @@ -22,7 +22,7 @@ require "chef/dsl/declare_resource" class Chef class Provider class AptUpdate < Chef::Provider - include Chef::DSL::DeclareResource + use_inline_resources provides :apt_update, os: "linux" @@ -36,18 +36,14 @@ class Chef def load_current_resource end - def action_periodic + action :periodic do if !apt_up_to_date? - converge_by "update new lists of packages" do - do_update - end + do_update end end - def action_update - converge_by "force update new lists of packages" do - do_update - end + action :update do + do_update end private @@ -62,16 +58,17 @@ class Chef def do_update [STAMP_DIR, APT_CONF_DIR].each do |d| - build_resource(:directory, d, caller[0]) do + declare_resource(:directory, d, caller[0]) do recursive true - end.run_action(:create) + end end - build_resource(:file, "#{APT_CONF_DIR}/15update-stamp", caller[0]) do + declare_resource(:file, "#{APT_CONF_DIR}/15update-stamp", caller[0]) do content "APT::Update::Post-Invoke-Success {\"touch #{STAMP_DIR}/update-success-stamp 2>/dev/null || true\";};" - end.run_action(:create_if_missing) + action :create_if_missing + end - shell_out!("apt-get -q update") + declare_resource(:execute, "apt-get -q update", caller[0]) end end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 21b2308688..53b4c52052 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1316,7 +1316,11 @@ class Chef # life as well. @@sorted_descendants = nil def self.sorted_descendants - @@sorted_descendants ||= descendants.sort_by { |x| x.to_s } + # so it turns out Class.to_s just blindly returns the name of the class, + # which can be a Symbol in addition to being a String. as a result, we + # may get a Symbol back from Class.to_s, so we call #to_s on that again. + # (buggy on at least ruby-2.4.4) + @@sorted_descendants ||= descendants.sort_by { |x| x.to_s.to_s } end def self.inherited(child) diff --git a/lib/chef/resource/action_class.rb b/lib/chef/resource/action_class.rb index 3d9f2f3e7c..89b23499d0 100644 --- a/lib/chef/resource/action_class.rb +++ b/lib/chef/resource/action_class.rb @@ -17,10 +17,13 @@ # require "chef/exceptions" +require "chef/dsl/recipe" class Chef class Resource module ActionClass + include Chef::DSL::Recipe + def to_s "#{new_resource || ""} action #{action ? action.inspect : ""}" end diff --git a/spec/unit/lwrp_spec.rb b/spec/unit/lwrp_spec.rb index 6eba001af4..6574a91f13 100644 --- a/spec/unit/lwrp_spec.rb +++ b/spec/unit/lwrp_spec.rb @@ -754,7 +754,7 @@ describe "LWRP" do it "lets you extend the recipe DSL" do expect(Chef::Recipe).to receive(:include).with(MyAwesomeDSLExensionClass) - expect(Chef::Provider::InlineResources).to receive(:include).with(MyAwesomeDSLExensionClass) + expect(Chef::Resource::ActionClass).to receive(:include).with(MyAwesomeDSLExensionClass) Chef::DSL::Recipe.send(:include, MyAwesomeDSLExensionClass) end diff --git a/spec/unit/provider/apt_update_spec.rb b/spec/unit/provider/apt_update_spec.rb index b72f7d9a76..3e3e6ba07a 100644 --- a/spec/unit/provider/apt_update_spec.rb +++ b/spec/unit/provider/apt_update_spec.rb @@ -44,19 +44,19 @@ describe Chef::Provider::AptUpdate do context "when the apt config directory does not exist" do before do FileUtils.rmdir config_dir - expect(File.exist?(config_dir)).to be_falsey - allow(provider).to receive(:shell_out!).with("apt-get -q update") + expect(File.exist?(config_dir)).to be false + allow_any_instance_of(Chef::Provider::Execute).to receive(:shell_out!).with("apt-get -q update", anything()) end it "should create the directory" do provider.run_action(:update) - expect(File.exist?(config_dir)).to be_truthy - expect(File.directory?(config_dir)).to be_truthy + expect(File.exist?(config_dir)).to be true + expect(File.directory?(config_dir)).to be true end it "should create the config file" do provider.run_action(:update) - expect(File.exist?(config_file)).to be_truthy + expect(File.exist?(config_file)).to be true expect(File.read(config_file)).to match(/^APT::Update.*#{stamp_dir}/) end end @@ -64,7 +64,7 @@ describe Chef::Provider::AptUpdate do describe "#action_update" do it "should update the apt cache" do provider.load_current_resource - expect(provider).to receive(:shell_out!).with("apt-get -q update").and_return(double) + expect_any_instance_of(Chef::Provider::Execute).to receive(:shell_out!).with("apt-get -q update", anything()) provider.run_action(:update) expect(new_resource).to be_updated_by_last_action end @@ -78,14 +78,14 @@ describe Chef::Provider::AptUpdate do it "should run if the time stamp is old" do expect(File).to receive(:mtime).with("#{stamp_dir}/update-success-stamp").and_return(Time.now - 86_500) - expect(provider).to receive(:shell_out!).with("apt-get -q update") + expect_any_instance_of(Chef::Provider::Execute).to receive(:shell_out!).with("apt-get -q update", anything()) provider.run_action(:periodic) expect(new_resource).to be_updated_by_last_action end it "should not run if the time stamp is new" do expect(File).to receive(:mtime).with("#{stamp_dir}/update-success-stamp").and_return(Time.now) - expect(provider).to_not receive(:shell_out!).with("apt-get -q update") + expect_any_instance_of(Chef::Provider::Execute).not_to receive(:shell_out!).with("apt-get -q update", anything()) provider.run_action(:periodic) expect(new_resource).to_not be_updated_by_last_action end @@ -97,14 +97,14 @@ describe Chef::Provider::AptUpdate do it "should run if the time stamp is old" do expect(File).to receive(:mtime).with("#{stamp_dir}/update-success-stamp").and_return(Time.now - 500) - expect(provider).to receive(:shell_out!).with("apt-get -q update") + expect_any_instance_of(Chef::Provider::Execute).to receive(:shell_out!).with("apt-get -q update", anything()) provider.run_action(:periodic) expect(new_resource).to be_updated_by_last_action end it "should not run if the time stamp is new" do expect(File).to receive(:mtime).with("#{stamp_dir}/update-success-stamp").and_return(Time.now - 300) - expect(provider).to_not receive(:shell_out!).with("apt-get -q update") + expect_any_instance_of(Chef::Provider::Execute).not_to receive(:shell_out!).with("apt-get -q update", anything()) provider.run_action(:periodic) expect(new_resource).to_not be_updated_by_last_action end -- cgit v1.2.1