diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-05-25 12:13:38 -0500 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-05-25 12:15:15 -0500 |
commit | 02b5706a1d1d58e8004ef0d26786bafc6fc36ba3 (patch) | |
tree | cc6d95da3d547aff64722fb2bed8411ad0a17d46 | |
parent | 29bea245bee7af04e01b12890cdfe54a34c29bbf (diff) | |
download | chef-02b5706a1d1d58e8004ef0d26786bafc6fc36ba3.tar.gz |
convert breakpoint resource to a custom resource
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/provider/breakpoint.rb | 38 | ||||
-rw-r--r-- | lib/chef/providers.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource/breakpoint.rb | 15 | ||||
-rw-r--r-- | spec/unit/provider/breakpoint_spec.rb | 40 | ||||
-rw-r--r-- | spec/unit/provider_resolver_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/resource/breakpoint_spec.rb | 39 |
6 files changed, 62 insertions, 73 deletions
diff --git a/lib/chef/provider/breakpoint.rb b/lib/chef/provider/breakpoint.rb deleted file mode 100644 index 9f8b8a6467..0000000000 --- a/lib/chef/provider/breakpoint.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# Author:: Daniel DeLeo (<dan@kallistec.com>) -# Copyright:: Copyright 2008-2017, Chef Software Inc. -# 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. -# - -class Chef - class Provider - class Breakpoint < Chef::Provider - - provides :breakpoint - - def load_current_resource - end - - def action_break - if defined?(Shell) && Shell.running? - run_context.resource_collection.iterator.pause - new_resource.updated_by_last_action(true) - run_context.resource_collection.iterator - end - end - - end - end -end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 41de44a1d6..0ea1786594 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -19,7 +19,6 @@ require "chef/provider/apt_update" require "chef/provider/apt_repository" require "chef/provider/batch" -require "chef/provider/breakpoint" require "chef/provider/cookbook_file" require "chef/provider/cron" require "chef/provider/cron/solaris" diff --git a/lib/chef/resource/breakpoint.rb b/lib/chef/resource/breakpoint.rb index a5eed0da94..d059800e53 100644 --- a/lib/chef/resource/breakpoint.rb +++ b/lib/chef/resource/breakpoint.rb @@ -21,12 +21,27 @@ require "chef/resource" class Chef class Resource class Breakpoint < Chef::Resource + provides :breakpoint + resource_name :breakpoinst + default_action :break def initialize(action = "break", *args) super(caller.first, *args) end + def load_current_resource + end + + action :break do + if defined?(Shell) && Shell.running? + with_run_context :parent do + run_context.resource_collection.iterator.pause + new_resource.updated_by_last_action(true) + run_context.resource_collection.iterator + end + end + end end end end diff --git a/spec/unit/provider/breakpoint_spec.rb b/spec/unit/provider/breakpoint_spec.rb index ffe8c8261f..5d3bb0e352 100644 --- a/spec/unit/provider/breakpoint_spec.rb +++ b/spec/unit/provider/breakpoint_spec.rb @@ -17,37 +17,33 @@ # require "spec_helper" -describe Chef::Provider::Breakpoint do +describe "le breakpoint provider" do - before do - @resource = Chef::Resource::Breakpoint.new - @node = Chef::Node.new - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, {}, @events) - @collection = double("resource collection") - allow(@run_context).to receive(:resource_collection).and_return(@collection) - @provider = Chef::Provider::Breakpoint.new(@resource, @run_context) - end + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:collection) { double("resource collection") } + let(:resource) { Chef::Resource::Breakpoint.new("name", run_context) } + let(:provider) { resource.provider_for_action(:break) } - it "responds to load_current_resource" do - expect(@provider).to respond_to(:load_current_resource) + before do + allow(run_context).to receive(:resource_collection).and_return(collection) end it "gets the iterator from @collection and pauses it" do allow(Shell).to receive(:running?).and_return(true) - @iterator = double("stepable_iterator") - allow(@collection).to receive(:iterator).and_return(@iterator) - expect(@iterator).to receive(:pause) - @provider.action_break - expect(@resource).to be_updated + iterator = double("stepable_iterator") + allow(collection).to receive(:iterator).and_return(iterator) + expect(iterator).to receive(:pause) + provider.action_break + expect(resource).to be_updated end it "doesn't pause the iterator if chef-shell isn't running" do allow(Shell).to receive(:running?).and_return(false) - @iterator = double("stepable_iterator") - allow(@collection).to receive(:iterator).and_return(@iterator) - expect(@iterator).not_to receive(:pause) - @provider.action_break + iterator = double("stepable_iterator") + allow(collection).to receive(:iterator).and_return(iterator) + expect(iterator).not_to receive(:pause) + provider.action_break end - end diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb index 0a504fa5fe..1902fb5375 100644 --- a/spec/unit/provider_resolver_spec.rb +++ b/spec/unit/provider_resolver_spec.rb @@ -554,7 +554,7 @@ describe Chef::ProviderResolver do PROVIDERS = { bash: [ Chef::Resource::Bash, Chef::Provider::Script ], - breakpoint: [ Chef::Resource::Breakpoint, Chef::Provider::Breakpoint ], + breakpoint: [ Chef::Resource::Breakpoint, Chef::Resource::Breakpoint.action_class ], chef_gem: [ Chef::Resource::ChefGem, Chef::Provider::Package::Rubygems ], cookbook_file: [ Chef::Resource::CookbookFile, Chef::Provider::CookbookFile ], csh: [ Chef::Resource::Csh, Chef::Provider::Script ], diff --git a/spec/unit/resource/breakpoint_spec.rb b/spec/unit/resource/breakpoint_spec.rb index a5b27bae16..ce0df676dd 100644 --- a/spec/unit/resource/breakpoint_spec.rb +++ b/spec/unit/resource/breakpoint_spec.rb @@ -17,31 +17,48 @@ # require "spec_helper" -require "support/shared/unit/resource/static_provider_resolution" describe Chef::Resource::Breakpoint do - static_provider_resolution( - resource: Chef::Resource::Breakpoint, - provider: Chef::Provider::Breakpoint, - name: :breakpoint, - action: :break - ) + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:collection) { double("resource collection") } + let(:resource) { Chef::Resource::Breakpoint.new("name", run_context) } + let(:provider) { resource.provider_for_action(:break) } before do - @breakpoint = Chef::Resource::Breakpoint.new + allow(run_context).to receive(:resource_collection).and_return(collection) + end + + it "gets the iterator from @collection and pauses it" do + allow(Shell).to receive(:running?).and_return(true) + iterator = double("stepable_iterator") + allow(collection).to receive(:iterator).and_return(iterator) + expect(iterator).to receive(:pause) + provider.action_break + expect(resource).to be_updated + end + + it "doesn't pause the iterator if chef-shell isn't running" do + allow(Shell).to receive(:running?).and_return(false) + iterator = double("stepable_iterator") + allow(collection).to receive(:iterator).and_return(iterator) + expect(iterator).not_to receive(:pause) + provider.action_break end it "allows the action :break" do - expect(@breakpoint.allowed_actions).to include(:break) + expect(resource.allowed_actions).to include(:break) end it "defaults to the break action" do - expect(@breakpoint.action).to eq([:break]) + expect(resource.action).to eq([:break]) end it "names itself after the line number of the file where it's created" do - expect(@breakpoint.name).to match(/breakpoint_spec\.rb\:[\d]{2}\:in \`new\'$/) + resource = Chef::Resource::Breakpoint.new + expect(resource.name).to match(/breakpoint_spec\.rb\:[\d]{2}\:in \`new\'$/) end end |