diff options
-rw-r--r-- | lib/chef/resource/chocolatey_source.rb | 33 | ||||
-rw-r--r-- | spec/unit/resource/chocolatey_source_spec.rb | 55 |
2 files changed, 87 insertions, 1 deletions
diff --git a/lib/chef/resource/chocolatey_source.rb b/lib/chef/resource/chocolatey_source.rb index e8ef95aad5..2b358a22f7 100644 --- a/lib/chef/resource/chocolatey_source.rb +++ b/lib/chef/resource/chocolatey_source.rb @@ -31,9 +31,17 @@ class Chef property :bypass_proxy, [TrueClass, FalseClass], default: false, description: "Whether or not to bypass the system's proxy settings to access the source." + property :admin_only, [TrueClass, FalseClass], default: false, + description: "Whether or not to set the source to be accessible to only admins." + + property :allow_self_service, [TrueClass, FalseClass], default: false, + description: "Whether or not to set the source to be used for self service." + property :priority, Integer, default: 0, description: "The priority level of the source." + property :disabled, [TrueClass, FalseClass], default: false, desired_state: false, skip_docs: true + load_current_value do element = fetch_source_element(source_name) current_value_does_not_exist! if element.nil? @@ -41,7 +49,10 @@ class Chef source_name element["id"] source element["value"] bypass_proxy element["bypassProxy"] == "true" + admin_only element["adminOnly"] == "true" + allow_self_service element["selfService"] == "true" priority element["priority"].to_i + disabled element["disabled"] == "true" end # @param [String] id the source name @@ -77,6 +88,26 @@ class Chef end end + action :disable do + description "Disables a Chocolatey source." + + if current_resource.disabled != true + converge_by("disable Chocolatey source '#{new_resource.source_name}'") do + shell_out!(choco_cmd("disable")) + end + end + end + + action :enable do + description "Enables a Chocolatey source." + + if current_resource.disabled == true + converge_by("enable Chocolatey source '#{new_resource.source_name}'") do + shell_out!(choco_cmd("enable")) + end + end + end + action_class do # @param [String] action the name of the action to perform # @return [String] the choco source command string @@ -85,6 +116,8 @@ class Chef if action == "add" cmd << " -s #{new_resource.source} --priority=#{new_resource.priority}" cmd << " --bypassproxy" if new_resource.bypass_proxy + cmd << " --allowselfservice" if new_resource.allow_self_service + cmd << " --adminonly" if new_resource.admin_only end cmd end diff --git a/spec/unit/resource/chocolatey_source_spec.rb b/spec/unit/resource/chocolatey_source_spec.rb index 1463a448de..60bd773594 100644 --- a/spec/unit/resource/chocolatey_source_spec.rb +++ b/spec/unit/resource/chocolatey_source_spec.rb @@ -19,7 +19,14 @@ require "spec_helper" describe Chef::Resource::ChocolateySource do - let(:resource) { Chef::Resource::ChocolateySource.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::ChocolateySource.new("fakey_fakerton", run_context) } + let(:disable_provider) { resource.provider_for_action(:disable) } + let(:enable_provider) { resource.provider_for_action(:enable) } + let(:current_resource) { Chef::Resource::ChocolateySource.new("fakey_fakerton") } let(:config) do <<-CONFIG <?xml version="1.0" encoding="utf-8"?> @@ -40,6 +47,11 @@ describe Chef::Resource::ChocolateySource do # we save off the ENV and set ALLUSERSPROFILE so these specs will work on *nix and non-C drive Windows installs before(:each) do + disable_provider # vivify before mocking + enable_provider + allow(resource).to receive(:provider_for_action).and_return(disable_provider) + allow(resource).to receive(:provider_for_action).and_return(enable_provider) + allow(resource).to receive(:dup).and_return(current_resource) @original_env = ENV.to_hash ENV["ALLUSERSPROFILE"] = 'C:\ProgramData' end @@ -64,6 +76,8 @@ describe Chef::Resource::ChocolateySource do it "supports :add and :remove actions" do expect { resource.action :add }.not_to raise_error expect { resource.action :remove }.not_to raise_error + expect { resource.action :disable }.not_to raise_error + expect { resource.action :enable }.not_to raise_error end it "bypass_proxy property defaults to false" do @@ -74,6 +88,45 @@ describe Chef::Resource::ChocolateySource do expect { resource.priority.to eq(0) } end + it "admin_only property defaults to false" do + expect { resource.admin_only.to be_false } + end + + it "allow_self_service property defaults to false" do + expect { resource.allow_self_service.to be_false } + end + + describe "#load_current_resource" do + it "sets disabled to true when the XML disabled property is true" do + allow(current_resource).to receive(:fetch_source_element).with("fakey_fakerton").and_return(OpenStruct.new(disabled: "true")) + disable_provider.load_current_resource + expect(current_resource.disabled).to be true + end + + it "sets disabled to false when the XML disabled property is false" do + allow(current_resource).to receive(:fetch_source_element).with("fakey_fakerton").and_return(OpenStruct.new(disabled: "false")) + enable_provider.load_current_resource + expect(current_resource.disabled).to be false + end + end + + describe "run_action(:enable)" do + it "when source is disabled, it enables it correctly" do + resource.disabled true + allow(current_resource).to receive(:fetch_source_element).with("fakey_fakerton").and_return(OpenStruct.new(disabled: "true")) + expect(enable_provider).to receive(:shell_out!).with("C:\\ProgramData\\chocolatey\\bin\\choco source enable -n \"fakey_fakerton\"") + resource.run_action(:enable) + expect(resource.updated_by_last_action?).to be true + end + + it "when source is enabled, it is idempotent when trying to enable" do + resource.disabled false + allow(current_resource).to receive(:fetch_source_element).with("fakey_fakerton").and_return(OpenStruct.new(disabled: "false")) + resource.run_action(:enable) + expect(resource.updated_by_last_action?).to be false + end + end + describe "#fetch_source_element" do it "raises and error if the config file cannot be found" do allow(::File).to receive(:exist?).with('C:\ProgramData\chocolatey\config\chocolatey.config').and_return(false) |