diff options
author | Matt Wrock <matt@mattwrock.com> | 2016-01-12 09:06:07 -0800 |
---|---|---|
committer | Matt Wrock <matt@mattwrock.com> | 2016-01-12 09:06:07 -0800 |
commit | 7f32905c7b5600de2004f83bf4f05053db2b3b12 (patch) | |
tree | 3b4eda21353c637495b4a3912ce4e01a1c84c3b2 | |
parent | 0944320b72ee1ab16a18a149f5ecb743ace0c0d3 (diff) | |
download | chef-7f32905c7b5600de2004f83bf4f05053db2b3b12.tar.gz |
assert candidates exist for alternate sources and when pinning versions
-rw-r--r-- | lib/chef/provider/package/chocolatey.rb | 20 | ||||
-rw-r--r-- | spec/unit/provider/package/chocolatey_spec.rb | 34 |
2 files changed, 49 insertions, 5 deletions
diff --git a/lib/chef/provider/package/chocolatey.rb b/lib/chef/provider/package/chocolatey.rb index 3f52370939..7a9173e077 100644 --- a/lib/chef/provider/package/chocolatey.rb +++ b/lib/chef/provider/package/chocolatey.rb @@ -40,6 +40,19 @@ class Chef current_resource end + def define_resource_requirements + super + + # Chocolatey source attribute points to an alternate feed + # and not a package specific alternate source like other providers + # so we want to assert candidates exist for the alternate source + requirements.assert(:upgrade, :install) do |a| + a.assertion { candidates_exist_for_all_uninstalled? } + a.failure_message(Chef::Exceptions::Package, "No candidate version available for #{packages_missing_candidates.join(", ")}") + a.whyrun("Assuming a repository that offers #{packages_missing_candidates.join(", ")} would have been configured") + end + end + # Lazy initializer for candidate_version. A nil value means that there is no candidate # version and the package is not installable (generally an error). # @@ -191,15 +204,18 @@ class Chef end # Available packages in chocolatey as a Hash of names mapped to versions + # If pinning a package to a specific version, filter out all non matching versions # (names are downcased for case-insensitive matching) # # @return [Hash] name-to-version mapping of available packages def available_packages @available_packages ||= begin - cmd = [ "list -r #{package_name_array.join ' '}" ] + cmd = [ "list -ar #{package_name_array.join ' '}" ] cmd.push( "-source #{new_resource.source}" ) if new_resource.source - parse_list_output(*cmd) + parse_list_output(*cmd).reject do |name,version| + desired_name_versions[name] && desired_name_versions[name] != version + end end end diff --git a/spec/unit/provider/package/chocolatey_spec.rb b/spec/unit/provider/package/chocolatey_spec.rb index d2d6fabb19..b2e1d2daaf 100644 --- a/spec/unit/provider/package/chocolatey_spec.rb +++ b/spec/unit/provider/package/chocolatey_spec.rb @@ -54,7 +54,7 @@ git|2.6.2 munin-node|1.6.1.20130823 EOF remote_list_obj = double(stdout: remote_list_stdout) - allow(provider).to receive(:shell_out!).with("#{choco_exe} list -r #{package_names.join ' '}#{args}", {timeout: timeout}).and_return(remote_list_obj) + allow(provider).to receive(:shell_out!).with("#{choco_exe} list -ar #{package_names.join ' '}#{args}", {timeout: timeout}).and_return(remote_list_obj) end describe "#initialize" do @@ -284,12 +284,30 @@ munin-node|1.6.1.20130823 expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package) end + it "installing a package version that does not exist throws an error" do + allow_remote_list(["git"]) + new_resource.package_name("git") + new_resource.version("2.7.0") + provider.load_current_resource + expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package) + end + it "installing multiple packages with a package that does not exist throws an error" do allow_remote_list(["git", "package-does-not-exist"]) new_resource.package_name(["git", "package-does-not-exist"]) provider.load_current_resource expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package) end + + context "alternate source" do + it "installing a package that does not exist throws an error" do + allow_remote_list(["package-does-not-exist"], " -source alternate_source") + new_resource.package_name("package-does-not-exist") + new_resource.source("alternate_source") + provider.load_current_resource + expect { provider.run_action(:install) }.to raise_error(Chef::Exceptions::Package) + end + end end describe "#action_upgrade" do @@ -330,9 +348,9 @@ munin-node|1.6.1.20130823 it "version pins work as well" do allow_remote_list(["git"]) - new_resource.version("99.99.99.99") + new_resource.version("2.6.2") provider.load_current_resource - expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y -version 99.99.99.99 git", {:timeout=>timeout}) + expect(provider).to receive(:shell_out!).with("#{choco_exe} upgrade -y -version 2.6.2 git", {:timeout=>timeout}) provider.run_action(:upgrade) expect(new_resource).to be_updated_by_last_action end @@ -358,6 +376,16 @@ munin-node|1.6.1.20130823 provider.load_current_resource expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package) end + + context "alternate source" do + it "installing a package that does not exist throws an error" do + allow_remote_list(["package-does-not-exist"], " -source alternate_source") + new_resource.package_name("package-does-not-exist") + new_resource.source("alternate_source") + provider.load_current_resource + expect { provider.run_action(:upgrade) }.to raise_error(Chef::Exceptions::Package) + end + end end describe "#action_remove" do |