diff options
author | Phil Dibowitz <phil@ipom.com> | 2015-01-16 17:24:15 -0800 |
---|---|---|
committer | Phil Dibowitz <phil@ipom.com> | 2015-02-03 19:32:36 -0800 |
commit | 521f7bb7548dc00b626870dec7188dde18870d18 (patch) | |
tree | 5cce33359240375ba712a3f0bd8366c7b8cb7940 | |
parent | 0665a18112e269b0a8f81ad021cd88af84421fe9 (diff) | |
download | chef-521f7bb7548dc00b626870dec7188dde18870d18.tar.gz |
Some fixes for package provider, lots of unittests
-rw-r--r-- | lib/chef/provider/package.rb | 16 | ||||
-rw-r--r-- | spec/unit/provider/package_spec.rb | 200 |
2 files changed, 212 insertions, 4 deletions
diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb index 5c852017cb..58de80c868 100644 --- a/lib/chef/provider/package.rb +++ b/lib/chef/provider/package.rb @@ -74,10 +74,10 @@ class Chef def action_install # If we specified a version, and it's not the current version, move to the specified version - if !@new_resource.version.nil? && !(target_version_already_installed?) + if new_version_array.any? && !(target_version_already_installed?) install_version = @new_resource.version # If it's not installed at all, install it - elsif @current_resource.version.nil? + elsif current_version_array.any? { |x| x.nil? } install_version = candidate_version else Chef::Log.debug("#{@new_resource} is already installed - nothing to do") @@ -127,13 +127,21 @@ class Chef end end + def have_any_matching_version? + f = [] + new_version_array.each_with_index do |item, index| + f << (item == current_version_array[index]) + end + f.any? + end + def removing_package? if !current_version_array.any? # ! any? means it's all nil's, which means nothing is installed false elsif !new_version_array.any? true # remove any version of all packages - elsif @new_resource.version == @current_resource.version + elsif have_any_matching_version? true # remove the version we have else false # we don't have the version we want to remove @@ -239,7 +247,7 @@ class Chef end def target_version_already_installed? - @new_resource.version == @current_resource.version + new_version_array == current_version_array end private diff --git a/spec/unit/provider/package_spec.rb b/spec/unit/provider/package_spec.rb index 2aeaf717e6..3eb4a47095 100644 --- a/spec/unit/provider/package_spec.rb +++ b/spec/unit/provider/package_spec.rb @@ -425,3 +425,203 @@ describe Chef::Provider::Package do end end + +describe "Chef::Provider::Package - Multi" do + before do + @node = Chef::Node.new + @events = Chef::EventDispatch::Dispatcher.new + @run_context = Chef::RunContext.new(@node, {}, @events) + @new_resource = Chef::Resource::Package.new(['emacs', 'vi']) + @current_resource = Chef::Resource::Package.new(['emacs', 'vi']) + @provider = Chef::Provider::Package.new(@new_resource, @run_context) + @provider.current_resource = @current_resource + @provider.candidate_version = ['1.0', '6.2'] + end + + describe "when installing multiple packages" do + before(:each) do + @provider.current_resource = @current_resource + allow(@provider).to receive(:install_package).and_return(true) + end + + it "should install the candidate versions when none are installed" do + @provider.run_action(:install) + expect(@new_resource).to be_updated + end + + it "should install the candidate versions when some are installed" do + expect(@provider).to receive(:install_package).with( + @new_resource.name, + @provider.candidate_version + ).and_return(true) + @current_resource.version(['1.0', nil]) + @provider.run_action(:install) + expect(@new_resource).to be_updated + end + + it "should install the specified version when some are out of date" do + @current_resource.version(['1.0', '6.2']) + @new_resource.version(['1.0', '6.1']) + @provider.run_action(:install) + expect(@new_resource).to be_updated + end + + it "should not install any version if all are installed at the right version" do + @current_resource.version(['1.0', '6.2']) + @new_resource.version(['1.0', '6.2']) + @provider.run_action(:install) + expect(@new_resource).not_to be_updated_by_last_action + end + + it "should not install any version if all are installed, and no version was specified" do + @current_resource.version(['1.0', '6.2']) + @provider.run_action(:install) + expect(@new_resource).not_to be_updated_by_last_action + end + end + + describe "when upgrading multiple packages" do + before(:each) do + @provider.current_resource = @current_resource + allow(@provider).to receive(:upgrade_package).and_return(true) + end + + it "should upgrade the package if the current versions are not the candidate version" do + @current_resource.version ['0.9', '6.1'] + expect(@provider).to receive(:upgrade_package).with( + @new_resource.name, + @provider.candidate_version + ).and_return(true) + @provider.run_action(:upgrade) + expect(@new_resource).to be_updated_by_last_action + end + + it "should upgrade the package if some of current versions are not the candidate versions" do + @current_resource.version ['1.0', '6.1'] + expect(@provider).to receive(:upgrade_package).with( + @new_resource.name, + @provider.candidate_version + ).and_return(true) + @provider.run_action(:upgrade) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not install the package if the current versions are the candidate version" do + @current_resource.version ['1.0', '6.2'] + expect(@provider).not_to receive(:upgrade_package) + @provider.run_action(:upgrade) + expect(@new_resource).not_to be_updated_by_last_action + end + end + + describe "When removing multiple packages " do + before(:each) do + allow(@provider).to receive(:remove_package).and_return(true) + @current_resource.version ['1.0', '6.2'] + end + + it "should remove the packages if all are installed" do + expect(@provider).to be_removing_package + expect(@provider).to receive(:remove_package).with(['emacs', 'vi'], nil) + @provider.run_action(:remove) + expect(@new_resource).to be_updated + expect(@new_resource).to be_updated_by_last_action + end + + it "should remove the packages if some are installed" do + @current_resource.version ['1.0', nil] + expect(@provider).to be_removing_package + expect(@provider).to receive(:remove_package).with(['emacs', 'vi'], nil) + @provider.run_action(:remove) + expect(@new_resource).to be_updated + expect(@new_resource).to be_updated_by_last_action + end + + it "should remove the packages at a specific version if they are installed at that version" do + @new_resource.version ['1.0', '6.2'] + expect(@provider).to be_removing_package + expect(@provider).to receive(:remove_package).with(['emacs', 'vi'], ['1.0', '6.2']) + @provider.run_action(:remove) + expect(@new_resource).to be_updated_by_last_action + end + + it "should remove the packages at a specific version any are is installed at that version" do + @new_resource.version ['0.5', '6.2'] + expect(@provider).to be_removing_package + expect(@provider).to receive(:remove_package).with(['emacs', 'vi'], ['0.5', '6.2']) + @provider.run_action(:remove) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not remove the packages at a specific version if they are not installed at that version" do + @new_resource.version ['0.5', '6.0'] + expect(@provider).not_to be_removing_package + expect(@provider).not_to receive(:remove_package) + @provider.run_action(:remove) + expect(@new_resource).not_to be_updated_by_last_action + end + + it "should not remove the packages if they are not installed" do + expect(@provider).not_to receive(:remove_package) + allow(@current_resource).to receive(:version).and_return(nil) + @provider.run_action(:remove) + expect(@new_resource).not_to be_updated_by_last_action + end + + end + + describe "When purging multiple packages " do + before(:each) do + allow(@provider).to receive(:purge_package).and_return(true) + @current_resource.version ['1.0', '6.2'] + end + + it "should purge the packages if all are installed" do + expect(@provider).to be_removing_package + expect(@provider).to receive(:purge_package).with(['emacs', 'vi'], nil) + @provider.run_action(:purge) + expect(@new_resource).to be_updated + expect(@new_resource).to be_updated_by_last_action + end + + it "should purge the packages if some are installed" do + @current_resource.version ['1.0', nil] + expect(@provider).to be_removing_package + expect(@provider).to receive(:purge_package).with(['emacs', 'vi'], nil) + @provider.run_action(:purge) + expect(@new_resource).to be_updated + expect(@new_resource).to be_updated_by_last_action + end + + it "should purge the packages at a specific version if they are installed at that version" do + @new_resource.version ['1.0', '6.2'] + expect(@provider).to be_removing_package + expect(@provider).to receive(:purge_package).with(['emacs', 'vi'], ['1.0', '6.2']) + @provider.run_action(:purge) + expect(@new_resource).to be_updated_by_last_action + end + + it "should purge the packages at a specific version any are is installed at that version" do + @new_resource.version ['0.5', '6.2'] + expect(@provider).to be_removing_package + expect(@provider).to receive(:purge_package).with(['emacs', 'vi'], ['0.5', '6.2']) + @provider.run_action(:purge) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not purge the packages at a specific version if they are not installed at that version" do + @new_resource.version ['0.5', '6.0'] + expect(@provider).not_to be_removing_package + expect(@provider).not_to receive(:purge_package) + @provider.run_action(:purge) + expect(@new_resource).not_to be_updated_by_last_action + end + + it "should not purge the packages if they are not installed" do + expect(@provider).not_to receive(:purge_package) + allow(@current_resource).to receive(:version).and_return(nil) + @provider.run_action(:purge) + expect(@new_resource).not_to be_updated_by_last_action + end + end +end |